class RVM::Environment
Implements the actual wrapper around the api. For more information about this design, see the RVM
module.
Constants
- PREFIX_OPTIONS
Attributes
Public Class Methods
Gets the default option or the current environment variable for a given env var.
# File lib/rvm/environment/configuration.rb, line 40 def self.config_value_for(value) value = value.to_s config[value] || ENV[value] end
Returns the currentl environment. Note that when the ruby is changed, this is reset - Also, if the gemset is changed it will also be reset.
# File lib/rvm/environment/utility.rb, line 42 def self.current @current_environment ||= Environment.new(current_environment_id) end
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
Returns the ruby string that represents the current environment.
# File lib/rvm/environment/utility.rb, line 26 def self.current_ruby_string identifier_to_ruby_string(current_environment_id) 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}=") { |v| RVM::Environment.merge_config! arg => v } end end
# File lib/rvm/environment/utility.rb, line 35 def self.identifier_to_gemset_name(identifier) identifier.gsub(/^.*@/, '') end
Converts a ruby identifier (string + gemset) to just the ruby string.
# File lib/rvm/environment/utility.rb, line 31 def self.identifier_to_ruby_string(identifier) identifier.gsub(/@.*$/, '') 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
Sets the current environment back to the currently running ruby or the system env (if it can't be determined from GEM_HOME).
# File lib/rvm/environment/utility.rb, line 48 def self.reset_current! @current_environment = nil end
Protected Class Methods
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 119 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
Public Instance Methods
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
Shows the full ruby string that a given alias points to.
# File lib/rvm/environment/alias.rb, line 17 def alias_show(name) normalize rvm(:alias, :show, name.to_s).stdout end
Returns an aliases proxy which can be used in a more Ruby-like manner.
# File lib/rvm/environment/alias.rb, line 32 def aliases @aliases ||= AliasWrapper.new(self) end
Run commands inside the given directory.
# File lib/rvm/environment/utility.rb, line 81 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
Returns a ruby-like wrapper for the env functions
# File lib/rvm/environment/env.rb, line 22 def env @env_wrapper ||= EnvWrapper.new(self) end
Returns the contents of the env file.
env.env_contents # => ['export PATH= .…', …]
# File lib/rvm/environment/env.rb, line 8 def env_contents rvm(:env, environment_name).stdout.split end
Returns the path to the env file Suppose that you are in the 1.9.2 environment.
env.env_path # => “~/.rvm/environments/ruby-1.9.2-p0”
# File lib/rvm/environment/env.rb, line 17 def env_path normalize_array rvm(:env, environment_name, :path => true).stdout 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
Returns the expanded name, using the same method as used by the rvm command line.
Suppose that you are in the 1.9.2 patchlevel Environment
.
env.expanded_name # => “ruby-1.9.2-p0”
# File lib/rvm/environment.rb, line 42 def expanded_name @expanded_name ||= tools_identifier.to_s end
Returns the Ruby-like wrapper for gemset operations.
# File lib/rvm/environment/gemset.rb, line 107 def gemset @gemset_wrapper ||= GemsetWrapper.new(self) 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
Creates a new gemset with the given name.
# File lib/rvm/environment/gemset.rb, line 25 def gemset_create(*names) names = names.flatten rvm(:gemset, :create, *names).successful? end
Deletes the gemset with a given name.
# File lib/rvm/environment/gemset.rb, line 36 def gemset_delete(name) run("echo 'yes' | rvm", :gemset, :delete, name.to_s).successful? end
Removes all gem-related stuff from the current gemset.
# File lib/rvm/environment/gemset.rb, line 41 def gemset_empty run("echo 'yes' | rvm", :gemset, :empty).successful? end
Exports a gemset.
# File lib/rvm/environment/gemset.rb, line 14 def gemset_export(gemset_or_file = nil) args = [gemset_or_file].compact rvm(:gemset, :export, *args).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
Loads a gemset into the current environment. If an argument is given, it will load it from file_prefix.gems
# File lib/rvm/environment/gemset.rb, line 7 def gemset_import(file_prefix = nil) args = [file_prefix].compact rvm(:gemset, :import, *args).successful? end
Initializes gemsets for a given ruby.
# File lib/rvm/environment/gemset.rb, line 61 def gemset_initial rvm(:gemset, :initial).successful? end
# File lib/rvm/environment/gemset.rb, line 20 def gemset_list normalize_array rvm(:gemset, :list).stdout end
Resets the current gemset to a pristine state.
# File lib/rvm/environment/gemset.rb, line 46 def gemset_pristine rvm(:gemset, :pristine).successful? end
Prunes the gem cache for the current ruby.
# File lib/rvm/environment/gemset.rb, line 56 def gemset_prune rvm(:gemset, :prune).successful? end
Updates all gems in the current gemset.
# File lib/rvm/environment/gemset.rb, line 51 def gemset_update rvm(@environment_name, :gemset, :update).successful? 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 = self.class.identifier_to_gemset_name(result[:GEM_HOME] || result[:rvm_env_string]) @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
Return a Hash with the same output that command:
$ rvm info
# File lib/rvm/environment/info.rb, line 10 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
Installs the given ruby
# File lib/rvm/environment/rubies.rb, line 5 def install(rubies, opts = {}) rvm(:install, normalize_ruby_string(rubies), opts).successful? end
Returns an interface to a more Ruby-like interface for list.
# File lib/rvm/environment/list.rb, line 49 def list @list_helper ||= ListWrapper.new(self) end
Lists the default ruby (minus gemset) Suppose that Ruby 1.9.2 patchlevel 0, is the default:
env.list_default # => “ruby-1.9.2-p0”
# File lib/rvm/environment/list.rb, line 25 def list_default normalize rvm(:list, :default, :string).stdout end
Returns a raw array list of ruby + gemset combinations.
env.list_gemsets # => ['ruby-1.9.2-p0@my_gemset', 'jruby@my_gemset', …]
# File lib/rvm/environment/list.rb, line 8 def list_gemsets normalize_listing_output rvm(:list, :gemsets, :strings).stdout end
Lists all known ruby strings (raw, filtered output)
# File lib/rvm/environment/list.rb, line 31 def list_known normalize_listing_output rvm(:list, :known).stdout end
Lists all known ruby strings
# File lib/rvm/environment/list.rb, line 37 def list_known_strings normalize_listing_output rvm(:list, :known_strings).stdout end
Returns a raw array list of installed ruby strings, including aliases.
env.list_strings # => [“ruby-1.9.2-p0”, “jruby-1.5.3”]
# File lib/rvm/environment/list.rb, line 16 def list_strings normalize_listing_output rvm(:list, :strings).stdout.tr(' ', "\n") end
Returns the path for the given command
Suppose that you are in the 1.9.2 environment.
env.path_for(:rspec) # => '~/.rvm/gems/ruby-1.9.2-p0/bin/rspec' env.path_for(:ruby) # => '~/.rvm/rubies/ruby-1.9.2-p0/bin/ruby'
# File lib/rvm/environment/env.rb, line 33 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
Removes a given ruby from being managed by rvm.
# File lib/rvm/environment/rubies.rb, line 15 def remove(rubies, opts = {}) rvm(:remove, normalize_ruby_string(rubies), opts).successful? 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
Eval the given code within ruby.
# File lib/rvm/environment/sets.rb, line 20 def ruby_eval(code, options = {}) perform_set_operation :ruby, "-e", code.to_s, options end
Run the given path as a ruby script.
# File lib/rvm/environment/sets.rb, line 25 def ruby_run(path, options = {}) perform_set_operation :ruby, path.to_s, options end
Lets you build a command up, without needing to see the output. Examples:
rvm :use, "ree@rails3", :install => true > rvm use ree@rails3 --install rvm :install, "ree@rails3", :rvm_by_path => true, :rubygems_version => "1.8.23" > rvm_rubygems_version=1.8.23 $rvm_path/bin/rvm install ree@rails3
# File lib/rvm/environment/utility.rb, line 61 def rvm(*args) options = extract_options!(args) silent = options.delete(:silent) rvm_by_path = options.delete(:rvm_by_path) rubygems_version = options.delete(:rubygems_version) rearrange_options!(args, options) args += hash_to_options(options) args.map! { |a| a.to_s } rvm_path = config_value_for(:rvm_path, self.class.default_rvm_path, false) program = rvm_by_path ? "#{rvm_path}/bin/rvm" : "rvm" program = "rvm_rubygems_version=#{rubygems_version} #{program}" if rubygems_version if silent run_silently(program, *args) else run(program, *args) end end
Use the rvm spec runner for specs.
# File lib/rvm/environment/sets.rb, line 48 def specs(options = {}) perform_set_operation :specs, options 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
Use the rvm test runner for unit tests.
# File lib/rvm/environment/sets.rb, line 43 def tests(options = {}) perform_set_operation :tests, options end
Return the tools wrapper.
# File lib/rvm/environment/tools.rb, line 34 def tools @tools_wrapper ||= ToolsWrapper.new(self) end
Gets the full name for the current env.
# File lib/rvm/environment/tools.rb, line 5 def tools_identifier normalize rvm(:tools, :identifier).stdout 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_flag 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
Uninstalls a ruby (remove but keeps src etc)
# File lib/rvm/environment/rubies.rb, line 10 def uninstall(rubies, opts = {}) rvm(:uninstall, normalize_ruby_string(rubies), opts).successful? end
Changes the ruby string for the current environment.
env.use '1.9.2' # => true env.use 'ree' # => true env.use 'foo' # => false
# File lib/rvm/environment/rubies.rb, line 25 def use(ruby_string, opts = {}) ruby_string = ruby_string.to_s result = rvm(:use, ruby_string) successful = result.successful? if successful @environment_name = ruby_string @expanded_name = nil use_env_from_result! result if opts[:replace_env] end successful end
Like use but with :replace_env defaulting to true.
# File lib/rvm/environment/rubies.rb, line 38 def use!(ruby_string, opts = {}) use ruby_string, opts.merge(:replace_env => true) 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 46 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
If available, return the path to the wrapper for the given executable. Will return ni if the wrapper is unavailable.
# File lib/rvm/environment/wrapper.rb, line 18 def wrapper_path_for(executable) raise NotImplementedError end
Protected Instance Methods
Checks whether the given environment is compatible with the current ruby interpeter.
# File lib/rvm/environment/utility.rb, line 113 def compatible_with_current?(result) ruby_string(result) == self.class.current_ruby_string 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 138 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 146 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 127 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 133 def normalize_array(value) value.split("\n").map { |line| line.strip }.reject { |line| line.empty? } end
Takes a list of rubies / items, 1 per line and strips comments and blank lines.
# File lib/rvm/environment/list.rb, line 129 def normalize_listing_output(results) lines = [] results.each_line do |line| line = line.gsub(/#.*/, '').strip lines << line unless line.empty? end lines.sort end
Recursively normalize options.
# File lib/rvm/environment/utility.rb, line 161 def normalize_option_value(value) case value when Array value.map { |option| normalize_option_value(option) }.join(",") else value.to_s end end
# File lib/rvm/environment/rubies.rb, line 52 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 92 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 103 def ruby_string(result) if result && result[:rvm_env_string] Environment.identifier_to_ruby_string result[:rvm_env_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 53 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 170 def use_env_from_result!(result) if compatible_with_current?(result) ENV['GEM_HOME'] = result[:GEM_HOME] ENV['GEM_PATH'] = result[:GEM_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
# File lib/rvm/environment.rb, line 64 def use_rvm_environment rvm :use, @environment_name, :silent => true end