lib/fig/environment.rb in fig-0.1.57 vs lib/fig/environment.rb in fig-0.1.59
- old
+ new
@@ -17,11 +17,11 @@
class Environment
DEFAULT_VERSION_NAME = 'current'
def initialize(repository, variables_override, retriever)
@repository = repository
- @variables = variables_override || get_system_environment_variables
+ @variables = variables_override || OperatingSystem.get_environment_variables
@retrieve_vars = {}
@packages = {}
@retriever = retriever
end
@@ -40,11 +40,11 @@
return
end
def register_package(package)
- name = package.package_name
+ name = package.name
if get_package(name)
Logging.fatal %Q<There is already a package with the name "#{name}".>
raise RepositoryError.new
end
@@ -52,12 +52,12 @@
@packages[name] = package
return
end
- def get_package(package_name)
- return @packages[package_name]
+ def get_package(name)
+ return @packages[name]
end
def packages
return @packages.values
end
@@ -74,19 +74,19 @@
return
end
def execute_shell(command)
- with_environment do
+ @variables.with_environment do
yield command.map{|arg| expand_command_line_argument(arg)}
end
return
end
def execute_command(command, args, package)
- with_environment do
+ @variables.with_environment do
argument =
expand_command_line_argument(
"#{command.command} #{args.join(' ')}"
)
@@ -94,25 +94,31 @@
end
return
end
- def find_config_name_in_package(package_name)
- package = get_package(package_name)
+ def find_config_name_in_package(name)
+ package = get_package(name)
if not package
return Package::DEFAULT_CONFIG
end
return package.primary_config_name || Package::DEFAULT_CONFIG
end
- def execute_config(base_package, package_name, config_name, version_name, args, &block)
- config_name ||= find_config_name_in_package(package_name)
+ def execute_config(base_package, descriptor, args, &block)
+ config_name =
+ descriptor.config || find_config_name_in_package(descriptor.name)
+
+ name = descriptor.name || base_package.name
package = lookup_package(
- package_name || base_package.package_name,
- version_name,
- Backtrace.new(nil, package_name, version_name, config_name)
+ name,
+ descriptor.version,
+ Backtrace.new(
+ nil,
+ PackageDescriptor.new(name, descriptor.version, config_name)
+ )
)
command = package[config_name].command
if command
execute_command(command, args, package, &block)
@@ -124,123 +130,107 @@
end
def apply_config_statement(base_package, statement, backtrace)
case statement
when Statement::Path
- append_variable(base_package, statement.name, statement.value)
+ prepend_variable(base_package, statement.name, statement.value)
when Statement::Set
set_variable(base_package, statement.name, statement.value)
when Statement::Include
- include_config(base_package, statement.package_name, statement.config_name, statement.version_name, statement.overrides, backtrace)
+ include_config(
+ base_package, statement.descriptor, statement.overrides, backtrace
+ )
when Statement::Command
# ignore
else
fail "Unexpected statement: #{statement}"
end
return
end
- def include_config(base_package, package_name, config_name, version_name, overrides, backtrace)
+ def include_config(base_package, descriptor, overrides, backtrace)
+ resolved_descriptor = nil
+
# Check to see if this include has been overridden.
if backtrace
- override = backtrace.get_override(package_name || base_package.package_name)
+ override = backtrace.get_override(
+ descriptor.name || base_package.name
+ )
if override
- version_name = override
+ resolved_descriptor =
+ PackageDescriptor.new(
+ descriptor.name, override, descriptor.config
+ )
end
end
- new_backtrace = Backtrace.new(backtrace, package_name, version_name, config_name)
+ resolved_descriptor ||= descriptor
+
+ new_backtrace = Backtrace.new(backtrace, resolved_descriptor)
overrides.each do |override|
- new_backtrace.add_override(override.package_name, override.version_name)
+ new_backtrace.add_override(override.package_name, override.version)
end
package = lookup_package(
- package_name || base_package.package_name, version_name, new_backtrace
+ resolved_descriptor.name || base_package.name,
+ resolved_descriptor.version,
+ new_backtrace
)
apply_config(
- package, config_name || Package::DEFAULT_CONFIG, new_backtrace
+ package,
+ resolved_descriptor.config || Package::DEFAULT_CONFIG,
+ new_backtrace
)
return
end
private
- def get_system_environment_variables
- vars = {}
- ENV.each { |key,value| vars[key]=value }
-
- return vars
- end
-
def set_variable(base_package, name, value)
@variables[name] = expand_and_retrieve_variable_value(base_package, name, value)
return
end
- def append_variable(base_package, name, value)
+ def prepend_variable(base_package, name, value)
value = expand_and_retrieve_variable_value(base_package, name, value)
- # TODO: converting all environment variables to upcase is not a robust
- # comparison. It also assumes all env vars will be in upcase
- # in package.fig
- prev = nil
- @variables.each do |key, val|
- if key.upcase == name.upcase
- name = key
- prev = val
- end
- end
- if prev
- @variables[name] = value + File::PATH_SEPARATOR + prev
- else
- @variables[name] = value
- end
+ @variables.prepend_variable(name, value)
return
end
- def with_environment
- old_env = {}
- begin
- @variables.each { |key,value| old_env[key] = ENV[key]; ENV[key] = value }
- yield
- ensure
- old_env.each { |key,value| ENV[key] = value }
- end
-
- return
- end
-
- def lookup_package(package_name, version_name, backtrace)
- package = get_package(package_name)
+ def lookup_package(name, version, backtrace)
+ package = get_package(name)
if package.nil?
- if not version_name
- Logging.fatal "No version specified for #{package_name}."
+ if not version
+ Logging.fatal "No version specified for #{name}."
raise RepositoryError.new
end
- package = @repository.get_package(package_name, version_name)
+ package = @repository.get_package(
+ PackageDescriptor.new(name, version, nil)
+ )
package.backtrace = backtrace
- @packages[package_name] = package
- elsif version_name && version_name != package.version_name
+ @packages[name] = package
+ elsif version && version != package.version
string_handle = StringIO.new
backtrace.dump(string_handle) if backtrace
package.backtrace.dump(string_handle) if package.backtrace
stacktrace = string_handle.string
Logging.fatal \
- "Version mismatch: #{package_name}" \
+ "Version mismatch: #{name}" \
+ ( stacktrace.empty? ? '' : "\n#{stacktrace}" )
raise RepositoryError.new
end
return package
end
# Replace @ symbol with the package's directory, "[package]" with the
# package name.
def expand_and_retrieve_variable_value(base_package, name, value)
- return value unless base_package && base_package.package_name
+ return value unless base_package && base_package.name
file = expand_path(value, base_package)
if @retrieve_vars.member?(name)
# A '//' in the source file's path tells us to preserve path
@@ -257,12 +247,12 @@
)
if not File.directory?(file)
target = File.join(target, File.basename(file))
end
end
- @retriever.with_package_config(
- base_package.package_name, base_package.version_name
+ @retriever.with_package_version(
+ base_package.name, base_package.version
) do
@retriever.retrieve(file, target)
end
file = target
end
@@ -333,12 +323,9 @@
return
end
def translate_retrieve_variables(base_package, name)
return \
- @retrieve_vars[name].gsub(
- / \[package\] /x,
- base_package.package_name
- )
+ @retrieve_vars[name].gsub(/ \[package\] /x, base_package.name)
end
end
end