class TF::Environment

Constants

HANDLER

Public Class Methods

parse_array(name, value) click to toggle source
# File lib/tf/environment.rb, line 82
def parse_array name, value
  if value[0] && value[0].chars.to_a.first == '['
    value = value.map do |string|
      string =~ /\[([^\]]+)\]=(.*)/m
      [ $1, $2 ]
    end
  else
    value = value.to_enum.with_index.map{|v,i|[(i+1).to_s,v]}.to_a
    # TODO: zsh -c 'typeset -A arr; arr[ala]=1; arr[kot]=2; set | grep -a ^arr=' => arr=(ala 1 kot 2 ) - space on the end
  end
  [ name, Hash[ value ] ]
end
parse_env(output) click to toggle source
# File lib/tf/environment.rb, line 38
def parse_env output
  env = []
  holder=nil
  terminator=nil
  output.each do |line|
    line.chomp!
    if holder.nil?
      if line =~ /^[^=]+=([\('\$]?)/
        holder = line
        if $1 && !$1.empty?
          terminator = $1.sub(/\$/,"'").sub(/\(/,")")
        end
      elsif line =~ /^[^=]*=/
        holder = line
        terminator=nil
      else
        $stderr.puts "Unknown environment token: #{line}." if ENV["TF_DEBUG"]
      end
    else
      holder += line
    end
    if terminator && line.chars.to_a.last == terminator
      terminator=nil
    end
    if holder && terminator.nil?
      env << parse_var( holder.strip )
      holder=nil
    end
  end
  Hash[ env ]
end
parse_var(definition) click to toggle source
# File lib/tf/environment.rb, line 69
def parse_var definition
  definition =~ /\A([^=]*)=([$]?[\(']?)(.*?)([\)']?)\z/m
  name  = $1
  type1 = $2
  value = $3
  type2 = $4
  case type2
  when ')'
    parse_array( name, value.shellsplit.map{|v|v.gsub(/'\''/,'\'')} )
  else
    [ name, value.gsub(/'\''/,'\'') ]
  end
end
show_env_command() click to toggle source
# File lib/tf/environment.rb, line 35
def show_env_command
  TF::Environment::HANDLER
end