Class: TF::Environment

Inherits:
Object
  • Object
show all
Defined in:
lib/tf/environment.rb

Constant Summary collapse

HANDLER =
<<EOF.gsub(/\n/,'')
set | awk -F= '
  BEGIN{v=0;}
  /^[a-zA-Z_][a-zA-Z0-9_]*=/{v=1;}
  v==1&&$2~/^['\\''\\$]/{v=2;}
  v==1&&$2~/^\\(/{v=3;}
  v==2&&/'\\''$/&&!/'\\'\\''$/{v=1;}
  v==3&&/\\)$/{v=1;}
  v{print;}
  v==1{v=0;}
'
EOF

Class Method Summary collapse

Class Method Details

.parse_array(name, value) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
# 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) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# 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) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
# 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_commandObject



35
36
37
# File 'lib/tf/environment.rb', line 35

def show_env_command
  TF::Environment::HANDLER
end