diff options
| author | Markus Reiter | 2016-09-24 13:52:43 +0200 |
|---|---|---|
| committer | Markus Reiter | 2016-09-24 16:00:58 +0200 |
| commit | b86c8efb79b3ed835d552c4d7416640ef10caf21 (patch) | |
| tree | 7e1edc8a8f339e4d2781f43576d40c9c79aebcdc /Library/Homebrew/cask/lib/hbc/system_command.rb | |
| parent | 687f0fcf721c8e36f32570ed72d0988a6eaf986f (diff) | |
| download | brew-b86c8efb79b3ed835d552c4d7416640ef10caf21.tar.bz2 | |
Cask: Use nested classes and modules.
Diffstat (limited to 'Library/Homebrew/cask/lib/hbc/system_command.rb')
| -rw-r--r-- | Library/Homebrew/cask/lib/hbc/system_command.rb | 284 |
1 files changed, 145 insertions, 139 deletions
diff --git a/Library/Homebrew/cask/lib/hbc/system_command.rb b/Library/Homebrew/cask/lib/hbc/system_command.rb index 9ffe9da38..160aadff9 100644 --- a/Library/Homebrew/cask/lib/hbc/system_command.rb +++ b/Library/Homebrew/cask/lib/hbc/system_command.rb @@ -1,173 +1,179 @@ require "open3" require "shellwords" -class Hbc::SystemCommand - attr_reader :command +module Hbc + class SystemCommand + attr_reader :command - def self.run(executable, options = {}) - new(executable, options).run! - end + def self.run(executable, options = {}) + new(executable, options).run! + end - def self.run!(command, options = {}) - run(command, options.merge(must_succeed: true)) - end + def self.run!(command, options = {}) + run(command, options.merge(must_succeed: true)) + end - def run! - @processed_output = { stdout: "", stderr: "" } - odebug "Executing: #{expanded_command.utf8_inspect}" - - each_output_line do |type, line| - case type - when :stdout - processed_output[:stdout] << line - ohai line.chomp if options[:print_stdout] - when :stderr - processed_output[:stderr] << line - ohai line.chomp if options[:print_stderr] + def run! + @processed_output = { stdout: "", stderr: "" } + odebug "Executing: #{expanded_command.utf8_inspect}" + + each_output_line do |type, line| + case type + when :stdout + processed_output[:stdout] << line + ohai line.chomp if options[:print_stdout] + when :stderr + processed_output[:stderr] << line + ohai line.chomp if options[:print_stderr] + end end - end - assert_success if options[:must_succeed] - result - end + assert_success if options[:must_succeed] + result + end - def initialize(executable, options) - @executable = executable - @options = options - process_options! - end + def initialize(executable, options) + @executable = executable + @options = options + process_options! + end - private - - 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 - sudo_prefix = %w[/usr/bin/sudo -E --] - bsexec_prefix = ["/bin/launchctl", "bsexec", options[:bsexec] == :startup ? "/" : options[:bsexec]] - @command = [executable] - options[:print_stderr] = true unless options.key?(:print_stderr) - @command.unshift(*bsexec_prefix) if options[:bsexec] - @command.unshift(*sudo_prefix) if options[:sudo] - @command.concat(options[:args]) if options.key?(:args) && !options[:args].empty? - @command[0] = Shellwords.shellescape(@command[0]) if @command.size == 1 - nil - end + private + + 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 + sudo_prefix = %w[/usr/bin/sudo -E --] + bsexec_prefix = ["/bin/launchctl", "bsexec", options[:bsexec] == :startup ? "/" : options[:bsexec]] + @command = [executable] + options[:print_stderr] = true unless options.key?(:print_stderr) + @command.unshift(*bsexec_prefix) if options[:bsexec] + @command.unshift(*sudo_prefix) if options[:sudo] + @command.concat(options[:args]) if options.key?(:args) && !options[:args].empty? + @command[0] = Shellwords.shellescape(@command[0]) if @command.size == 1 + nil + end - def assert_success - return if processed_status && processed_status.success? - raise Hbc::CaskCommandFailedError.new(command.utf8_inspect, processed_output[:stdout], processed_output[:stderr], processed_status) - end + def assert_success + return if processed_status && processed_status.success? + raise CaskCommandFailedError.new(command.utf8_inspect, processed_output[:stdout], processed_output[:stderr], processed_status) + end - def expanded_command - @expanded_command ||= command.map { |arg| - if arg.respond_to?(:to_path) - File.absolute_path(arg) - else - String(arg) - end - } - end + def expanded_command + @expanded_command ||= command.map { |arg| + if arg.respond_to?(:to_path) + File.absolute_path(arg) + else + String(arg) + end + } + end - def each_output_line(&b) - raw_stdin, raw_stdout, raw_stderr, raw_wait_thr = - Open3.popen3(*expanded_command) + def each_output_line(&b) + raw_stdin, raw_stdout, raw_stderr, raw_wait_thr = + Open3.popen3(*expanded_command) - write_input_to(raw_stdin) if options[:input] - raw_stdin.close_write - each_line_from [raw_stdout, raw_stderr], &b + write_input_to(raw_stdin) if options[:input] + raw_stdin.close_write + each_line_from [raw_stdout, raw_stderr], &b - @processed_status = raw_wait_thr.value - end + @processed_status = raw_wait_thr.value + end - def write_input_to(raw_stdin) - Array(options[:input]).each { |line| raw_stdin.puts line } - end + def write_input_to(raw_stdin) + Array(options[:input]).each { |line| raw_stdin.puts line } + end - def each_line_from(sources) - loop do - readable_sources = IO.select(sources)[0] - readable_sources.delete_if(&:eof?).first(1).each do |source| - type = (source == sources[0] ? :stdout : :stderr) - begin - yield(type, source.readline_nonblock || "") - rescue IO::WaitReadable, EOFError - next + def each_line_from(sources) + loop do + readable_sources = IO.select(sources)[0] + readable_sources.delete_if(&:eof?).first(1).each do |source| + type = (source == sources[0] ? :stdout : :stderr) + begin + yield(type, source.readline_nonblock || "") + rescue IO::WaitReadable, EOFError + next + end end + break if readable_sources.empty? end - break if readable_sources.empty? + sources.each(&:close_read) end - sources.each(&:close_read) - end - def result - Hbc::SystemCommand::Result.new(command, - processed_output[:stdout], - processed_output[:stderr], - processed_status.exitstatus) + def result + Result.new(command, + processed_output[:stdout], + processed_output[:stderr], + processed_status.exitstatus) + end end end -class Hbc::SystemCommand::Result - attr_accessor :command, :stdout, :stderr, :exit_status +module Hbc + class SystemCommand + class Result + attr_accessor :command, :stdout, :stderr, :exit_status - def initialize(command, stdout, stderr, exit_status) - @command = command - @stdout = stdout - @stderr = stderr - @exit_status = exit_status - end + def initialize(command, stdout, stderr, exit_status) + @command = command + @stdout = stdout + @stderr = stderr + @exit_status = exit_status + end - def plist - @plist ||= self.class._parse_plist(@command, @stdout.dup) - end + def plist + @plist ||= self.class._parse_plist(@command, @stdout.dup) + end - def success? - @exit_status.zero? - end + def success? + @exit_status.zero? + end - def merged_output - @merged_output ||= @stdout + @stderr - end + def merged_output + @merged_output ||= @stdout + @stderr + end - def to_s - @stdout - end + def to_s + @stdout + end - def self._warn_plist_garbage(command, garbage) - return true unless garbage =~ %r{\S} - external = File.basename(command.first) - lines = garbage.strip.split("\n") - opoo "Non-XML stdout from #{external}:" - $stderr.puts lines.map { |l| " #{l}" } - end + def self._warn_plist_garbage(command, garbage) + return true unless garbage =~ %r{\S} + external = File.basename(command.first) + lines = garbage.strip.split("\n") + opoo "Non-XML stdout from #{external}:" + $stderr.puts lines.map { |l| " #{l}" } + end - def self._parse_plist(command, output) - raise Hbc::CaskError, "Empty plist input" unless output =~ %r{\S} - output.sub!(%r{\A(.*?)(<\?\s*xml)}m, '\2') - _warn_plist_garbage(command, Regexp.last_match[1]) if Hbc.debug - output.sub!(%r{(<\s*/\s*plist\s*>)(.*?)\Z}m, '\1') - _warn_plist_garbage(command, Regexp.last_match[2]) - xml = Plist.parse_xml(output) - unless xml.respond_to?(:keys) && !xml.keys.empty? - raise Hbc::CaskError, <<-EOS -Empty result parsing plist output from command. - command was: - #{command.utf8_inspect} - output we attempted to parse: - #{output} - EOS + def self._parse_plist(command, output) + raise CaskError, "Empty plist input" unless output =~ %r{\S} + output.sub!(%r{\A(.*?)(<\?\s*xml)}m, '\2') + _warn_plist_garbage(command, Regexp.last_match[1]) if Hbc.debug + output.sub!(%r{(<\s*/\s*plist\s*>)(.*?)\Z}m, '\1') + _warn_plist_garbage(command, Regexp.last_match[2]) + xml = Plist.parse_xml(output) + unless xml.respond_to?(:keys) && !xml.keys.empty? + raise CaskError, <<-EOS + Empty result parsing plist output from command. + command was: + #{command.utf8_inspect} + output we attempted to parse: + #{output} + EOS + end + xml + rescue Plist::ParseError => e + raise CaskError, <<-EOS + Error parsing plist output from command. + command was: + #{command.utf8_inspect} + error was: + #{e} + output we attempted to parse: + #{output} + EOS + end end - xml - rescue Plist::ParseError => e - raise Hbc::CaskError, <<-EOS -Error parsing plist output from command. - command was: - #{command.utf8_inspect} - error was: - #{e} - output we attempted to parse: - #{output} - EOS end end |
