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

Replace Teeplate with LuckyTemplate #1861

Merged
merged 8 commits into from
Apr 1, 2024
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
6 changes: 3 additions & 3 deletions shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions spec/support/cleanup_helper.cr
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require "file_utils"

module CleanupHelper
private def cleanup
FileUtils.rm_rf("./tmp")
Expand Down
34 changes: 16 additions & 18 deletions spec/tasks/gen/component_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
27 changes: 20 additions & 7 deletions tasks/gen/action/action_generator.cr
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -58,7 +71,7 @@ module Gen::ActionGenerator
end

private def output_path
"./src/actions/#{path}"
Path["./src/actions/#{path}"]
end

private def path
Expand Down
1 change: 0 additions & 1 deletion tasks/gen/action/browser.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require "lucky_task"
require "teeplate"
require "./action_generator"
require "../page"

Expand Down
46 changes: 29 additions & 17 deletions tasks/gen/component.cr
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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

Expand All @@ -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."
Comment on lines +52 to +58
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is mainly just to keep the output the same as it is now. I have an issue on LuckyTask luckyframework/lucky_task#8 so we can address this later.

end

private def invalid_format_error
Expand All @@ -48,18 +64,14 @@ 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

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
Expand Down
25 changes: 17 additions & 8 deletions tasks/gen/page.cr
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
25 changes: 20 additions & 5 deletions tasks/gen/task.cr
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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}
Expand Down
Loading