Class: DeploYML::Shell

Inherits:
Object
  • Object
show all
Includes:
Shellwords, Thor::Shell
Defined in:
lib/deployml/shell.rb

Overview

Provides common methods used by both LocalShell and RemoteShell.

Direct Known Subclasses

LocalShell, RemoteShell

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, environment = nil) {|session| ... } ⇒ Shell

Initializes a shell session.

Parameters:

  • uri (Addressable::URI, String)

    The URI of the shell.

  • environment (Environment) (defaults to: nil)

    The environment the shell is connected to.

Yields:

  • (session)

    If a block is given, it will be passed the new shell session.

Yield Parameters:

  • session (ShellSession)

    The shell session.



31
32
33
34
35
36
37
38
39
40
# File 'lib/deployml/shell.rb', line 31

def initialize(uri,environment=nil)
  @uri = uri
  @environment = environment

  if block_given?
    status "Entered #{@uri}."
    yield self
    status "Leaving #{@uri} ..."
  end
end

Instance Attribute Details

#uriObject (readonly)

The URI of the Shell.



14
15
16
# File 'lib/deployml/shell.rb', line 14

def uri
  @uri
end

Instance Method Details

#echo(message) ⇒ Object

Place holder method.

Since:

  • 0.5.0



66
67
# File 'lib/deployml/shell.rb', line 66

def echo(message)
end

#exec(command) ⇒ Object

Place holder method.

Parameters:

  • command (String)

    The command to execute.

Since:

  • 0.5.0



58
59
# File 'lib/deployml/shell.rb', line 58

def exec(command)
end

#rake(task, *arguments) ⇒ Object

Executes a Rake task.

Parameters:

  • task (Symbol, String)

    Name of the Rake task to run.

  • arguments (Array<String>)

    Additional arguments for the Rake task.



103
104
105
# File 'lib/deployml/shell.rb', line 103

def rake(task,*arguments)
  ruby('rake', rake_task(task,*arguments))
end

#rake_task(name, *arguments) ⇒ String (protected)

Builds a rake task name.

Parameters:

  • name (String, Symbol)

    The name of the rake task.

  • arguments (Array)

    Additional arguments to pass to the rake task.

Returns:

  • (String)

    The rake task name to be called.



170
171
172
173
174
175
176
177
178
# File 'lib/deployml/shell.rb', line 170

def rake_task(name,*arguments)
  name = name.to_s

  unless arguments.empty?
    name += ('[' + arguments.join(',') + ']')
  end

  return name
end

#ruby(program, *arguments) ⇒ Object

Executes a Ruby program.

Parameters:

  • program (Symbol, String)

    Name of the Ruby program to run.

  • arguments (Array<String>)

    Additional arguments for the Ruby program.

Since:

  • 0.5.2



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/deployml/shell.rb', line 80

def ruby(program,*arguments)
  command = [program, *arguments]

  # assume that `.rb` scripts do not have a `#!/usr/bin/env ruby`
  command.unshift('ruby') if program[-3,3] == '.rb'

  # if the environment uses bundler, run all ruby commands via `bundle exec`
  if (@environment && @environment.bundler)
    command.unshift('bundle','exec')
  end

  run(*command)
end

#run(program, *arguments) ⇒ Object

Place holder method.

Since:

  • 0.5.0



47
48
# File 'lib/deployml/shell.rb', line 47

def run(program,*arguments)
end

#shellescape(str) ⇒ String (protected)

Note:

Vendored from shellwords.rb line 72 from Ruby 1.9.2.

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.

Examples:

open("| grep #{Shellwords.escape(pattern)} file") { |pipe|
  # ...
}

Parameters:

  • str (String)

    The string to escape.

Returns:

  • (String)

    The shell-escaped string.



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/deployml/shell.rb', line 141

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

#status(message) ⇒ Object

Prints a status message.

Parameters:

  • message (String)

    The message to print.

Since:

  • 0.4.0



115
116
117
# File 'lib/deployml/shell.rb', line 115

def status(message)
  echo "#{Color::GREEN}>>> #{message}#{Color::CLEAR}"
end