From ddf5583208e473ef00447834a3a712411e61e190 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Tue, 4 Oct 2016 15:24:58 +0200 Subject: Refactor Cask’s extensions. --- Library/Homebrew/cask/lib/hbc.rb | 1 - Library/Homebrew/cask/lib/hbc/artifact/artifact.rb | 4 +++- Library/Homebrew/cask/lib/hbc/artifact/pkg.rb | 4 +++- Library/Homebrew/cask/lib/hbc/artifact/relocated.rb | 4 +++- Library/Homebrew/cask/lib/hbc/cli.rb | 2 ++ Library/Homebrew/cask/lib/hbc/extend.rb | 6 ------ Library/Homebrew/cask/lib/hbc/extend/hash.rb | 7 ------- Library/Homebrew/cask/lib/hbc/extend/io.rb | 10 ---------- Library/Homebrew/cask/lib/hbc/extend/optparse.rb | 6 ------ Library/Homebrew/cask/lib/hbc/extend/string.rb | 5 ----- Library/Homebrew/cask/lib/hbc/system_command.rb | 7 ++++++- Library/Homebrew/cask/lib/hbc/utils/hash_validator.rb | 7 +++++++ Library/Homebrew/diagnostic.rb | 2 +- Library/Homebrew/extend/io.rb | 10 ++++++++++ Library/Homebrew/extend/optparse.rb | 6 ++++++ Library/Homebrew/test/test_diagnostic.rb | 2 +- 16 files changed, 42 insertions(+), 41 deletions(-) delete mode 100644 Library/Homebrew/cask/lib/hbc/extend.rb delete mode 100644 Library/Homebrew/cask/lib/hbc/extend/hash.rb delete mode 100644 Library/Homebrew/cask/lib/hbc/extend/io.rb delete mode 100644 Library/Homebrew/cask/lib/hbc/extend/optparse.rb delete mode 100644 Library/Homebrew/cask/lib/hbc/extend/string.rb create mode 100644 Library/Homebrew/cask/lib/hbc/utils/hash_validator.rb create mode 100644 Library/Homebrew/extend/io.rb create mode 100644 Library/Homebrew/extend/optparse.rb (limited to 'Library/Homebrew') diff --git a/Library/Homebrew/cask/lib/hbc.rb b/Library/Homebrew/cask/lib/hbc.rb index 62b391638..69b6e8f21 100644 --- a/Library/Homebrew/cask/lib/hbc.rb +++ b/Library/Homebrew/cask/lib/hbc.rb @@ -1,7 +1,6 @@ module Hbc; end require "hardware" -require "hbc/extend" require "hbc/artifact" require "hbc/audit" require "hbc/auditor" diff --git a/Library/Homebrew/cask/lib/hbc/artifact/artifact.rb b/Library/Homebrew/cask/lib/hbc/artifact/artifact.rb index cb35821cc..b42db877d 100644 --- a/Library/Homebrew/cask/lib/hbc/artifact/artifact.rb +++ b/Library/Homebrew/cask/lib/hbc/artifact/artifact.rb @@ -1,5 +1,7 @@ require "hbc/artifact/moved" +require "hbc/utils/hash_validator" + module Hbc module Artifact class Artifact < Moved @@ -16,7 +18,7 @@ module Hbc raise CaskInvalidError.new(@cask.token, "no source given for artifact") if source_string.nil? @source = @cask.staged_path.join(source_string) raise CaskInvalidError.new(@cask.token, "target required for generic artifact #{source_string}") unless target_hash.is_a?(Hash) - target_hash.assert_valid_keys(:target) + target_hash.extend(HashValidator).assert_valid_keys(:target) @target = Pathname.new(target_hash[:target]) end end diff --git a/Library/Homebrew/cask/lib/hbc/artifact/pkg.rb b/Library/Homebrew/cask/lib/hbc/artifact/pkg.rb index e590a9082..372740631 100644 --- a/Library/Homebrew/cask/lib/hbc/artifact/pkg.rb +++ b/Library/Homebrew/cask/lib/hbc/artifact/pkg.rb @@ -1,5 +1,7 @@ require "hbc/artifact/base" +require "hbc/utils/hash_validator" + module Hbc module Artifact class Pkg < Base @@ -14,7 +16,7 @@ module Hbc @pkg_install_opts = pkg_description.shift begin if @pkg_install_opts.respond_to?(:keys) - @pkg_install_opts.assert_valid_keys(:allow_untrusted) + @pkg_install_opts.extend(HashValidator).assert_valid_keys(:allow_untrusted) elsif @pkg_install_opts raise end diff --git a/Library/Homebrew/cask/lib/hbc/artifact/relocated.rb b/Library/Homebrew/cask/lib/hbc/artifact/relocated.rb index 953045b32..1b5a4ff47 100644 --- a/Library/Homebrew/cask/lib/hbc/artifact/relocated.rb +++ b/Library/Homebrew/cask/lib/hbc/artifact/relocated.rb @@ -1,5 +1,7 @@ require "hbc/artifact/base" +require "hbc/utils/hash_validator" + module Hbc module Artifact class Relocated < Base @@ -46,7 +48,7 @@ module Hbc @source = @cask.staged_path.join(source_string) if target_hash raise CaskInvalidError unless target_hash.respond_to?(:keys) - target_hash.assert_valid_keys(:target) + target_hash.extend(HashValidator).assert_valid_keys(:target) @target = Hbc.send(self.class.artifact_dirmethod).join(target_hash[:target]) else @target = Hbc.send(self.class.artifact_dirmethod).join(source.basename) diff --git a/Library/Homebrew/cask/lib/hbc/cli.rb b/Library/Homebrew/cask/lib/hbc/cli.rb index f637ae7af..2173a52d0 100644 --- a/Library/Homebrew/cask/lib/hbc/cli.rb +++ b/Library/Homebrew/cask/lib/hbc/cli.rb @@ -1,6 +1,8 @@ require "optparse" require "shellwords" +require "extend/optparse" + require "hbc/cli/base" require "hbc/cli/audit" require "hbc/cli/cat" diff --git a/Library/Homebrew/cask/lib/hbc/extend.rb b/Library/Homebrew/cask/lib/hbc/extend.rb deleted file mode 100644 index e836b6bc8..000000000 --- a/Library/Homebrew/cask/lib/hbc/extend.rb +++ /dev/null @@ -1,6 +0,0 @@ -# monkeypatching -require "hbc/extend/hash" -require "hbc/extend/io" -require "hbc/extend/optparse" -require "extend/pathname" -require "hbc/extend/string" diff --git a/Library/Homebrew/cask/lib/hbc/extend/hash.rb b/Library/Homebrew/cask/lib/hbc/extend/hash.rb deleted file mode 100644 index 3a06ba3af..000000000 --- a/Library/Homebrew/cask/lib/hbc/extend/hash.rb +++ /dev/null @@ -1,7 +0,0 @@ -class Hash - def assert_valid_keys(*valid_keys) - unknown_keys = keys - valid_keys - return if unknown_keys.empty? - raise CaskError, %Q{Unknown keys: #{unknown_keys.inspect}. Running "#{UPDATE_CMD}" will likely fix it.} - end -end diff --git a/Library/Homebrew/cask/lib/hbc/extend/io.rb b/Library/Homebrew/cask/lib/hbc/extend/io.rb deleted file mode 100644 index 1357293cd..000000000 --- a/Library/Homebrew/cask/lib/hbc/extend/io.rb +++ /dev/null @@ -1,10 +0,0 @@ -class IO - def readline_nonblock(sep = $INPUT_RECORD_SEPARATOR) - buffer = "" - buffer.concat(read_nonblock(1)) while buffer[-1] != sep - buffer - rescue IO::WaitReadable, EOFError => e - raise e if buffer.empty? - buffer - end -end diff --git a/Library/Homebrew/cask/lib/hbc/extend/optparse.rb b/Library/Homebrew/cask/lib/hbc/extend/optparse.rb deleted file mode 100644 index 784d6d699..000000000 --- a/Library/Homebrew/cask/lib/hbc/extend/optparse.rb +++ /dev/null @@ -1,6 +0,0 @@ -require "optparse" -require "pathname" - -OptionParser.accept Pathname do |path| - Pathname(path).expand_path if path -end diff --git a/Library/Homebrew/cask/lib/hbc/extend/string.rb b/Library/Homebrew/cask/lib/hbc/extend/string.rb deleted file mode 100644 index 38c284194..000000000 --- a/Library/Homebrew/cask/lib/hbc/extend/string.rb +++ /dev/null @@ -1,5 +0,0 @@ -class String - def undent - gsub(%r{^.{#{(slice(%r{^ +}) || '').length}}}, "") - end -end diff --git a/Library/Homebrew/cask/lib/hbc/system_command.rb b/Library/Homebrew/cask/lib/hbc/system_command.rb index 160aadff9..aa4b9b295 100644 --- a/Library/Homebrew/cask/lib/hbc/system_command.rb +++ b/Library/Homebrew/cask/lib/hbc/system_command.rb @@ -1,6 +1,10 @@ require "open3" require "shellwords" +require "extend/io" + +require "hbc/utils/hash_validator" + module Hbc class SystemCommand attr_reader :command @@ -43,7 +47,8 @@ module Hbc attr_reader :executable, :options, :processed_output, :processed_status def process_options! - options.assert_valid_keys :input, :print_stdout, :print_stderr, :args, :must_succeed, :sudo, :bsexec + options.extend(HashValidator) + .assert_valid_keys :input, :print_stdout, :print_stderr, :args, :must_succeed, :sudo, :bsexec sudo_prefix = %w[/usr/bin/sudo -E --] bsexec_prefix = ["/bin/launchctl", "bsexec", options[:bsexec] == :startup ? "/" : options[:bsexec]] @command = [executable] diff --git a/Library/Homebrew/cask/lib/hbc/utils/hash_validator.rb b/Library/Homebrew/cask/lib/hbc/utils/hash_validator.rb new file mode 100644 index 000000000..b9e11720d --- /dev/null +++ b/Library/Homebrew/cask/lib/hbc/utils/hash_validator.rb @@ -0,0 +1,7 @@ +module HashValidator + def assert_valid_keys(*valid_keys) + unknown_keys = keys - valid_keys + return if unknown_keys.empty? + raise CaskError, %Q{Unknown keys: #{unknown_keys.inspect}. Running "#{UPDATE_CMD}" will likely fix it.} + end +end diff --git a/Library/Homebrew/diagnostic.rb b/Library/Homebrew/diagnostic.rb index 0db92592c..394b766ae 100644 --- a/Library/Homebrew/diagnostic.rb +++ b/Library/Homebrew/diagnostic.rb @@ -79,7 +79,7 @@ module Homebrew end def inject_file_list(list, string) - list.inject(string) { |a, e| a << " #{e}\n" } + list.inject(string) { |a, e| a << " #{e}\n" } end ############# END HELPERS diff --git a/Library/Homebrew/extend/io.rb b/Library/Homebrew/extend/io.rb new file mode 100644 index 000000000..1357293cd --- /dev/null +++ b/Library/Homebrew/extend/io.rb @@ -0,0 +1,10 @@ +class IO + def readline_nonblock(sep = $INPUT_RECORD_SEPARATOR) + buffer = "" + buffer.concat(read_nonblock(1)) while buffer[-1] != sep + buffer + rescue IO::WaitReadable, EOFError => e + raise e if buffer.empty? + buffer + end +end diff --git a/Library/Homebrew/extend/optparse.rb b/Library/Homebrew/extend/optparse.rb new file mode 100644 index 000000000..784d6d699 --- /dev/null +++ b/Library/Homebrew/extend/optparse.rb @@ -0,0 +1,6 @@ +require "optparse" +require "pathname" + +OptionParser.accept Pathname do |path| + Pathname(path).expand_path if path +end diff --git a/Library/Homebrew/test/test_diagnostic.rb b/Library/Homebrew/test/test_diagnostic.rb index b9b995f0f..33cf27b74 100644 --- a/Library/Homebrew/test/test_diagnostic.rb +++ b/Library/Homebrew/test/test_diagnostic.rb @@ -16,7 +16,7 @@ class DiagnosticChecksTest < Homebrew::TestCase def test_inject_file_list assert_equal "foo:\n", @checks.inject_file_list([], "foo:\n") - assert_equal "foo:\n /a\n /b\n", + assert_equal "foo:\n /a\n /b\n", @checks.inject_file_list(%w[/a /b], "foo:\n") end -- cgit v1.2.3