class Envo::Context

Attributes

host[R]
state[R]

Public Class Methods

new(host, log, opts, state = nil) click to toggle source
# File lib/envo/context.rb, line 3
def initialize(host, log, opts, state = nil)
  @host = host
  @log = log
  @default_opts = opts
  @opts = opts
  reflect_opts_change

  @state = state || State.new(host.env)

  create_common_locals
end

Public Instance Methods

ask(question) click to toggle source

io

# File lib/envo/context.rb, line 130
def ask(question)
  return true if force?
  return false if noforce?

  print "#{question} (y/n): "
  answer = STDIN.gets.chomp
  answer.downcase!
  return answer == 'y' || answer == 'yes'
end
create_common_locals() click to toggle source
# File lib/envo/context.rb, line 80
def create_common_locals
  @locals = {
    '@path' => host.shell.path_var_name,
    '@home' => host.shell.home_var_name,
  }
end
debug(text) click to toggle source
# File lib/envo/context.rb, line 144
def debug(text); @log.debug(text); end
error(text) click to toggle source
# File lib/envo/context.rb, line 140
def error(text); @log.error(text); end
execute(pack) click to toggle source

execution

# File lib/envo/context.rb, line 147
def execute(pack)
  pack_opts = pack.opts
  pack.cmds.each do |cmd|
    @opts = @default_opts.merge(cmd.opts, pack_opts)
    reflect_opts_change
    cmd.cmd.execute(self)
  end
end
expand_name(name) click to toggle source

parse access

# File lib/envo/context.rb, line 55
def expand_name(name)
  @locals[name] || name
end
expand_value(val) click to toggle source
# File lib/envo/context.rb, line 58
def expand_value(val)
  if raw?
    if val.class == Array
      if val.size == 1
        StringVal.new(val[0])
      else
        ListVal.new(val)
      end
    else
      StringVal.new(val)
    end
  else
    ValBuilder.from_user_text(val, @host)
  end
end
find_script(script) click to toggle source
# File lib/envo/context.rb, line 24
def find_script(script)
  if @host.shell.likely_rel_path?(script) || @host.shell.likely_abs_path?(script)
    return script if @host.path_exists?(script)
    raise Envo::Error.new "'#{script}' doesn't exist"
  end

  # look for '.envo/<file>.envoscript'
  script = script + '.envoscript'
  dir = @host.pwd
  found = while true
    check = File.join(dir, '.envo', script)
    break check if @host.path_exists?(check)
    new_dir = File.dirname(dir)
    break nil if new_dir == dir
    dir = new_dir
  end

  if !found
    check = File.join(@host.home, '.envo', script)
    found = check if @host.path_exists?(check)
  end

  raise Envo::Error.new "Can't find '#{script}' in .envo/ parent dirs or in <home>/.envo/" if !found
  found
end
force?() click to toggle source
# File lib/envo/context.rb, line 115
def force?
  @opts[:interact] == :force
end
interact?() click to toggle source
# File lib/envo/context.rb, line 121
def interact?
  @opts[:interact] == :interact
end
load_script(path) click to toggle source
# File lib/envo/context.rb, line 50
def load_script(path)
  File.readlines(path)
end
local_var_name?(name) click to toggle source

local vars

# File lib/envo/context.rb, line 74
def local_var_name?(name)
  name =~ /^@[a-zA-Z]/
end
new_scope(defaults = {}) click to toggle source

create another context based on this one (same state, log, and host) which provides different opts an locals thus scripts can be executed which don't leak values in the scope above

# File lib/envo/context.rb, line 20
def new_scope(defaults = {})
  Context.new(@host, @log, @opts.merge(defaults), @state)
end
noforce?() click to toggle source
# File lib/envo/context.rb, line 118
def noforce?
  @opts[:interact] == :noforce
end
print(text) click to toggle source
puts(text) click to toggle source
# File lib/envo/context.rb, line 143
def puts(text);  @log.puts(text); end
raw?() click to toggle source

opt queries

# File lib/envo/context.rb, line 112
def raw?
  @opts[:raw]
end
raw_get(name) click to toggle source
# File lib/envo/context.rb, line 91
def raw_get(name)
  @state.get(name)
end
raw_set(name, value) click to toggle source
# File lib/envo/context.rb, line 103
def raw_set(name, value)
  @state.set(name, value)
end
reflect_opts_change() click to toggle source
# File lib/envo/context.rb, line 125
def reflect_opts_change
  @log.max_level = @opts[:log_level]
end
set_local_var(name, value) click to toggle source
# File lib/envo/context.rb, line 77
def set_local_var(name, value)
  @locals[name] = value
end
smart_get(name) click to toggle source

env access

# File lib/envo/context.rb, line 88
def smart_get(name)
  ValBuilder.from_env_string(raw_get(name), @host)
end
smart_set(name, value) click to toggle source
# File lib/envo/context.rb, line 95
def smart_set(name, value)
  if value.list?
    rv = @host.shell.ar_to_list(value.ar)
    raw_set(name, rv)
  else
    raw_set(name, value.to_env_s)
  end
end
unset(name) click to toggle source
# File lib/envo/context.rb, line 107
def unset(name)
  @state.unset(name)
end
warn(text) click to toggle source
# File lib/envo/context.rb, line 141
def warn(text);  @log.warn(text); end