From 2fa8596302b4dadfb7a58d8529e2f4a8609c0884 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Tue, 6 Jun 2017 14:49:28 +0200 Subject: [PATCH 1/2] Sort `Hbc::installed`. --- Library/Homebrew/cask/lib/hbc/scopes.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/Library/Homebrew/cask/lib/hbc/scopes.rb b/Library/Homebrew/cask/lib/hbc/scopes.rb index 149c2c3435..a469ae0fa6 100644 --- a/Library/Homebrew/cask/lib/hbc/scopes.rb +++ b/Library/Homebrew/cask/lib/hbc/scopes.rb @@ -27,6 +27,7 @@ module Hbc # TODO: speed up Hbc::Source::Tapped (main perf drag is calling Hbc.all_tokens repeatedly) # TODO: ability to specify expected source when calling CaskLoader.load (minor perf benefit) Pathname.glob(caskroom.join("*")) + .sort .map do |caskroom_path| token = caskroom_path.basename.to_s From b40e4afac539cc375597d5ddd0530b8a4b652382 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Sat, 3 Jun 2017 01:29:29 +0200 Subject: [PATCH 2/2] Fix parsing `--require-sha` and `HOMEBREW_CASK_OPTS`. --- Library/Homebrew/cask/lib/hbc/cli.rb | 46 ++++++++++++++----- .../cask/lib/hbc/cli/abstract_command.rb | 1 + Library/Homebrew/cask/lib/hbc/cli/install.rb | 1 - Library/Homebrew/cask/lib/hbc/cli/search.rb | 4 -- Library/Homebrew/test/cask/cli_spec.rb | 2 +- 5 files changed, 36 insertions(+), 18 deletions(-) diff --git a/Library/Homebrew/cask/lib/hbc/cli.rb b/Library/Homebrew/cask/lib/hbc/cli.rb index 99980b88df..eec2788ec9 100644 --- a/Library/Homebrew/cask/lib/hbc/cli.rb +++ b/Library/Homebrew/cask/lib/hbc/cli.rb @@ -92,10 +92,10 @@ module Hbc command.is_a?(Class) && !command.abstract? && command.needs_init? end - def self.run_command(command, *rest) + def self.run_command(command, *args) if command.respond_to?(:run) # usual case: built-in command verb - command.run(*rest) + command.run(*args) elsif require?(which("brewcask-#{command}.rb")) # external command as Ruby library on PATH, Homebrew-style elsif command.to_s.include?("/") && require?(command.to_s) @@ -111,7 +111,7 @@ module Hbc if klass.respond_to?(:run) # invoke "run" on a Ruby library which follows our coding conventions # other Ruby libraries must do everything via "require" - klass.run(*rest) + klass.run(*args) end elsif which("brewcask-#{command}") # arbitrary external executable on PATH, Homebrew-style @@ -124,7 +124,7 @@ module Hbc exec command, *ARGV[1..-1] else # failure - NullCommand.new(command).run + NullCommand.new(command, *args).run end end @@ -136,9 +136,30 @@ module Hbc @args = process_options(*args) end + def detect_command_and_arguments(*args) + command = args.detect do |arg| + if self.class.commands.include?(arg) + true + else + break unless arg.start_with?("-") + end + end + + if index = args.index(command) + args.delete_at(index) + end + + [*command, *args] + end + def run - command_name, *args = *@args - command = help? ? "help" : self.class.lookup_command(command_name) + command_name, *args = detect_command_and_arguments(*@args) + command = if help? + args.unshift(command_name) + "help" + else + self.class.lookup_command(command_name) + end MacOS.full_version = ENV["MACOS_VERSION"] unless ENV["MACOS_VERSION"].nil? @@ -147,7 +168,7 @@ module Hbc self.class.run_command(command, *args) rescue CaskError, CaskSha256MismatchError, ArgumentError, OptionParser::InvalidOption => e msg = e.message - msg << e.backtrace.join("\n") if ARGV.debug? + msg << e.backtrace.join("\n").prepend("\n") if ARGV.debug? onoe msg exit 1 rescue StandardError, ScriptError, NoMemoryError => e @@ -199,18 +220,19 @@ module Hbc end class NullCommand - def initialize(attempted_verb) - @attempted_verb = attempted_verb + def initialize(command, *args) + @command = command + @args = args end def run(*_args) purpose usage - return if @attempted_verb.to_s.strip.empty? - return if @attempted_verb == "help" + return if @command == "help" && @args.empty? - raise ArgumentError, "Unknown command: #{@attempted_verb}" + unknown_command = @args.empty? ? @command : @args.first + raise ArgumentError, "Unknown command: #{unknown_command}" end def purpose diff --git a/Library/Homebrew/cask/lib/hbc/cli/abstract_command.rb b/Library/Homebrew/cask/lib/hbc/cli/abstract_command.rb index cdb7f5ec83..c83fc3e426 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/abstract_command.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/abstract_command.rb @@ -9,6 +9,7 @@ module Hbc option "--debug", :debug, false option "--verbose", :verbose, false option "--outdated", :outdated_only, false + option "--require-sha", :require_sha, false def self.command_name @command_name ||= name.sub(/^.*:/, "").gsub(/(.)([A-Z])/, '\1_\2').downcase diff --git a/Library/Homebrew/cask/lib/hbc/cli/install.rb b/Library/Homebrew/cask/lib/hbc/cli/install.rb index 72f85fc69e..4d0abc6775 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/install.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/install.rb @@ -3,7 +3,6 @@ module Hbc class Install < AbstractCommand option "--force", :force, false option "--skip-cask-deps", :skip_cask_deps, false - option "--require-sha", :require_sha, false def initialize(*) super diff --git a/Library/Homebrew/cask/lib/hbc/cli/search.rb b/Library/Homebrew/cask/lib/hbc/cli/search.rb index b24091aef6..9d1a16f157 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/search.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/search.rb @@ -1,10 +1,6 @@ module Hbc class CLI class Search < AbstractCommand - def initialize(*args) - @args = args - end - def run results = self.class.search(*args) self.class.render_results(*results) diff --git a/Library/Homebrew/test/cask/cli_spec.rb b/Library/Homebrew/test/cask/cli_spec.rb index 569b831de2..caa17b0319 100644 --- a/Library/Homebrew/test/cask/cli_spec.rb +++ b/Library/Homebrew/test/cask/cli_spec.rb @@ -42,7 +42,7 @@ describe Hbc::CLI, :cask do it "prints help output when subcommand receives `--help` flag" do command = Hbc::CLI.new("noop", "--help") - expect(described_class).to receive(:run_command).with("help") + expect(described_class).to receive(:run_command).with("help", "noop") command.run expect(command.help?).to eq(true) end