Class: Fig::RuntimeEnvironment

Inherits:
Object
  • Object
show all
Defined in:
lib/fig/runtime_environment.rb

Overview

Manages the program’s metadata, including packages and environment variables, and sets things up for running commands (from “command” statements in definition files or from the command-line).

Instance Method Summary collapse

Constructor Details

#initialize(repository, non_repository_packages, suppress_includes, variables_override, working_directory_maintainer) ⇒ RuntimeEnvironment

Note: when reading this code, understand that the word “retrieve” is a noun and not a verb, e.g. “retrieve path” means the value of a retrieve statement and not the action of retrieving a path.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/fig/runtime_environment.rb', line 30

def initialize(
  repository,
  non_repository_packages,
  suppress_includes,
  variables_override,
  working_directory_maintainer
)
  @repository                   = repository
  @non_repository_packages      = non_repository_packages
  @suppress_includes            = suppress_includes
  @variables                    =
    variables_override || Fig::OperatingSystem.get_environment_variables()
  @retrieves                    = {}
  @named_packages               = {}
  @working_directory_maintainer = working_directory_maintainer
  @all_override_statements      = []
end

Instance Method Details

#[](name) ⇒ Object

Returns the value of an environment variable



49
50
51
# File 'lib/fig/runtime_environment.rb', line 49

def [](name)
  return @variables[name]
end

#add_retrieve(retrieve_statement) ⇒ Object

Indicates that the values from a particular environment variable path should be copied to a local directory.



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/fig/runtime_environment.rb', line 59

def add_retrieve(retrieve_statement)
  name = retrieve_statement.variable
  if @retrieves.has_key?(name)
    Fig::Logging.warn \
      %Q<About to overwrite "#{name}" retrieve path of "#{@retrieves[name].tokenized_path.to_escaped_string}" with "#{retrieve_statement.tokenized_path.to_escaped_string}".>
  end

  @retrieves[name] = retrieve_statement
  retrieve_statement.added_to_environment(true)

  return
end

#apply_config(package, config_name, backtrace, current_package_depth = 0) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/fig/runtime_environment.rb', line 94

def apply_config(package, config_name, backtrace, current_package_depth = 0)
  if package.applied_config_names.member?(config_name)
    return
  end

  log_config_processing(package.name, package.version, config_name)
  Fig::Logging.debug(
    "Applying #{package.to_descriptive_string_with_config config_name}, package depth #{current_package_depth}."
  )

  new_backtrace = backtrace ||
    Fig::IncludeBacktrace.new(
      nil,
      Fig::PackageDescriptor.new(
        package.name,
        package.version,
        config_name,
        :file_path   => package.file_path,
        :description => package.description
      )
    )

  config = nil
  begin
    config = package[config_name]
  rescue Fig::NoSuchPackageConfigError => error
    raise_repository_error(error.message, new_backtrace, error.package)
  end

  package.add_applied_config_name(config_name)
  config.statements.each do
    |statement|
    apply_config_statement(package, statement, new_backtrace, current_package_depth)
  end

  return
end

#apply_config_statement(package, statement, backtrace, current_package_depth) ⇒ Object

In order for this to work correctly, any Overrides need to be processed before any other kind of Statement. The Statement::Configuration class guarantees that those come first in its set of Statements.



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/fig/runtime_environment.rb', line 167

def apply_config_statement(package, statement, backtrace, current_package_depth)
  case statement
  when Fig::Statement::Path
    prepend_variable(package, statement, backtrace)
  when Fig::Statement::Set
    set_variable(package, statement, backtrace)
  when Fig::Statement::Include
    include_config(package, statement, backtrace, current_package_depth)
  when Fig::Statement::IncludeFile
    include_file_config(package, statement, backtrace, current_package_depth)
  when Fig::Statement::Override
    backtrace.add_override(statement)
    @all_override_statements << statement
  end

  return
end

#check_for_unused_overridesObject



200
201
202
203
204
205
206
207
# File 'lib/fig/runtime_environment.rb', line 200

def check_for_unused_overrides()
  @all_override_statements.each do |statement|
    if statement.loaded_but_not_referenced?
      Fig::Logging.warn \
        %Q<Override "#{statement.package_name}/#{statement.version}"#{statement.position_string} was never used.>
    end
  end
end

#check_for_unused_retrievesObject



185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/fig/runtime_environment.rb', line 185

def check_for_unused_retrieves()
  @retrieves.keys().sort().each do
    |name|

    statement = @retrieves[name]
    if statement.loaded_but_not_referenced?
      text, * = Fig::Deparser.determine_version_and_deparse(
        [statement], :emit_as_input
      )
      Fig::Logging.warn \
        %Q<The #{name} variable was never referenced or didn't need expansion, so "#{text.strip}"#{statement.position_string} was ignored.>
    end
  end
end

#expand_command_line(base_package, base_config, descriptor, command_line) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/fig/runtime_environment.rb', line 132

def expand_command_line(base_package, base_config, descriptor, command_line)
  package, * =
    determine_package_for_execution(base_package, base_config, descriptor)

  expanded_command_line =
    command_line.map {
      |argument| expand_command_line_argument(argument, package)
    }

  @variables.with_environment { yield expanded_command_line }

  return
end

#expand_command_statement_from_config(base_package, base_config, descriptor, extra_arguments, &block) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/fig/runtime_environment.rb', line 146

def expand_command_statement_from_config(
  base_package, base_config, descriptor, extra_arguments, &block
)
  package, config_name =
    determine_package_for_execution(base_package, base_config, descriptor)

  command_statement = package[config_name].command_statement
  if command_statement
    expand_command(command_statement, extra_arguments, package, &block)
  else
    raise Fig::UserInputError.new(
      %Q<The "#{package.to_s}" package with the "#{config_name}" configuration does not contain a command.>
    )
  end

  return
end

#get_package(name) ⇒ Object



90
91
92
# File 'lib/fig/runtime_environment.rb', line 90

def get_package(name)
  return @named_packages[name]
end

#register_package(package) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/fig/runtime_environment.rb', line 72

def register_package(package)
  name = package.name

  if get_package(name)
    raise_repository_error(
      name.nil? \
        ? %Q<There is already a package with the name "#{name}".> \
        : %q<There is already an unnamed package.>,
      nil,
      package
    )
  end

  @named_packages[name] = package

  return
end

#variablesObject



53
54
55
# File 'lib/fig/runtime_environment.rb', line 53

def variables
  return @variables.clone
end