Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CLI] Adds support for generating pages #674

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions spec/amber/cli/commands/generator_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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}"

Expand Down
2 changes: 1 addition & 1 deletion src/amber/cli/commands/generate.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/amber/cli/templates/template.cr
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require "./socket"
require "./channel"
require "./auth"
require "./error"
require "./view"

module Amber::CLI
class Template
Expand Down Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions src/amber/cli/templates/view.cr
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>This is an example Post Comment in HTML.</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
p This is an example Post Comment body in HTML.