Class: PrepKit::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/prep_kit/context.rb

Instance Method Summary collapse

Constructor Details

#initialize(host, user, options = {}) ⇒ Context

Returns a new instance of Context.



3
4
5
6
7
# File 'lib/prep_kit/context.rb', line 3

def initialize(host, user, options = {})
  @vars     = {}
  @env      = RemoteMarshal.new(host, user, options)
  @memoized = {}
end

Instance Method Details

#call(name) ⇒ Object

Raises:



19
20
21
22
23
# File 'lib/prep_kit/context.rb', line 19

def call(name)
  raise NotFoundError, name unless Proc === (block = @vars[name])

  Task.new(self).action &block
end

#current_directoryObject



55
56
57
# File 'lib/prep_kit/context.rb', line 55

def current_directory
  nil
end

#load(filename) ⇒ Object



15
16
17
# File 'lib/prep_kit/context.rb', line 15

def load(filename)
  instance_eval open(filename, &:read), filename
end

#sh(command) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/prep_kit/context.rb', line 25

def sh(command)
  PrepKit.logger.info "sh: #{command}"

  output = @memoized.fetch(command) do
    status, output, error = @env.exec!(command)

    raise RuntimeError, error unless status == 0

    @memoized[command] = '[SKIP]'

    "<- \n" + output
  end

  output.tap { |output| PrepKit.logger.info output }
end

#task(name, &block) ⇒ Object

Raises:



9
10
11
12
13
# File 'lib/prep_kit/context.rb', line 9

def task(name, &block)
  raise AssignError, name if @vars[name]

  @vars[name] = block
end

#test?(path, option) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
44
45
# File 'lib/prep_kit/context.rb', line 41

def test?(path, option)
  status, *_ = @env.test?(path, option)

  status == 0
end