module Pebblescape::Receiver::ShellHelpers
Public Class Methods
# File lib/pebblescape/receiver/shell_helpers.rb, line 21 def self.blacklist?(key) %w(PATH GEM_PATH GEM_HOME GIT_DIR).include?(key) end
# File lib/pebblescape/receiver/shell_helpers.rb, line 25 def self.initialize_env(path) env_dir = Pathname.new("#{path}") if env_dir.exist? && env_dir.directory? env_dir.each_child do |file| key = file.basename.to_s value = file.read.strip user_env_hash[key] = value unless blacklist?(key) end end end
# File lib/pebblescape/receiver/shell_helpers.rb, line 9 def self.user_env_hash @@user_env_hash end
Public Instance Methods
# File lib/pebblescape/receiver/shell_helpers.rb, line 78 def command_options_to_string(command, options) options[:env] ||= {} options[:out] ||= "2>&1" options[:env] = user_env_hash.merge(options[:env]) if options[:user_env] env = options[:env].map {|key, value| "#{key.shellescape}=#{value.shellescape}" }.join(" ") "/usr/bin/env #{env} bash -c #{command.shellescape} #{options[:out]} " end
# File lib/pebblescape/receiver/shell_helpers.rb, line 133 def deprecate(message) @deprecations ||= [] @deprecations << message end
# File lib/pebblescape/receiver/shell_helpers.rb, line 17 def env(var) ENV[var] || user_env_hash[var] end
display error message and stop the build process @param [String] error message
# File lib/pebblescape/receiver/shell_helpers.rb, line 38 def error(message) Kernel.puts " !" message.split("\n").each do |line| Kernel.puts " ! #{line.strip}" end Kernel.puts " !" log "exit", :error => message if respond_to?(:log) exit 1 end
# File lib/pebblescape/receiver/shell_helpers.rb, line 138 def noshellescape(string) NoShellEscape.new(string) end
run a shell command and stream the output @param [String] command to be run
# File lib/pebblescape/receiver/shell_helpers.rb, line 88 def pipe(command, options = {}) output = "" IO.popen(command_options_to_string(command, options)) do |io| until io.eof? buffer = io.gets output << buffer options[:no_indent] ? Kernel.puts(buffer) : puts(buffer) end end output end
# File lib/pebblescape/receiver/shell_helpers.rb, line 101 def pipe!(command, options = {}) pipe(command, options) exit 1 unless $?.success? end
display a message in line (indented by 6 spaces) @param [String] message to be displayed
# File lib/pebblescape/receiver/shell_helpers.rb, line 117 def puts(message) message.split("\n").each do |line| super " #{line.strip}" end $stdout.flush end
run a shell command and pipe stderr to stdout @param [String] command @option options [String] :out the IO redirect of the command @option options [Hash] :env explicit environment to run command in @option options [Boolean] :user_env whether or not a user’s environment variables will be loaded
# File lib/pebblescape/receiver/shell_helpers.rb, line 66 def run(command, options = {}) %x{ #{command_options_to_string(command, options)} } end
# File lib/pebblescape/receiver/shell_helpers.rb, line 48 def run!(command, options = {}) result = run(command, options) error("Command: '#{command}' failed unexpectedly:\n#{result}") unless $?.success? return result end
doesn’t do any special piping. stderr won’t be redirected. @param [String] command to be run @return [String] output of stdout
# File lib/pebblescape/receiver/shell_helpers.rb, line 57 def run_no_pipe(command, options = {}) run(command, options.merge({:out => ""})) end
run a shell command and pipe stderr to /dev/null @param [String] command to be run @return [String] output of stdout
# File lib/pebblescape/receiver/shell_helpers.rb, line 73 def run_stdout(command, options = {}) options[:out] ||= '2>/dev/null' run(command, options) end
display a topic message (denoted by —–>) @param [String] topic message to be displayed
# File lib/pebblescape/receiver/shell_helpers.rb, line 109 def topic(message) Kernel.puts "-----> #{message}" $stdout.flush end
# File lib/pebblescape/receiver/shell_helpers.rb, line 13 def user_env_hash @@user_env_hash end
# File lib/pebblescape/receiver/shell_helpers.rb, line 124 def warn(message, options = {}) if options.key?(:inline) ? options[:inline] : false topic "Warning:" puts message end @warnings ||= [] @warnings << message end