diff --git a/shard.yml b/shard.yml index 6ce19be3c..70c669733 100644 --- a/shard.yml +++ b/shard.yml @@ -54,9 +54,9 @@ dependencies: pulsar: github: luckyframework/pulsar version: ~> 0.2.3 - teeplate: - github: luckyframework/teeplate - version: ~> 0.8.5 + lucky_template: + github: luckyframework/lucky_template + version: ~> 0.1.0 development_dependencies: ameba: diff --git a/spec/support/cleanup_helper.cr b/spec/support/cleanup_helper.cr index 898ac67f4..0fb70e9e4 100644 --- a/spec/support/cleanup_helper.cr +++ b/spec/support/cleanup_helper.cr @@ -1,3 +1,5 @@ +require "file_utils" + module CleanupHelper private def cleanup FileUtils.rm_rf("./tmp") diff --git a/spec/tasks/gen/component_spec.cr b/spec/tasks/gen/component_spec.cr index bf8412b1a..bde0e4f96 100644 --- a/spec/tasks/gen/component_spec.cr +++ b/spec/tasks/gen/component_spec.cr @@ -6,47 +6,45 @@ include GeneratorHelper describe Gen::Component do it "generates a component" do with_cleanup do - io = IO::Memory.new valid_name = "Users::Row" - ARGV.push(valid_name) - Gen::Component.new.call(io) + task = Gen::Component.new + task.output = IO::Memory.new + task.print_help_or_call(args: [valid_name]) - should_create_files_with_contents io, + should_create_files_with_contents task.output, "./src/components/users/row.cr": valid_name end end it "generates a root component" do with_cleanup do - io = IO::Memory.new valid_name = "Root" - ARGV.push(valid_name) - Gen::Component.new.call(io) + task = Gen::Component.new + task.output = IO::Memory.new + task.print_help_or_call(args: [valid_name]) - should_create_files_with_contents io, + should_create_files_with_contents task.output, "./src/components/root.cr": valid_name end end it "displays an error if given no arguments" do - io = IO::Memory.new + task = Gen::Component.new + task.output = IO::Memory.new + task.print_help_or_call(args: [] of String) - Gen::Component.new.call(io) - - io.to_s.should contain("Component name is required.") + task.output.to_s.should contain("Component name is required.") end it "displays an error if not given a class" do with_cleanup do - io = IO::Memory.new - invalid_component = "mycomponent" - ARGV.push(invalid_component) - - Gen::Component.new.call(io) + task = Gen::Component.new + task.output = IO::Memory.new + task.print_help_or_call(args: ["mycomponent"]) - io.to_s.should contain("Component name should be camel case") + task.output.to_s.should contain("Component name should be camel case") end end end diff --git a/tasks/gen/action/action_generator.cr b/tasks/gen/action/action_generator.cr index f8dbc34e7..2684b9818 100644 --- a/tasks/gen/action/action_generator.cr +++ b/tasks/gen/action/action_generator.cr @@ -1,24 +1,37 @@ require "colorize" -require "file_utils" -require "teeplate" +require "lucky_template" require "../../../src/lucky/route_inferrer" -class Lucky::ActionTemplate < Teeplate::FileTree +class Lucky::ActionTemplate @name : String @action : String @inherit_from : String @route : String - - directory "#{__DIR__}/../templates/action" + @save_path : String def initialize(@name, @action, @inherit_from, @route) + @save_path = @name.split("::").map(&.underscore.downcase)[0..-2].join('/') + end + + def render(path : Path) + LuckyTemplate.write!(path, template_folder) + end + + def template_folder + LuckyTemplate.create_folder do |root_dir| + root_dir.add_folder(Path["src/actions/#{@save_path}"]) do |actions_dir| + actions_dir.add_file("#{@action}.cr") do |io| + ECR.embed("#{__DIR__}/../templates/action/action.cr.ecr", io) + end + end + end end end module Gen::ActionGenerator private def render_action_template(io, inherit_from : String) if valid? - Lucky::ActionTemplate.new(action_name, action, inherit_from, route).render(output_path) + Lucky::ActionTemplate.new(action_name, action, inherit_from, route).render(Path["."]) io.puts success_message else io.puts @error.colorize(:red) @@ -58,7 +71,7 @@ module Gen::ActionGenerator end private def output_path - "./src/actions/#{path}" + Path["./src/actions/#{path}"] end private def path diff --git a/tasks/gen/action/browser.cr b/tasks/gen/action/browser.cr index 991cdbf4b..b8e637d32 100644 --- a/tasks/gen/action/browser.cr +++ b/tasks/gen/action/browser.cr @@ -1,5 +1,4 @@ require "lucky_task" -require "teeplate" require "./action_generator" require "../page" diff --git a/tasks/gen/component.cr b/tasks/gen/component.cr index 1587117c8..51751d596 100644 --- a/tasks/gen/component.cr +++ b/tasks/gen/component.cr @@ -1,15 +1,25 @@ require "lucky_task" -require "teeplate" +require "lucky_template" require "colorize" -require "file_utils" -class Lucky::ComponentTemplate < Teeplate::FileTree +class Lucky::ComponentTemplate @filename : String @class : String + @output_path : Path - directory "#{__DIR__}/templates/component" + def initialize(@filename, @class, @output_path) + end + + def render(path : Path) + LuckyTemplate.write!(path, template_folder) + end - def initialize(@filename, @class) + def template_folder + LuckyTemplate.create_folder do |root_dir| + root_dir.add_file(Path["#{@output_path}/#{@filename}.cr"]) do |io| + ECR.embed("#{__DIR__}/templates/component/component.cr.ecr", io) + end + end end end @@ -23,12 +33,14 @@ class Gen::Component < LuckyTask::Task lucky gen.component SettingsMenu TEXT - def call(io : IO = STDOUT) + positional_arg :component_class, "The name of the component" + + def call if error - io.puts error.colorize(:red) + output.puts error.colorize(:red) else - Lucky::ComponentTemplate.new(component_filename, component_class).render(output_path) - io.puts success_message + Lucky::ComponentTemplate.new(component_filename, component_class, output_path).render(Path["."]) + output.puts success_message end end @@ -37,9 +49,13 @@ class Gen::Component < LuckyTask::Task end private def missing_name_error - if ARGV.first?.nil? - "Component name is required." - end + # Doing this because `component_class` will raise an exception if the value is missing + # but the error message would say "component_class is missing" which isn't as nice of + # an error message. This lets the UI remain the same until this whole deal can be refactored + component_class + nil + rescue + "Component name is required." end private def invalid_format_error @@ -48,10 +64,6 @@ class Gen::Component < LuckyTask::Task end end - private def component_class - ARGV.first - end - private def component_filename component_class.split("::").last.underscore.downcase end @@ -59,7 +71,7 @@ class Gen::Component < LuckyTask::Task private def output_path parts = component_class.split("::") parts.pop - "./src/components/#{parts.map(&.underscore).map(&.downcase).join("/")}" + Path["./src/components/#{parts.map(&.underscore.downcase).join('/')}"] end private def output_path_with_filename diff --git a/tasks/gen/page.cr b/tasks/gen/page.cr index 99f1d3341..c88144cf7 100644 --- a/tasks/gen/page.cr +++ b/tasks/gen/page.cr @@ -1,17 +1,26 @@ require "lucky_task" -require "teeplate" +require "lucky_template" require "colorize" -require "file_utils" -class Lucky::PageTemplate < Teeplate::FileTree +class Lucky::PageTemplate @page_filename : String @page_class : String - @output_path : String - - directory "#{__DIR__}/templates/page" + @output_path : Path def initialize(@page_filename, @page_class, @output_path) end + + def render(path : Path) + LuckyTemplate.write!(path, template_folder) + end + + def template_folder + LuckyTemplate.create_folder do |root_dir| + root_dir.add_file(Path["#{@output_path}/#{@page_filename}.cr"]) do |io| + ECR.embed("#{__DIR__}/templates/page/page.cr.ecr", io) + end + end + end end class Gen::Page < LuckyTask::Task @@ -30,7 +39,7 @@ class Gen::Page < LuckyTask::Task if error output.puts error.colorize(:red) else - Lucky::PageTemplate.new(page_filename, page_class, output_path).render(output_path) + Lucky::PageTemplate.new(page_filename, page_class, output_path).render(Path["."]) output.puts success_message end end @@ -58,7 +67,7 @@ class Gen::Page < LuckyTask::Task private def output_path page_parts = page_class.split("::") page_parts.pop - "./src/pages/#{page_parts.map(&.underscore).map(&.downcase).join("/")}" + Path["./src/pages/#{page_parts.map(&.underscore.downcase).join('/')}"] end private def output_path_with_filename diff --git a/tasks/gen/task.cr b/tasks/gen/task.cr index c729cbed7..41bed39ad 100644 --- a/tasks/gen/task.cr +++ b/tasks/gen/task.cr @@ -1,16 +1,31 @@ require "lucky_task" -require "teeplate" +require "lucky_template" require "colorize" -require "file_utils" -class Lucky::TaskTemplate < Teeplate::FileTree - directory "#{__DIR__}/templates/task" +class Lucky::TaskTemplate + @save_path : String def initialize( @task_filename : String, @task_name : String, @summary : String ) + @save_path = @task_name.split("::").map(&.underscore.downcase)[0..-2].join('/') + end + + def render(path : Path) + LuckyTemplate.write!(path, template_folder) + end + + def template_folder + LuckyTemplate.create_folder do |root_dir| + save_path = @save_path.presence.nil? ? "tasks" : "tasks/#{@save_path}" + root_dir.add_folder(Path[save_path]) do |tasks_dir| + tasks_dir.add_file(@task_filename) do |io| + ECR.embed("#{__DIR__}/templates/task/task.cr.ecr", io) + end + end + end end end @@ -37,7 +52,7 @@ class Gen::Task < LuckyTask::Task else Lucky::TaskTemplate .new(task_filename, rendered_task_name, rendered_summary) - .render(output_path.to_s) + .render(Path["."]) output.puts <<-TEXT Generated #{output_path.join(task_filename).colorize.green} diff --git a/tasks/gen/templates/action/{{action}}.cr.ecr b/tasks/gen/templates/action/action.cr.ecr similarity index 100% rename from tasks/gen/templates/action/{{action}}.cr.ecr rename to tasks/gen/templates/action/action.cr.ecr diff --git a/tasks/gen/templates/component/{{filename}}.cr.ecr b/tasks/gen/templates/component/component.cr.ecr similarity index 100% rename from tasks/gen/templates/component/{{filename}}.cr.ecr rename to tasks/gen/templates/component/component.cr.ecr diff --git a/tasks/gen/templates/page/{{page_filename}}.cr.ecr b/tasks/gen/templates/page/page.cr.ecr similarity index 100% rename from tasks/gen/templates/page/{{page_filename}}.cr.ecr rename to tasks/gen/templates/page/page.cr.ecr diff --git a/tasks/gen/templates/task/{{task_filename}}.ecr b/tasks/gen/templates/task/task.cr.ecr similarity index 100% rename from tasks/gen/templates/task/{{task_filename}}.ecr rename to tasks/gen/templates/task/task.cr.ecr