Skip to content

Commit

Permalink
Fixing the compilation issue. The methods now should really direct yo…
Browse files Browse the repository at this point in the history
…u to the macros for overriding
  • Loading branch information
jwoertink committed Sep 22, 2023
1 parent d8798b2 commit 6f63283
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 17 deletions.
2 changes: 1 addition & 1 deletion spec/lucky_task/runner_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ include HaveDefaultHelperMessageMatcher

describe LuckyTask::Runner do
it "adds tasks to the runner when task classes are created" do
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"]
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"]

task_names = LuckyTask::Runner.tasks.map(&.task_name)
task_names.size.should eq(expected_task_names.size)
Expand Down
22 changes: 17 additions & 5 deletions spec/lucky_task/task_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ end

describe LuckyTask::Task do
it "creates a task_name from the class name when inheriting" do
My::CoolTask.new.task_name.should eq "my.cool_task"
My::CoolTask.task_name.should eq "my.cool_task"
end

it "uses a specified task_name over the auto generated task_name" do
Some::Other::Task.new.task_name.should eq "my.custom_name"
Some::Other::Task.task_name.should eq "my.custom_name"
end

it "creates summary text" do
My::CoolTask.new.summary.should eq "This task does something awesome"
My::CoolTask.task_summary.should eq "This task does something awesome"
end

it "has a default help message" do
message = My::CoolTask.new.help_message
message = My::CoolTask.task_help_message
message.should contain "Run this task with 'lucky my.cool_task'"
message.should contain(My::CoolTask.new.summary)
message.should contain(My::CoolTask.task_summary)
end

describe "print_help_or_call" do
Expand Down Expand Up @@ -161,4 +161,16 @@ describe LuckyTask::Task do
task.output.to_s.should contain "Fancy output"
end
end

describe "methods that used to conflict" do
it "allows you to name the args whatever" do
task = TaskWithSimilarMethodNames.new
task.print_help_or_call(args: ["--name=name", "--task-name=task-name", "--summary=summary", "--task-summary=task-summary", "--help-message=help-message"]).as(TaskWithSimilarMethodNames)
task.name.should eq("name")
task.task_name.should eq("task-name")
task.summary.should eq("summary")
task.task_summary.should eq("task-summary")
task.help_message.should eq("help-message")
end
end
end
16 changes: 13 additions & 3 deletions spec/support/tasks.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ end

class Some::Other::Task < LuckyTask::Task
summary "bar"
task_name "my.custom_name"
name "my.custom_name"
help_message "Custom help message"

def call
Expand Down Expand Up @@ -94,10 +94,20 @@ class TaskWithPositionalArgs < LuckyTask::Task
end

class TaskWithFancyOutput < LuckyTask::Task
summary "This is a task with some fancy output"

def call
output.puts "Fancy output".colorize.green
self
end
end

class TaskWithSimilarMethodNames < LuckyTask::Task
arg :name, "Using name"
arg :task_name, "Using task_name"
arg :summary, "Using summary"
arg :task_summary, "Using task_summary"
arg :help_message, "Using help_message"

def call
self
end
end
2 changes: 1 addition & 1 deletion src/lucky_task/runner.cr
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class LuckyTask::Runner
tasks.each do |task|
list << (" #{arrow} " + task.task_name).colorize(:green)
list << list_padding_for(task.task_name)
list << task.summary
list << task.task_summary
list << "\n"
end
end
Expand Down
14 changes: 7 additions & 7 deletions src/lucky_task/task.cr
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,24 @@ abstract class LuckyTask::Task

# By default, task summaries are optional.
# Use the `summary` macro to define a custom summary
def self.summary : String
def self.task_summary : String
""
end

# The help text to be displayed when a help flag
# is passed in (e.g. -h, --help)
# Use the `help_message`
def self.help_message : String
def self.task_help_message : String
<<-TEXT.strip
#{summary}
#{task_summary}
Run this task with 'lucky #{task_name}'
TEXT
end

def print_help_or_call(args : Array(String))
if wants_help_message?(args)
output.puts self.class.help_message
output.puts self.class.task_help_message
else
\{% for opt in @type.constant(:PARSER_OPTS) %}
set_opt_for_\{{ opt.id }}(args)
Expand All @@ -54,7 +54,7 @@ abstract class LuckyTask::Task
# This is used in the help_text when a help flag is passed
# to the task through the CLI
macro summary(summary_text)
def self.summary : String
def self.task_summary : String
{{summary_text}}
end
end
Expand All @@ -73,7 +73,7 @@ abstract class LuckyTask::Task
# # other methods, etc.
# end
# ```
macro task_name(name_text)
macro name(name_text)
def self.task_name : String
{{name_text}}
end
Expand All @@ -90,7 +90,7 @@ abstract class LuckyTask::Task
# end
# ```
macro help_message(help_text)
def self.help_message : String
def self.task_help_message : String
{{help_text}}
end
end
Expand Down

0 comments on commit 6f63283

Please sign in to comment.