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



21
22
23
24
25
26
27
# File 'lib/build-tool/environment.rb', line 21

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.



18
19
20
# File 'lib/build-tool/environment.rb', line 18

def feature
  @feature
end

#nameObject (readonly)

Name for the environment



13
14
15
# File 'lib/build-tool/environment.rb', line 13

def name
  @name
end

#parentObject

Parent environment



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

def parent
  @parent
end

Instance Method Details

#[](name) ⇒ Object

Get the value of a environment variable



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/build-tool/environment.rb', line 106

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)


29
30
31
32
33
34
35
# File 'lib/build-tool/environment.rb', line 29

def active?
    if @feature.nil?
        true
    else
        @feature.active?
    end
end

#append(name, value) ⇒ Object

Set a environment variable



39
40
41
42
43
44
45
# File 'lib/build-tool/environment.rb', line 39

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.



48
49
50
51
52
53
54
55
# File 'lib/build-tool/environment.rb', line 48

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.



58
59
60
61
62
63
64
# File 'lib/build-tool/environment.rb', line 58

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



66
67
68
# File 'lib/build-tool/environment.rb', line 66

def set( name, value )
    @vars[name.to_s] = value.to_s
end

#shell(wd = nil) ⇒ 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.



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/build-tool/environment.rb', line 75

def shell( wd = nil )
    begin
        ENV['BUILD_TOOL_ENV'] = name
        self.class.adjust_environment( wd, values ) {
            logger.info "Starting #{ENV['SHELL']}"
            system( ENV['SHELL'] )
            logger.info "#{ENV['SHELL']} stopped."
            return 0;
        }
    ensure
        ENV['BUILD_TOOL_ENV'] = nil
    end
end

#to_sObject



126
127
128
# File 'lib/build-tool/environment.rb', line 126

def to_s
    "Environment: #{name}"
end

#valuesObject



97
98
99
100
101
102
103
# File 'lib/build-tool/environment.rb', line 97

def values
    vals = {}
    vars.each do |var|
        vals[var] = self[var]
    end
    vals
end

#varsObject



89
90
91
92
93
94
95
# File 'lib/build-tool/environment.rb', line 89

def vars
    if @parent
        ( @vars.keys + @parent.vars ).uniq
    else
        @vars.keys
    end
end