Skip to content

Commit

Permalink
Remove abbrev dependency (#419)
Browse files Browse the repository at this point in the history
Our use of the `abbrev` gem is simple enough that we can replace it with
our own simple implementation. This eliminates the gem dependency so
that tomo can continue to have "minimal dependencies", as advertised in
the README.
  • Loading branch information
mattbrictson authored Dec 26, 2023
1 parent bbf9d9c commit 0aade77
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
11 changes: 8 additions & 3 deletions lib/tomo/cli.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require "abbrev"

module Tomo
class CLI
autoload :Command, "tomo/cli/command"
Expand Down Expand Up @@ -63,14 +61,21 @@ def lookup_command(argv)
commands = COMMANDS.merge(COMMAND_ALIASES)

command_name = argv.first unless Completions.active? && argv.length == 1
command_name = Abbrev.abbrev(commands.keys)[command_name]
command_name = expand_abbrev(commands.keys, command_name)
argv.shift if command_name

command_name = "run" if command_name.nil? && task_format?(argv.first)
command = commands[command_name] || Tomo::Commands::Default
[command, command_name]
end

def expand_abbrev(names, abbrev)
return nil if abbrev.to_s.empty?

matches = names.select { |name| name.start_with?(abbrev) }
matches.first if matches.one?
end

def task_format?(arg)
arg.to_s.match?(/\A\S+:\S*\z/)
end
Expand Down
8 changes: 8 additions & 0 deletions test/tomo/cli_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ def test_execute_task_with_implicit_run_command
assert_match "Simulated bundler:install", @tester.stdout
end

def test_commands_can_be_abbreviated
stdout, = capture_io { @tester.run "i", "--help" }
assert_match "Usage: tomo init", stdout

stdout, = capture_io { @tester.run "d", "--help" }
assert_match "Usage: tomo deploy", stdout
end

def test_dash_t_is_alias_for_tasks
@tester.run "init"
@tester.run "-T"
Expand Down
2 changes: 0 additions & 2 deletions tomo.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,4 @@ Gem::Specification.new do |spec|
spec.bindir = "exe"
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]

spec.add_dependency "abbrev"
end

0 comments on commit 0aade77

Please sign in to comment.