Class: Pinspec::Runner::Runtime

Inherits:
Object
  • Object
show all
Defined in:
lib/pinspec/runner/runtime.rb

Defined Under Namespace

Classes: Result

Constant Summary collapse

MANAGERS =
%i[rvm rbenv asdf mise chruby].freeze
BUNDLER_VARS =
%w[
  BUNDLE_GEMFILE BUNDLE_PATH BUNDLE_BIN_PATH BUNDLE_APP_CONFIG
  BUNDLER_VERSION BUNDLER_SETUP RUBYOPT RUBYLIB
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_root, home: Dir.home, current: RUBY_VERSION) ⇒ Runtime

Returns a new instance of Runtime.



27
28
29
30
31
# File 'lib/pinspec/runner/runtime.rb', line 27

def initialize(app_root, home: Dir.home, current: RUBY_VERSION)
  @app_root = app_root
  @home = home
  @current = current
end

Class Method Details

.for(app_root, home: Dir.home, current: RUBY_VERSION) ⇒ Object



23
24
25
# File 'lib/pinspec/runner/runtime.rb', line 23

def self.for(app_root, home: Dir.home, current: RUBY_VERSION)
  new(app_root, home: home, current: current).resolve
end

Instance Method Details

#declared_versionObject



54
55
56
# File 'lib/pinspec/runner/runtime.rb', line 54

def declared_version
  from_file(".ruby-version") || from_tool_versions
end

#resolveObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/pinspec/runner/runtime.rb', line 33

def resolve
  wanted = declared_version
  return Result.new(env: scrub, ruby_version: @current, manager: nil, note: nil) if wanted.nil?
  return Result.new(env: scrub, ruby_version: @current, manager: nil, note: nil) if satisfied_by_current?(wanted)

  found = MANAGERS.filter_map { |manager| send(:"#{manager}_env", wanted)&.then { |e| [manager, e] } }.first

  if found.nil?
    return Result.new(
      env: scrub, ruby_version: wanted, manager: nil,
      note: "#{@app_root} declares Ruby #{wanted} and this is #{@current}, but no " \
            "installation of #{wanted} was found under rvm, rbenv, asdf, mise or " \
            "chruby. Pass the app's runtime explicitly:\n" \
            "  --app-env PATH=/path/to/ruby-#{wanted}/bin:$PATH"
    )
  end

  manager, env = found
  Result.new(env: scrub.merge(env), ruby_version: wanted, manager: manager, note: nil)
end