Skip to content

Commit 3c5c296

Browse files
authored
Replace Teeplate with LuckyTemplate (#1861)
* Swapping out Teeplate for LuckyTemplate. Fixes #1832 * Migrating task generator from teeplate to lucky_template * Migrating page generation from teeplate to lucky_template * Migrating component generation from teeplate to lucky_template * Fixing ActionGenerator ouput * Fixing page generator * Fixing components and tasks generation
1 parent 5ea0f8c commit 3c5c296

File tree

12 files changed

+107
-59
lines changed

12 files changed

+107
-59
lines changed

shard.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ dependencies:
5454
pulsar:
5555
github: luckyframework/pulsar
5656
version: ~> 0.2.3
57-
teeplate:
58-
github: luckyframework/teeplate
59-
version: ~> 0.8.5
57+
lucky_template:
58+
github: luckyframework/lucky_template
59+
version: ~> 0.1.0
6060

6161
development_dependencies:
6262
ameba:

spec/support/cleanup_helper.cr

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require "file_utils"
2+
13
module CleanupHelper
24
private def cleanup
35
FileUtils.rm_rf("./tmp")

spec/tasks/gen/component_spec.cr

+16-18
Original file line numberDiff line numberDiff line change
@@ -6,47 +6,45 @@ include GeneratorHelper
66
describe Gen::Component do
77
it "generates a component" do
88
with_cleanup do
9-
io = IO::Memory.new
109
valid_name = "Users::Row"
11-
ARGV.push(valid_name)
1210

13-
Gen::Component.new.call(io)
11+
task = Gen::Component.new
12+
task.output = IO::Memory.new
13+
task.print_help_or_call(args: [valid_name])
1414

15-
should_create_files_with_contents io,
15+
should_create_files_with_contents task.output,
1616
"./src/components/users/row.cr": valid_name
1717
end
1818
end
1919

2020
it "generates a root component" do
2121
with_cleanup do
22-
io = IO::Memory.new
2322
valid_name = "Root"
24-
ARGV.push(valid_name)
2523

26-
Gen::Component.new.call(io)
24+
task = Gen::Component.new
25+
task.output = IO::Memory.new
26+
task.print_help_or_call(args: [valid_name])
2727

28-
should_create_files_with_contents io,
28+
should_create_files_with_contents task.output,
2929
"./src/components/root.cr": valid_name
3030
end
3131
end
3232

3333
it "displays an error if given no arguments" do
34-
io = IO::Memory.new
34+
task = Gen::Component.new
35+
task.output = IO::Memory.new
36+
task.print_help_or_call(args: [] of String)
3537

36-
Gen::Component.new.call(io)
37-
38-
io.to_s.should contain("Component name is required.")
38+
task.output.to_s.should contain("Component name is required.")
3939
end
4040

4141
it "displays an error if not given a class" do
4242
with_cleanup do
43-
io = IO::Memory.new
44-
invalid_component = "mycomponent"
45-
ARGV.push(invalid_component)
46-
47-
Gen::Component.new.call(io)
43+
task = Gen::Component.new
44+
task.output = IO::Memory.new
45+
task.print_help_or_call(args: ["mycomponent"])
4846

49-
io.to_s.should contain("Component name should be camel case")
47+
task.output.to_s.should contain("Component name should be camel case")
5048
end
5149
end
5250
end

tasks/gen/action/action_generator.cr

+20-7
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,37 @@
11
require "colorize"
2-
require "file_utils"
3-
require "teeplate"
2+
require "lucky_template"
43
require "../../../src/lucky/route_inferrer"
54

6-
class Lucky::ActionTemplate < Teeplate::FileTree
5+
class Lucky::ActionTemplate
76
@name : String
87
@action : String
98
@inherit_from : String
109
@route : String
11-
12-
directory "#{__DIR__}/../templates/action"
10+
@save_path : String
1311

1412
def initialize(@name, @action, @inherit_from, @route)
13+
@save_path = @name.split("::").map(&.underscore.downcase)[0..-2].join('/')
14+
end
15+
16+
def render(path : Path)
17+
LuckyTemplate.write!(path, template_folder)
18+
end
19+
20+
def template_folder
21+
LuckyTemplate.create_folder do |root_dir|
22+
root_dir.add_folder(Path["src/actions/#{@save_path}"]) do |actions_dir|
23+
actions_dir.add_file("#{@action}.cr") do |io|
24+
ECR.embed("#{__DIR__}/../templates/action/action.cr.ecr", io)
25+
end
26+
end
27+
end
1528
end
1629
end
1730

1831
module Gen::ActionGenerator
1932
private def render_action_template(io, inherit_from : String)
2033
if valid?
21-
Lucky::ActionTemplate.new(action_name, action, inherit_from, route).render(output_path)
34+
Lucky::ActionTemplate.new(action_name, action, inherit_from, route).render(Path["."])
2235
io.puts success_message
2336
else
2437
io.puts @error.colorize(:red)
@@ -58,7 +71,7 @@ module Gen::ActionGenerator
5871
end
5972

6073
private def output_path
61-
"./src/actions/#{path}"
74+
Path["./src/actions/#{path}"]
6275
end
6376

6477
private def path

tasks/gen/action/browser.cr

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
require "lucky_task"
2-
require "teeplate"
32
require "./action_generator"
43
require "../page"
54

tasks/gen/component.cr

+29-17
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
11
require "lucky_task"
2-
require "teeplate"
2+
require "lucky_template"
33
require "colorize"
4-
require "file_utils"
54

6-
class Lucky::ComponentTemplate < Teeplate::FileTree
5+
class Lucky::ComponentTemplate
76
@filename : String
87
@class : String
8+
@output_path : Path
99

10-
directory "#{__DIR__}/templates/component"
10+
def initialize(@filename, @class, @output_path)
11+
end
12+
13+
def render(path : Path)
14+
LuckyTemplate.write!(path, template_folder)
15+
end
1116

12-
def initialize(@filename, @class)
17+
def template_folder
18+
LuckyTemplate.create_folder do |root_dir|
19+
root_dir.add_file(Path["#{@output_path}/#{@filename}.cr"]) do |io|
20+
ECR.embed("#{__DIR__}/templates/component/component.cr.ecr", io)
21+
end
22+
end
1323
end
1424
end
1525

@@ -23,12 +33,14 @@ class Gen::Component < LuckyTask::Task
2333
lucky gen.component SettingsMenu
2434
TEXT
2535

26-
def call(io : IO = STDOUT)
36+
positional_arg :component_class, "The name of the component"
37+
38+
def call
2739
if error
28-
io.puts error.colorize(:red)
40+
output.puts error.colorize(:red)
2941
else
30-
Lucky::ComponentTemplate.new(component_filename, component_class).render(output_path)
31-
io.puts success_message
42+
Lucky::ComponentTemplate.new(component_filename, component_class, output_path).render(Path["."])
43+
output.puts success_message
3244
end
3345
end
3446

@@ -37,9 +49,13 @@ class Gen::Component < LuckyTask::Task
3749
end
3850

3951
private def missing_name_error
40-
if ARGV.first?.nil?
41-
"Component name is required."
42-
end
52+
# Doing this because `component_class` will raise an exception if the value is missing
53+
# but the error message would say "component_class is missing" which isn't as nice of
54+
# an error message. This lets the UI remain the same until this whole deal can be refactored
55+
component_class
56+
nil
57+
rescue
58+
"Component name is required."
4359
end
4460

4561
private def invalid_format_error
@@ -48,18 +64,14 @@ class Gen::Component < LuckyTask::Task
4864
end
4965
end
5066

51-
private def component_class
52-
ARGV.first
53-
end
54-
5567
private def component_filename
5668
component_class.split("::").last.underscore.downcase
5769
end
5870

5971
private def output_path
6072
parts = component_class.split("::")
6173
parts.pop
62-
"./src/components/#{parts.map(&.underscore).map(&.downcase).join("/")}"
74+
Path["./src/components/#{parts.map(&.underscore.downcase).join('/')}"]
6375
end
6476

6577
private def output_path_with_filename

tasks/gen/page.cr

+17-8
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
11
require "lucky_task"
2-
require "teeplate"
2+
require "lucky_template"
33
require "colorize"
4-
require "file_utils"
54

6-
class Lucky::PageTemplate < Teeplate::FileTree
5+
class Lucky::PageTemplate
76
@page_filename : String
87
@page_class : String
9-
@output_path : String
10-
11-
directory "#{__DIR__}/templates/page"
8+
@output_path : Path
129

1310
def initialize(@page_filename, @page_class, @output_path)
1411
end
12+
13+
def render(path : Path)
14+
LuckyTemplate.write!(path, template_folder)
15+
end
16+
17+
def template_folder
18+
LuckyTemplate.create_folder do |root_dir|
19+
root_dir.add_file(Path["#{@output_path}/#{@page_filename}.cr"]) do |io|
20+
ECR.embed("#{__DIR__}/templates/page/page.cr.ecr", io)
21+
end
22+
end
23+
end
1524
end
1625

1726
class Gen::Page < LuckyTask::Task
@@ -30,7 +39,7 @@ class Gen::Page < LuckyTask::Task
3039
if error
3140
output.puts error.colorize(:red)
3241
else
33-
Lucky::PageTemplate.new(page_filename, page_class, output_path).render(output_path)
42+
Lucky::PageTemplate.new(page_filename, page_class, output_path).render(Path["."])
3443
output.puts success_message
3544
end
3645
end
@@ -58,7 +67,7 @@ class Gen::Page < LuckyTask::Task
5867
private def output_path
5968
page_parts = page_class.split("::")
6069
page_parts.pop
61-
"./src/pages/#{page_parts.map(&.underscore).map(&.downcase).join("/")}"
70+
Path["./src/pages/#{page_parts.map(&.underscore.downcase).join('/')}"]
6271
end
6372

6473
private def output_path_with_filename

tasks/gen/task.cr

+20-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,31 @@
11
require "lucky_task"
2-
require "teeplate"
2+
require "lucky_template"
33
require "colorize"
4-
require "file_utils"
54

6-
class Lucky::TaskTemplate < Teeplate::FileTree
7-
directory "#{__DIR__}/templates/task"
5+
class Lucky::TaskTemplate
6+
@save_path : String
87

98
def initialize(
109
@task_filename : String,
1110
@task_name : String,
1211
@summary : String
1312
)
13+
@save_path = @task_name.split("::").map(&.underscore.downcase)[0..-2].join('/')
14+
end
15+
16+
def render(path : Path)
17+
LuckyTemplate.write!(path, template_folder)
18+
end
19+
20+
def template_folder
21+
LuckyTemplate.create_folder do |root_dir|
22+
save_path = @save_path.presence.nil? ? "tasks" : "tasks/#{@save_path}"
23+
root_dir.add_folder(Path[save_path]) do |tasks_dir|
24+
tasks_dir.add_file(@task_filename) do |io|
25+
ECR.embed("#{__DIR__}/templates/task/task.cr.ecr", io)
26+
end
27+
end
28+
end
1429
end
1530
end
1631

@@ -37,7 +52,7 @@ class Gen::Task < LuckyTask::Task
3752
else
3853
Lucky::TaskTemplate
3954
.new(task_filename, rendered_task_name, rendered_summary)
40-
.render(output_path.to_s)
55+
.render(Path["."])
4156

4257
output.puts <<-TEXT
4358
Generated #{output_path.join(task_filename).colorize.green}

0 commit comments

Comments
 (0)