Class: Origen::Application::Target
- Defined in:
- lib/origen/application/target.rb
Overview
Class to handle the target.
The target is a Ruby file that is run prior to generating each pattern, and it should be used to instantiate the top-level models used by the application. It can also be used to override and settings within these classes after they have been instantiated and before they are run.
All target files must live in Origen.root/target.
An instance of this class is automatically instantiated and available globally as Origen.app.target
Constant Summary collapse
- DIR =
:nodoc:
"#{Origen.root}/target"
- SAVE_FILE =
:nodoc:
"#{DIR}/.default"
- DEFAULT_FILE =
:nodoc:
"#{DIR}/default.rb"
Instance Method Summary collapse
-
#all_targets ⇒ Object
Returns Array of all targets available.
-
#default=(name) ⇒ Object
As #temporary= except that the given target will be set to the workspace default.
-
#default_file ⇒ Object
Load the default file from the workspace default if it exists and return it, otherwise returns nil.
-
#describe ⇒ Object
Prints out the current target details to the command line.
-
#each_production(options = {}) ⇒ Object
Use this to implement a loop for each production target, it will automatically load each target before yielding to the block.
-
#each_unique_production(options = {}) ⇒ Object
As each_production except it only yields unique targets.
-
#exists?(name) ⇒ Boolean
(also: #exist?)
Returns true if the target exists, this can be used to test for the presence of a target before calling one of the other methods to actually apply it.
-
#file ⇒ Object
Returns the target file (a Pathname object) if it has been defined, otherwise nil.
-
#file! ⇒ Object
As file except will raise an exception if it hasn’t been defined yet.
-
#file=(path) ⇒ Object
:nodoc:.
-
#find(name) ⇒ Object
Returns an array of matching target file paths.
-
#forget ⇒ Object
Remove the workspace default target.
-
#is_a_moo_number?(name) ⇒ Boolean
Returns true if the supplied target name is a moo number format.
-
#load!(options = {}) ⇒ Object
Load the target, calling this will re-instantiate all top-level objects defined there.
-
#loop(options = {}) ⇒ Object
Implement a target loop based on the supplied options.
-
#moo ⇒ Object
If the production_targets moo number mapping inclues the current target then the MOO number will be returned, otherwise nil.
-
#moo_number_minus_revision(name) ⇒ Object
:nodoc:.
-
#name ⇒ Object
Returns the name (the filename) of the current target.
-
#prod_targets ⇒ Object
Returns config.production_targets with all keys forced to upper case.
-
#production_targets ⇒ Object
Returns an array containing all current production targets.
-
#resolve_mapping(name) ⇒ Object
Resolves the target name to a target file if a MOO number is supplied and app.config.production_targets has been defined.
-
#save ⇒ Object
Saves the current target as the workspace default, i.e.
- #set_signature(options) ⇒ Object private
-
#signature ⇒ Object
Returns a signature for the current target, can be used to track target changes in cases where the name is not unique - i.e.
-
#temporary=(name) ⇒ Object
(also: #switch, #switch_to)
Switch to the supplied target, name can be a fragment as long as it allows a unique target to be identified.
-
#temporary? ⇒ Boolean
Returns true if running with a temporary target, i.e.
-
#unique?(name) ⇒ Boolean
Similar to the exists? method, this will return true only if the given name resolves to a single valid target.
Instance Method Details
#all_targets ⇒ Object
Returns Array of all targets available
121 122 123 124 125 126 127 |
# File 'lib/origen/application/target.rb', line 121 def all_targets targets = [] find('').sort.each do |file| targets << File.basename(file) end targets # return end |
#default=(name) ⇒ Object
As #temporary= except that the given target will be set to the workspace default
206 207 208 209 210 211 212 213 |
# File 'lib/origen/application/target.rb', line 206 def default=(name) if name self.temporary = name else @file = nil end save end |
#default_file ⇒ Object
Load the default file from the workspace default if it exists and return it, otherwise returns nil
259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/origen/application/target.rb', line 259 def default_file return @default_file if @default_file if File.exist?(SAVE_FILE) File.open(SAVE_FILE) do |f| @default_file = Marshal.load(f) end elsif File.exist?(DEFAULT_FILE) @default_file = Pathname.new(DEFAULT_FILE) end @default_file end |
#describe ⇒ Object
Prints out the current target details to the command line
216 217 218 219 220 221 222 223 224 |
# File 'lib/origen/application/target.rb', line 216 def describe f = self.file! puts "Current target: #{f.basename}" puts '*' * 70 File.open(f).each do |line| puts " #{line}" end puts '*' * 70 end |
#each_production(options = {}) ⇒ Object
Use this to implement a loop for each production target, it will automatically load each target before yielding to the block.
The production targets are defined by the production_targets configuration option.
Example
Origen.app.target.each_production do
Run something within the context of each target
end
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/origen/application/target.rb', line 52 def each_production( = {}) = { force_debug: false, # Set true to force debug mode for all targets }.merge() prod_targets.each do |moo, targets| [targets].flatten.each do |target| self.temporary = target Origen.app.load_target!() yield moo end end end |
#each_unique_production(options = {}) ⇒ Object
As each_production except it only yields unique targets. i.e. if you have two MOOs that use the same target file defined in the production_targets then this method will only yield once.
An array of MOOs that use each target is returned each time.
Example
Origen.app.target.each_unique_production do |moos|
Run something within the context of each unique target
end
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/origen/application/target.rb', line 74 def each_unique_production( = {}) = { force_debug: false, # Set true to force debug mode for all targets }.merge() targets = {} prod_targets.each do |moo, moos_targets| [moos_targets].flatten.each do |target| if targets[target] targets[target] << moo else targets[target] = [moo] end end end targets.each do |target, moos| self.temporary = target Origen.app.load_target!() yield moos end end |
#exists?(name) ⇒ Boolean Also known as: exist?
Returns true if the target exists, this can be used to test for the presence of a target before calling one of the other methods to actually apply it.
It will return true if one or more targets are found matching the given name, use the unique? method to test if the given name uniquely identifies a valid target.
140 141 142 143 144 |
# File 'lib/origen/application/target.rb', line 140 def exists?(name) tgts = resolve_mapping(name) targets = tgts.is_a?(Array) ? tgts : find(tgts) targets.size > 0 end |
#file ⇒ Object
Returns the target file (a Pathname object) if it has been defined, otherwise nil
272 273 274 275 276 277 |
# File 'lib/origen/application/target.rb', line 272 def file # :nodoc: return @file if @file if default_file && File.exist?(default_file) @file = default_file end end |
#file! ⇒ Object
As file except will raise an exception if it hasn’t been defined yet
280 281 282 283 284 285 286 287 288 |
# File 'lib/origen/application/target.rb', line 280 def file! # :nodoc: unless file puts 'No target has been specified!' puts 'To specify a target use the -t switch.' puts 'Look in the target directory for a list of available target names.' exit 1 end file end |
#file=(path) ⇒ Object
:nodoc:
290 291 292 293 294 295 296 |
# File 'lib/origen/application/target.rb', line 290 def file=(path) # :nodoc: if path @file = Pathname.new(path) else @file = nil end end |
#find(name) ⇒ Object
Returns an array of matching target file paths
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/origen/application/target.rb', line 227 def find(name) if name name = name.gsub('*', '') if File.exist?(name) [name] elsif File.exist?("#{Origen.root}/target/#{name}") && name != '' ["#{Origen.root}/target/#{name}"] else # The below weirdness is to make it recurse into symlinked directories Dir.glob("#{DIR}/**{,/*/**}/*").sort.uniq.select do |file| File.basename(file) =~ /#{name}/ && file !~ /.*\.rb.+$/ end end else [nil] end end |
#forget ⇒ Object
Remove the workspace default target
299 300 301 302 |
# File 'lib/origen/application/target.rb', line 299 def forget File.delete(SAVE_FILE) if File.exist?(SAVE_FILE) @default_file = nil end |
#is_a_moo_number?(name) ⇒ Boolean
Returns true if the supplied target name is a moo number format
364 365 366 |
# File 'lib/origen/application/target.rb', line 364 def is_a_moo_number?(name) # :nodoc: !!(name.to_s.upcase =~ /^\d?\d?\*?[A-Z]\d\d[A-Z]$/) end |
#load!(options = {}) ⇒ Object
Load the target, calling this will re-instantiate all top-level objects defined there.
113 114 115 116 117 118 |
# File 'lib/origen/application/target.rb', line 113 def load!( = {}) = { force_debug: false, # Set true to force debug mode for all targets }.merge() Origen.app.load_target!() end |
#loop(options = {}) ⇒ Object
Implement a target loop based on the supplied options. The options can contain the keys :target or :targets or neither.
In the neither case the loop will run once for the current workspace default target.
In the case where one of the keys is present the loop will run for each target and the options will be passed into the block with the key :target set to the current target.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/origen/application/target.rb', line 28 def loop( = {}) = { set_target: true, force_debug: false, # Set true to force debug mode for all targets }.merge() targets = [.delete(:target), .delete(:targets)].flatten.compact.uniq targets = [file!.basename.to_s] if targets.empty? set = .delete(:set_target) targets.each do |target| Origen.load_target(target, ) if set [:target] = target yield end end |
#moo ⇒ Object
If the production_targets moo number mapping inclues the current target then the MOO number will be returned, otherwise nil
97 98 99 100 101 102 103 104 |
# File 'lib/origen/application/target.rb', line 97 def moo prod_targets.each do |moo, targets| [targets].flatten.each do |target| return moo if File.basename(target, '.rb').to_s == file.basename('.rb').to_s end end nil end |
#moo_number_minus_revision(name) ⇒ Object
:nodoc:
368 369 370 371 |
# File 'lib/origen/application/target.rb', line 368 def moo_number_minus_revision(name) # :nodoc: name.to_s.upcase =~ /^\d?\d?([A-Z]\d\d[A-Z])$/ Regexp.last_match[1] end |
#name ⇒ Object
Returns the name (the filename) of the current target
107 108 109 |
# File 'lib/origen/application/target.rb', line 107 def name file.basename('.rb').to_s if file end |
#prod_targets ⇒ Object
Returns config.production_targets with all keys forced to upper case
353 354 355 356 357 358 359 360 361 |
# File 'lib/origen/application/target.rb', line 353 def prod_targets # :nodoc: return {} unless Origen.config.production_targets return @prod_targets if @prod_targets @prod_targets = {} Origen.config.production_targets.each do |key, value| @prod_targets[key.upcase] = value end @prod_targets end |
#production_targets ⇒ Object
Returns an array containing all current production targets
130 131 132 |
# File 'lib/origen/application/target.rb', line 130 def production_targets prod_targets.map { |_moo, targets| targets }.uniq end |
#resolve_mapping(name) ⇒ Object
Resolves the target name to a target file if a MOO number is supplied and app.config.production_targets has been defined
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 |
# File 'lib/origen/application/target.rb', line 335 def resolve_mapping(name) # :nodoc: if is_a_moo_number?(name) && prod_targets # If an exact match if prod_targets[name.upcase] prod_targets[name.upcase] # If a wildcard match elsif prod_targets["*#{moo_number_minus_revision(name)}"] prod_targets["*#{moo_number_minus_revision(name)}"] # Else just return the given name else name end else name end end |
#save ⇒ Object
Saves the current target as the workspace default, i.e. the current target will be used by Origen the next time if no other target is specified
247 248 249 250 251 252 253 254 255 |
# File 'lib/origen/application/target.rb', line 247 def save # :nodoc: if @file File.open(SAVE_FILE, 'w') do |f| Marshal.dump(file, f) end else forget end end |
#set_signature(options) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
200 201 202 203 |
# File 'lib/origen/application/target.rb', line 200 def set_signature() ||= {} @signature = .merge(_tname: name).to_a.hash end |
#signature ⇒ Object
Returns a signature for the current target, can be used to track target changes in cases where the name is not unique - i.e. when using a configurable target
195 196 197 |
# File 'lib/origen/application/target.rb', line 195 def signature @signature ||= set_signature(nil) end |
#temporary=(name) ⇒ Object Also known as: switch, switch_to
Switch to the supplied target, name can be a fragment as long as it allows a unique target to be identified.
The name can also be a MOO number mapping from the config.production_targets attribute of the application.
Calling this method does not affect the default target setting in the workspace.
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/origen/application/target.rb', line 162 def temporary=(name) tgts = resolve_mapping(name) targets = tgts.is_a?(Array) ? tgts : find(tgts) if targets.size == 0 puts "Sorry no targets were found matching '#{name}'!" puts 'Here are the available options:' find('').sort.each do |file| puts File.basename(file) end exit 1 elsif targets.size > 1 if is_a_moo_number?(name) && prod_targets puts "Multiple production targets exist for #{name.upcase}, use one of the following instead of the MOO number:" targets.sort.each do |file| puts File.basename(file) end else puts 'Please try again with one of the following targets:' targets.sort.each do |file| puts File.basename(file) end end exit 1 else self.file = targets[0] end end |
#temporary? ⇒ Boolean
Returns true if running with a temporary target, i.e. if the current target is not the same as the default target
329 330 331 |
# File 'lib/origen/application/target.rb', line 329 def temporary? @file == @default_file end |
#unique?(name) ⇒ Boolean
Similar to the exists? method, this will return true only if the given name resolves to a single valid target.
149 150 151 152 153 |
# File 'lib/origen/application/target.rb', line 149 def unique?(name) tgts = resolve_mapping(name) targets = tgts.is_a?(Array) ? tgts : find(tgts) targets.size == 1 end |