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      = environment(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

#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

Raises:

  • (RuntimeError)


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

def sh(command)
  PrepKit.logger.info "sh: ".bold + command.underline

  status = @memoized.fetch(command) do
    @memoized[command] = :skip

    @env.exec!(command) == 0 ? :success : :failure
  end

  raise RuntimeError, command if status == :failure

  status
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)


39
40
41
# File 'lib/prep_kit/context.rb', line 39

def test?(path, option)
  @env.test?(path, option) == 0
end