Class: Fig::Environment

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

Overview

This class manages the program’s state, including the value of all environment variables, and which packages have already been applied

Constant Summary collapse

DEFAULT_VERSION_NAME =
"current"

Instance Method Summary collapse

Constructor Details

#initialize(os, repository, variables) ⇒ Environment

Returns a new instance of Environment.



8
9
10
11
12
13
14
15
# File 'lib/fig/environment.rb', line 8

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

Instance Method Details

#[](name) ⇒ Object

Returns the value of an envirionment variable



18
19
20
# File 'lib/fig/environment.rb', line 18

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

#add_retrieve(name, path) ⇒ Object

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



24
25
26
# File 'lib/fig/environment.rb', line 24

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

#apply_config(package, config_name) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/fig/environment.rb', line 37

def apply_config(package, config_name)
  if (@applied_configs[package.package_name] ||= []).member?(config_name)
    return
  end
  config = package[config_name]
  config.statements.each { |stmt| apply_config_statement(package, stmt) }
  @applied_configs[package.package_name] << config_name
end

#apply_config_statement(base_package, statement) ⇒ Object



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

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

#direct_retrieve(package_name, source_path, target_path) ⇒ Object



85
86
87
88
89
# File 'lib/fig/environment.rb', line 85

def direct_retrieve(package_name, source_path, target_path)
  package = lookup_package(package_name, nil)
  FileUtils.mkdir_p(target_path)
  FileUtils.cp_r(File.join(package.directory, source_path, '.'), target_path)
end

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



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

def execute_config(base_package, package_name, config_name, version_name, args)
  package = lookup_package(package_name || base_package.package_name, version_name)
  result = nil
  commands = package[config_name || "default"].commands
  with_environment do
    # todo nil check
    commands.each do |command|
      result = yield expand_arg("#{command.command} #{args.join(' ')}").gsub("@",package.directory).split(" ")
    end
  end
  result
end

#execute_shell(command) ⇒ Object



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

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

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



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

def include_config(base_package, package_name, config_name, version_name)
  package = lookup_package(package_name || base_package.package_name, version_name)
  apply_config(package, config_name || "default")
end

#register_package(package) ⇒ Object



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

def register_package(package)
  name = package.package_name
  if @packages[name]
    $stderr.puts "Package already exists with name: #{name}"
    exit 10
  end
  @packages[name] = package
end