require 0.1.8 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -22,9 +22,10 @@ Create <code>require.rb</code> in your project's root directory:
22
22
 
23
23
  <pre>
24
24
  require 'rubygems'
25
+ gem 'require'
25
26
  require 'require'
26
27
 
27
- Require File.dirname(__FILE__) do
28
+ Require do
28
29
 
29
30
  gem(:sinatra, '=0.9.4') { require 'sinatra/base' }
30
31
  gem(:haml, '=2.2.16') { require %w(haml sass) }
@@ -43,8 +44,11 @@ Then in your library file (<code>lib/whatever.rb</code>):
43
44
  <pre>
44
45
  require File.expand_path("#{File.dirname(__FILE_)}/../require")
45
46
  Require.lib!
46
- # Activates sinatra and haml gems
47
- # Requires sinatra, haml, and sass
48
- # Adds vendor/authlogic/lib to the load paths
49
- # Requires authlogic
50
- </pre>
47
+ </pre>
48
+
49
+ What does <code>Require.lib!</code> do?
50
+
51
+ * Activates sinatra and haml gems
52
+ * Requires sinatra, haml, and sass
53
+ * Adds vendor/authlogic/lib to the load paths
54
+ * Requires authlogic
data/lib/require.rb CHANGED
@@ -4,117 +4,135 @@ require "#{File.dirname(__FILE__)}/require/gemspec"
4
4
 
5
5
  class Require
6
6
 
7
- @@dsl = Dsl.new
8
- @@gemspec = Gemspec.new
9
- @@root = nil
7
+ @dsl = {}
8
+ @gemspec = {}
10
9
 
11
- def self.all(*args)
12
- @@dsl.all *args
13
- end
10
+ class <<self
14
11
 
15
- def self.call(root=nil, &block)
16
- @@root = File.expand_path(root) if root
17
- @@dsl.call &block
18
- end
12
+ def all(*args)
13
+ dsl.all *args
14
+ end
19
15
 
20
- def self.get(*args)
21
- @@dsl.get *args
22
- end
16
+ def call(&block)
17
+ dsl(true).call &block
18
+ end
19
+
20
+ def dsl(force=false)
21
+ @dsl[root(force)] ||= Dsl.new
22
+ end
23
23
 
24
- def self.instance
25
- @@gemspec.instance
26
- end
24
+ def get(*args)
25
+ dsl.get *args
26
+ end
27
27
 
28
- def self.name
29
- @@gemspec.name
30
- end
28
+ def gemspec
29
+ (@gemspec[root] ||= Gemspec.new).instance
30
+ end
31
31
 
32
- def self.method_missing(method, value=nil, options=nil)
33
- method = method.to_s
34
- if method.include?('!')
35
- method = method.gsub!('!', '').intern
36
- gem = get(:gem, method)
37
- profile = get(method)
38
- if profile
39
- profile.dsl.each do |dsl|
40
- if dsl.gem?
41
- require_gem! dsl.name, dsl.version, dsl.dsl
42
- elsif dsl.load_path?
43
- load_path! dsl.path
44
- elsif dsl.require?
45
- require! dsl.path
32
+ def name
33
+ (@gemspec[root] ||= Gemspec.new).name
34
+ end
35
+
36
+ def method_missing(method, value=nil, options=nil)
37
+ method = method.to_s
38
+ if method.include?('!')
39
+ method = method.gsub!('!', '').intern
40
+ gem = get(:gem, method)
41
+ profile = get(method)
42
+ if profile
43
+ profile.dsl.each do |dsl|
44
+ if dsl.gem?
45
+ require_gem! dsl.name, dsl.version, dsl.dsl
46
+ elsif dsl.load_path?
47
+ load_path! dsl.path
48
+ elsif dsl.require?
49
+ require! dsl.path
50
+ end
46
51
  end
52
+ elsif gem
53
+ require_gem! gem.name
54
+ end
55
+ else
56
+ raise "Require##{method} does not exist"
57
+ end
58
+ end
59
+
60
+ def require!(paths)
61
+ return unless paths
62
+ [ paths ].flatten.each do |path|
63
+ path_with_root = "#{root}/#{path}"
64
+ if file_exists?(path_with_root)
65
+ Kernel.require path_with_root
66
+ else
67
+ Kernel.require path
47
68
  end
48
- elsif gem
49
- require_gem! gem.name
50
69
  end
51
- else
52
- raise "Require##{method} does not exist"
53
70
  end
54
- end
55
-
56
- def self.reset(root=nil, &block)
57
- @@dsl = Dsl.new
58
- @@gemspec = Gemspec.new
59
- call root, &block
60
- end
61
71
 
62
- def self.root
63
- @@root
64
- end
72
+ def reset(&block)
73
+ @dsl = {}
74
+ call &block
75
+ end
65
76
 
66
- private
77
+ def root(force=false)
78
+ paths = caller.collect do |p|
79
+ File.expand_path(p.split(':').first)
80
+ end
81
+ path = paths.detect do |p|
82
+ !p.index(/\/require[-\.\d]*\/lib\//) &&
83
+ !p.include?('/rubygems/specification.rb') &&
84
+ !p.include?('/lib/rake/') &&
85
+ !p.include?('/lib/spec/')
86
+ end
87
+ paths = @dsl.keys.sort { |a,b| b.length <=> a.length }
88
+ if force
89
+ File.dirname(path)
90
+ else
91
+ paths.detect { |p| path[0..p.length-1] == p }
92
+ end
93
+ end
67
94
 
68
- def self.file_exists?(path)
69
- (File.exists?(path) && File.file?(path)) ||
70
- (File.exists?("#{path}.rb") && File.file?("#{path}.rb"))
71
- end
95
+ private
72
96
 
73
- def self.dir_exists?(path)
74
- File.exists?(path) && File.directory?(path)
75
- end
97
+ def dir_exists?(path)
98
+ File.exists?(path) && File.directory?(path)
99
+ end
76
100
 
77
- def self.load_path!(paths)
78
- return unless paths
79
- [ paths ].flatten.each do |path|
80
- path_with_root = "#{root}/#{path}"
81
- if root && dir_exists?(path_with_root)
82
- $: << path_with_root
83
- else
84
- $: << path
85
- end
101
+ def file_exists?(path)
102
+ (File.exists?(path) && File.file?(path)) ||
103
+ (File.exists?("#{path}.rb") && File.file?("#{path}.rb"))
86
104
  end
87
- end
88
105
 
89
- def self.require_gem!(name, overwrite_version=nil, overwrite_dsl=nil)
90
- gem = get(:gem, name)
91
- if gem
92
- if overwrite_version || gem.version
93
- Kernel.send :gem, name.to_s, overwrite_version || gem.version
94
- else
95
- Kernel.send :gem, name.to_s
96
- end
97
- if overwrite_dsl || gem.dsl
98
- (overwrite_dsl || gem.dsl).all(:require).each do |dsl|
99
- require! dsl.path
106
+ def load_path!(paths)
107
+ return unless paths
108
+ [ paths ].flatten.each do |path|
109
+ path_with_root = "#{root}/#{path}"
110
+ if root && dir_exists?(path_with_root)
111
+ $: << path_with_root
112
+ else
113
+ $: << path
100
114
  end
101
115
  end
102
116
  end
103
- end
104
117
 
105
- def self.require!(paths)
106
- return unless paths
107
- [ paths ].flatten.each do |path|
108
- path_with_root = "#{root}/#{path}"
109
- if file_exists?(path_with_root)
110
- Kernel.require path_with_root
111
- else
112
- Kernel.require path
118
+ def require_gem!(name, overwrite_version=nil, overwrite_dsl=nil)
119
+ gem = get(:gem, name)
120
+ if gem
121
+ if overwrite_version || gem.version
122
+ Kernel.send :gem, name.to_s, overwrite_version || gem.version
123
+ else
124
+ Kernel.send :gem, name.to_s
125
+ end
126
+ if overwrite_dsl || gem.dsl
127
+ (overwrite_dsl || gem.dsl).all(:require).each do |dsl|
128
+ require! dsl.path
129
+ end
130
+ end
113
131
  end
114
132
  end
115
133
  end
116
134
  end
117
135
 
118
- def Require(root=nil, &block)
119
- Require.call root, &block
136
+ def Require(&block)
137
+ Require.call &block
120
138
  end
@@ -1,28 +1,34 @@
1
1
  class Require
2
2
  class Gemspec
3
-
3
+
4
4
  def clean_paths(paths, more=nil)
5
5
  paths.collect { |p| p.gsub("#{root}/#{more}", '') }
6
6
  end
7
-
7
+
8
8
  def executables
9
9
  clean_paths Dir["#{root}/bin/*"], 'bin/'
10
10
  end
11
-
11
+
12
12
  def extra_doc_files
13
13
  clean_paths Dir["#{root}/README.*"]
14
14
  end
15
-
15
+
16
16
  def files
17
- ignore = File.read("#{root}/.gitignore").split("\n").collect do |path|
18
- "#{root}/#{path.strip}"
19
- end
17
+ gitignore = "#{root}/.gitignore"
18
+ ignore =
19
+ if File.exists?(gitignore)
20
+ File.read(gitignore).split("\n").collect do |path|
21
+ "#{root}/#{path.strip}"
22
+ end
23
+ else
24
+ []
25
+ end
20
26
  clean_paths (Dir["#{root}/**/*"] - Dir[*ignore])
21
27
  end
22
-
28
+
23
29
  def instance
24
30
  raise "Require must be called with a root path parameter" unless root
25
-
31
+
26
32
  defaults = {
27
33
  :executables => executables,
28
34
  :extra_rdoc_files => extra_doc_files,
@@ -31,7 +37,7 @@ class Require
31
37
  :platform => Gem::Platform::RUBY,
32
38
  :require_path => 'lib'
33
39
  }
34
-
40
+
35
41
  ::Gem::Specification.new do |s|
36
42
  Require.get(:gemspec).all.each do |(option, value)|
37
43
  case option
@@ -53,13 +59,13 @@ class Require
53
59
  end
54
60
  end
55
61
  end
56
-
62
+
57
63
  def name
58
64
  Require.get(:gemspec).get(:name)[1] rescue nil
59
65
  end
60
-
66
+
61
67
  def root
62
- Require.root
68
+ @root ||= Require.root
63
69
  end
64
70
  end
65
71
  end
data/lib/require/tasks.rb CHANGED
@@ -35,8 +35,8 @@ class Require
35
35
  end
36
36
 
37
37
  desc "Package gem"
38
- Rake::GemPackageTask.new(Require.instance) do |pkg|
39
- pkg.gem_spec = Require.instance
38
+ Rake::GemPackageTask.new(Require.gemspec) do |pkg|
39
+ pkg.gem_spec = Require.gemspec
40
40
  end
41
41
 
42
42
  namespace :gem do
data/require.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  require "#{File.dirname(__FILE__)}/lib/require"
2
2
 
3
- Require File.dirname(__FILE__) do
3
+ Require do
4
4
 
5
5
  gem(:rake, '=0.8.7') { require 'rake' }
6
6
  gem(:rspec, '=1.3.0')
@@ -11,7 +11,7 @@ Require File.dirname(__FILE__) do
11
11
  name 'require'
12
12
  homepage "http://github.com/winton/#{name}"
13
13
  summary "Manage your project's dependencies with a pretty DSL"
14
- version '0.1.8'
14
+ version '0.2.0'
15
15
  end
16
16
 
17
17
  rakefile do
@@ -4,7 +4,8 @@ class Require
4
4
  describe Gemspec do
5
5
 
6
6
  it "should generate a valid gemspec instance" do
7
- Require.reset "#{File.dirname(__FILE__)}/../fixture" do
7
+ Require.stub!(:root).and_return(SPEC + "/fixture")
8
+ Require.reset do
8
9
  gem :rspec, '=1.3.0'
9
10
 
10
11
  gemspec do
@@ -22,7 +23,7 @@ class Require
22
23
 
23
24
  FileUtils.mkdir_p(File.expand_path("#{File.dirname(__FILE__)}/../fixture/ignore_me"))
24
25
 
25
- s = Require.instance
26
+ s = Require.gemspec
26
27
  s.authors.should == [ "Winton Welsh" ]
27
28
  s.date.should == Time.utc(Date.today.year, Date.today.mon, Date.today.mday, 8)
28
29
  s.default_executable.should == "bin"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: require
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Winton Welsh
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-02-03 00:00:00 -08:00
12
+ date: 2010-02-06 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies: []
15
15