Class: RVM::Environment

Inherits:
Object
  • Object
show all
Extended by:
Forwardable, ConfigMixin
Includes:
ConfigMixin
Defined in:
lib/rvm/environment.rb,
lib/rvm/environment/env.rb,
lib/rvm/environment/info.rb,
lib/rvm/environment/list.rb,
lib/rvm/environment/sets.rb,
lib/rvm/environment/alias.rb,
lib/rvm/environment/tools.rb,
lib/rvm/environment/gemset.rb,
lib/rvm/environment/rubies.rb,
lib/rvm/environment/cleanup.rb,
lib/rvm/environment/utility.rb,
lib/rvm/environment/wrapper.rb,
lib/rvm/environment/configuration.rb

Overview

Implements the actual wrapper around the api. For more information about this design, see the RVM module.

Defined Under Namespace

Modules: ConfigMixin Classes: AliasWrapper, CleanupWrapper, EnvWrapper, GemsetWrapper, ListWrapper, ToolsWrapper

Constant Summary collapse

PREFIX_OPTIONS =
[:trace, :json, :yaml]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ConfigMixin

clear_config!, config, merge_config!

Constructor Details

#initialize(environment_name = "default", options = {}) ⇒ Environment

Creates a new environment with the given name and optionally a set of extra environment variables to be set on load.



22
23
24
25
26
27
28
29
30
# 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

Instance Attribute Details

#environment_nameObject (readonly)

Returns the value of attribute environment_name.



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

def environment_name
  @environment_name
end

#shell_wrapperObject (readonly)

Returns the value of attribute shell_wrapper.



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

def shell_wrapper
  @shell_wrapper
end

Class Method Details

.config_value_for(value) ⇒ Object

Gets the default option or the current environment variable for a given env var.



40
41
42
43
# File 'lib/rvm/environment/configuration.rb', line 40

def self.config_value_for(value)
  value = value.to_s
  config[value] || ENV[value]
end

.currentObject

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.



38
39
40
# File 'lib/rvm/environment/utility.rb', line 38

def self.current
  @current_environment ||= Environment.new(current_environment_id)
end

.current_environment_idObject

Returns the environment identifier for the current environment, as determined from the GEM_HOME.



13
14
15
16
17
18
19
20
21
22
23
# 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

.current_ruby_stringObject

Returns the ruby string that represents the current environment.



26
27
28
# File 'lib/rvm/environment/utility.rb', line 26

def self.current_ruby_string
  identifier_to_ruby_string(current_environment_id)
end

.default_rvm_pathObject



6
7
8
9
# 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_config_accessor(*args) ⇒ Object

Define the config accessor, which basically uses config_value_for and the linke to set the env variable.



6
7
8
9
10
11
12
# 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

.identifier_to_ruby_string(identifier) ⇒ Object

Converts a ruby identifier (string + gemset) to just the ruby string.



31
32
33
# File 'lib/rvm/environment/utility.rb', line 31

def self.identifier_to_ruby_string(identifier)
  identifier.gsub(/@.*$/, '')
end

.reset_current!Object

Sets the current environment back to the currently running ruby or the system env (if it can’t be determined from GEM_HOME).



44
45
46
# File 'lib/rvm/environment/utility.rb', line 44

def self.reset_current!
  @current_environment = nil
end

Instance Method Details

#alias_create(name, ruby_string) ⇒ Object

Creates an alias with the given name.



27
28
29
# 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

#alias_delete(name) ⇒ Object

Deletes an alias and returns the exit status.



22
23
24
# File 'lib/rvm/environment/alias.rb', line 22

def alias_delete(name)
  rvm(:alias, :delete, name.to_s).successful?
end

#alias_listObject

Returns a hash of aliases.



5
6
7
8
9
10
11
12
13
14
# 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

#alias_show(name) ⇒ Object

Shows the full ruby string that a given alias points to.



17
18
19
# File 'lib/rvm/environment/alias.rb', line 17

def alias_show(name)
  normalize rvm(:alias, :show, name.to_s).stdout
end

#aliasesObject

Returns an aliases proxy which can be used in a more Ruby-like manner.



32
33
34
# File 'lib/rvm/environment/alias.rb', line 32

def aliases
  @aliases ||= AliasWrapper.new(self)
end

#chdir(dir) ⇒ Object

Run commands inside the given directory.



72
73
74
75
76
77
# 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

#cleanupObject

Returns the ruby-like interface defined by CleanupWrapper



12
13
14
# File 'lib/rvm/environment/cleanup.rb', line 12

def cleanup
  @cleanup_wrapper ||= CleanupWrapper.new(self)
end

#config_value_for(key, default = nil, check_live = true) ⇒ Object

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.



48
49
50
51
52
# 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

#default_wrappers(ruby_string, *binaries) ⇒ Object

Generates the default wrappers.



11
12
13
# File 'lib/rvm/environment/wrapper.rb', line 11

def default_wrappers(ruby_string, *binaries)
  wrapper ruby_string, '', *binaries
end

#defined_configObject

Returns a hash of all of the user defined configuration.



55
56
57
# File 'lib/rvm/environment/configuration.rb', line 55

def defined_config
  self.class.config.merge(self.config)
end

#envObject

Returns a ruby-like wrapper for the env functions



22
23
24
# File 'lib/rvm/environment/env.rb', line 22

def env
  @env_wrapper ||= EnvWrapper.new(self)
end

#env_contentsObject

Returns the contents of the env file.

env.env_contents # => [‘export PATH= .…’, …]



8
9
10
# File 'lib/rvm/environment/env.rb', line 8

def env_contents
  rvm(:env, environment_name).stdout.split
end

#env_pathObject

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”



17
18
19
# File 'lib/rvm/environment/env.rb', line 17

def env_path
  normalize_array rvm(:env, environment_name, :path => true).stdout
end

#exec(command, *args) ⇒ Object

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.



63
64
65
66
# 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

#expanded_nameObject

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”



42
43
44
# File 'lib/rvm/environment.rb', line 42

def expanded_name
  @expanded_name ||= tools_identifier.to_s
end

#gemsetObject

Returns the Ruby-like wrapper for gemset operations.



107
108
109
# File 'lib/rvm/environment/gemset.rb', line 107

def gemset
  @gemset_wrapper ||= GemsetWrapper.new(self)
end

#gemset_copy(from, to) ⇒ Object

Copies the gems between two different gemsets.



31
32
33
# File 'lib/rvm/environment/gemset.rb', line 31

def gemset_copy(from, to)
  rvm(:gemset, :copy, from, to).successful?
end

#gemset_create(*names) ⇒ Object

Creates a new gemset with the given name.



25
26
27
28
# File 'lib/rvm/environment/gemset.rb', line 25

def gemset_create(*names)
  names = names.flatten
  rvm(:gemset, :create, *names).successful?
end

#gemset_delete(name) ⇒ Object

Deletes the gemset with a given name.



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

def gemset_delete(name)
  run("echo 'yes' | rvm", :gemset, :delete, name.to_s).successful?
end

#gemset_emptyObject

Removes all gem-related stuff from the current gemset.



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

def gemset_empty
  run("echo 'yes' | rvm", :gemset, :empty).successful?
end

#gemset_export(gemset_or_file = nil) ⇒ Object Also known as: gemset_dump

Exports a gemset.



14
15
16
17
# 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

#gemset_globalcache(enable = true) ⇒ Object

Enable or disable the rvm gem global cache.



66
67
68
69
70
71
72
73
74
75
76
77
# 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

#gemset_import(file_prefix = nil) ⇒ Object Also known as: gemset_load

Loads a gemset into the current environment. If an argument is given, it will load it from file_prefix.gems



7
8
9
10
# File 'lib/rvm/environment/gemset.rb', line 7

def gemset_import(file_prefix = nil)
  args = [file_prefix].compact
  rvm(:gemset, :import, *args).successful?
end

#gemset_initialObject

Initializes gemsets for a given ruby.



61
62
63
# File 'lib/rvm/environment/gemset.rb', line 61

def gemset_initial
  rvm(:gemset, :initial).successful?
end

#gemset_listObject



20
21
22
# File 'lib/rvm/environment/gemset.rb', line 20

def gemset_list
  normalize_array rvm(:gemset, :list).stdout
end

#gemset_pristineObject

Resets the current gemset to a pristine state.



46
47
48
# File 'lib/rvm/environment/gemset.rb', line 46

def gemset_pristine
  rvm(:gemset, :pristine).successful?
end

#gemset_pruneObject

Prunes the gem cache for the current ruby.



56
57
58
# File 'lib/rvm/environment/gemset.rb', line 56

def gemset_prune
  rvm(:gemset, :prune).successful?
end

#gemset_updateObject

Updates all gems in the current gemset.



51
52
53
# File 'lib/rvm/environment/gemset.rb', line 51

def gemset_update
  rvm(@environment_name, :gemset, :update).successful?
end

#gemset_use(gemset, options = {}) ⇒ Object

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.



82
83
84
85
86
87
88
89
90
91
92
93
# 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

#gemset_use!(name, options = {}) ⇒ Object

Like gemset_use, but replaces the env by default.



96
97
98
# File 'lib/rvm/environment/gemset.rb', line 96

def gemset_use!(name, options = {})
  gemset_use name, {:replace_env => true}.merge(options)
end

#info(*ruby_strings) ⇒ Object

Return a Hash with the same output that command:

$ rvm info



10
11
12
13
14
# 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

#inspectObject



32
33
34
# File 'lib/rvm/environment.rb', line 32

def inspect
  "#<#{self.class.name} environment_name=#{@environment_name.inspect}>"
end

#install(rubies, opts = {}) ⇒ Object

Installs the given ruby



5
6
7
# File 'lib/rvm/environment/rubies.rb', line 5

def install(rubies, opts = {})
  rvm(:install, normalize_ruby_string(rubies), opts).successful?
end

#listObject

Returns an interface to a more Ruby-like interface for list.



49
50
51
# File 'lib/rvm/environment/list.rb', line 49

def list
  @list_helper ||= ListWrapper.new(self)
end

#list_defaultObject

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”



25
26
27
# File 'lib/rvm/environment/list.rb', line 25

def list_default
  normalize rvm(:list, :default, :string).stdout
end

#list_gemsetsObject

Returns a raw array list of ruby + gemset combinations.

env.list_gemsets # => [‘ruby-1.9.2-p0@my_gemset’, ‘jruby@my_gemset’, …]



8
9
10
# File 'lib/rvm/environment/list.rb', line 8

def list_gemsets
  normalize_listing_output rvm(:list, :gemsets, :strings).stdout
end

#list_knownObject

Lists all known ruby strings (raw, filtered output)



31
32
33
# File 'lib/rvm/environment/list.rb', line 31

def list_known
  normalize_listing_output rvm(:list, :known).stdout
end

#list_known_stringsObject

Lists all known ruby strings



37
38
39
# File 'lib/rvm/environment/list.rb', line 37

def list_known_strings
  normalize_listing_output rvm(:list, :known_strings).stdout
end

#list_ruby_svn_tagsObject

Lists all known svn tags.



43
44
45
# File 'lib/rvm/environment/list.rb', line 43

def list_ruby_svn_tags
  normalize_listing_output rvm(:list, :ruby_svn_tags).stdout
end

#list_stringsObject

Returns a raw array list of installed ruby strings, including aliases.

env.list_strings # => [“ruby-1.9.2-p0”, “jruby-1.5.3”]



16
17
18
# File 'lib/rvm/environment/list.rb', line 16

def list_strings
  normalize_listing_output rvm(:list, :strings).stdout.tr(' ', "\n")
end

#path_for(command) ⇒ Object Also known as: which

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’



33
34
35
# File 'lib/rvm/environment/env.rb', line 33

def path_for(command)
  run(:command, "-v", command).stdout.strip
end

#rake(file = nil, options = {}) ⇒ Object

Execute rake (optionally taking the path to a rake file), then change back.



31
32
33
34
35
36
37
38
39
40
# 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

#remove(rubies, opts = {}) ⇒ Object

Removes a given ruby from being managed by rvm.



15
16
17
# File 'lib/rvm/environment/rubies.rb', line 15

def remove(rubies, opts = {})
  rvm(:remove, normalize_ruby_string(rubies), opts).successful?
end

#ruby(runnable, options = {}) ⇒ Object

Passed either something containing ruby code or a path to a ruby file, will attempt to exectute it in the current environment.



7
8
9
10
11
12
13
14
15
16
17
# 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

#ruby_eval(code, options = {}) ⇒ Object

Eval the given code within ruby.



20
21
22
# File 'lib/rvm/environment/sets.rb', line 20

def ruby_eval(code, options = {})
  perform_set_operation :ruby, "-e", code.to_s, options
end

#ruby_run(path, options = {}) ⇒ Object

Run the given path as a ruby script.



25
26
27
# File 'lib/rvm/environment/sets.rb', line 25

def ruby_run(path, options = {})
  perform_set_operation :ruby, path.to_s, options
end

#rvm(*args) ⇒ Object

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


57
58
59
60
61
62
63
64
65
66
67
68
69
# 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

#specs(options = {}) ⇒ Object

Use the rvm spec runner for specs.



48
49
50
# File 'lib/rvm/environment/sets.rb', line 48

def specs(options = {})
  perform_set_operation :specs, options
end

#system(command, *args) ⇒ Object

Like Kernel.system, but evaluates it within the environment. Also note that it doesn’t support redirection etc.



54
55
56
57
58
# 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

#tests(options = {}) ⇒ Object

Use the rvm test runner for unit tests.



43
44
45
# File 'lib/rvm/environment/sets.rb', line 43

def tests(options = {})
  perform_set_operation :tests, options
end

#toolsObject

Return the tools wrapper.



34
35
36
# File 'lib/rvm/environment/tools.rb', line 34

def tools
  @tools_wrapper ||= ToolsWrapper.new(self)
end

#tools_identifierObject

Gets the full name for the current env.



5
6
7
# File 'lib/rvm/environment/tools.rb', line 5

def tools_identifier
  normalize rvm(:tools, :identifier).stdout
end

#tools_path_identifier(path) ⇒ Object

Gets the identifier after cd’ing to a path, no destructive.



10
11
12
13
14
15
16
17
18
# 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

#tools_strings(*rubies) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# 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

#uninstall(rubies, opts = {}) ⇒ Object

Uninstalls a ruby (remove but keeps src etc)



10
11
12
# File 'lib/rvm/environment/rubies.rb', line 10

def uninstall(rubies, opts = {})
  rvm(:uninstall, normalize_ruby_string(rubies), opts).successful?
end

#use(ruby_string, opts = {}) ⇒ Object

Changes the ruby string for the current environment.

env.use ‘1.9.2’ # => nil env.use ‘ree’ # => nil



24
25
26
27
28
29
30
31
32
# File 'lib/rvm/environment/rubies.rb', line 24

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

#use!(ruby_string, opts = {}) ⇒ Object

Like use but with :replace_env defaulting to true.



35
36
37
# File 'lib/rvm/environment/rubies.rb', line 35

def use!(ruby_string, opts = {})
  use ruby_string, opts.merge(:replace_env => true)
end

#use_from_path!(path) ⇒ Object

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



43
44
45
# File 'lib/rvm/environment/rubies.rb', line 43

def use_from_path!(path)
 use! tools.path_identifier(path)
end

#wrapper(ruby_string, wrapper_prefix, *binaries) ⇒ Object

Generates wrappers with the specified prefix, pointing to ruby_string.



6
7
8
# File 'lib/rvm/environment/wrapper.rb', line 6

def wrapper(ruby_string, wrapper_prefix, *binaries)
  rvm(:wrapper, ruby_string, wrapper_prefix, *binaries).successful?
end

#wrapper_path_for(executable) ⇒ Object

If available, return the path to the wrapper for the given executable. Will return ni if the wrapper is unavailable.

Raises:

  • (NotImplementedError)


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

def wrapper_path_for(executable)
  raise NotImplementedError
end