Class: Herve::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/herve/config.rb

Constant Summary collapse

DEFAULT_ROOT_DIR =
"/"
DEFAULT_SEARCH_RUBIES =
%w[$HOME/.rubies /opt/rubies /usr/local/rubies].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root_dir, project_dir, gemfile, release_base_url = RELEASES_URL) ⇒ Config

Returns a new instance of Config.



100
101
102
103
104
105
106
107
# File 'lib/herve/config.rb', line 100

def initialize(root_dir, project_dir, gemfile, release_base_url = RELEASES_URL)
  @root_dir = root_dir
  @project_dir = project_dir
  @current_exe = Pathname.new($PROGRAM_NAME).realpath
  @gemfile = gemfile
  @release_base_url = release_base_url
  @cache = Cache.new(Pathname.new(XDG::Cache.new.home).join("herve"))
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



20
21
22
# File 'lib/herve/config.rb', line 20

def cache
  @cache
end

#current_exeObject (readonly)

Returns the value of attribute current_exe.



20
21
22
# File 'lib/herve/config.rb', line 20

def current_exe
  @current_exe
end

#gemfileObject (readonly)

Returns the value of attribute gemfile.



20
21
22
# File 'lib/herve/config.rb', line 20

def gemfile
  @gemfile
end

#project_dirObject (readonly)

Returns the value of attribute project_dir.



20
21
22
# File 'lib/herve/config.rb', line 20

def project_dir
  @project_dir
end

#release_base_urlObject (readonly)

Returns the value of attribute release_base_url.



20
21
22
# File 'lib/herve/config.rb', line 20

def release_base_url
  @release_base_url
end

#root_dirObject (readonly)

Returns the value of attribute root_dir.



20
21
22
# File 'lib/herve/config.rb', line 20

def root_dir
  @root_dir
end

#ruby_dirsObject

Returns the value of attribute ruby_dirs.



21
22
23
# File 'lib/herve/config.rb', line 21

def ruby_dirs
  @ruby_dirs
end

Class Method Details

.env_for(ruby) ⇒ Array(Array<String>, Hash(String => String)>

Returns ].

Parameters:

Returns:

  • (Array(Array<String>, Hash(String => String)>)

    ]



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/herve/config.rb', line 39

def env_for(ruby)
  env = ENV_VARS.to_h { |var| [var, nil] }
  paths = split_path(ENV["PATH"])

  old_ruby_paths = %w[RUBY_ROOT GEM_ROOT GEM_HOME].map { |v| ENV[v] }
                                                  .compact
                                                  .map { |p| File.join(p, "bin") }
  old_gem_paths = split_path(ENV["GEM_PATH"])

  # Remove old Ruby and Gem paths from PATH
  paths.delete_if { |p| old_ruby_paths.include?(p) || old_gem_paths.include?(p) }
  set_env_for(ruby, env, paths) unless ruby.nil?
  env["PATH"] = paths.join(":")
  env
end

.from_env_and_options(env, options) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/herve/config.rb', line 24

def from_env_and_options(env, options)
  root_dir = Pathname.new(env["RV_ROOT_DIR"] || DEFAULT_ROOT_DIR)
  project_dir = if options.key?(:project_dir) && !options[:project_dir].nil?
                  Pathname.new(options[:project_dir])
                else
                  find_project_dir(Pathname.pwd, root_dir)
                end
  bundle_gemfile = env.fetch("BUNDLE_GEMFILE", nil)
  gemfile = bundle_gemfile ? Pathname.new(bundle_gemfile) : nil
  env.fetch("HERVE_RELEASES_URL", RELEASES_URL)
  new(root_dir, project_dir, gemfile)
end

Instance Method Details

#default_ruby_dirsObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/herve/config.rb', line 109

def default_ruby_dirs
  DEFAULT_SEARCH_RUBIES.map do |dir|
    path = Herve.expand_path(dir)
    joinable_path = path.relative_path_from("/")
    joined_path = root_dir.join(joinable_path)
    if joined_path.split.last.to_s == ".rubies"
      # Ensure ~/.rubies is in list, even if it does not exist yet
      joined_path
    else
      begin
        joined_path.realpath
      rescue SystemCallError
        nil
      end
    end
  end.compact
end

#matching_ruby(request) ⇒ Object



149
150
151
152
153
# File 'lib/herve/config.rb', line 149

def matching_ruby(request)
  return nil if request.nil?

  rubies.reverse.find { |ruby| request.satisfied_by(ruby) }
end

#project_rubyObject



131
132
133
# File 'lib/herve/config.rb', line 131

def project_ruby
  matching_ruby(ruby_request)
end

#rubiesObject



127
128
129
# File 'lib/herve/config.rb', line 127

def rubies
  discover_rubies
end

#ruby_requestRuby::Request?

Returns:



136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/herve/config.rb', line 136

def ruby_request
  return nil if project_dir.nil?

  begin
    file = project_dir.join(".ruby-version")
    Ruby::Request.parse(File.read(file))
  rescue Error
    nil
  rescue SystemCallError => e
    raise Error, e.message
  end
end

#ruby_versionObject

Raises:



155
156
157
158
159
160
# File 'lib/herve/config.rb', line 155

def ruby_version
  file = project_dir.join(".ruby-version")
  raise ConfigError, "no .ruby-version file in #{project_dir}" unless file.exist?

  file.read
end

#ruby_version=(version) ⇒ Object



162
163
164
165
# File 'lib/herve/config.rb', line 162

def ruby_version=(version)
  dir = project_dir || Dir.cwd
  dir.join(".ruby-version").write("#{version}\n")
end