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



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/fig/runtime_environment.rb', line 25

def initialize(
  repository,
  suppress_includes,
  variables_override,
  working_directory_maintainer
)
  @repository                   = repository
  @suppress_includes            = suppress_includes
  @variables                    =
    variables_override || Fig::OperatingSystem.get_environment_variables()
  @retrieves                    = {}
  @packages                     = {}
  @working_directory_maintainer = working_directory_maintainer
end

Instance Method Details

#[](name) ⇒ Object

Returns the value of an environment variable



41
42
43
# File 'lib/fig/runtime_environment.rb', line 41

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.



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/fig/runtime_environment.rb', line 51

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



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/fig/runtime_environment.rb', line 86

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

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

  config = package[config_name]

  Fig::Logging.debug(
    "Applying #{package.to_descriptive_string_with_config config_name}."
  )
  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.



151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/fig/runtime_environment.rb', line 151

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.descriptor, backtrace)
  when Fig::Statement::Override
    backtrace.add_override(statement)
  end

  return
end

#check_unused_retrievesObject



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

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

    statement = @retrieves[name]
    if statement.loaded_but_not_referenced?
      text, * = Fig::Unparser.determine_version_and_unparse(
        [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



116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/fig/runtime_environment.rb', line 116

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, base_package)
    }

  @variables.with_environment { yield expanded_command_line }

  return
end

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



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

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



82
83
84
# File 'lib/fig/runtime_environment.rb', line 82

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

#register_package(package) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/fig/runtime_environment.rb', line 64

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

  @packages[name] = package

  return
end

#variablesObject



45
46
47
# File 'lib/fig/runtime_environment.rb', line 45

def variables
  return @variables.clone
end