Skip to content
Browse files

Fixed more issues with requiring projects that use Require

  • Loading branch information...
1 parent 51fec15 commit b614be6a3c9eb9c3ef082782e070104a5d0544f0 @winton committed
Showing with 84 additions and 77 deletions.
  1. +25 −16 lib/require.rb
  2. +58 −60 lib/require/gemspec.rb
  3. +1 −1 require.rb
View
41 lib/require.rb
@@ -3,21 +3,22 @@
require "#{File.dirname(__FILE__)}/require/gemspec"
class Require
+
+ @dsl = {}
+ @gemspec = {}
+
class <<self
-
- @dsl = {}
def all(*args)
dsl.all *args
end
def call(&block)
- dsl.call &block
+ dsl(true).call &block
end
- def dsl
- @dsl[root] ||= Dsl.new
- @dsl[root]
+ def dsl(force=false)
+ @dsl[root(force)] ||= Dsl.new
end
def get(*args)
@@ -25,7 +26,7 @@ def get(*args)
end
def gemspec
- Gemspec.instance
+ (@gemspec[root] ||= Gemspec.new).instance
end
def name
@@ -73,14 +74,22 @@ def reset(&block)
call &block
end
- def root
- path = caller.detect do |p|
- !p.include?('/require/lib/')
+ def root(force=false)
+ paths = caller.collect do |p|
+ File.expand_path(p.split(':').first)
+ end
+ path = paths.detect do |p|
+ !p.index(/\/require[-\.\d]*\/lib\//) &&
+ !p.include?('/rubygems/specification.rb') &&
+ !p.include?('/lib/rake/') &&
+ !p.include?('/lib/spec/')
+ end
+ paths = @dsl.keys.sort { |a,b| b.length <=> a.length }
+ if force
+ File.dirname(path)
+ else
+ paths.detect { |p| path[0..p.length-1] == p }
end
- path = path.split(':').first
- paths = @dsl.keys.sort { |a,b| a.length <=> b.length }
- path = paths.detect { |p| p[0..path.length-1] == path } || path
- File.dirname(path)
end
private
@@ -124,6 +133,6 @@ def require_gem!(name, overwrite_version=nil, overwrite_dsl=nil)
end
end
-def Require(root=nil, &block)
- Require.call root, &block
+def Require(&block)
+ Require.call &block
end
View
118 lib/require/gemspec.rb
@@ -1,73 +1,71 @@
class Require
class Gemspec
- class <<self
-
- def clean_paths(paths, more=nil)
- paths.collect { |p| p.gsub("#{root}/#{more}", '') }
- end
-
- def executables
- clean_paths Dir["#{root}/bin/*"], 'bin/'
- end
+
+ def clean_paths(paths, more=nil)
+ paths.collect { |p| p.gsub("#{root}/#{more}", '') }
+ end
+
+ def executables
+ clean_paths Dir["#{root}/bin/*"], 'bin/'
+ end
+
+ def extra_doc_files
+ clean_paths Dir["#{root}/README.*"]
+ end
+
+ def files
+ gitignore = "#{root}/.gitignore"
+ ignore =
+ if File.exists?(gitignore)
+ File.read(gitignore).split("\n").collect do |path|
+ "#{root}/#{path.strip}"
+ end
+ else
+ []
+ end
+ clean_paths (Dir["#{root}/**/*"] - Dir[*ignore])
+ end
+
+ def instance
+ raise "Require must be called with a root path parameter" unless root
- def extra_doc_files
- clean_paths Dir["#{root}/README.*"]
- end
+ defaults = {
+ :executables => executables,
+ :extra_rdoc_files => extra_doc_files,
+ :files => files,
+ :has_rdoc => false,
+ :platform => Gem::Platform::RUBY,
+ :require_path => 'lib'
+ }
- def files
- gitignore = "#{root}/.gitignore"
- ignore =
- if File.exists?(gitignore)
- File.read(gitignore).split("\n").collect do |path|
- "#{root}/#{path.strip}"
+ ::Gem::Specification.new do |s|
+ Require.get(:gemspec).all.each do |(option, value)|
+ case option
+ when :dependencies then
+ value.all(:gem).each do |dependency|
+ gem = Require.get(:gem, dependency.name)
+ version = dependency.version || (gem.version rescue nil)
+ s.add_dependency(dependency.name.to_s, version)
end
else
- []
- end
- clean_paths (Dir["#{root}/**/*"] - Dir[*ignore])
- end
-
- def instance
- raise "Require must be called with a root path parameter" unless root
-
- defaults = {
- :executables => executables,
- :extra_rdoc_files => extra_doc_files,
- :files => files,
- :has_rdoc => false,
- :platform => Gem::Platform::RUBY,
- :require_path => 'lib'
- }
-
- ::Gem::Specification.new do |s|
- Require.get(:gemspec).all.each do |(option, value)|
- case option
- when :dependencies then
- value.all(:gem).each do |dependency|
- gem = Require.get(:gem, dependency.name)
- version = dependency.version || (gem.version rescue nil)
- s.add_dependency(dependency.name.to_s, version)
- end
- else
- if s.respond_to?("#{option}=")
- s.send "#{option}=", value || defaults[option]
- defaults[option] = nil
- end
+ if s.respond_to?("#{option}=")
+ s.send "#{option}=", value || defaults[option]
+ defaults[option] = nil
end
end
- defaults.each do |option, value|
- s.send("#{option}=", value) if value
- end
+ end
+ defaults.each do |option, value|
+ s.send("#{option}=", value) if value
end
end
-
- def name
- Require.get(:gemspec).get(:name)[1] rescue nil
- end
-
- def root
- Require.root
- end
+ end
+
+ def name
+ Require.get(:gemspec).get(:name)[1] rescue nil
+ end
+
+ def root
+ @root ||= Require.root
end
end
end
View
2 require.rb
@@ -1,6 +1,6 @@
require "#{File.dirname(__FILE__)}/lib/require"
-Require File.dirname(__FILE__) do
+Require do
gem(:rake, '=0.8.7') { require 'rake' }
gem(:rspec, '=1.3.0')

0 comments on commit b614be6

Please sign in to comment.
Something went wrong with that request. Please try again.