From 6bc478ef60b818be9e6dbdce5b26890c218a8d9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Mon, 29 Jun 2009 17:42:31 +0200 Subject: [PATCH] Make pending specs pass. --- lib/thor.rb | 34 +++++++++++++++++++++++++++++----- lib/thor/base.rb | 34 ---------------------------------- lib/thor/group.rb | 17 +++++++++++++++++ lib/thor/runner.rb | 4 +++- spec/runner_spec.rb | 18 ++++++------------ 5 files changed, 55 insertions(+), 52 deletions(-) diff --git a/lib/thor.rb b/lib/thor.rb index 73d8ae1ab..ee92812a1 100644 --- a/lib/thor.rb +++ b/lib/thor.rb @@ -117,6 +117,35 @@ def method_option(name, options) build_option(name, options, scope) end + # Parses the task and options from the given args, instantiate the class + # and invoke the task. This method is used when the arguments must be parsed + # from an array. If you are inside Ruby and want to use a Thor class, you + # can simply initialize it: + # + # script = MyScript.new(args, options, config) + # script.invoke(:task, first_arg, second_arg, third_arg) + # + def start(given_args=ARGV, config={}) + config[:shell] ||= Thor::Base.shell.new + + meth = normalize_task_name(given_args.shift) + task = all_tasks[meth] + + if task + args, opts = Thor::Options.split(given_args) + config.merge!(:task_options => task.options) + else + args, opts = given_args, {} + end + + task ||= Task.dynamic(meth) + trailing = args[Range.new(arguments.size, -1)] + + new(args, opts, config).invoke(task, trailing || []) + rescue Thor::Error => e + config[:shell].error e.message + end + # Prints help information. If a task name is given, it shows information # only about the specific task. # @@ -185,11 +214,6 @@ def initialize_added #:nodoc: @method_options = nil end - def normalize_arguments(args, config) #:nodoc: - meth = normalize_task_name(args.shift) - all_tasks[meth] || Task.dynamic(meth) - 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. # diff --git a/lib/thor/base.rb b/lib/thor/base.rb index f6ec0a839..e52085678 100644 --- a/lib/thor/base.rb +++ b/lib/thor/base.rb @@ -359,33 +359,6 @@ def namespace(name=nil) end end - # Parses the task and options from the given args, instantiate the class - # and invoke the task. This method is used when the arguments must be parsed - # from an array. If you are inside Ruby and want to use a Thor class, you - # can simply initialize it: - # - # script = MyScript.new(args, options, config) - # script.invoke(:task, first_arg, second_arg, third_arg) - # - def start(given_args=ARGV, config={}) - config[:shell] ||= Thor::Base.shell.new - - task = normalize_arguments(given_args, config) - args, opts = Thor::Options.split(given_args) - - if task - trailing = args[Range.new(arguments.size, -1)] - config.merge!(:task_options => task.options) - elsif Thor::HELP_MAPPINGS.include?(given_args.first) - help(config[:shell]) - return - end - - new(args, opts, config).invoke(task, trailing || []) - rescue Thor::Error => e - config[:shell].error e.message - end - protected # Prints the class options per group. If an option does not belong to @@ -539,13 +512,6 @@ def create_task(meth) #:nodoc: # class. def initialize_added #:nodoc: end - - # SIGNATURE: Normalize arguments when invoked through start. Should - # return the name of the task to be invoked. Returning nil makes the - # start process to exist without error message. - def normalize_arguments(args, config) #:nodoc: - end - end end end diff --git a/lib/thor/group.rb b/lib/thor/group.rb index ac664a0e5..b7b7f35f7 100644 --- a/lib/thor/group.rb +++ b/lib/thor/group.rb @@ -18,6 +18,23 @@ def desc(description=nil) end end + # Start works differently in Thor::Group, it simply invokes all tasks + # inside the class. + # + def start(given_args=ARGV, config={}) + config[:shell] ||= Thor::Base.shell.new + + if Thor::HELP_MAPPINGS.include?(given_args.first) + help(config[:shell]) + return + end + + args, opts = Thor::Options.split(given_args) + new(args, opts, config).invoke + rescue Thor::Error => e + config[:shell].error e.message + end + # Prints help information. # # ==== Options diff --git a/lib/thor/runner.rb b/lib/thor/runner.rb index f2261663e..5c546b0d9 100644 --- a/lib/thor/runner.rb +++ b/lib/thor/runner.rb @@ -25,7 +25,9 @@ def help(meth=nil) def method_missing(meth, *args) meth = meth.to_s initialize_thorfiles(meth) - invoke(meth) + klass, task = Thor::Util.namespace_to_thor_class(meth) + args.unshift(task) if task + klass.start(args, _shared_config) end desc "install NAME", "Install a Thor file into your system tasks, optionally named for future updates" diff --git a/spec/runner_spec.rb b/spec/runner_spec.rb index feeb8cd5e..c19e9774e 100644 --- a/spec/runner_spec.rb +++ b/spec/runner_spec.rb @@ -52,17 +52,13 @@ end it "forwads arguments to the invoked task" do - pending do - ARGV.replace ["my_script:animal", "horse"] - Thor::Runner.start.must == ["horse"] - end + ARGV.replace ["my_script:animal", "horse"] + Thor::Runner.start.must == ["horse"] end it "invokes tasks through shortcuts" do - pending do - ARGV.replace ["my_script", "-T", "horse"] - Thor::Runner.start.must == ["horse"] - end + ARGV.replace ["my_script", "-T", "horse"] + Thor::Runner.start.must == ["horse"] end it "invokes a Thor::Group" do @@ -71,10 +67,8 @@ end it "raises an error if class/task can't be found" do - pending do - ARGV.replace ["unknown"] - capture(:stderr){ Thor::Runner.start }.must =~ /could not find Thor class or task 'unknown'/ - end + ARGV.replace ["unknown"] + capture(:stderr){ Thor::Runner.start }.must =~ /could not find Thor class or task 'unknown'/ end it "does not swallow NoMethodErrors that occur inside the called method" do