Class: Fig::Environment

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

Overview

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

Constant Summary collapse

DEFAULT_VERSION_NAME =
'current'

Instance Method Summary collapse

Constructor Details

#initialize(os, repository, variables_override, retriever) ⇒ Environment

Returns a new instance of Environment.



19
20
21
22
23
24
25
26
27
# File 'lib/fig/environment.rb', line 19

def initialize(os, repository, variables_override, retriever)
  @os = os
  @repository = repository
  @variables = variables_override || get_environment_variables
  @retrieve_vars = {}
  @packages = {}
  @applied_configs = {}
  @retriever = retriever
end

Instance Method Details

#[](name) ⇒ Object

Returns the value of an envirionment variable



36
37
38
# File 'lib/fig/environment.rb', line 36

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

#add_retrieve(name, path) ⇒ Object

Indicates that the values from a particular envrionment variable path



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

def add_retrieve(name, path)
  @retrieve_vars[name] = path
end

#apply_config(package, config_name, backtrace) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/fig/environment.rb', line 54

def apply_config(package, config_name, backtrace)
  if (@applied_configs[package.package_name] ||= []).member?(config_name)
    return
  end
  new_backtrace = backtrace

  config = package[config_name]
  config.statements.each { |stmt| apply_config_statement(package, stmt, new_backtrace) }
  @applied_configs[package.package_name] << config_name
end

#apply_config_statement(base_package, statement, backtrace) ⇒ Object



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

def apply_config_statement(base_package, statement, backtrace)
  case statement
  when Package::Path
    append_variable(base_package, statement.name, statement.value)
  when Package::Set
    set_variable(base_package, statement.name, statement.value)
  when Package::Include
    include_config(base_package, statement.package_name, statement.config_name, statement.version_name, statement.overrides, backtrace)
  when Package::Command
    # ignore
  else
    fail "Unexpected statement: #{statement}"
  end
end

#execute_command(command, args, package) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'lib/fig/environment.rb', line 71

def execute_command(command, args, package)
  with_environment do
    argument =
      expand_command_line_argument(
        "#{command.command} #{args.join(' ')}"
      )

    yield expand_path(argument, package).split(' ')
  end
end

#execute_config(base_package, package_name, config_name, version_name, args, &block) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/fig/environment.rb', line 86

def execute_config(base_package, package_name, config_name, version_name, args, &block)
  config_name ||= find_config_name_in_package(package_name)
  package = lookup_package(
    package_name || base_package.package_name,
    version_name,
    Backtrace.new(nil, package_name, version_name, config_name)
  )

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

#execute_shell(command) ⇒ Object



65
66
67
68
69
# File 'lib/fig/environment.rb', line 65

def execute_shell(command)
  with_environment do
    yield command.map{|arg| expand_command_line_argument(arg)}
  end
end

#find_config_name_in_package(package_name) ⇒ Object



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

def find_config_name_in_package(package_name)
  return @applied_configs.key?(package_name) ? @applied_configs[package_name].first : 'default'
end

#get_environment_variablesObject



29
30
31
32
33
# File 'lib/fig/environment.rb', line 29

def get_environment_variables
  vars = {}
  ENV.each { |key,value| vars[key]=value }
  return vars
end

#include_config(base_package, package_name, config_name, version_name, overrides, backtrace) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/fig/environment.rb', line 117

def include_config(base_package, package_name, config_name, version_name, overrides, backtrace)
  # Check to see if this include has been overridden.
  if backtrace
    override = backtrace.get_override(package_name || base_package.package_name)
    if override
      version_name = override
    end
  end
  new_backtrace = Backtrace.new(backtrace, package_name, version_name, config_name)
  overrides.each do |override|
    new_backtrace.add_override(override.package_name, override.version_name)
  end
  package = lookup_package(package_name || base_package.package_name, version_name, new_backtrace)
  apply_config(package, config_name || 'default', new_backtrace)
end

#register_package(package) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/fig/environment.rb', line 45

def register_package(package)
  name = package.package_name
  if @packages[name]
    Logging.fatal %Q<There is already a package with the name "#{name}".>
    raise RepositoryError.new
  end
  @packages[name] = package
end