Class: Hiiro::Rbenv

Inherits:
Object
  • Object
show all
Defined in:
lib/hiiro/rbenv.rb

Class Method Summary collapse

Class Method Details

.capture(*cmd, version: current_version) ⇒ Object

Run a command through ‘rbenv exec` and capture stdout. Returns the output string.



50
51
52
53
# File 'lib/hiiro/rbenv.rb', line 50

def capture(*cmd, version: current_version)
  out, = Open3.capture2({ 'RBENV_VERSION' => version.to_s }, 'rbenv', 'exec', *cmd)
  out
end

.current_versionObject

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.

Returns:

  • (Boolean)


67
68
69
# File 'lib/hiiro/rbenv.rb', line 67

def gem_installed?(gem_name, version: current_version)
  !capture('gem', 'list', gem_name, version: version).strip.empty?
end

.global_versionObject

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.

Returns:

  • (Boolean)


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.



74
75
76
77
# File 'lib/hiiro/rbenv.rb', line 74

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.



80
81
82
# File 'lib/hiiro/rbenv.rb', line 80

def install_gem_in_all(gem_name, pre: false)
  versions.each { |ver| install_gem(gem_name, pre: pre, version: ver) }
end

.local_versionObject

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

.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.



45
46
47
# File 'lib/hiiro/rbenv.rb', line 45

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.



56
57
58
59
60
61
62
# File 'lib/hiiro/rbenv.rb', line 56

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

.versionsObject 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.



87
88
89
# File 'lib/hiiro/rbenv.rb', line 87

def which(cmd, version: current_version)
  capture('which', cmd, version: version).strip
end