PREFIX_OPTIONS | = | [:trace, :json, :yaml] |
environment_name | [R] | |
shell_wrapper | [R] |
Returns the environment identifier for the current environment, as determined from the GEM_HOME.
# File lib/rvm/environment/utility.rb, line 13 def self.current_environment_id @current_environment_id ||= begin gem_home = ENV['GEM_HOME'].to_s.strip if !gem_home.empty? && gem_home =~ /rvm\/gems\// File.basename(gem_home) else matching_path = $:.select { |item| item =~ /rvm\/rubies/ }.first matching_path.to_s.gsub(/^.*rvm\/rubies\//, '').split('/')[0] || "system" end end end
# File lib/rvm/environment/utility.rb, line 6 def self.default_rvm_path value = `bash '#{File.expand_path('../shell/calculate_rvm_path.sh', File.dirname(__FILE__))}'`.strip $?.success? && !value.empty? ? File.expand_path(value) : nil end
Define the config accessor, which basically uses config_value_for and the linke to set the env variable.
# File lib/rvm/environment/configuration.rb, line 6 def self.define_config_accessor(*args) singleton = (class << self; self; end) args.each do |arg| singleton.send(:define_method, arg) { RVM::Environment.config_value_for(arg) } singleton.send(:define_method, "#{arg}=""#{arg}=") { |v| RVM::Environment.merge_config! arg => v } end end
Creates a new environment with the given name and optionally a set of extra environment variables to be set on load.
# File lib/rvm/environment.rb, line 22 def initialize(environment_name = "default", options = {}) merge_config! options @environment_name = environment_name @shell_wrapper = Shell.default_wrapper.new @shell_wrapper.setup do |s| source_rvm_environment use_rvm_environment end end
Given an environment identifier, it will add the the given gemset to the end to form a qualified identifier name.
# File lib/rvm/environment/utility.rb, line 110 def self.environment_with_gemset(environment, gemset) environment_name, gemset_name = environment.split("@", 2) environment_name = "default" if environment_name.to_s.empty? environment_name << "@#{gemset}" unless gemset.to_s.empty? environment_name end
Creates an alias with the given name.
# File lib/rvm/environment/alias.rb, line 27 def alias_create(name, ruby_string) rvm(:alias, :create, name.to_s, ruby_string.to_s).successful? end
Deletes an alias and returns the exit status.
# File lib/rvm/environment/alias.rb, line 22 def alias_delete(name) rvm(:alias, :delete, name.to_s).successful? end
Returns a hash of aliases.
# File lib/rvm/environment/alias.rb, line 5 def alias_list lines = normalize_array(rvm(:alias, :list).stdout) lines.inject({}) do |acc, current| alias_name, ruby_string = current.to_s.split(" => ") unless alias_name.empty? || ruby_string.empty? acc[alias_name] = ruby_string end acc end end
Run commands inside the given directory.
# File lib/rvm/environment/utility.rb, line 72 def chdir(dir) run_silently :pushd, dir.to_s result = Dir.chdir(dir) { yield } run_silently :popd result end
Returns the ruby-like interface defined by CleanupWrapper
# File lib/rvm/environment/cleanup.rb, line 12 def cleanup @cleanup_wrapper ||= CleanupWrapper.new(self) end
Returns the value for a configuration option (mapping to an environment variable). If check_live is true (which it is by default), it will also check the environment for a value.
# File lib/rvm/environment/configuration.rb, line 48 def config_value_for(key, default = nil, check_live = true) key = key.to_s value = check_live ? self[key.to_s] : nil value || config[key] || self.class.config_value_for(key) || default end
Generates the default wrappers.
# File lib/rvm/environment/wrapper.rb, line 11 def default_wrappers(ruby_string, *binaries) wrapper ruby_string, '', *binaries end
Returns a hash of all of the user defined configuration.
# File lib/rvm/environment/configuration.rb, line 55 def defined_config self.class.config.merge(self.config) end
Executes a command, replacing the current shell. exec is a bit of an odd ball compared to the others, since it has to use the Kernel.exec builtin.
# File lib/rvm/environment/sets.rb, line 63 def exec(command, *args) command = @shell_wrapper.build_cli_call(:exec, [command] + args) Kernel.exec "bash", "-c", "source '#{env_path}'; #{command}" end
Copies the gems between two different gemsets.
# File lib/rvm/environment/gemset.rb, line 31 def gemset_copy(from, to) rvm(:gemset, :copy, from, to).successful? end
Enable or disable the rvm gem global cache.
# File lib/rvm/environment/gemset.rb, line 66 def gemset_globalcache(enable = true) case enable when "enabled", :enabled run(:__rvm_using_gemset_globalcache).successful? when true, "enable", :enable rvm(:gemset, :globalcache, :enable).successful? when false, "disable", :disable rvm(:gemset, :globalcache, :disable).successful? else false end end
# File lib/rvm/environment/gemset.rb, line 20 def gemset_list normalize_array rvm(:gemset, :list).stdout end
Changes the current environments gemset. If :replace_env is passed and the ruby is compatible, it will attempt to replace the current processes gem home and path with the one requested.
# File lib/rvm/environment/gemset.rb, line 82 def gemset_use(gemset, options = {}) replace_env = options.delete(:replace_env) result = rvm(:gemset, :use, gemset, options) if result.successful? gemset_name = result[:rvm_gemset_name] @environment_name = self.class.environment_with_gemset(@environment_name, gemset_name) @expanded_name = nil self.class.reset_current! use_env_from_result! result if replace_env true end end
Like gemset_use, but replaces the env by default.
# File lib/rvm/environment/gemset.rb, line 96 def gemset_use!(name, options = {}) gemset_use name, {:replace_env => true}.merge(options) end
# File lib/rvm/environment/info.rb, line 6 def info(*ruby_strings) ruby_string = normalize_ruby_string(ruby_strings) res = rvm(:info, ruby_string) res.successful? ? YAML.load(res.stdout) : {} end
# File lib/rvm/environment.rb, line 32 def inspect "#<#{self.class.name} environment_name=#{@environment_name.inspect}>" end
Lists all known svn tags.
# File lib/rvm/environment/list.rb, line 30 def list_ruby_svn_tags normalize_listing_output rvm(:list, :ruby_svn_tags).stdout end
Returns the path for the given command
# File lib/rvm/environment/env.rb, line 20 def path_for(command) run(:command, "-v", command).stdout.strip end
Execute rake (optionally taking the path to a rake file), then change back.
# File lib/rvm/environment/sets.rb, line 31 def rake(file = nil, options = {}) if file.nil? perform_set_operation :rake, options else file = File.expand_path(file) chdir(File.dirname(file)) do perform_set_operation(:rake, options.merge(:rakefile => file)) end end end
Passed either something containing ruby code or a path to a ruby file, will attempt to exectute it in the current environment.
# File lib/rvm/environment/sets.rb, line 7 def ruby(runnable, options = {}) if runnable.respond_to?(:path) # Call the path ruby_run runnable.path, options elsif runnable.respond_to?(:to_str) runnable = runnable.to_str File.exist?(runnable) ? ruby_run(runnable, options) : ruby_eval(runnable, options) elsif runnable.respond_to?(:read) ruby_run runnable.read end end
Lets you build a command up, without needing to see the output. As an example,
rvm :use, "ree@rails3", :install => true
Will call the following:
rvm use ree@rails3 --install
# File lib/rvm/environment/utility.rb, line 57 def rvm(*args) options = extract_options!(args) silent = options.delete(:silent) rearrange_options!(args, options) args += hash_to_options(options) args.map! { |a| a.to_s } if silent run_silently('rvm', *args) else run('rvm', *args) end end
Like Kernel.system, but evaluates it within the environment. Also note that it doesn‘t support redirection etc.
# File lib/rvm/environment/sets.rb, line 54 def system(command, *args) identifier = extract_identifier!(args) args = [identifier, :exec, command, *args].compact rvm(*args).successful? end
Gets the identifier after cd‘ing to a path, no destructive.
# File lib/rvm/environment/tools.rb, line 10 def tools_path_identifier(path) path_identifier = rvm(:tools, "path-identifier", path.to_s) if path_identifier.exit_status == 2 error_message = "The rvmrc located in '#{path}' could not be loaded, likely due to trust mechanisms." error_message << " Please run 'rvm rvmrc {trust,untrust} \"#{path}\"' to continue, or set rvm_trust_rvmrcs to 1." raise ErrorLoadingRVMRC, error_message end return normalize(path_identifier.stdout) end
# File lib/rvm/environment/tools.rb, line 20 def tools_strings(*rubies) rubies = rubies.flatten.join(",").split(",").uniq names = {} value = rvm(:tools, :strings, *rubies) if value.successful? parts = value.stdout.split rubies.each_with_index do |key, index| names[key] = normalize(parts[index]) end end names end
Changes the ruby string for the current environment.
# File lib/rvm/environment/rubies.rb, line 20 def use(ruby_string, opts = {}) ruby_string = ruby_string.to_s result = rvm(:use, ruby_string) if result.successful? @environment_name = ruby_string @expanded_name = nil use_env_from_result! result if opts[:replace_env] end end
Will get the ruby from the given path. If there is a compatible ruby found, it will then attempt to use the associated gemset. e.g. RVM::Environment.current.use_from_path! Dir.pwd
# File lib/rvm/environment/rubies.rb, line 39 def use_from_path!(path) use! tools.path_identifier(path) end
Generates wrappers with the specified prefix, pointing to ruby_string.
# File lib/rvm/environment/wrapper.rb, line 6 def wrapper(ruby_string, wrapper_prefix, *binaries) rvm(:wrapper, ruby_string, wrapper_prefix, *binaries).successful? end
From an options hash, extract the environment identifier.
# File lib/rvm/environment/sets.rb, line 89 def extract_environment!(options) values = [] [:environment, :env, :rubies, :ruby].each do |k| values << options.delete(k) end values.compact.first end
Shorthand to extra an identifier from args. Since we
# File lib/rvm/environment/sets.rb, line 99 def extract_identifier!(args) options = extract_options!(args) identifier = normalize_set_identifier(extract_environment!(options)) args << options identifier end
Extract options from a hash.
# File lib/rvm/environment/utility.rb, line 129 def extract_options!(args) args.last.is_a?(Hash) ? args.pop : {} end
Converts a hash of options to an array of command line argumets. If the value is false, it wont be added but if it is true only the key will be added. Lastly, when the value is neither true or false, to_s will becalled on it and it shall be added to the array.
# File lib/rvm/environment/utility.rb, line 137 def hash_to_options(options) result = [] options.each_pair do |key, value| real_key = "--#{key.to_s.gsub("_", "-")}" if value == true result << real_key elsif value != false result << real_key result << value.to_s end end result end
Returns a value, or nil if it is blank.
# File lib/rvm/environment/utility.rb, line 118 def normalize(value) value = value.to_s.strip value.empty? ? nil : value end
Normalizes an array, removing blank lines.
# File lib/rvm/environment/utility.rb, line 124 def normalize_array(value) value.split("\n").map { |line| line.strip }.reject { |line| line.empty? } end
# File lib/rvm/environment/rubies.rb, line 45 def normalize_ruby_string(rubies) Array(rubies).join(",") end
Converts the given identifier to a rvm-friendly form. Unlike using sets directly, a nil identifier is set to mean the current ruby (not all). :all or "all" will instead return the a blank identifier / run it against all rubies.
# File lib/rvm/environment/sets.rb, line 75 def normalize_set_identifier(identifier) case identifier when nil, "" @environment_name when :all, "all" nil when Array identifier.map { |i| normalize_set_identifier(i) }.uniq.join(",") else identifier.to_s end end
Performs a set operation. If the :env or :environment option is given, it will return a yaml summary (instead of the stdout / stderr etc via a Result object.
# File lib/rvm/environment/sets.rb, line 109 def perform_set_operation(*args) options = extract_options!(args) environment = extract_environment!(options) identifier = normalize_set_identifier(environment) # Uses yaml when we have multiple identifiers. uses_yaml = !environment.nil? options.merge!(:yaml => true) if uses_yaml args.unshift(identifier) unless identifier.nil? args << options result = rvm(*args) uses_yaml ? YAML.load(result.stdout) : result end
Moves certain options (e.g. yaml, json etc) to the front of the arguments list, making stuff like sets work.
# File lib/rvm/environment/utility.rb, line 83 def rearrange_options!(args, options) prefix_options = {} (PREFIX_OPTIONS + PREFIX_OPTIONS.map { |o| o.to_s }).each do |k| if options.has_key?(k) value = options.delete(k) prefix_options[k.to_sym] = value end end hash_to_options(prefix_options).reverse.each { |o| args.unshift(o) } end
# File lib/rvm/environment/utility.rb, line 94 def ruby_string(result) if result && result[:rvm_ruby_string] result[:rvm_ruby_string] else self.class.identifier_to_ruby_string(expanded_name) end end
Automatically load rvm config from the multiple sources.
# File lib/rvm/environment.rb, line 48 def source_rvm_environment rvm_path = config_value_for(:rvm_path, self.class.default_rvm_path, false) actual_config = defined_config.merge('rvm_path' => rvm_path) config = [] actual_config.each_pair do |k, v| config << "#{k}=#{escape_argument(v.to_s)}" end run_silently :export, config.join(" ") run_silently :source, File.join(rvm_path, "scripts", "rvm") end
# File lib/rvm/environment/utility.rb, line 161 def use_env_from_result!(result) if compatible_with_current?(result) ENV['GEM_HOME'] = result[:GEM_HOME] ENV['GEM_PATH'] = result[:GEM_PATH] ENV['BUNDLE_PATH'] = result[:BUNDLE_PATH] Gem.clear_paths if defined?(Gem) else raise IncompatibleRubyError.new(result, "The given ruby environment requires #{ruby_string(result)} (versus #{self.class.current_ruby_string})") end end