From 0b8b33d4d69ae352177dd83b65eb3a6ff69e6723 Mon Sep 17 00:00:00 2001 From: Jeremy Woertink Date: Sun, 27 Aug 2023 14:31:44 -0700 Subject: [PATCH] Some code cleanup (#21) * Adding some return types, and cleaning up the method name to register tasks * use the correct types on these helpers --- README.md | 2 +- src/lucky_task/runner.cr | 18 +++++++++++------- src/lucky_task/task.cr | 6 +++--- src/lucky_task/task_not_found_error_message.cr | 6 +++--- src/lucky_task/text_helpers.cr | 6 +++--- 5 files changed, 21 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 0cca29a..e416c89 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,7 @@ class SendDailyNotifications < LuckyTask::Task # Name is inferred from class name ("send_daily_notifications") # It can be overridden: # - # name "app.send_daily_notifications" + # task_name "app.send_daily_notifications" def call # Code that sends notifications to all your users... diff --git a/src/lucky_task/runner.cr b/src/lucky_task/runner.cr index 446f39f..43c37c1 100644 --- a/src/lucky_task/runner.cr +++ b/src/lucky_task/runner.cr @@ -1,10 +1,14 @@ class LuckyTask::Runner @@tasks = [] of LuckyTask::Task - class_property? exit_with_error_if_not_found = true + class_property? exit_with_error_if_not_found : Bool = true extend LuckyTask::TextHelpers - def self.tasks + def self.register_task(task : LuckyTask::Task) : Nil + @@tasks.push(task) + end + + def self.tasks : Array(LuckyTask::Task) @@tasks.sort_by!(&.task_name) end @@ -32,7 +36,7 @@ class LuckyTask::Runner end end - def self.help_text + def self.help_text : Nil puts <<-HELP_TEXT Usage: lucky name.of.task [options] @@ -42,11 +46,11 @@ class LuckyTask::Runner HELP_TEXT end - def self.find_task(task_name : String) + def self.find_task(task_name : String) : LuckyTask::Task? @@tasks.find { |task| task.task_name == task_name } end - def self.tasks_list + def self.tasks_list : String String.build do |list| tasks.each do |task| list << (" #{arrow} " + task.task_name).colorize(:green) @@ -57,11 +61,11 @@ class LuckyTask::Runner end end - def self.list_padding_for(task_name) + def self.list_padding_for(task_name) : String " " * (longest_task_name - task_name.size + 2) end - def self.longest_task_name + def self.longest_task_name : Int32 tasks.max_of(&.task_name.size) end end diff --git a/src/lucky_task/task.cr b/src/lucky_task/task.cr index dc42279..206dc73 100644 --- a/src/lucky_task/task.cr +++ b/src/lucky_task/task.cr @@ -6,7 +6,7 @@ abstract class LuckyTask::Task property output : IO = STDOUT {% if !@type.abstract? %} - LuckyTask::Runner.tasks << self.new + LuckyTask::Runner.register_task(self.new) {% end %} @[Deprecated("Use `task_name` instead.")] @@ -44,7 +44,7 @@ abstract class LuckyTask::Task end macro summary(summary_text) - def summary + def summary : String {{summary_text}} end end @@ -279,5 +279,5 @@ abstract class LuckyTask::Task end abstract def call - abstract def summary + abstract def summary : String end diff --git a/src/lucky_task/task_not_found_error_message.cr b/src/lucky_task/task_not_found_error_message.cr index da2e94f..71f300f 100644 --- a/src/lucky_task/task_not_found_error_message.cr +++ b/src/lucky_task/task_not_found_error_message.cr @@ -4,11 +4,11 @@ class LuckyTask::TaskNotFoundErrorMessage def initialize(@task_name : String, @io : IO = STDERR) end - def self.print(*args) + def self.print(*args) : Nil new(*args).print end - def print + def print : Nil message = "Task #{@task_name.colorize(:cyan)} not found." similar_task_name.try do |name| @@ -18,7 +18,7 @@ class LuckyTask::TaskNotFoundErrorMessage @io.puts message end - private def similar_task_name + private def similar_task_name : String? Levenshtein::Finder.find( @task_name, LuckyTask::Runner.tasks.map(&.task_name), diff --git a/src/lucky_task/text_helpers.cr b/src/lucky_task/text_helpers.cr index 5b8c806..17942ef 100644 --- a/src/lucky_task/text_helpers.cr +++ b/src/lucky_task/text_helpers.cr @@ -1,13 +1,13 @@ module LuckyTask::TextHelpers - def arrow + def arrow : String "▸" end - def red_arrow + def red_arrow : Colorize::Object(String) arrow.colorize(:red) end - def green_arrow + def green_arrow : Colorize::Object(String) arrow.colorize(:green) end end