Skip to content

Commit

Permalink
Roll our own Gem#latest_load_paths and use it when Bundler not active
Browse files Browse the repository at this point in the history
  • Loading branch information
timcharper committed May 6, 2011
1 parent 27f07fb commit 50c670d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/spork.rb
Expand Up @@ -11,6 +11,7 @@ module Spork
autoload :Runner, (LIBDIR + 'spork/runner').to_s
autoload :Forker, (LIBDIR + 'spork/forker').to_s
autoload :Diagnoser, (LIBDIR + 'spork/diagnoser').to_s
autoload :GemHelpers, (LIBDIR + 'spork/gem_helpers').to_s

class << self
# Run a block, during prefork mode. By default, if prefork is called twice in the same file and line number, the supplied block will only be ran once.
Expand Down Expand Up @@ -104,7 +105,7 @@ def detect_and_require(subfolder)

# This method is used to auto-discover peer plugins such as spork-testunit.
def other_spork_gem_load_paths
@other_spork_gem_load_paths ||= $LOAD_PATH.uniq.grep(/spork/).select do |g|
@other_spork_gem_load_paths ||= Spork::GemHelpers.latest_load_paths.grep(/spork/).select do |g|
not g.match(%r{/spork-[0-9\-.]+/lib}) # don't include other versions of spork
end
end
Expand Down
34 changes: 34 additions & 0 deletions lib/spork/gem_helpers.rb
@@ -0,0 +1,34 @@
module Spork::GemHelpers
extend self

class GemPath
attr_reader :name, :version, :path, :version_numbers
include Comparable
def initialize(p)
@path = p
@name, @version = File.basename(p).scan(/^(.+?)-([^-]+)$/).flatten
@version_numbers = @version.split(/[^0-9]+/).map(&:to_i)
end

def <=>(other)
raise "Not comparable gem paths ('#{name}' is not '#{other.name}')" unless name == other.name
@version_numbers <=> other.version_numbers
end
end

def latest_load_paths
if defined?(Bundler)
$LOAD_PATH.uniq
else
Dir["{#{Gem.paths.path.join(',')}}" + "/gems/*"].inject({}) do |h,f|
gem_path = GemPath.new(f)
if h[gem_path.name]
h[gem_path.name] = gem_path if gem_path > h[gem_path.name]
else
h[gem_path.name] = gem_path
end
h
end
end
end
end

0 comments on commit 50c670d

Please sign in to comment.