From d3e4ff30fe3d56da20260e164d9e70a6a4e3402e Mon Sep 17 00:00:00 2001 From: David Patterson Date: Wed, 7 Mar 2018 02:06:29 -0500 Subject: [PATCH] [CLI] Adds support for generating pages Addresses #671 --- spec/amber/cli/commands/generator_spec.cr | 14 +++++++++++ src/amber/cli/commands/generate.cr | 2 +- src/amber/cli/templates/template.cr | 4 +++ src/amber/cli/templates/view.cr | 25 +++++++++++++++++++ .../view/src/views/{{name}}_view.html.ecr.ecr | 1 + .../view/src/views/{{name}}_view.html.slang | 1 + 6 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 src/amber/cli/templates/view.cr create mode 100644 src/amber/cli/templates/view/src/views/{{name}}_view.html.ecr.ecr create mode 100644 src/amber/cli/templates/view/src/views/{{name}}_view.html.slang diff --git a/spec/amber/cli/commands/generator_spec.cr b/spec/amber/cli/commands/generator_spec.cr index 160f51873..45218e5f0 100644 --- a/spec/amber/cli/commands/generator_spec.cr +++ b/spec/amber/cli/commands/generator_spec.cr @@ -130,6 +130,20 @@ module Amber::CLI end end + describe "view" do + it "follows naming conventions for all files and class names" do + [camel_case, snake_case].each do |arg| + MainCommand.run ["generate", "view", arg] + filename = snake_case + src_filepath = "./src/views/#{filename}_view.html.slang" + + File.exists?(src_filepath).should be_true + File.read(src_filepath).should contain display + File.delete(src_filepath) + end + end + end + describe "socket" do struct_definition_prefix = "struct #{camel_case}" diff --git a/src/amber/cli/commands/generate.cr b/src/amber/cli/commands/generate.cr index c212262d3..736676aab 100644 --- a/src/amber/cli/commands/generate.cr +++ b/src/amber/cli/commands/generate.cr @@ -6,7 +6,7 @@ module Amber::CLI class Generate < Command class Options - arg "type", desc: "scaffold, model, controller, migration, mailer, socket, channel, auth, error", required: true + arg "type", desc: "scaffold, model, controller, migration, mailer, socket, channel, auth, error, view", required: true arg "name", desc: "name of resource", required: false arg_array "fields", desc: "user:reference name:string body:text age:integer published:bool" bool "--no-color", desc: "Disable colored output", default: false diff --git a/src/amber/cli/templates/template.cr b/src/amber/cli/templates/template.cr index 11dd9eec5..78c0912d5 100644 --- a/src/amber/cli/templates/template.cr +++ b/src/amber/cli/templates/template.cr @@ -17,6 +17,7 @@ require "./socket" require "./channel" require "./auth" require "./error" +require "./view" module Amber::CLI class Template @@ -101,6 +102,9 @@ module Amber::CLI puts "Rendering Error Template" actions = ["forbidden", "not_found", "internal_server_error"] ErrorTemplate.new("error", actions).render(directory, list: true, color: true) + when "view" + puts "Rendering View #{name}" + View.new(name, fields).render(directory, list: true, color: true) else raise "Template not found" end diff --git a/src/amber/cli/templates/view.cr b/src/amber/cli/templates/view.cr new file mode 100644 index 000000000..1e74e347a --- /dev/null +++ b/src/amber/cli/templates/view.cr @@ -0,0 +1,25 @@ +require "./field.cr" + +module Amber::CLI + class View < Teeplate::FileTree + include Amber::CLI::Helpers + directory "#{__DIR__}/view" + + @name : String + @language : String + @fields : Array(Field) + + def initialize(@name, fields) + @language = CLI.config.language + @fields = fields.map { |field| Field.new(field) } + + add_dependencies <<-DEPENDENCY + require "../src/views/**" + DEPENDENCY + end + + def filter(entries) + entries.reject { |entry| entry.path.includes?("src/views") && !entry.path.includes?("#{@language}") } + end + end +end diff --git a/src/amber/cli/templates/view/src/views/{{name}}_view.html.ecr.ecr b/src/amber/cli/templates/view/src/views/{{name}}_view.html.ecr.ecr new file mode 100644 index 000000000..15873d8dd --- /dev/null +++ b/src/amber/cli/templates/view/src/views/{{name}}_view.html.ecr.ecr @@ -0,0 +1 @@ +

This is an example Post Comment in HTML.

diff --git a/src/amber/cli/templates/view/src/views/{{name}}_view.html.slang b/src/amber/cli/templates/view/src/views/{{name}}_view.html.slang new file mode 100644 index 000000000..6ea09064e --- /dev/null +++ b/src/amber/cli/templates/view/src/views/{{name}}_view.html.slang @@ -0,0 +1 @@ +p This is an example Post Comment body in HTML.