Class: BuildTool::Environment
- Inherits:
-
Object
- Object
- BuildTool::Environment
- Includes:
- MJ::Tools::SubProcess
- Defined in:
- lib/build-tool/environment.rb
Overview
Encapsulates all environment related options for a build
Instance Attribute Summary collapse
-
#feature ⇒ Object
Returns the value of attribute feature.
-
#name ⇒ Object
readonly
Name for the environment.
-
#parent ⇒ Object
Parent environment.
Instance Method Summary collapse
-
#[](name) ⇒ Object
Get the value of a environment variable.
- #active? ⇒ Boolean
-
#append(name, value) ⇒ Object
Set a environment variable.
-
#execute(command, wd = nil, envadd = nil) ⇒ Object
Execute command in a shell with the environment set.
-
#initialize(name) ⇒ Environment
constructor
Create a environment object.
-
#prepend(name, value) ⇒ Object
Prepend
value
to variablename
. - #set(name, value) ⇒ Object
-
#shell(command = nil, options = {}) ⇒ Object
Open a shell with the environment set.
- #to_s ⇒ Object
- #values ⇒ Object
- #vars ⇒ Object
Constructor Details
#initialize(name) ⇒ Environment
Create a environment object
23 24 25 26 27 28 29 |
# File 'lib/build-tool/environment.rb', line 23 def initialize( name ) raise StandardError.new "Environment.name has to be set" if name.nil? @name = name @vars = Hash.new @parent = nil @feature = nil end |
Instance Attribute Details
#feature ⇒ Object
Returns the value of attribute feature.
20 21 22 |
# File 'lib/build-tool/environment.rb', line 20 def feature @feature end |
#name ⇒ Object (readonly)
Name for the environment
15 16 17 |
# File 'lib/build-tool/environment.rb', line 15 def name @name end |
#parent ⇒ Object
Parent environment
18 19 20 |
# File 'lib/build-tool/environment.rb', line 18 def parent @parent end |
Instance Method Details
#[](name) ⇒ Object
Get the value of a environment variable
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/build-tool/environment.rb', line 117 def []( name ) parentval = "" # Get the value from the parent if @parent parentval = @parent[name] end # If we don't know the var or the env is not active return the parents value if !@vars.has_key?( name.to_s ) or !active? return parentval end if parentval.empty? val = @vars[name.to_s].sub( ":$#{name.to_s}:", "" ) val.sub!( ":$#{name.to_s}", "" ) val.sub!( "$#{name.to_s}:", "" ) return val else return @vars[name.to_s].sub( "$#{name.to_s}", parentval ) end end |
#active? ⇒ Boolean
31 32 33 34 35 36 37 |
# File 'lib/build-tool/environment.rb', line 31 def active? if @feature.nil? true else @feature.active? end end |
#append(name, value) ⇒ Object
Set a environment variable
41 42 43 44 45 46 47 |
# File 'lib/build-tool/environment.rb', line 41 def append( name, value ) if @vars.has_key?(name.to_s) and !@vars[name.to_s].empty? @vars[name.to_s] = "#{@vars[name.to_s]}:#{value.to_s}" else set( name, "$#{name.to_s}:#{value}" ) end end |
#execute(command, wd = nil, envadd = nil) ⇒ Object
Execute command in a shell with the environment set.
50 51 52 53 54 55 56 57 |
# File 'lib/build-tool/environment.rb', line 50 def execute( command, wd = nil, envadd = nil ) env = Hash.new for var in vars do env[var] = self[var] end env.merge!( envadd ) if envadd return self.class.execute( command.to_s, wd, env ) end |
#prepend(name, value) ⇒ Object
Prepend value
to variable name
. A ‘:’ is added when necessary.
60 61 62 63 64 65 66 |
# File 'lib/build-tool/environment.rb', line 60 def prepend( name, value ) if @vars.has_key?(name.to_s) and !@vars[name.to_s].empty? @vars[name.to_s] = "#{value.to_s}:#{@vars[name.to_s]}" else set( name, "#{value}:$#{name.to_s}" ) end end |
#set(name, value) ⇒ Object
68 69 70 |
# File 'lib/build-tool/environment.rb', line 68 def set( name, value ) @vars[name.to_s] = value.to_s end |
#shell(command = nil, options = {}) ⇒ Object
Open a shell with the environment set.
It sets an environment variable BUILD_TOOL_ENV with the name of the environment set. This can be used to add this information to the prompt.
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/build-tool/environment.rb', line 77 def shell( command = nil, = {} ) wd = [ :wd ] detach = [ :detach ] || false begin logger.verbose( "BUILD_TOOL_ENV = #{name}" ) ENV['BUILD_TOOL_ENV'] = name self.class.adjust_environment( wd, values ) { pid = Process.fork { exec( command ) } if detach return Process.detach( pid ) else Process.wait( pid ) return $? end } ensure ENV['BUILD_TOOL_ENV'] = nil end end |
#to_s ⇒ Object
137 138 139 |
# File 'lib/build-tool/environment.rb', line 137 def to_s "Environment: #{name}" end |
#values ⇒ Object
108 109 110 111 112 113 114 |
# File 'lib/build-tool/environment.rb', line 108 def values vals = {} vars.each do |var| vals[var] = self[var] end vals end |
#vars ⇒ Object
100 101 102 103 104 105 106 |
# File 'lib/build-tool/environment.rb', line 100 def vars if @parent ( @vars.keys + @parent.vars ).uniq else @vars.keys end end |