Permalink
Browse files

Improved DSL, version bump (0.1.2)

  • Loading branch information...
1 parent afff2e9 commit eecba51f49563434d7eeaced5da16aba57e1f084 @winton committed Jan 27, 2010
Showing with 244 additions and 138 deletions.
  1. +22 −21 config/dep.rb
  2. +33 −29 lib/dep.rb
  3. +64 −18 lib/dep/dsl.rb
  4. +18 −23 lib/dep/gemspec.rb
  5. +2 −2 lib/dep/tasks.rb
  6. +61 −15 spec/dep/dsl_spec.rb
  7. +14 −17 spec/dep/gemspec_spec.rb
  8. +17 −11 spec/dep_spec.rb
  9. +13 −2 spec/spec_helper.rb
View
@@ -1,28 +1,29 @@
require File.expand_path("#{File.dirname(__FILE__)}/../lib/dep")
-Dep.gem do
+Dep do
- rake '=0.8.7', :require => %w(rake)
- rspec '=1.3.0'
-end
-
-Dep.gemspec do
-
- author 'Winton Welsh'
- email 'mail@wintoni.us'
- name 'dep'
- homepage "http://github.com/winton/#{name}"
- root File.expand_path("#{File.dirname(__FILE__)}/../")
- summary "Dependency manager"
- version '0.1.1'
-end
-
-Dep.profile do
+ gem(:rake, '=0.8.7') { require 'rake' }
+ gem(:rspec, '=1.3.0') { require 'rspec' }
- rakefile :require => %w(lib/dep/tasks) do
- rake :require => %w(rake/gempackagetask)
- rspec :require => %w(spec/rake/spectask)
+ gemspec do
+ author 'Winton Welsh'
+ email 'mail@wintoni.us'
+ name 'dep'
+ homepage "http://github.com/winton/#{name}"
+ root File.expand_path("#{File.dirname(__FILE__)}/../")
+ summary "Dependency manager"
+ version '0.1.2'
end
- spec_helper :require => %w(lib/dep/spec_helper lib/dep pp)
+ rakefile do
+ gem(:rake) { require 'rake/gempackagetask' }
+ gem(:rspec) { require 'spec/rake/spectask' }
+ require 'lib/dep/tasks'
+ end
+
+ spec_helper do
+ require 'lib/dep/spec_helper'
+ require 'lib/dep'
+ require 'pp'
+ end
end
View
@@ -4,51 +4,49 @@
class Dep
- @@gem = Dsl.new
- @@gemspec = Dsl.new
- @@gemspec_instance = Gemspec.new
- @@profile = Dsl.new
+ @@dsl = Dsl.new
+ @@gemspec = Gemspec.new
- def self.gem(&block)
- @@gem.call &block
+ def self.call(&block)
+ @@dsl.call &block
end
- def self.gemspec(&block)
- @@gemspec.call &block
+ def self.instance
+ @@gemspec.instance
end
- def self.gemspec_instance
- @@gemspec_instance.instance
+ def self.get(*args)
+ @@dsl.get *args
end
def self.name
- @@gemspec_instance.name
+ @@gemspec.name
end
def self.method_missing(method, value=nil, options=nil)
method = method.to_s
if method.include?('!')
method = method.gsub!('!', '').intern
- if profile[method]
- version, options, children = profile[method]
- children.each do |name, (overwrite_version, merge_options)|
- require_gem! name, overwrite_version, merge_options
+ gem = get(:gem, method)
+ profile = get(method)
+ if profile
+ profile.dsl.each do |dsl|
+ if dsl.require?
+ require! dsl.path
+ elsif dsl.gem?
+ require_gem! dsl.name, dsl.version, dsl.dsl
+ end
end
- require! options[:require]
- elsif gem[method]
- require_gem! method
+ elsif gem
+ require_gem! gem.name
end
else
raise "Dep##{method} does not exist"
end
end
- def self.profile(&block)
- @@profile.call &block
- end
-
def self.root
- @@gemspec_instance.root
+ @@gemspec.root
end
private
@@ -58,12 +56,14 @@ def self.file_exists?(path)
(File.exists?("#{path}.rb") && File.file?("#{path}.rb"))
end
- def self.require_gem!(name, overwrite_version=nil, merge_options={})
- version, options = gem.get(name)
- version = overwrite_version || version
- options = options.merge(merge_options)
- Kernel.send :gem, name.to_s, version
- require! options[:require]
+ def self.require_gem!(name, overwrite_version=nil, overwrite_dsl=nil)
+ gem = get(:gem, name)
+ if gem
+ Kernel.send :gem, name.to_s, overwrite_version || gem.version
+ (overwrite_dsl || gem.dsl).all(:require).each do |dsl|
+ require! dsl.path
+ end
+ end
end
def self.require!(paths)
@@ -77,4 +77,8 @@ def self.require!(paths)
end
end
end
+end
+
+def Dep(&block)
+ Dep.call &block
end
View
@@ -1,30 +1,76 @@
class Dep
- class Dsl < Hash
-
+ class Dsl < Array
+
+ def all(*values)
+ if values.empty?
+ self
+ else
+ Dsl.new(self.select { |a| a[0..values.length-1] == values })
+ end
+ end
+
def call(&block)
instance_eval(&block) if block_given?
self
end
- def get(key, index=nil)
- if index
- self[key][index] rescue nil
+ def get(*values)
+ self.detect { |a| a[0..values.length-1] == values }
+ end
+
+ def gem(*args, &block)
+ args.unshift(:gem)
+ method_missing *args, &block
+ end
+
+ def method_missing(*args, &block)
+ if args.length == 1 && a = self.get(args[0])
+ a[1]
else
- self[key] || [ nil, {}, {} ]
+ if block_given?
+ args << Dsl.new.call(&block)
+ end
+ self << Args.new(args)
end
end
-
- def method_missing(method, *args, &block)
- value = args.detect { |a| a.class != Hash }
- options = args.detect { |a| a.class == Hash }
- if self[method] && value.nil? && options.nil?
- self.get(method, 0)
- else
- self[method] = [
- value,
- options || {},
- block_given? ? Dsl.new.call(&block) : {}
- ]
+
+ def require(*args, &block)
+ args.unshift(:require)
+ method_missing *args, &block
+ end
+
+ class Args < Array
+
+ def all(*values)
+ dsl.all(*values) if dsl
+ end
+
+ def dsl
+ self[-1] if self[-1].class == Dep::Dsl::Args || self[-1].class == Dsl
+ end
+
+ def get(*values)
+ dsl.get(*values) if dsl
+ end
+
+ def gem?
+ self[0] == :gem
+ end
+
+ def name
+ self[1] if gem? && self[1] != dsl
+ end
+
+ def path
+ self[1] if require? && self[1] != dsl
+ end
+
+ def require?
+ self[0] == :require
+ end
+
+ def version
+ self[2] if self[0] == :gem && self[2] != dsl
end
end
end
View
@@ -31,39 +31,34 @@ def instance
:require_path => 'lib'
}
- options = %w(
- author
- email
- executables
- extra_rdoc_files
- files
- has_rdoc
- homepage
- name
- platform
- summary
- version
- )
-
::Gem::Specification.new do |s|
- options.each do |option|
- option = option.intern
- s.send "#{option}=", Dep.gemspec.get(option, 0) || defaults[option]
+ Dep.get(:gemspec).all.each do |(option, value)|
+ case option
+ when :dependencies then
+ value.all(:gem).each do |dependency|
+ gem = Dep.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
+ end
end
-
- version, options, children = Dep.profile.get(:gemspec)
- children.each do |name, (overwrite_version, merge_options)|
- s.add_dependency(name.to_s, overwrite_version || Dep.gem.get(name, 0))
+ defaults.each do |option, value|
+ s.send("#{option}=", value) if value
end
end
end
def name
- Dep.gemspec.get(:name, 0)
+ Dep.get(:gemspec).get(:name)[1] rescue nil
end
def root
- Dep.gemspec.get(:root, 0)
+ Dep.get(:gemspec).get(:root)[1] rescue nil
end
end
end
View
@@ -1,6 +1,6 @@
desc "Package gem"
-Rake::GemPackageTask.new(Dep.gemspec_instance) do |pkg|
- pkg.gem_spec = Dep.gemspec_instance
+Rake::GemPackageTask.new(Dep.instance) do |pkg|
+ pkg.gem_spec = Dep.instance
end
desc "Install gem"
Oops, something went wrong.

0 comments on commit eecba51

Please sign in to comment.