Permalink
Browse files

Upgrade Rake compatibility for the more constrained Rake 0.9

  • Loading branch information...
1 parent e2c505c commit 11c8748933cc611972f32831657ab99e6701bd93 @wycats wycats committed Jul 3, 2011
Showing with 27 additions and 15 deletions.
  1. +11 −7 lib/thor/rake_compat.rb
  2. +5 −1 lib/thor/task.rb
  3. +11 −7 spec/rake_compat_spec.rb
View
@@ -1,4 +1,5 @@
require 'rake'
+require 'rake/dsl_definition'
class Thor
# Adds a compatibility layer to your Thor classes which allows you to use
@@ -16,6 +17,8 @@ class Thor
# end
#
module RakeCompat
+ include Rake::DSL if defined?(Rake::DSL)
+
def self.rake_classes
@rake_classes ||= []
end
@@ -29,12 +32,12 @@ def self.included(base)
end
end
-class Object #:nodoc:
- alias :rake_task :task
- alias :rake_namespace :namespace
+# override task on (main), for compatibility with Rake 0.9
+self.instance_eval do
+ alias rake_namespace namespace
- def task(*args, &block)
- task = rake_task(*args, &block)
+ def task(*)
+ task = super
if klass = Thor::RakeCompat.rake_classes.last
non_namespaced_name = task.name.split(':').last
@@ -52,15 +55,16 @@ def task(*args, &block)
task
end
- def namespace(name, &block)
+ def namespace(name)
if klass = Thor::RakeCompat.rake_classes.last
const_name = Thor::Util.camel_case(name.to_s).to_sym
klass.const_set(const_name, Class.new(Thor))
new_klass = klass.const_get(const_name)
Thor::RakeCompat.rake_classes << new_klass
end
- rake_namespace(name, &block)
+ super
Thor::RakeCompat.rake_classes.pop
end
end
+
View
@@ -104,7 +104,11 @@ def initialize(name, options=nil)
def run(instance, args=[])
if (instance.methods & [name.to_s, name.to_sym]).empty?
- super
+ if ((instance.protected_methods + instance.public_methods) & ([:method_missing, "method_missing"])).empty?
+ super
+ else
+ instance.send(:method_missing, name.to_sym, *args)
+ end
else
instance.class.handle_no_task_error(name)
end
View
@@ -2,20 +2,24 @@
require 'thor/rake_compat'
require 'rake/tasklib'
+$main = self
+
class RakeTask < Rake::TaskLib
def initialize
define
end
def define
- desc "Say it's cool"
- task :cool do
- puts "COOL"
- end
+ $main.instance_eval do
+ desc "Say it's cool"
+ task :cool do
+ puts "COOL"
+ end
- namespace :hiper_mega do
- task :super do
- puts "HIPER MEGA SUPER"
+ namespace :hiper_mega do
+ task :super do
+ puts "HIPER MEGA SUPER"
+ end
end
end
end

0 comments on commit 11c8748

Please sign in to comment.