Skip to content

Commit

Permalink
Some code cleanup (#21)
Browse files Browse the repository at this point in the history
* Adding some return types, and cleaning up the method name to register tasks

* use the correct types on these helpers
  • Loading branch information
jwoertink authored Aug 27, 2023
1 parent f6c9c2f commit 0b8b33d
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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...
Expand Down
18 changes: 11 additions & 7 deletions src/lucky_task/runner.cr
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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]
Expand All @@ -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)
Expand All @@ -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
6 changes: 3 additions & 3 deletions src/lucky_task/task.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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.")]
Expand Down Expand Up @@ -44,7 +44,7 @@ abstract class LuckyTask::Task
end

macro summary(summary_text)
def summary
def summary : String
{{summary_text}}
end
end
Expand Down Expand Up @@ -279,5 +279,5 @@ abstract class LuckyTask::Task
end

abstract def call
abstract def summary
abstract def summary : String
end
6 changes: 3 additions & 3 deletions src/lucky_task/task_not_found_error_message.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand All @@ -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),
Expand Down
6 changes: 3 additions & 3 deletions src/lucky_task/text_helpers.cr
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 0b8b33d

Please sign in to comment.