Skip to content

Commit

Permalink
config.gem_paths = ["path1","path2",...] for environment.rb
Browse files Browse the repository at this point in the history
there seems to be 3 main times when rails will want to load up and work with vendor/gems.

1) the Rake tasks to freeze / unfreeze gems
2) the Rails startup code in the very beginning, will try to load once the gems
3) the time after environment.rb and *after* the config,gem in run initializer do config.

This commit works in doing muliple paths for 3), but not 1) and 2).
You can put in my environment.rb a configuration line like this:

config.gem_paths = ["#{PLUGIN_PATH}/vendor/gems"]

Inside of the do |config| loop
  • Loading branch information
dreamcat4 committed Jun 11, 2009
1 parent 84a755b commit 0b347ba
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 17 deletions.
2 changes: 1 addition & 1 deletion railties/environments/boot.rb
Expand Up @@ -44,7 +44,7 @@ class VendorBoot < Boot
def load_initializer
require "#{RAILS_ROOT}/vendor/rails/railties/lib/initializer"
Rails::Initializer.run(:install_gem_spec_stubs)
Rails::GemDependency.add_frozen_gem_path
Rails::Initializer.run(:add_frozen_gem_paths)
end
end

Expand Down
21 changes: 19 additions & 2 deletions railties/lib/initializer.rb
Expand Up @@ -235,6 +235,10 @@ def install_gem_spec_stubs
end
end

def add_frozen_gem_paths
Rails::GemDependency.add_frozen_gem_paths(configuration.gem_paths)
end

# Set the <tt>$LOAD_PATH</tt> based on the value of
# Configuration#load_paths. Duplicates are removed.
def set_load_path
Expand Down Expand Up @@ -295,13 +299,16 @@ def add_plugin_load_paths
end

def add_gem_load_paths
Rails::GemDependency.add_frozen_gem_path
Rails::GemDependency.add_frozen_gem_paths(configuration.gem_paths)
unless @configuration.gems.empty?
require "rubygems"
@configuration.gems.each { |gem| gem.add_load_paths }
@configuration.gems.each { |gem| gem.add_load_paths(configuration.gem_paths) }
end
end

# Loads all gems in <tt>config.gem_paths</tt>. <tt>gem_paths</tt> defaults
# to <tt>vendor/gems</tt> but may also be set to a list of paths, such as
# config.gem_paths = ["#{RAILS_ROOT}/lib/gems", "#{RAILS_ROOT}/vendor/gems"]
def load_gems
unless $gems_rake_task
@configuration.gems.each { |gem| gem.load }
Expand Down Expand Up @@ -797,6 +804,11 @@ def reload_plugins?
# You can add gems with the #gem method.
attr_accessor :gems

# The path to the root of the gems directory. By default, it is <tt>vendor/gems</tt>.
# May also be set to a list of paths, in the enrivonment.rb, do |config| block:
# config.gem_paths = ["#{RAILS_ROOT}/lib/gems", "#{RAILS_ROOT}/vendor/gems"]
attr_accessor :gem_paths

# Adds a single Gem dependency to the rails application. By default, it will require
# the library with the same name as the gem. Use :lib to specify a different name.
#
Expand Down Expand Up @@ -854,6 +866,7 @@ def initialize
self.database_configuration_file = default_database_configuration_file
self.routes_configuration_file = default_routes_configuration_file
self.gems = default_gems
self.gem_paths = default_gem_paths
self.i18n = default_i18n

for framework in default_frameworks
Expand Down Expand Up @@ -1077,6 +1090,10 @@ def default_gems
[]
end

def default_gem_paths
[Rails::GemDependency.default_gem_path]
end

def default_i18n
i18n = Rails::OrderedOptions.new
i18n.load_path = []
Expand Down
2 changes: 1 addition & 1 deletion railties/lib/rails/backtrace_cleaner.rb
Expand Up @@ -34,7 +34,7 @@ def add_gem_filters
add_filter { |line| line.sub(/(#{path})\/gems\/([a-z]+)-([0-9.]+)\/(.*)/, '\2 (\3) \4')}
end

vendor_gems_path = Rails::GemDependency.unpacked_path.sub("#{RAILS_ROOT}/",'')
vendor_gems_path = Rails::GemDependency.default_gem_path.sub("#{RAILS_ROOT}/",'')
add_filter { |line| line.sub(/(#{vendor_gems_path})\/([a-z]+)-([0-9.]+)\/(.*)/, '\2 (\3) [v] \4')}
end
end
Expand Down
25 changes: 16 additions & 9 deletions railties/lib/rails/gem_dependency.rb
Expand Up @@ -10,22 +10,29 @@ module Rails
class GemDependency < Gem::Dependency
attr_accessor :lib, :source, :dep

def self.unpacked_path
@unpacked_path ||= File.join(RAILS_ROOT, 'vendor', 'gems')
def self.default_gem_path
@default_gem_path ||= File.join(RAILS_ROOT, 'vendor', 'gems')
end

@@framework_gems = {}
@@loaded_paths = []

def self.add_frozen_gem_path
@@paths_loaded ||= begin
source_index = Rails::VendorGemSourceIndex.new(Gem.source_index)
add_frozen_gem_paths([default_gem_path])
end

def self.add_frozen_gem_paths(gem_paths)
gem_paths = gem_paths - [default_gem_path] unless gem_paths == [default_gem_path]
new_paths = gem_paths - ( @@loaded_paths & gem_paths )
unless new_paths.empty?
source_index = Rails::VendorGemSourceIndex.new(Gem.source_index, gem_paths)
Gem.clear_paths
Gem.source_index = source_index
# loaded before us - we can't change them, so mark them
Gem.loaded_specs.each do |name, spec|
@@framework_gems[name] = spec
end
true
@@loaded_paths = @@loaded_paths + new_paths
end
end

Expand Down Expand Up @@ -56,16 +63,16 @@ def initialize(name, options = {})
super(name, req)
end

def add_load_paths
self.class.add_frozen_gem_path
def add_load_paths(gem_paths)
self.class.add_frozen_gem_paths(gem_paths)
return if @loaded || @load_paths_added
if framework_gem?
@load_paths_added = @loaded = @frozen = true
return
end
gem self
@spec = Gem.loaded_specs[name]
@frozen = @spec.loaded_from.include?(self.class.unpacked_path) if @spec
@frozen = @spec.loaded_from.include?(self.class.default_gem_path) if @spec
@load_paths_added = true
rescue Gem::LoadError
end
Expand Down Expand Up @@ -280,7 +287,7 @@ def unpack_command
end

def unpack_base
Rails::GemDependency.unpacked_path
Rails::GemDependency.default_gem_path
end

def unpacked_gem_directory
Expand Down
10 changes: 7 additions & 3 deletions railties/lib/rails/vendor_gem_source_index.rb
@@ -1,5 +1,6 @@
require 'rubygems'
require 'yaml'
require 'active_support/core_ext/enumerable'

module Rails

Expand All @@ -12,8 +13,10 @@ class VendorGemSourceIndex

attr_reader :installed_source_index
attr_reader :vendor_source_index
attr_reader :gem_paths

@@silence_spec_warnings = false
# @@silence_spec_warnings = true

def self.silence_spec_warnings
@@silence_spec_warnings
Expand All @@ -23,9 +26,9 @@ def self.silence_spec_warnings=(v)
@@silence_spec_warnings = v
end

def initialize(installed_index, vendor_dir=Rails::GemDependency.unpacked_path)
def initialize(installed_index, gem_paths)
@installed_source_index = installed_index
@vendor_dir = vendor_dir
@gem_paths = gem_paths
refresh!
end

Expand All @@ -42,7 +45,8 @@ def refresh!
end

# load specifications from vendor/gems
Dir[File.join(Rails::GemDependency.unpacked_path, '*')].each do |d|
gem_unpacked_paths = gem_paths.sum { |path| Dir["#{path}/**"] }
Dir.glob( gem_unpacked_paths ).each do |d|
dir_name = File.basename(d)
dir_version = version_for_dir(dir_name)
spec = load_specification(d)
Expand Down
2 changes: 1 addition & 1 deletion railties/test/backtrace_cleaner_test.rb
Expand Up @@ -53,7 +53,7 @@ def setup
end

test "should format vendor gems correctly" do
@backtrace = [ "#{Rails::GemDependency.unpacked_path}/nosuchgem-1.2.3/lib/foo.rb" ]
@backtrace = [ "#{Rails::GemDependency.default_gem_path}/nosuchgem-1.2.3/lib/foo.rb" ]
@result = @cleaner.clean(@backtrace)
assert_equal "nosuchgem (1.2.3) [v] lib/foo.rb", @result[0]
end
Expand Down

0 comments on commit 0b347ba

Please sign in to comment.