Skip to content

Commit

Permalink
Define each command's description inside the command class
Browse files Browse the repository at this point in the history
  • Loading branch information
davidstosik committed May 8, 2023
1 parent 721b197 commit c127e09
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 19 deletions.
18 changes: 9 additions & 9 deletions lib/packwerk/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ class Cli

class << self
extend T::Sig
sig { params(name: String, help: String, aliases: T::Array[String]).void }
def register_command(name, help:, aliases: [])
CommandRegistry.register(name, help: help, aliases: aliases)
sig { params(name: String, aliases: T::Array[String]).void }
def register_command(name, aliases: [])
CommandRegistry.register(name, aliases: aliases)
end
end

Expand Down Expand Up @@ -83,10 +83,10 @@ def execute_command(args)
end
end

Cli.register_command("init", help: "set up packwerk")
Cli.register_command("check", help: "run all checks")
Cli.register_command("update-todo", aliases: ["update"], help: "update package_todo.yml files")
Cli.register_command("validate", help: "verify integrity of packwerk and package configuration")
Cli.register_command("version", help: "output packwerk version")
Cli.register_command("help", help: "display help information about packwerk")
Cli.register_command("init")
Cli.register_command("check")
Cli.register_command("update-todo", aliases: ["update"])
Cli.register_command("validate")
Cli.register_command("version")
Cli.register_command("help")
end
15 changes: 15 additions & 0 deletions lib/packwerk/cli/base_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,21 @@ class BaseCommand
extend T::Helpers
abstract!

@description = T.let("", String)

class << self
extend T::Sig

sig { params(description: T.nilable(String)).returns(String) }
def description(description = nil)
if description
@description = description
else
@description
end
end
end

sig do
params(
args: T::Array[String],
Expand Down
2 changes: 2 additions & 0 deletions lib/packwerk/cli/check_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class CheckCommand < BaseCommand
extend T::Sig
include UsesParseRun

description "run all checks"

sig { override.returns(T::Boolean) }
def run
if @files_for_processing.files.empty?
Expand Down
19 changes: 10 additions & 9 deletions lib/packwerk/cli/command_registry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ class CommandRegistry
class << self
extend T::Sig

sig { params(name: String, help: String, aliases: T::Array[String]).void }
def register(name, help:, aliases: [])
registry << new(name, help: help, aliases: aliases)
sig { params(name: String, aliases: T::Array[String]).void }
def register(name, aliases: [])
registry << new(name, aliases: aliases)
end

sig { params(name_or_alias: String).returns(T.nilable(T.class_of(Cli::BaseCommand))) }
Expand All @@ -37,13 +37,9 @@ def registry
sig { returns(String) }
attr_reader :name

sig { returns(String) }
attr_reader :help

sig { params(name: String, help: String, aliases: T::Array[String]).void }
def initialize(name, help:, aliases: [])
sig { params(name: String, aliases: T::Array[String]).void }
def initialize(name, aliases: [])
@name = name
@help = help
@aliases = aliases
end

Expand All @@ -53,6 +49,11 @@ def command_class
Cli.const_get(classname) # rubocop:disable Sorbet/ConstantsFromStrings
end

sig { returns(String) }
def description
command_class.description
end

sig { params(name_or_alias: String).returns(T::Boolean) }
def matches_command?(name_or_alias)
@name == name_or_alias || @aliases.include?(name_or_alias)
Expand Down
4 changes: 3 additions & 1 deletion lib/packwerk/cli/help_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ class Cli
class HelpCommand < BaseCommand
extend T::Sig

description "display help information about packwerk"

sig { override.returns(T::Boolean) }
def run
@err_out.puts(<<~USAGE)
Expand All @@ -23,7 +25,7 @@ def run
sig { returns(String) }
def command_help_lines
CommandRegistry.all.map do |command|
" #{command.name} - #{command.help}"
" #{command.name} - #{command.description}"
end.join("\n")
end
end
Expand Down
2 changes: 2 additions & 0 deletions lib/packwerk/cli/init_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ class Cli
class InitCommand < BaseCommand
extend T::Sig

description "set up packwerk"

sig { override.returns(T::Boolean) }
def run
@out.puts("📦 Initializing Packwerk...")
Expand Down
2 changes: 2 additions & 0 deletions lib/packwerk/cli/update_todo_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class UpdateTodoCommand < BaseCommand
extend T::Sig
include UsesParseRun

description "update package_todo.yml files"

sig { override.returns(T::Boolean) }
def run
if @files_for_processing.files_specified?
Expand Down
2 changes: 2 additions & 0 deletions lib/packwerk/cli/validate_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ class Cli
class ValidateCommand < BaseCommand
extend T::Sig

description "verify integrity of packwerk and package configuration"

sig { override.returns(T::Boolean) }
def run
validator_result = T.let(nil, T.nilable(Validator::Result))
Expand Down
2 changes: 2 additions & 0 deletions lib/packwerk/cli/version_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ class Cli
class VersionCommand < BaseCommand
extend T::Sig

description "output packwerk version"

sig { override.returns(T::Boolean) }
def run
@out.puts(Packwerk::VERSION)
Expand Down

0 comments on commit c127e09

Please sign in to comment.