Skip to content

Commit 1acdb48

Browse files
authored
Refactor internal API (#25)
* refactoring how the internal method API is handled to avoid name collisions. Fixes #23 * oops, missing something * Fixing the compilation issue. The methods now should really direct you to the macros for overriding
1 parent c657bc5 commit 1acdb48

File tree

5 files changed

+80
-45
lines changed

5 files changed

+80
-45
lines changed

spec/lucky_task/runner_spec.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ include HaveDefaultHelperMessageMatcher
44

55
describe LuckyTask::Runner do
66
it "adds tasks to the runner when task classes are created" do
7-
expected_task_names = ["another_task", "my.cool_task", "my.custom_name", "task_with_args", "task_with_required_format_args", "task_with_switch_flags", "task_with_int32_flags", "task_with_float64_flags", "task_with_positional_args", "task_with_fancy_output"]
7+
expected_task_names = ["another_task", "my.cool_task", "my.custom_name", "task_with_args", "task_with_required_format_args", "task_with_switch_flags", "task_with_int32_flags", "task_with_float64_flags", "task_with_positional_args", "task_with_fancy_output", "task_with_similar_method_names"]
88

99
task_names = LuckyTask::Runner.tasks.map(&.task_name)
1010
task_names.size.should eq(expected_task_names.size)

spec/lucky_task/task_spec.cr

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,21 @@ end
77

88
describe LuckyTask::Task do
99
it "creates a task_name from the class name when inheriting" do
10-
My::CoolTask.new.task_name.should eq "my.cool_task"
10+
My::CoolTask.task_name.should eq "my.cool_task"
1111
end
1212

1313
it "uses a specified task_name over the auto generated task_name" do
14-
Some::Other::Task.new.task_name.should eq "my.custom_name"
14+
Some::Other::Task.task_name.should eq "my.custom_name"
1515
end
1616

1717
it "creates summary text" do
18-
My::CoolTask.new.summary.should eq "This task does something awesome"
18+
My::CoolTask.task_summary.should eq "This task does something awesome"
1919
end
2020

2121
it "has a default help message" do
22-
message = My::CoolTask.new.help_message
22+
message = My::CoolTask.task_help_message
2323
message.should contain "Run this task with 'lucky my.cool_task'"
24-
message.should contain(My::CoolTask.new.summary)
24+
message.should contain(My::CoolTask.task_summary)
2525
end
2626

2727
describe "print_help_or_call" do
@@ -161,4 +161,16 @@ describe LuckyTask::Task do
161161
task.output.to_s.should contain "Fancy output"
162162
end
163163
end
164+
165+
describe "methods that used to conflict" do
166+
it "allows you to name the args whatever" do
167+
task = TaskWithSimilarMethodNames.new
168+
task.print_help_or_call(args: ["--name=name", "--task-name=task-name", "--summary=summary", "--task-summary=task-summary", "--help-message=help-message"]).as(TaskWithSimilarMethodNames)
169+
task.name.should eq("name")
170+
task.task_name.should eq("task-name")
171+
task.summary.should eq("summary")
172+
task.task_summary.should eq("task-summary")
173+
task.help_message.should eq("help-message")
174+
end
175+
end
164176
end

spec/support/tasks.cr

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,8 @@ end
88

99
class Some::Other::Task < LuckyTask::Task
1010
summary "bar"
11-
task_name "my.custom_name"
12-
13-
def help_message
14-
"Custom help message"
15-
end
11+
name "my.custom_name"
12+
help_message "Custom help message"
1613

1714
def call
1815
end
@@ -97,10 +94,20 @@ class TaskWithPositionalArgs < LuckyTask::Task
9794
end
9895

9996
class TaskWithFancyOutput < LuckyTask::Task
100-
summary "This is a task with some fancy output"
101-
10297
def call
10398
output.puts "Fancy output".colorize.green
10499
self
105100
end
106101
end
102+
103+
class TaskWithSimilarMethodNames < LuckyTask::Task
104+
arg :name, "Using name"
105+
arg :task_name, "Using task_name"
106+
arg :summary, "Using summary"
107+
arg :task_summary, "Using task_summary"
108+
arg :help_message, "Using help_message"
109+
110+
def call
111+
self
112+
end
113+
end

src/lucky_task/runner.cr

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
class LuckyTask::Runner
2-
@@tasks = [] of LuckyTask::Task
2+
@@tasks = [] of LuckyTask::Task.class
33
class_property? exit_with_error_if_not_found : Bool = true
44

55
extend LuckyTask::TextHelpers
66

7-
def self.register_task(task : LuckyTask::Task) : Nil
7+
def self.register_task(task : LuckyTask::Task.class) : Nil
88
@@tasks.push(task)
99
end
1010

11-
def self.tasks : Array(LuckyTask::Task)
11+
def self.tasks : Array(LuckyTask::Task.class)
1212
@@tasks.sort_by!(&.task_name)
1313
end
1414

@@ -21,7 +21,7 @@ class LuckyTask::Runner
2121
io.puts <<-HELP_TEXT
2222
Missing a task name
2323
24-
To see a list of available tasks, run #{"lucky --help".colorize(:green)}
24+
To see a list of available tasks, run #{"lucky tasks".colorize(:green)}
2525
HELP_TEXT
2626
else
2727
if task = find_task(task_name)
@@ -47,15 +47,18 @@ class LuckyTask::Runner
4747
end
4848

4949
def self.find_task(task_name : String) : LuckyTask::Task?
50-
@@tasks.find { |task| task.task_name == task_name }
50+
found_task = @@tasks.find { |task| task.task_name == task_name }
51+
if found_task
52+
found_task.new
53+
end
5154
end
5255

5356
def self.tasks_list : String
5457
String.build do |list|
5558
tasks.each do |task|
5659
list << (" #{arrow} " + task.task_name).colorize(:green)
5760
list << list_padding_for(task.task_name)
58-
list << task.summary
61+
list << task.task_summary
5962
list << "\n"
6063
end
6164
end

src/lucky_task/task.cr

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,35 @@ abstract class LuckyTask::Task
66
property output : IO = STDOUT
77

88
{% if !@type.abstract? %}
9-
LuckyTask::Runner.register_task(self.new)
9+
LuckyTask::Runner.register_task(self)
1010
{% end %}
1111

12-
@[Deprecated("Use `task_name` instead.")]
13-
def name : String
14-
task_name
12+
# The name of your task as derived by the class name
13+
# Use the `task_name` macro to define a custom task name
14+
def self.task_name : String
15+
"{{@type.name.gsub(/::/, ".").underscore}}"
1516
end
1617

17-
def task_name : String
18-
"{{@type.name.gsub(/::/, ".").underscore}}"
18+
# By default, task summaries are optional.
19+
# Use the `summary` macro to define a custom summary
20+
def self.task_summary : String
21+
""
1922
end
2023

21-
def help_message : String
22-
<<-TEXT
23-
#{summary}
24+
# The help text to be displayed when a help flag
25+
# is passed in (e.g. -h, --help)
26+
# Use the `help_message`
27+
def self.task_help_message : String
28+
<<-TEXT.strip
29+
#{task_summary}
2430
2531
Run this task with 'lucky #{task_name}'
2632
TEXT
2733
end
2834

2935
def print_help_or_call(args : Array(String))
3036
if wants_help_message?(args)
31-
output.puts help_message
37+
output.puts self.class.task_help_message
3238
else
3339
\{% for opt in @type.constant(:PARSER_OPTS) %}
3440
set_opt_for_\{{ opt.id }}(args)
@@ -43,24 +49,16 @@ abstract class LuckyTask::Task
4349
end
4450
end
4551

52+
# The general description of what this task does
53+
#
54+
# This is used in the help_text when a help flag is passed
55+
# to the task through the CLI
4656
macro summary(summary_text)
47-
def summary : String
57+
def self.task_summary : String
4858
{{summary_text}}
4959
end
5060
end
5161

52-
# DEPRECATED: Use `task_name` instead
53-
macro name(name_text)
54-
@[Deprecated("Use `task_name` instead.")]
55-
def name
56-
task_name
57-
end
58-
59-
def task_name : String
60-
{{name_text}}
61-
end
62-
end
63-
6462
# Renames the task name for CLI use
6563
#
6664
# By default the task name is derived from the full module and class name.
@@ -75,12 +73,28 @@ abstract class LuckyTask::Task
7573
# # other methods, etc.
7674
# end
7775
# ```
78-
macro task_name(name_text)
79-
def task_name : String
76+
macro name(name_text)
77+
def self.task_name : String
8078
{{name_text}}
8179
end
8280
end
8381

82+
# Customize your help message with the provided `help_text`
83+
#
84+
# ```
85+
# class KeyGen < LuckyTask::Task
86+
# summary "Generate a new key"
87+
# help_message "Call lucky key_gen to generate a new key"
88+
#
89+
# # other methods, etc.
90+
# end
91+
# ```
92+
macro help_message(help_text)
93+
def self.task_help_message : String
94+
{{help_text}}
95+
end
96+
end
97+
8498
# Creates a method of `arg_name` that returns the value passed in from the CLI.
8599
# The CLI arg position is based on the order in which `positional_arg` is specified
86100
# with the first call being position 0, and so on.
@@ -279,5 +293,4 @@ abstract class LuckyTask::Task
279293
end
280294

281295
abstract def call
282-
abstract def summary : String
283296
end

0 commit comments

Comments
 (0)