Class: Herve::Config
- Inherits:
-
Object
- Object
- Herve::Config
- 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
-
#cache ⇒ Object
readonly
Returns the value of attribute cache.
-
#current_exe ⇒ Object
readonly
Returns the value of attribute current_exe.
-
#gemfile ⇒ Object
readonly
Returns the value of attribute gemfile.
-
#project_dir ⇒ Object
readonly
Returns the value of attribute project_dir.
-
#release_base_url ⇒ Object
readonly
Returns the value of attribute release_base_url.
-
#root_dir ⇒ Object
readonly
Returns the value of attribute root_dir.
-
#ruby_dirs ⇒ Object
Returns the value of attribute ruby_dirs.
Class Method Summary collapse
-
.env_for(ruby) ⇒ Array(Array<String>, Hash(String => String)>
].
- .from_env_and_options(env, options) ⇒ Object
Instance Method Summary collapse
- #default_ruby_dirs ⇒ Object
-
#initialize(root_dir, project_dir, gemfile, release_base_url = RELEASES_URL) ⇒ Config
constructor
A new instance of Config.
- #matching_ruby(request) ⇒ Object
- #project_ruby ⇒ Object
- #rubies ⇒ Object
- #ruby_request ⇒ Ruby::Request?
- #ruby_version ⇒ Object
- #ruby_version=(version) ⇒ Object
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
#cache ⇒ Object (readonly)
Returns the value of attribute cache.
20 21 22 |
# File 'lib/herve/config.rb', line 20 def cache @cache end |
#current_exe ⇒ Object (readonly)
Returns the value of attribute current_exe.
20 21 22 |
# File 'lib/herve/config.rb', line 20 def current_exe @current_exe end |
#gemfile ⇒ Object (readonly)
Returns the value of attribute gemfile.
20 21 22 |
# File 'lib/herve/config.rb', line 20 def gemfile @gemfile end |
#project_dir ⇒ Object (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_url ⇒ Object (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_dir ⇒ Object (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_dirs ⇒ Object
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 ].
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 (env, ) root_dir = Pathname.new(env["RV_ROOT_DIR"] || DEFAULT_ROOT_DIR) project_dir = if .key?(:project_dir) && ![:project_dir].nil? Pathname.new([: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_dirs ⇒ Object
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.(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_ruby ⇒ Object
131 132 133 |
# File 'lib/herve/config.rb', line 131 def project_ruby matching_ruby(ruby_request) end |
#rubies ⇒ Object
127 128 129 |
# File 'lib/herve/config.rb', line 127 def rubies discover_rubies end |
#ruby_request ⇒ Ruby::Request?
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. end end |
#ruby_version ⇒ Object
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 |