Skip to content

Commit

Permalink
better to keep ENV['GEM_PATH'] updates by default - keeps Bundler is …
Browse files Browse the repository at this point in the history
…more happy ...

seems current Bundler (1.6) does not pick up `Gem.path` directly from rubygems
- re-invented **jruby.rack.env.gem_path** param instead of **jruby.rack.gem_path**
  • Loading branch information
kares committed Jun 20, 2014
1 parent f5ce32b commit d3ee8f0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 25 deletions.
47 changes: 27 additions & 20 deletions src/main/ruby/jruby/rack/booter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,48 +105,55 @@ def boot!

def adjust_gem_path
gem_path = self.gem_path
case gem_path?
when false then # org.jruby.rack.RackLogger::DEBUG
if gem_path && ! gem_path.empty? &&
( ! defined?(Gem.path) || ! Gem.path.include?(gem_path) )
@rack_context.log("Gem.path won't be updated although seems configured: #{gem_path}")
end
return false
case set_gem_path = env_gem_path
when true then
if env_gem_path = ENV['GEM_PATH']
if env_path = ENV['GEM_PATH']
if gem_path.nil? || gem_path.empty?
return # keep ENV['GEM_PATH'] as is
elsif env_gem_path != gem_path
elsif env_path != gem_path
separator = File::PATH_SEPARATOR
unless env_gem_path.split(separator).include?(gem_path)
ENV['GEM_PATH'] = "#{gem_path}#{separator}#{env_gem_path}"
unless env_path.split(separator).include?(gem_path)
ENV['GEM_PATH'] = "#{gem_path}#{separator}#{env_path}"
end
end
else
ENV['GEM_PATH'] = gem_path
end
else # nil (default)
when false then
begin
require 'rubygems' unless defined? Gem.path
rescue LoadError
else
return if gem_path.nil? || gem_path.empty?
Gem.path.unshift(gem_path) unless Gem.path.include?(gem_path)
end
return false
when nil then # org.jruby.rack.RackLogger::DEBUG
if gem_path && ! gem_path.empty? &&
( ! defined?(Gem.path) || ! Gem.path.include?(gem_path) )
@rack_context.log("Gem.path won't be updated although seems configured: #{gem_path}")
end
return nil
else # 'jruby.rack.env.gem_path' "forced" to an explicit value
ENV['GEM_PATH'] = set_gem_path
end
end

# @return whether to update Gem.path and/or the environment GEM_PATH
# - true/'env' forces ENV['GEM_PATH'] to be updated
# - false disabled Gem.path mangling for good (leaves all as is)
# - true (default) forces ENV['GEM_PATH'] to be updated due compatibility
# Bundler 1.6 fails to revolve gems correctly when Gem.path is updated
# instead of the ENV['GEM_PATH'] environment variable
# - false disables ENV['GEM_PATH'] mangling for good (updates Gem.path)
#
# - if not specified Gem.path will be updated based on setting
def gem_path?
return @_gem_path if defined? @_gem_path
gem_path = @rack_context.getInitParameter('jruby.rack.gem_path')
return @_gem_path = nil if gem_path.nil?
return @_gem_path = false if gem_path.empty? || gem_path == 'false'
@_gem_path = true # true / 'env'
def env_gem_path
gem_path = @rack_context.getInitParameter('jruby.rack.env.gem_path')
return true if gem_path.nil? || gem_path.to_s == 'true'
return false if gem_path.to_s == 'false'
return nil if gem_path.empty? # set to an empty disables mangling
gem_path
end
private :env_gem_path

# @note called during {#boot!}
def export_global_settings
Expand Down
11 changes: 6 additions & 5 deletions src/spec/ruby/jruby/rack/booter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@
booter.rack_env.should == 'production'
end

it "prepends gem_path to Gem.path" do
it "prepends gem_path to Gem.path (when configured to not mangle with ENV)" do
@rack_context.should_receive(:getInitParameter).with("jruby.rack.env.gem_path").and_return 'false'
Gem.path.replace [ '/opt/gems' ]
booter.gem_path = "wsjar:file:/opt/deploy/sample.war!/WEB-INF/gems"
booter.boot!
Expand All @@ -111,8 +112,8 @@
expect( Gem.path ).to eql [ '/opt/gems' ]
end

it "prepends gem_path to ENV['GEM_PATH'] if jruby.rack.gem_path set to env" do
@rack_context.should_receive(:getInitParameter).with("jruby.rack.gem_path").and_return "env"
it "prepends gem_path to ENV['GEM_PATH'] if jruby.rack.gem_path set to true" do
@rack_context.should_receive(:getInitParameter).with("jruby.rack.env.gem_path").and_return 'true'
ENV['GEM_PATH'] = '/opt/gems'
@rack_context.should_receive(:getRealPath).with("/WEB-INF").and_return "/opt/deploy/sample.war!/WEB-INF"
@rack_context.should_receive(:getRealPath).with("/WEB-INF/gems").and_return "/opt/deploy/sample.war!/WEB-INF/gems"
Expand All @@ -123,6 +124,7 @@
end

it "does not prepend gem_path to ENV['GEM_PATH'] if jruby.rack.gem_path set not set" do
@rack_context.should_receive(:getInitParameter).with("jruby.rack.env.gem_path").and_return ''
ENV['GEM_PATH'] = '/opt/gems'
@rack_context.should_receive(:getRealPath).with("/WEB-INF").and_return "/opt/deploy/sample.war!/WEB-INF"
@rack_context.should_receive(:getRealPath).with("/WEB-INF/gems").and_return "/opt/deploy/sample.war!/WEB-INF/gems"
Expand All @@ -133,7 +135,6 @@
end

it "prepends gem_path to ENV['GEM_PATH'] if not already present" do
@rack_context.should_receive(:getInitParameter).with("jruby.rack.gem_path").and_return "env"
ENV['GEM_PATH'] = "/home/gems#{File::PATH_SEPARATOR}/usr/local/gems"
booter.gem_path = '/usr/local/gems'
booter.boot!
Expand All @@ -152,7 +153,7 @@
# end

it "sets ENV['GEM_PATH'] to the value of gem_path if ENV['GEM_PATH'] is not present" do
@rack_context.should_receive(:getInitParameter).with("jruby.rack.gem_path").and_return 'true'
@rack_context.should_receive(:getInitParameter).with("jruby.rack.env.gem_path").and_return 'true'
ENV.delete('GEM_PATH')
@rack_context.should_receive(:getRealPath).with("/WEB-INF").and_return "/blah"
@rack_context.should_receive(:getRealPath).with("/WEB-INF/gems").and_return "/blah/gems"
Expand Down

0 comments on commit d3ee8f0

Please sign in to comment.