lib/ggem/cli/commands.rb in ggem-1.9.5 vs lib/ggem/cli/commands.rb in ggem-1.10.0

- old
+ new

@@ -1,11 +1,12 @@ # frozen_string_literal: true require "ggem/cli/clirb" -require "much-plugin" +require "much-mixin" module GGem; end + class GGem::CLI InvalidCommandError = Class.new(ArgumentError) CommandExitError = Class.new(RuntimeError) class InvalidCommand @@ -14,16 +15,18 @@ def initialize(name) @name = name @clirb = CLIRB.new end - def new; self; end + def new + self + end def run(argv) @clirb.parse!([@name, argv].flatten.compact) raise CLIRB::HelpExit if @name.to_s.empty? - raise InvalidCommandError, "`#{self.name}` is not a command." + raise InvalidCommandError, "`#{name}` is not a command." end def help "Usage: ggem [COMMAND] [options]\n\n" \ "Options: #{@clirb}\n" \ @@ -31,18 +34,20 @@ "#{COMMANDS.to_s.split("\n").map{ |l| " #{l}" }.join("\n")}\n" end end module ValidCommand - include MuchPlugin + include MuchMixin - plugin_instance_methods do + mixin_instance_methods do def initialize(&clirb_build) @clirb = CLIRB.new(&clirb_build) end - def clirb; @clirb; end + def clirb + @clirb + end def run(argv, stdout = nil, stderr = nil) @clirb.parse!(argv) @stdout = stdout || $stdout @stderr = stderr || $stderr @@ -53,39 +58,39 @@ end end end module NotifyCmdCommand - include MuchPlugin + include MuchMixin - plugin_instance_methods do + mixin_instance_methods do private def notify(success_msg, &cmd_block) cmd(&cmd_block) @stdout.puts success_msg end def cmd(&cmd_block) - cmd, status, output = cmd_block.call + cmd, _status, output = cmd_block.call if ENV["DEBUG"] @stdout.puts cmd @stdout.puts output end end end end module GitRepoCommand - include MuchPlugin + include MuchMixin - plugin_included do + mixin_included do include ValidCommand include NotifyCmdCommand end - plugin_instance_methods do + mixin_instance_methods do def initialize(*args) super require "ggem/git_repo" @repo = GGem::GitRepo.new(Dir.pwd) @@ -94,12 +99,12 @@ private def notify(*args, &block) begin super - rescue GGem::GitRepo::CmdError => exception - @stderr.puts exception.message + rescue GGem::GitRepo::CmdError => ex + @stderr.puts ex.message raise CommandExitError end end end end @@ -112,13 +117,13 @@ begin require "ggem/gem" path = GGem::Gem.new(Dir.pwd, @clirb.args.first).save!.path @stdout.puts "created gem in #{path}" - rescue GGem::Gem::NoNameError => exception + rescue GGem::Gem::NoNameError => ex error = ArgumentError.new("GEM-NAME must be provided") - error.set_backtrace(exception.backtrace) + error.set_backtrace(ex.backtrace) raise error end @repo = GGem::GitRepo.new(path) notify("initialized gem git repo"){ @repo.run_init_cmd } @@ -130,43 +135,43 @@ def help "Usage: ggem generate [options] GEM-NAME\n\n" \ "Options: #{@clirb}\n" \ "Description:\n" \ - " #{self.summary}" + " #{summary}" end end module GemspecCommand - include MuchPlugin + include MuchMixin - plugin_included do + mixin_included do include ValidCommand include NotifyCmdCommand end - plugin_instance_methods do + mixin_instance_methods do def initialize(*args) super require "ggem/gemspec" begin @spec = GGem::Gemspec.new(Dir.pwd) - rescue GGem::Gemspec::NotFoundError => exception + rescue GGem::Gemspec::NotFoundError => ex error = ArgumentError.new("There are no gemspecs at #{Dir.pwd}") - error.set_backtrace(exception.backtrace) + error.set_backtrace(ex.backtrace) raise error end end private def notify(*args, &block) begin super - rescue GGem::Gemspec::CmdError => exception - @stderr.puts exception.message + rescue GGem::Gemspec::CmdError => ex + @stderr.puts ex.message raise CommandExitError end end end end @@ -188,11 +193,11 @@ def help "Usage: ggem build [options]\n\n" \ "Options: #{@clirb}\n" \ "Description:\n" \ - " #{self.summary}" + " #{summary}" end end class InstallCommand include GemspecCommand @@ -217,11 +222,11 @@ def help "Usage: ggem install [options]\n\n" \ "Options: #{@clirb}\n" \ "Description:\n" \ - " #{self.summary}" + " #{summary}" end end class PushCommand include GemspecCommand @@ -247,26 +252,26 @@ def help "Usage: ggem push [options]\n\n" \ "Options: #{@clirb}\n" \ "Description:\n" \ - " #{self.summary}" + " #{summary}" end end module ForceTagOptionCommand - include MuchPlugin + include MuchMixin - plugin_included do + mixin_included do include ValidCommand end - plugin_instance_methods do + mixin_instance_methods do def initialize super do option "force-tag", "force tagging even with uncommitted files", { - :abbrev => "f" + abbrev: "f", } end end end end @@ -280,13 +285,13 @@ super begin cmd{ @repo.run_validate_clean_cmd } cmd{ @repo.run_validate_committed_cmd } - rescue GGem::GitRepo::CmdError => err + rescue GGem::GitRepo::CmdError @stderr.puts "There are files that need to be committed first." - if self.clirb.opts["force-tag"] + if clirb.opts["force-tag"] @stderr.puts "Forcing tag anyway..." else raise CommandExitError end end @@ -301,12 +306,12 @@ cmd{ @repo.run_rm_tag_cmd(@spec.version_tag) } raise end @stdout.puts "Pushed git commits and tags." - rescue GGem::GitRepo::CmdError => err - @stderr.puts err.message + rescue GGem::GitRepo::CmdError => ex + @stderr.puts ex.message raise CommandExitError end def summary "Tag #{@spec.version_tag} and push git commits/tags" @@ -314,11 +319,11 @@ def help "Usage: ggem tag [options]\n\n" \ "Options: #{@clirb}\n" \ "Description:\n" \ - " #{self.summary}" + " #{summary}" end end class ReleaseCommand include GemspecCommand @@ -330,11 +335,11 @@ @push_command = PushCommand.new(*args) end def run(argv, *args) super - @tag_command.run(self.clirb.opts["force-tag"] ? ["--force-tag"] : []) + @tag_command.run(clirb.opts["force-tag"] ? ["--force-tag"] : []) @push_command.run([]) end def summary "Tag #{@spec.version_tag} and push built #{@spec.gem_file_name} to " \ @@ -343,27 +348,27 @@ def help "Usage: ggem release [options]\n\n" \ "Options: #{@clirb}\n" \ "Description:\n" \ - " #{self.summary}\n" \ + " #{summary}\n" \ " (macro for running `ggem tag && ggem push`)" end end class CommandSet def initialize(&unknown_cmd_block) - @lookup = Hash.new{ |h,k| unknown_cmd_block.call(k) } + @lookup = Hash.new{ |_h, k| unknown_cmd_block.call(k) } @names = [] @aliases = {} @summaries = {} end def add(klass, name, *aliases) begin cmd = klass.new - rescue StandardError => err + rescue # don't add any commands you can't init else ([name] + aliases).each{ |n| @lookup[n] = cmd } @to_s = nil @names << name @@ -386,14 +391,15 @@ def size @names.size end def to_s - max_name_size = @names.map{ |n| n.size }.max || 0 - max_alias_size = @aliases.values.map{ |v| v.size }.max || 0 + max_name_size = @names.map(&:size).max || 0 + max_alias_size = @aliases.values.map(&:size).max || 0 - @to_s ||= @names.map do |n| - "#{n.ljust(max_name_size)} #{@aliases[n].ljust(max_alias_size)} #{@summaries[n]}" - end.join("\n") + @to_s ||= @names.map{ |n| + "#{n.ljust(max_name_size)} #{@aliases[n].ljust(max_alias_size)} "\ + "#{@summaries[n]}" + }.join("\n") end end end