Class: Ocran::RuntimeEnvironment

Inherits:
Object
  • Object
show all
Defined in:
lib/ocran/runtime_environment.rb

Constant Summary collapse

BUNDLER_SETUP_FEATURE =

Matches the bundler/setup feature by which bundle exec sets a process up, whatever prefix the bundler gem happens to be installed under.

%r{[\\/]bundler[\\/]setup\.rb\z}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRuntimeEnvironment

Returns a new instance of RuntimeEnvironment.



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ocran/runtime_environment.rb', line 20

def initialize
  @env = ENV.to_hash.freeze
  @load_path = $LOAD_PATH.dup.freeze
  @loaded_features = $LOADED_FEATURES.dup.freeze
  @pwd = Dir.pwd.freeze
  # The gems RubyGems had activated at this point. Under `bundle exec`
  # that is the entire bundle, activated before OCRAN's first line runs,
  # so a snapshot taken before the dependency run is what tells the build
  # environment's gems apart from the application's.
  @activated_gems = (defined?(Gem) ? Gem.loaded_specs.keys : []).freeze
end

Instance Attribute Details

#activated_gemsObject (readonly)

Returns the value of attribute activated_gems.



18
19
20
# File 'lib/ocran/runtime_environment.rb', line 18

def activated_gems
  @activated_gems
end

#envObject (readonly)

Returns the value of attribute env.



18
19
20
# File 'lib/ocran/runtime_environment.rb', line 18

def env
  @env
end

#load_pathObject (readonly)

Returns the value of attribute load_path.



18
19
20
# File 'lib/ocran/runtime_environment.rb', line 18

def load_path
  @load_path
end

#loaded_featuresObject (readonly)

Returns the value of attribute loaded_features.



18
19
20
# File 'lib/ocran/runtime_environment.rb', line 18

def loaded_features
  @loaded_features
end

#pwdObject (readonly)

Returns the value of attribute pwd.



18
19
20
# File 'lib/ocran/runtime_environment.rb', line 18

def pwd
  @pwd
end

Instance Method Details

#bundler_setup_loaded?Boolean

Whether Bundler had already set this process up when the snapshot was taken. bundle exec arranges that through RUBYOPT (and, with newer RubyGems, through the BUNDLER_SETUP variable), so bundler/setup is in $LOADED_FEATURES before the command it runs gets to say anything.

Returns:

  • (Boolean)


36
37
38
# File 'lib/ocran/runtime_environment.rb', line 36

def bundler_setup_loaded?
  @loaded_features.any? { |feature| feature.to_s.match?(BUNDLER_SETUP_FEATURE) }
end

#expand_path(path) ⇒ Object

Expands the given path using the working directory stored in this instance as the base. This method resolves relative paths to absolute paths, ensuring they are fully qualified based on the working directory stored within this instance.



44
45
46
# File 'lib/ocran/runtime_environment.rb', line 44

def expand_path(path)
  File.expand_path(path, @pwd)
end

#find_load_path(path) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ocran/runtime_environment.rb', line 48

def find_load_path(path)
  path = Pathname.new(path) unless path.is_a?(Pathname)

  if path.absolute?
    # For an absolute path feature, find the load path that contains the feature
    # and determine the longest matching path (most specific path).
    @load_path.select { |load_path| path.subpath?(expand_path(load_path)) }
              .max_by { |load_path| expand_path(load_path).length }
  else
    # For a relative path feature, find the load path where the expanded feature exists
    # and select the longest load path (most specific path).
    @load_path.select { |load_path| path.expand_path(load_path).exist? }
              .max_by { |load_path| load_path.length }
  end
end