Class: Origen::SiteConfig

Inherits:
Object show all
Defined in:
lib/origen/site_config.rb

Constant Summary collapse

TRUE_VALUES =
['true', 'TRUE', '1', 1]
FALSE_VALUES =
['false', 'FALSE', '0', 0]
NON_BOOLEAN_PARAMETERS =

Adding parameters to this array will prevent them from being converted to booleans if they are assigned one of the values in the TRUE_VALUES/FALSE_VALUES arrays

[:lsf_cores]

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/origen/site_config.rb', line 133

def method_missing(method, *args, &block)
  method = method.to_s
  if method =~ /(.*)!$/
    method = Regexp.last_match(1)
    must_be_present = true
  end
  val = find_val(method)
  if must_be_present && val.nil?
    puts "No value assigned for site_config attribute '#{method}'"
    puts
    fail 'Missing site_config value!'
  end
  define_singleton_method(method) do
    find_val(method)
  end
  val
end

Instance Method Details

#add_as_highest(var, value) ⇒ Object Also known as: []=

Dynamically add a new site variable at the highest priority.



108
109
110
111
112
# File 'lib/origen/site_config.rb', line 108

def add_as_highest(var, value)
  # Don't want to override anything, so just shift in a dummy site config instance at the highest level and
  # set the value there.
  configs.prepend(var.to_s => value)
end

#add_as_lowest(var, value) ⇒ Object

Dynamically add a new site variable at the lowest priority. Essentially, this sets a new default value.



117
118
119
120
121
# File 'lib/origen/site_config.rb', line 117

def add_as_lowest(var, value)
  # Don't want to override anything, so just shift in a dummy site config at the lowest level and
  # set the value there.
  configs.append(var.to_s => value)
end

#add_site_config_as_highest(site_config_file) ⇒ Object

Adds a new site config file as the highest priority



124
125
126
# File 'lib/origen/site_config.rb', line 124

def add_site_config_as_highest(site_config_file)
  configs.prepend YAML.load_file(File.expand_path('../../../origen_site_config.yml', __FILE__))
end

#add_site_config_as_lowest(site_config_file) ⇒ Object

Adds a new site config file as the highest priority



129
130
131
# File 'lib/origen/site_config.rb', line 129

def add_site_config_as_lowest(site_config_file)
  configs.append YAML.load_file(File.expand_path('../../../origen_site_config.yml', __FILE__))
end

#clearObject



166
167
168
# File 'lib/origen/site_config.rb', line 166

def clear
  @configs.clear
end

#eval_path(path, options = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/origen/site_config.rb', line 45

def eval_path(path, options = {})
  # Any leading ~ should be expanded with whatever ~/ points to. This needs to be done now because later ~ will be replaced with just the username.
  path = path.sub(/^~/, File.expand_path('~/'))

  # Gsub the remaining ~ that aren't escaped.
  # If it was escaped, eat the escape character
  path.gsub!(/(?<!\\|\A)~/, "#{Etc.getlogin}")
  path.gsub!(/\\(?=~)/, '')

  # Now, expand the entire path for any other OS-specific symbols.
  # One note, if we still have a leading '~', that means it was escaped at the beginning. So, what we'll do for this is let it expand
  # then replace the leading File.expand_path('~/') with just '~', pretty much the opposite of path.sub(/^~/, File.expand_path('~/'))
  # Note, we can't just take it out, expand, then add it back  because expanding the path on Windows will expand to
  # C:\, or D:\ or whatever, so need to do this 'expand, then unexpand' method.
  if path.start_with?('~')
    path = File.expand_path(path).sub(/^#{Regexp.quote(File.expand_path('~/'))}/, '~')
  else
    path = File.expand_path(path)
  end

  append = find_val('append_dot_origen')
  append = '.origen' if append == true || append.nil?

  gem_append = find_val('append_gems')
  gem_append = 'gems' if gem_append == true || gem_append.nil?

  if append
    unless path.end_with?(append) || (path.end_with?(File.join(append, gem_append)) if gem_append)
      path = File.join(path, append)
    end
  end
  path
end

#gem_install_dirObject Also known as: user_gem_dir

Gets the gem_intall_dir. This is either site_config.home_dir/gems or the site configs gem_install_dir



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/origen/site_config.rb', line 17

def gem_install_dir
  if gems_use_tool_repo && tool_repo_install_dir && !user_install_enable
    path = eval_path(tool_repo_install_dir)
  else
    path = eval_path(find_val('user_gem_dir') || find_val('gem_install_dir') || home_dir)
  end

  append = find_val('append_gems')
  append = 'gems' if append == true || append.nil?

  if append
    unless path.end_with?(append)
      path = File.join(path, append)
    end
  end
  path
end

#get(val) ⇒ Object Also known as: []



151
152
153
# File 'lib/origen/site_config.rb', line 151

def get(val)
  find_val(val)
end

#get_all(val) ⇒ Object



156
157
158
159
160
161
162
163
164
# File 'lib/origen/site_config.rb', line 156

def get_all(val)
  ret = []
  @configs.each do |c|
    if c.key?(val)
      ret << c[val]
    end
  end
  ret
end

#home_dirObject



41
42
43
# File 'lib/origen/site_config.rb', line 41

def home_dir
  eval_path(find_val('home_dir') || '~/')
end

#rebuild!Object



170
171
172
# File 'lib/origen/site_config.rb', line 170

def rebuild!
  configs!
end

#remove_all_instances(var) ⇒ Object Also known as: purge

Dynamically remove all the instances of :var



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/origen/site_config.rb', line 92

def remove_all_instances(var)
  # Iterate though all the site configs, removing every instance of :var
  # Return an array containing the value of :var at each config,
  # from lowest priority to highest.
  # If [] is returned, it implies that there was no instancs of :var to be removed.
  ret = []
  @configs.each do |c|
    if c.key?(var)
      ret << c.delete(var)
    end
  end
  ret
end

#remove_highest(var) ⇒ Object

Dynamically remove the highest instance of :var



80
81
82
83
84
85
86
87
88
89
# File 'lib/origen/site_config.rb', line 80

def remove_highest(var)
  @configs.each do |c|
    if c.key?(var)
      return c.delete(var)
    end
  end

  # return nil if we haven't returned a value yet
  nil
end

#user_install_dirObject

Gets the user_install_dir. Like gem_install_dir, this default to somewhere home_dir, unless overridden



37
38
39
# File 'lib/origen/site_config.rb', line 37

def user_install_dir
  eval_path(find_val('user_install_dir') || home_dir)
end