Class: Origen::Application::Environment

Inherits:
Object
  • Object
show all
Defined in:
lib/origen/application/environment.rb

Overview

Class to control the environment.

The environment is a Ruby file that is loaded prior to generating every piece of output. It is optional, and is loaded before the target, thereby allowing targets to override environment settings.

A typical use case for the environment is to setup the test platform, or to set Origen to run in debug or simulation mode. It can generally be thought of as a global target.

All environment definition files must live in Origen.root/environment.

An instance of this class is automatically instantiated and available globally as Origen.environment.

Constant Summary collapse

DIR =

:nodoc:

"#{Origen.root}/environment"
SAVE_FILE =

:nodoc:

"#{DIR}/.default"
DEFAULT_FILE =

:nodoc:

"#{DIR}/default.rb"

Instance Method Summary collapse

Instance Method Details

#all_environmentsObject

Returns Array of all environments available



27
28
29
30
31
32
33
# File 'lib/origen/application/environment.rb', line 27

def all_environments
  envs = []
  find('').sort.each do |file|
    envs << File.basename(file)
  end
  envs
end

#default=(name) ⇒ Object

As #temporary= except that the given environment will be set to the workspace default



81
82
83
84
85
86
87
88
# File 'lib/origen/application/environment.rb', line 81

def default=(name)
  if name
    self.temporary = name
  else
    @file = nil
  end
  save
end

#default_fileObject

Load the default file from the workspace default if it exists and return it, otherwise returns nil



134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/origen/application/environment.rb', line 134

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

#describeObject

Prints out the current environment details to the command line



91
92
93
94
95
96
97
98
99
# File 'lib/origen/application/environment.rb', line 91

def describe
  f = file!
  puts "Current environment: #{f.basename}"
  puts '*' * 70
  File.open(f).each do |line|
    puts "  #{line}"
  end
  puts '*' * 70
end

#exists?(name) ⇒ Boolean Also known as: exist?

Returns true if the environment exists, this can be used to test for the presence of an environment before calling one of the other methods to actually apply it.

It will return true if one or more environments are found matching the given name, use the unique? method to test if the given name uniquely identifies a valid environment.

Returns:

  • (Boolean)


41
42
43
44
# File 'lib/origen/application/environment.rb', line 41

def exists?(name)
  envs = find(name)
  envs.size > 0
end

#fileObject

Returns the environment file (a Pathname object) if it has been defined, otherwise nil



148
149
150
151
152
153
154
# File 'lib/origen/application/environment.rb', line 148

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



157
158
159
160
161
162
163
164
165
# File 'lib/origen/application/environment.rb', line 157

def file! # :nodoc:
  unless file
    puts 'No environment has been specified!'
    puts 'To specify an environment use the -e switch.'
    puts 'Look in the environment directory for a list of available environment names.'
    exit 1
  end
  file
end

#file=(path) ⇒ Object

:nodoc:



167
168
169
170
171
172
173
# File 'lib/origen/application/environment.rb', line 167

def file=(path) # :nodoc:
  if path
    @file = Pathname.new(path)
  else
    @file = nil
  end
end

#find(name) ⇒ Object

Returns an array of matching environment file paths



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/origen/application/environment.rb', line 102

def find(name)
  if name
    name = name.gsub('*', '')
    if File.exist?(name)
      [name]
    elsif File.exist?("#{Origen.root}/environment/#{name}") && name != ''
      ["#{Origen.root}/environment/#{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

#forgetObject

Remove the workspace default environment



176
177
178
179
# File 'lib/origen/application/environment.rb', line 176

def forget
  File.delete(SAVE_FILE) if File.exist?(SAVE_FILE)
  @default_file = nil
end

#nameObject

Returns the name (the filename) of the current environment



22
23
24
# File 'lib/origen/application/environment.rb', line 22

def name
  file.basename('.rb').to_s if file
end

#saveObject

Saves the current environment as the workspace default, i.e. the current environment will be used by Origen the next time if no other environment is specified



122
123
124
125
126
127
128
129
130
# File 'lib/origen/application/environment.rb', line 122

def save # :nodoc:
  if @file
    File.open(SAVE_FILE, 'w') do |f|
      Marshal.dump(file, f)
    end
  else
    forget
  end
end

#temporary=(name) ⇒ Object Also known as: switch, switch_to

Switch to the supplied environment, name can be a fragment as long as it allows a unique environment to be identified.

Calling this method does not affect the default environment setting in the workspace.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/origen/application/environment.rb', line 58

def temporary=(name)
  envs = find(name)
  if envs.size == 0
    puts "Sorry no environments were found matching '#{name}'!"
    puts 'Here are the available options:'
    find('').sort.each do |file|
      puts File.basename(file)
    end
    exit 1
  elsif envs.size > 1
    puts 'Please try again with one of the following environments:'
    envs.sort.each do |file|
      puts File.basename(file)
    end
    exit 1
  else
    self.file = envs[0]
  end
end

#temporary?Boolean

Returns true if running with a temporary environment, i.e. if the current environment is not the same as the default environment

Returns:

  • (Boolean)


183
184
185
# File 'lib/origen/application/environment.rb', line 183

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 environment.

Returns:

  • (Boolean)


49
50
51
52
# File 'lib/origen/application/environment.rb', line 49

def unique?(name)
  envs = find(name)
  envs.size == 1
end