Skip to content

Commit

Permalink
Refactor Page and Action generators to use newer LuckyTask CLI interf…
Browse files Browse the repository at this point in the history
…ace. Adding new --with-page arg for Browser actions. Fixes #1795 (#1819)
  • Loading branch information
jwoertink committed Sep 1, 2023
1 parent 2ead003 commit 2e92959
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 61 deletions.
11 changes: 11 additions & 0 deletions spec/support/generator_helper.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
module GeneratorHelper
private def generate(generator : Class, args : Array(String) = [] of String) : IO
task = generator.new
task.output = IO::Memory.new
task.print_help_or_call(args: args)
task.output
end

private def should_create_files_with_contents(io : IO, **files_and_contents)
files_and_contents.each do |file_location, file_contents|
File.read(file_location.to_s).should contain(file_contents)
Expand All @@ -15,4 +22,8 @@ module GeneratorHelper
filename.should_not be_nil
File.read("./db/migrations/#{filename}").should contain(content)
end

private def should_have_generated(text : String, inside : String)
File.read(inside).should contain(text)
end
end
63 changes: 42 additions & 21 deletions spec/tasks/gen/action_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe Gen::Action do
it "generates a basic browser action" do
with_cleanup do
valid_action_name = "Users::Index"
io = generate valid_action_name, Gen::Action::Browser
io = generate Gen::Action::Browser, args: [valid_action_name]

filename = "./src/actions/users/index.cr"
should_have_generated "#{valid_action_name} < BrowserAction", inside: filename
Expand All @@ -17,10 +17,42 @@ describe Gen::Action do
end
end

describe "with_page" do
it "generates the action and a page for Browser action" do
with_cleanup do
valid_action_name = "Users::Index"
io = generate Gen::Action::Browser, args: [valid_action_name, "--with-page"]

action_filename = "./src/actions/users/index.cr"
page_filename = "./src/pages/users/index_page.cr"
should_have_generated "#{valid_action_name} < BrowserAction", inside: action_filename
should_have_generated "#{valid_action_name}Page < MainLayout", inside: page_filename

io.to_s.should contain(valid_action_name)
io.to_s.should contain("/src/actions/users")
io.to_s.should contain("/src/pages/users")
end
end

it "does not generate a page for Api action" do
with_cleanup do
valid_action_name = "Users::Index"
io = generate Gen::Action::Api, args: [valid_action_name, "--with-page"]

filename = "./src/actions/api/users/index.cr"
should_have_generated "#{valid_action_name} < ApiAction", inside: filename

io.to_s.should contain(valid_action_name)
io.to_s.should contain("/src/actions/api/users")
io.to_s.should contain("No page generated for ApiActions")
end
end
end

it "generates a basic api action" do
with_cleanup do
valid_action_name = "Users::Index"
io = generate valid_action_name, Gen::Action::Api
io = generate Gen::Action::Api, args: [valid_action_name]

filename = "./src/actions/api/users/index.cr"
should_have_generated "#{valid_action_name} < ApiAction", inside: filename
Expand All @@ -33,7 +65,7 @@ describe Gen::Action do
it "generates nested browser and api actions" do
with_cleanup do
valid_nested_action_name = "Users::Announcements::Index"
io = generate valid_nested_action_name, Gen::Action::Browser
io = generate Gen::Action::Browser, args: [valid_nested_action_name]

filename = "src/actions/users/announcements/index.cr"
should_have_generated "#{valid_nested_action_name} < BrowserAction", inside: filename
Expand All @@ -45,7 +77,7 @@ describe Gen::Action do

with_cleanup do
valid_nested_action_name = "Users::Announcements::Index"
io = generate valid_nested_action_name, Gen::Action::Api
io = generate Gen::Action::Api, args: [valid_nested_action_name]

filename = "src/actions/api/users/announcements/index.cr"
should_have_generated "#{valid_nested_action_name} < ApiAction", inside: filename
Expand All @@ -56,33 +88,22 @@ describe Gen::Action do
end

it "fails if called with non-resourceful action name" do
io = generate "Users::HostedEvents", Gen::Action::Browser
io = generate Gen::Action::Browser, args: ["Users::HostedEvents"]

io.to_s.should contain "Could not infer route for Users::HostedEvents"
end

it "displays an error if given no arguments" do
io = generate nil, Gen::Action::Browser

io.to_s.should contain("Action name is required.")
it "raises an error if given no arguments" do
expect_raises(Exception, /action_name is required/) do
generate Gen::Action::Browser
end
end

it "displays an error if given only one class" do
with_cleanup do
io = generate "Users", Gen::Action::Browser
io = generate Gen::Action::Browser, args: ["Users"]

io.to_s.should contain("That's not a valid Action.")
end
end
end

private def generate(name, generator : Class)
ARGV.push(name) if name
io = IO::Memory.new
generator.new.call(io)
io
end

private def should_have_generated(text, inside)
File.read(inside).should contain(text)
end
28 changes: 7 additions & 21 deletions spec/tasks/gen/page_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@ include GeneratorHelper
describe Gen::Page do
it "generates a page" do
with_cleanup do
io = IO::Memory.new
valid_page_name = "Users::IndexPage"
ARGV.push(valid_page_name)

Gen::Page.new.call(io)
io = generate Gen::Page, args: [valid_page_name]

should_create_files_with_contents io,
"./src/pages/users/index_page.cr": valid_page_name
Expand All @@ -19,44 +16,33 @@ describe Gen::Page do

it "generates a root page" do
with_cleanup do
io = IO::Memory.new
valid_page_name = "::IndexPage"
ARGV.push(valid_page_name)

Gen::Page.new.call(io)
io = generate Gen::Page, args: [valid_page_name]

should_create_files_with_contents io,
"./src/pages/index_page.cr": valid_page_name
end
end

it "displays an error if given no arguments" do
io = IO::Memory.new

Gen::Page.new.call(io)

io.to_s.should contain("Page name is required.")
expect_raises(Exception, /page_class is required/) do
generate Gen::Page
end
end

it "displays an error if given only one class" do
with_cleanup do
io = IO::Memory.new
invalid_page_name = "Users"
ARGV.push(invalid_page_name)

Gen::Page.new.call(io)
io = generate Gen::Page, args: [invalid_page_name]

io.to_s.should contain("That's not a valid Page.")
end
end

it "displays an error if missing ending 'Page'" do
with_cleanup do
io = IO::Memory.new
invalid_page_name = "Users::Index"
ARGV.push(invalid_page_name)

Gen::Page.new.call(io)
io = generate Gen::Page, args: [invalid_page_name]

io.to_s.should contain("That's not a valid Page.")
end
Expand Down
8 changes: 2 additions & 6 deletions tasks/gen/action/action_generator.cr
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ module Gen::ActionGenerator

private def name_is_present
@error = "Action name is required. Example: lucky gen.action Users::Index"
ARGV.first?
action_name.presence
end

private def name_matches_format
@error = "That's not a valid Action. Example: lucky gen.action Users::Index"
ARGV.first.includes?("::")
action_name.includes?("::")
end

private def route_generated_from_action_name
Expand All @@ -53,10 +53,6 @@ module Gen::ActionGenerator
@route ||= Lucky::RouteInferrer.new(action_class_name: action_name).generate_inferred_route
end

private def action_name
ARGV.first
end

private def action
path_args.last
end
Expand Down
12 changes: 9 additions & 3 deletions tasks/gen/action/api.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ class Gen::Action::Api < LuckyTask::Task

summary "Generate a new api action"

positional_arg :action_name, "The name of the action"
switch :with_page, "This flag is used with gen.action.browser Only"

def help_message
<<-TEXT
#{summary}
Expand All @@ -16,12 +19,15 @@ class Gen::Action::Api < LuckyTask::Task
TEXT
end

def call(io : IO = STDOUT)
render_action_template(io, inherit_from: "ApiAction")
def call
render_action_template(output, inherit_from: "ApiAction")
if with_page?
output.puts "No page generated for ApiActions".colorize.red
end
end

private def action_name
name = ARGV.first
name = previous_def
if name.downcase.starts_with?("api")
name
else
Expand Down
12 changes: 10 additions & 2 deletions tasks/gen/action/browser.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ class Gen::Action::Browser < LuckyTask::Task

summary "Generate a new browser action"

positional_arg :action_name, "The name of the action"
switch :with_page, "Generate a Page matching this Action"

def help_message
<<-TEXT
#{summary}
Expand All @@ -17,7 +20,12 @@ class Gen::Action::Browser < LuckyTask::Task
TEXT
end

def call(io : IO = STDOUT)
render_action_template(io, inherit_from: "BrowserAction")
def call
render_action_template(output, inherit_from: "BrowserAction")
if with_page?
page_task = Gen::Page.new
page_task.output = output
page_task.print_help_or_call(args: ["#{action_name}Page"])
end
end
end
14 changes: 6 additions & 8 deletions tasks/gen/page.cr
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ end
class Gen::Page < LuckyTask::Task
summary "Generate a new HTML page"

def call(io : IO = STDOUT)
positional_arg :page_class, "The name of the page"

def call
if error
io.puts error.colorize(:red)
output.puts error.colorize(:red)
else
Lucky::PageTemplate.new(page_filename, page_class, output_path).render(output_path)
io.puts success_message
output.puts success_message
end
end

Expand All @@ -41,7 +43,7 @@ class Gen::Page < LuckyTask::Task
end

private def missing_name_error
if ARGV.first?.nil?
if page_class.nil?
"Page name is required."
end
end
Expand All @@ -52,10 +54,6 @@ class Gen::Page < LuckyTask::Task
end
end

private def page_class
ARGV.first
end

private def page_filename
page_class.split("::").last.underscore.downcase
end
Expand Down

0 comments on commit 2e92959

Please sign in to comment.