class PrepKit::Context

Public Class Methods

new(host, user, options = {}) click to toggle source
# File lib/prep_kit/context.rb, line 3
def initialize(host, user, options = {})
  @vars     = {}
  @env      = environment(host, user, options)
  @memoized = {}
end

Public Instance Methods

call(name) click to toggle source
# 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) click to toggle source
# File lib/prep_kit/context.rb, line 15
def load(filename)
  instance_eval open(filename, &:read), filename
end
sh(command) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/prep_kit/context.rb, line 39
def test?(path, option)
  @env.test?(path, option) == 0
end

Private Instance Methods

environment(host, user, options) click to toggle source
# File lib/prep_kit/context.rb, line 53
def environment(host, user, options)
  Remote.new(host, user, options) do |type, data|
    data = ((data = data.chomp) =~ /\n/) ? "\n" + data : data

    case type
    when :output
      PrepKit.logger.info  data.colorize(:green) unless data.empty?
    when :error
      PrepKit.logger.error data.colorize(:red)   unless data.empty?
    end
  end
end