Class: BuildTool::Environment

Inherits:
Object
  • Object
show all
Includes:
MJ::Tools::SubProcess
Defined in:
lib/build-tool/environment.rb

Overview

Encapsulates all environment related options for a build

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#featureObject

Returns the value of attribute feature.



20
21
22
# File 'lib/build-tool/environment.rb', line 20

def feature
  @feature
end

#nameObject (readonly)

Name for the environment



15
16
17
# File 'lib/build-tool/environment.rb', line 15

def name
  @name
end

#parentObject

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

Returns:

  • (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, options = {} )
    wd     = options[ :wd ]
    detach = options[ :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_sObject



137
138
139
# File 'lib/build-tool/environment.rb', line 137

def to_s
    "Environment: #{name}"
end

#valuesObject



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

#varsObject



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