lib/deployml/remote_shell.rb in deployml-0.5.1 vs lib/deployml/remote_shell.rb in deployml-0.5.2

- old
+ new

@@ -16,20 +16,23 @@ # Initializes a remote shell session. # # @param [Addressable::URI, String] uri # The URI of the host to connect to. # + # @param [Environment] environment + # The environment the shell is connected to. + # # @yield [session] # If a block is given, it will be passed the new remote shell session. # # @yieldparam [ShellSession] session # The remote shell session. # - def initialize(uri,&block) + def initialize(uri,environment=nil,&block) @history = [] - super(uri,&block) + super(uri,environment,&block) replay if block end # @@ -44,10 +47,22 @@ def run(program,*arguments) @history << [program, *arguments] end # + # Adds a command to be executed. + # + # @param [String] command + # The command string. + # + # @since 0.5.2 + # + def exec(command) + @history << [command] + end + + # # Enqueues an `echo` command to be ran in the session. # # @param [String] message # The message to echo. # @@ -80,13 +95,20 @@ # # @return [String] # A single command string. # def join - @history.map { |command| - command.map { |word| shellescape(word.to_s) }.join(' ') - }.join(' && ') + commands = [] + + @history.each do |command| + program = command[0] + arguments = command[1..-1].map { |word| shellescape(word.to_s) } + + commands << [program, *arguments].join(' ') + end + + return commands.join(' && ') end # # Converts the URI to one compatible with SSH. # @@ -133,48 +155,9 @@ # # Replays the command history on the remote server. # def replay ssh(self.join) unless @history.empty? - end - - protected - - # - # Escapes a string so that it can be safely used in a Bourne shell - # command line. - # - # Note that a resulted string should be used unquoted and is not - # intended for use in double quotes nor in single quotes. - # - # @param [String] str - # The string to escape. - # - # @return [String] - # The shell-escaped string. - # - # @example - # open("| grep #{Shellwords.escape(pattern)} file") { |pipe| - # # ... - # } - # - # @note Vendored from `shellwords.rb` line 72 from Ruby 1.9.2. - # - def shellescape(str) - # An empty argument will be skipped, so return empty quotes. - return "''" if str.empty? - - str = str.dup - - # Process as a single byte sequence because not all shell - # implementations are multibyte aware. - str.gsub!(/([^A-Za-z0-9_\-.,:\/@\n])/n, "\\\\\\1") - - # A LF cannot be escaped with a backslash because a backslash + LF - # combo is regarded as line continuation and simply ignored. - str.gsub!(/\n/, "'\n'") - - return str end end end