Skip to content

Commit

Permalink
Upgrade Rake compatibility for the more constrained Rake 0.9
Browse files Browse the repository at this point in the history
  • Loading branch information
wycats committed Jul 3, 2011
1 parent e2c505c commit 11c8748
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 15 deletions.
18 changes: 11 additions & 7 deletions lib/thor/rake_compat.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -16,6 +17,8 @@ class Thor
# end
#
module RakeCompat
include Rake::DSL if defined?(Rake::DSL)

def self.rake_classes
@rake_classes ||= []
end
Expand All @@ -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
Expand All @@ -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

6 changes: 5 additions & 1 deletion lib/thor/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 11 additions & 7 deletions spec/rake_compat_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 11c8748

Please sign in to comment.