From 46fb57757d9677bfc06069ad8ae3c4bf180148e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Tue, 2 Jun 2009 11:35:52 +0200 Subject: [PATCH] Tidying up Thor and Thor::Generator classes. --- lib/thor.rb | 100 +++++++++++++++++++----------------------- lib/thor/generator.rb | 61 ++++++++++++-------------- 2 files changed, 74 insertions(+), 87 deletions(-) diff --git a/lib/thor.rb b/lib/thor.rb index d48e4aa26..6cc909a07 100644 --- a/lib/thor.rb +++ b/lib/thor.rb @@ -3,58 +3,6 @@ class Thor - # Invokes a task. - # - # ==== Errors - # Thor::Error:: A Thor error is raised if the user called an undefined task - # or called an exisisting task wrongly. - # - def invoke(meth, *args) - super - rescue ArgumentError => e - backtrace = sans_backtrace(e.backtrace, caller) - - if backtrace.empty? - task = self.class[meth] - raise Error, "'#{meth}' was called incorrectly. Call as '#{task.formatted_usage(self.class)}'" - else - raise e - end - rescue NoMethodError => e - if e.message =~ /^undefined method `#{meth}' for #{Regexp.escape(self.inspect)}$/ - raise Error, "The #{self.class.namespace} namespace doesn't have a '#{meth}' task" - else - raise e - end - end - - # Implement the hooks required by Thor::Base. - # - class << self - protected - def baseclass - Thor - end - - def valid_task?(meth) - public_instance_methods.include?(meth) && @usage && @desc - end - - def create_task(meth) - tasks[meth.to_s] = Thor::Task.new(meth, @desc, @usage, method_options) - @usage, @desc, @method_options = nil - end - - def initialize_added - class_options.merge!(method_options) - @method_options = nil - end - end - - include Thor::Base - - # Implement specific Thor methods. - # class << self # Sets the default task when thor is executed without an explicit task to be called. @@ -216,16 +164,34 @@ def help(meth=nil, options={}) protected + def baseclass #:nodoc: + Thor + end + + def valid_task?(meth) #:nodoc: + public_instance_methods.include?(meth) && @usage && @desc + end + + def create_task(meth) #:nodoc: + tasks[meth.to_s] = Thor::Task.new(meth, @desc, @usage, method_options) + @usage, @desc, @method_options = nil + end + + def initialize_added #:nodoc: + class_options.merge!(method_options) + @method_options = nil + end + # Receives a task name (can be nil), and try to get a map from it. # If a map can't be found use the sent name or the default task. # - def normalize_task_name(meth) + def normalize_task_name(meth) #:nodoc: mapping = map[meth.to_s] meth = mapping || meth || default_task meth.to_s.gsub('-','_') # treat foo-bar > foo_bar end - def options_help + def options_help #:nodoc: unless self.class_options.empty? list = self.class_options.map do |_, option| [ option.usage, option.description ] @@ -236,9 +202,35 @@ def options_help puts end end + end + # Invokes a task. + # + # ==== Errors + # Thor::Error:: A Thor error is raised if the user called an undefined task + # or called an exisisting task wrongly. + # + def invoke(meth, *args) + super + rescue ArgumentError => e + backtrace = sans_backtrace(e.backtrace, caller) + + if backtrace.empty? + task = self.class[meth] + raise Error, "'#{meth}' was called incorrectly. Call as '#{task.formatted_usage(self.class)}'" + else + raise e + end + rescue NoMethodError => e + if e.message =~ /^undefined method `#{meth}' for #{Regexp.escape(self.inspect)}$/ + raise Error, "The #{self.class.namespace} namespace doesn't have a '#{meth}' task" + else + raise e + end end + include Thor::Base + map HELP_MAPPINGS => :help desc "help [TASK]", "Describe available tasks or one specific task" diff --git a/lib/thor/generator.rb b/lib/thor/generator.rb index 70c54d31e..b538a12ed 100644 --- a/lib/thor/generator.rb +++ b/lib/thor/generator.rb @@ -3,39 +3,6 @@ class Thor::Generator - # Invokes a task. - # - # ==== Errors - # ArgumentError:: raised if the arity of the called task is different from 0. - # NoMethodError:: raised if the method being invoked does not exist. - # - def invoke(meth, *args) - arity = self.method(meth).arity - raise ArgumentError, "Tasks in generators must not accept any argument, but #{meth} has arity #{arity}." if arity != 0 - super(meth) - end - - # Implement the hooks required by Thor::Base. - # - class << self - protected - def baseclass - Thor::Generator - end - - def valid_task?(meth) - public_instance_methods.include?(meth) - end - - def create_task(meth) - tasks[meth.to_s] = Thor::Task.new(meth, nil, nil, nil) - end - end - - include Thor::Base - - # Implement specific Thor::Generator logic. - # class << self # Start in generators works differently. It invokes all tasks inside the class. @@ -81,5 +48,33 @@ def help(options={}) end end + protected + + def baseclass #:nodoc: + Thor::Generator + end + + def valid_task?(meth) #:nodoc: + public_instance_methods.include?(meth) + end + + def create_task(meth) #:nodoc: + tasks[meth.to_s] = Thor::Task.new(meth, nil, nil, nil) + end + + end + + # Invokes a task. + # + # ==== Errors + # ArgumentError:: raised if the arity of the called task is different from 0. + # NoMethodError:: raised if the method being invoked does not exist. + # + def invoke(meth, *args) + arity = self.method(meth).arity + raise ArgumentError, "Tasks in generators must not accept any argument, but #{meth} has arity #{arity}." if arity != 0 + super(meth) end + + include Thor::Base end