Class: Hiiro::Rbenv
- Inherits:
-
Object
- Object
- Hiiro::Rbenv
- Defined in:
- lib/hiiro/rbenv.rb
Class Method Summary collapse
-
.capture(*cmd, version: current_version) ⇒ Object
Run a command through ‘rbenv exec` and capture stdout.
-
.current_version ⇒ Object
The currently active version (respects RBENV_VERSION, .ruby-version, global).
-
.gem_installed?(gem_name, version: current_version) ⇒ Boolean
True if the named gem is installed in the given version.
-
.global_version ⇒ Object
The global default version.
-
.has_version?(ver) ⇒ Boolean
True if the given version is installed.
-
.install_gem(gem_name, version: current_version, pre: false) ⇒ Object
Install or update a gem in the given version.
-
.install_gem_in_all(gem_name, pre: false) ⇒ Object
Install or update a gem across all installed versions.
-
.local_version ⇒ Object
The local .ruby-version if one exists in the current directory tree, else nil.
-
.ruby_version(version: current_version) ⇒ Object
Full ‘ruby –version` string for the given version.
- .ruby_version_number(version: current_version) ⇒ Object
-
.run(*cmd, version: current_version) ⇒ Object
Run a command through ‘rbenv exec` in the given version.
-
.run_in_all(*cmd) ⇒ Object
Run a command in every installed version.
- .supported_ruby?(version) ⇒ Boolean
- .supported_versions ⇒ Object
-
.versions ⇒ Object
(also: all_versions)
All installed rbenv versions (bare list, one per line).
-
.which(cmd, version: current_version) ⇒ Object
Absolute path to the rbenv shim (or versioned binary) for a command.
Class Method Details
.capture(*cmd, version: current_version) ⇒ Object
Run a command through ‘rbenv exec` and capture stdout. Returns the output string.
66 67 68 69 |
# File 'lib/hiiro/rbenv.rb', line 66 def capture(*cmd, version: current_version) out, = Open3.capture2({ 'RBENV_VERSION' => version.to_s }, 'rbenv', 'exec', *cmd) out end |
.current_version ⇒ Object
The currently active version (respects RBENV_VERSION, .ruby-version, global).
16 17 18 |
# File 'lib/hiiro/rbenv.rb', line 16 def current_version `rbenv version-name`.strip end |
.gem_installed?(gem_name, version: current_version) ⇒ Boolean
True if the named gem is installed in the given version.
83 84 85 |
# File 'lib/hiiro/rbenv.rb', line 83 def gem_installed?(gem_name, version: current_version) !capture('gem', 'list', gem_name, version: version).strip.empty? end |
.global_version ⇒ Object
The global default version.
21 22 23 |
# File 'lib/hiiro/rbenv.rb', line 21 def global_version `rbenv global`.strip end |
.has_version?(ver) ⇒ Boolean
True if the given version is installed.
32 33 34 |
# File 'lib/hiiro/rbenv.rb', line 32 def has_version?(ver) versions.include?(ver.to_s) end |
.install_gem(gem_name, version: current_version, pre: false) ⇒ Object
Install or update a gem in the given version. Always uses gem install (handles both fresh installs and updates to the latest version) and pins to rubygems.org to bypass the local source cache.
90 91 92 93 |
# File 'lib/hiiro/rbenv.rb', line 90 def install_gem(gem_name, version: current_version, pre: false) pre_flag = pre ? ['--pre'] : [] run('gem', 'install', gem_name, '-u', *pre_flag, version: version) end |
.install_gem_in_all(gem_name, pre: false) ⇒ Object
Install or update a gem across all installed versions.
96 97 98 |
# File 'lib/hiiro/rbenv.rb', line 96 def install_gem_in_all(gem_name, pre: false) supported_versions.each { |ver| install_gem(gem_name, pre: pre, version: ver) } end |
.local_version ⇒ Object
The local .ruby-version if one exists in the current directory tree, else nil.
26 27 28 29 |
# File 'lib/hiiro/rbenv.rb', line 26 def local_version out = `rbenv local 2>/dev/null`.strip out.empty? ? nil : out end |
.ruby_version(version: current_version) ⇒ Object
Full ‘ruby –version` string for the given version.
37 38 39 |
# File 'lib/hiiro/rbenv.rb', line 37 def ruby_version(version: current_version) capture('ruby', '--version', version: version).strip end |
.ruby_version_number(version: current_version) ⇒ Object
41 42 43 |
# File 'lib/hiiro/rbenv.rb', line 41 def ruby_version_number(version: current_version) capture('ruby', '-e', 'print RUBY_VERSION', version: version).strip end |
.run(*cmd, version: current_version) ⇒ Object
Run a command through ‘rbenv exec` in the given version. Returns the exit status (true/false) like Kernel#system.
61 62 63 |
# File 'lib/hiiro/rbenv.rb', line 61 def run(*cmd, version: current_version) system({ 'RBENV_VERSION' => version.to_s }, 'rbenv', 'exec', *cmd) end |
.run_in_all(*cmd) ⇒ Object
Run a command in every installed version. Yields (version, success) if a block given.
72 73 74 75 76 77 78 |
# File 'lib/hiiro/rbenv.rb', line 72 def run_in_all(*cmd) versions.each do |ver| puts "VERSION: #{ver}" success = run(*cmd, version: ver) yield ver, success if block_given? end end |
.supported_ruby?(version) ⇒ Boolean
45 46 47 48 49 50 51 |
# File 'lib/hiiro/rbenv.rb', line 45 def supported_ruby?(version) Gem::Requirement.new(Hiiro::SUPPORTED_RUBY_VERSION).satisfied_by?( Gem::Version.new(ruby_version_number(version: version)) ) rescue ArgumentError false end |
.supported_versions ⇒ Object
53 54 55 |
# File 'lib/hiiro/rbenv.rb', line 53 def supported_versions versions.select { |ver| supported_ruby?(ver) } end |
.versions ⇒ Object Also known as: all_versions
All installed rbenv versions (bare list, one per line).
10 11 12 |
# File 'lib/hiiro/rbenv.rb', line 10 def versions `rbenv versions --bare`.lines(chomp: true) end |
.which(cmd, version: current_version) ⇒ Object
Absolute path to the rbenv shim (or versioned binary) for a command.
103 104 105 |
# File 'lib/hiiro/rbenv.rb', line 103 def which(cmd, version: current_version) capture('which', cmd, version: version).strip end |