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.



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

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
end

Instance Method Details

#[](name) ⇒ Object

Returns the value of an environment variable



47
48
49
# File 'lib/fig/runtime_environment.rb', line 47

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.



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

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].path}" with "#{retrieve_statement.path}".>
  end

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

  return
end

#apply_config(package, config_name, backtrace) ⇒ Object



92
93
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
# File 'lib/fig/runtime_environment.rb', line 92

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

  Fig::Logging.debug(
    "Applying #{package.to_descriptive_string_with_config config_name}."
  )

  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)
  end

  return
end

#apply_config_statement(package, statement, backtrace) ⇒ 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.



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/fig/runtime_environment.rb', line 164

def apply_config_statement(package, statement, backtrace)
  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)
  when Fig::Statement::IncludeFile
    include_file_config(package, statement, backtrace)
  when Fig::Statement::Override
    backtrace.add_override(statement)
  end

  return
end

#check_for_unused_retrievesObject



181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/fig/runtime_environment.rb', line 181

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



129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/fig/runtime_environment.rb', line 129

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



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

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



88
89
90
# File 'lib/fig/runtime_environment.rb', line 88

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

#register_package(package) ⇒ Object



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

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



51
52
53
# File 'lib/fig/runtime_environment.rb', line 51

def variables
  return @variables.clone
end