Class: Tomo::ShellBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/tomo/shell_builder.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeShellBuilder

Returns a new instance of ShellBuilder.



13
14
15
16
17
18
# File 'lib/tomo/shell_builder.rb', line 13

def initialize
  @env = {}
  @chdir = []
  @prefixes = []
  @umask = nil
end

Class Method Details

.raw(string) ⇒ Object



7
8
9
10
11
# File 'lib/tomo/shell_builder.rb', line 7

def self.raw(string)
  string.dup.tap do |raw_string|
    raw_string.define_singleton_method(:shellescape) { string }
  end
end

Instance Method Details

#build(*command, default_chdir: nil) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/tomo/shell_builder.rb', line 50

def build(*command, default_chdir: nil)
  return chdir(default_chdir) { build(*command) } if @chdir.empty? && default_chdir

  command_string = command_to_string(*command)
  modifiers = [cd_chdir, unset_env, export_env, set_umask].compact.flatten
  [*modifiers, command_string].join(" && ")
end

#chdir(dir) ⇒ Object



20
21
22
23
24
25
# File 'lib/tomo/shell_builder.rb', line 20

def chdir(dir)
  @chdir << dir
  yield
ensure
  @chdir.pop
end

#env(hash) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/tomo/shell_builder.rb', line 27

def env(hash)
  orig_env = @env
  @env = orig_env.merge(hash || {})
  yield
ensure
  @env = orig_env
end

#prepend(*command) ⇒ Object



35
36
37
38
39
40
# File 'lib/tomo/shell_builder.rb', line 35

def prepend(*command)
  prefixes.unshift(*command)
  yield
ensure
  prefixes.shift(command.length)
end

#umask(mask) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/tomo/shell_builder.rb', line 42

def umask(mask)
  orig_umask = @umask
  @umask = mask
  yield
ensure
  @umask = orig_umask
end