Permalink
Browse files

Revert "Revert "Update to Thor 0.15.2.rc""

This reverts commit 5f0c751.
  • Loading branch information...
1 parent 5f0c751 commit 71f989fc166f5c51f8df7d74f490007354d9a99b @indirect indirect committed Sep 19, 2011
View
@@ -5,7 +5,7 @@ class << self
# Sets the default task when thor is executed without an explicit task to be called.
#
# ==== Parameters
- # meth<Symbol>:: name of the defaut task
+ # meth<Symbol>:: name of the default task
#
def default_task(meth=nil)
case meth
@@ -108,6 +108,8 @@ def method_options(options=nil)
@method_options
end
+ alias options method_options
+
# Adds an option to the set of method options. If :for is given as option,
# it allows you to change the options from a previous defined task.
#
@@ -143,6 +145,8 @@ def method_option(name, options={})
build_option(name, options, scope)
end
+ alias option method_option
+
# Prints help information for the given task.
#
# ==== Parameters
@@ -202,7 +206,11 @@ def subcommands
def subcommand(subcommand, subcommand_class)
self.subcommands << subcommand.to_s
subcommand_class.subcommand_help subcommand
- define_method(subcommand) { |*args| invoke subcommand_class, args }
+
+ define_method(subcommand) do |*args|
+ args, opts = Thor::Arguments.split(args)
+ invoke subcommand_class, args, opts
+ end
end
# Extend check unknown options to accept a hash of conditions.
@@ -259,8 +267,11 @@ def dispatch(meth, given_args, given_opts, config) #:nodoc:
opts = given_opts || opts || []
config.merge!(:current_task => task, :task_options => task.options)
+ instance = new(args, opts, config)
+ yield instance if block_given?
+ args = instance.args
trailing = args[Range.new(arguments.size, -1)]
- new(args, opts, config).invoke_task(task, trailing || [])
+ instance.invoke_task(task, trailing || [])
end
# The banner for this class. You can customize it if you are invoking the
@@ -300,43 +311,52 @@ def initialize_added #:nodoc:
# Retrieve the task name from given args.
def retrieve_task_name(args) #:nodoc:
meth = args.first.to_s unless args.empty?
-
if meth && (map[meth] || meth !~ /^\-/)
args.shift
else
nil
end
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.
+ # receives a (possibly nil) task name and returns a name that is in
+ # the tasks hash. In addition to normalizing aliases, this logic
+ # will determine if a shortened command is an unambiguous prefix of
+ # a task or alias.
+ #
+ # +normalize_task_name+ also converts names like +animal-prison+
+ # into +animal_prison+.
def normalize_task_name(meth) #:nodoc:
- meth = map[meth.to_s] || find_subcommand_and_update_argv(meth) || meth || default_task
- meth.to_s.gsub('-','_') # treat foo-bar > foo_bar
- end
-
- # terrible hack that overwrites ARGV
- def find_subcommand_and_update_argv(subcmd_name) #:nodoc:
- return unless subcmd_name
- cmd = find_subcommand(subcmd_name)
- ARGV[0] = cmd if cmd
- cmd
- end
+ return default_task.to_s.gsub('-', '_') unless meth
- def find_subcommand(subcmd_name)
- possibilities = find_subcommand_possibilities subcmd_name
+ possibilities = find_task_possibilities(meth)
if possibilities.size > 1
- raise "Ambiguous subcommand #{subcmd_name} matches [#{possibilities.join(', ')}]"
+ raise ArgumentError, "Ambiguous task #{meth} matches [#{possibilities.join(', ')}]"
elsif possibilities.size < 1
- return nil
+ meth = meth || default_task
+ elsif map[meth]
+ meth = map[meth]
+ else
+ meth = possibilities.first
end
- possibilities.first
+ meth.to_s.gsub('-','_') # treat foo-bar as foo_bar
end
- def find_subcommand_possibilities(subcmd_name)
- len = subcmd_name.length
- all_tasks.map {|t| t.first}.select { |n| subcmd_name == n[0, len] }
+ # this is the logic that takes the task name passed in by the user
+ # and determines whether it is an unambiguous prefix of a task or
+ # alias name.
+ def find_task_possibilities(meth)
+ len = meth.to_s.length
+ possibilities = all_tasks.merge(map).keys.select { |n| meth == n[0, len] }.sort
+ unique_possibilities = possibilities.map { |k| map[k] || k }.uniq
+
+ if possibilities.include?(meth)
+ [meth]
+ elsif unique_possibilities.size == 1
+ unique_possibilities
+ else
+ possibilities
+ end
end
def subcommand_help(cmd)
@@ -41,7 +41,7 @@ def invoke!
invoke_with_conflict_check do
FileUtils.mkdir_p(File.dirname(destination))
# Create a symlink by default
- config[:symbolic] ||= true
+ config[:symbolic] = true if config[:symbolic].nil?
File.unlink(destination) if exists?
if config[:symbolic]
File.symlink(render, destination)
@@ -187,7 +187,7 @@ def append_to_file(path, *args, &block)
#
# ==== Examples
#
- # inject_into_class "app/controllers/application_controller.rb", " filter_parameter :password\n"
+ # inject_into_class "app/controllers/application_controller.rb", ApplicationController, " filter_parameter :password\n"
#
# inject_into_class "app/controllers/application_controller.rb", ApplicationController do
# " filter_parameter :password\n"
@@ -19,7 +19,7 @@ class Thor
action add_file create_file in_root inside run run_ruby_script)
module Base
- attr_accessor :options
+ attr_accessor :options, :parent_options, :args
# It receives arguments in an Array and two hashes, one for options and
# other for configuration.
@@ -38,22 +38,43 @@ module Base
# config<Hash>:: Configuration for this Thor class.
#
def initialize(args=[], options={}, config={})
- args = Thor::Arguments.parse(self.class.arguments, args)
- args.each { |key, value| send("#{key}=", value) }
-
parse_options = self.class.class_options
+ # The start method splits inbound arguments at the first argument
+ # that looks like an option (starts with - or --). It then calls
+ # new, passing in the two halves of the arguments Array as the
+ # first two parameters.
+
if options.is_a?(Array)
task_options = config.delete(:task_options) # hook for start
parse_options = parse_options.merge(task_options) if task_options
array_options, hash_options = options, {}
else
+ # Handle the case where the class was explicitly instantiated
+ # with pre-parsed options.
array_options, hash_options = [], options
end
+ # Let Thor::Options parse the options first, so it can remove
+ # declared options from the array. This will leave us with
+ # a list of arguments that weren't declared.
opts = Thor::Options.new(parse_options, hash_options)
self.options = opts.parse(array_options)
+
+ # If unknown options are disallowed, make sure that none of the
+ # remaining arguments looks like an option.
opts.check_unknown! if self.class.check_unknown_options?(config)
+
+ # Add the remaining arguments from the options parser to the
+ # arguments passed in to initialize. Then remove any positional
+ # arguments declared using #argument (this is primarily used
+ # by Thor::Group). Tis will leave us with the remaining
+ # positional arguments.
+ thor_args = Thor::Arguments.new(self.class.arguments)
+ thor_args.parse(args + opts.remaining).each { |k,v| send("#{k}=", v) }
+ args = thor_args.remaining
+
+ @args = args
end
class << self
@@ -405,8 +426,8 @@ def public_task(*names)
end
end
- def handle_no_task_error(task) #:nodoc:
- if $thor_runner
+ def handle_no_task_error(task, has_namespace = $thor_runner) #:nodoc:
+ if has_namespace
raise UndefinedTaskError, "Could not find task #{task.inspect} in #{namespace.inspect} namespace."
else
raise UndefinedTaskError, "Could not find task #{task.inspect}."
@@ -506,6 +527,7 @@ def find_and_refresh_task(name) #:nodoc:
# and file into baseclass.
def inherited(klass)
Thor::Base.register_klass_file(klass)
+ klass.instance_variable_set(:@no_tasks, false)
end
# Fire this callback whenever a method is added. Added methods are
@@ -187,9 +187,9 @@ def get_options_from_invocations(group_options, base_options) #:nodoc:
human_name = value.respond_to?(:classify) ? value.classify : value
group_options[human_name] ||= []
- group_options[human_name] += klass.class_options.values.select do |option|
- base_options[option.name.to_sym].nil? && option.group.nil? &&
- !group_options.values.flatten.any? { |i| i.name == option.name }
+ group_options[human_name] += klass.class_options.values.select do |class_option|
+ base_options[class_option.name.to_sym].nil? && class_option.group.nil? &&
+ !group_options.values.flatten.any? { |i| i.name == class_option.name }
end
yield klass if block_given?
@@ -220,10 +220,14 @@ def dispatch(task, given_args, given_opts, config) #:nodoc:
args, opts = Thor::Options.split(given_args)
opts = given_opts || opts
+ instance = new(args, opts, config)
+ yield instance if block_given?
+ args = instance.args
+
if task
- new(args, opts, config).invoke_task(all_tasks[task])
+ instance.invoke_task(all_tasks[task])
else
- new(args, opts, config).invoke_all
+ instance.invoke_all
end
end
@@ -106,7 +106,9 @@ def invoke(name=nil, *args)
raise "Expected Thor class, got #{klass}" unless klass <= Thor::Base
args, opts, config = _parse_initialization_options(args, opts, config)
- klass.send(:dispatch, task, args, opts, config)
+ klass.send(:dispatch, task, args, opts, config) do |instance|
+ instance.parent_options = options
+ end
end
# Invoke the given task if the given args.
@@ -49,6 +49,10 @@ def parse(args)
@assigns
end
+ def remaining
+ @pile
+ end
+
private
def no_or_skip?(arg)
@@ -38,7 +38,7 @@ def initialize(hash_options={}, defaults={})
@non_assigned_required.delete(hash_options[key])
end
- @shorts, @switches, @unknown = {}, {}, []
+ @shorts, @switches, @extra = {}, {}, []
options.each do |option|
@switches[option.switch_name] = option
@@ -49,14 +49,19 @@ def initialize(hash_options={}, defaults={})
end
end
+ def remaining
+ @extra
+ end
+
def parse(args)
@pile = args.dup
while peek
match, is_switch = current_is_switch?
+ shifted = shift
if is_switch
- case shift
+ case shifted
when SHORT_SQ_RE
unshift($1.split('').map { |f| "-#{f}" })
next
@@ -71,9 +76,10 @@ def parse(args)
option = switch_option(switch)
@assigns[option.human_name] = parse_peek(switch, option)
elsif match
- @unknown << shift
+ @extra << shifted
+ @extra << shift while peek && peek !~ /^-/
else
- shift
+ @extra << shifted
end
end
@@ -85,9 +91,9 @@ def parse(args)
end
def check_unknown!
- unless ARGV.include?("exec") || ARGV.include?("config")
- raise UnknownArgumentError, "Unknown switches '#{@unknown.join(', ')}'" unless @unknown.empty?
- end
+ # an unknown option starts with - or -- and has no more --'s afterward.
+ unknown = @extra.select { |str| str =~ /^--?(?:(?!--).)*$/ }
+ raise UnknownArgumentError, "Unknown switches '#{unknown.join(', ')}'" unless unknown.empty?
end
protected
@@ -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
@@ -43,7 +46,8 @@ def task(*args, &block)
description << task.arg_names.map{ |n| n.to_s.upcase }.join(' ')
description.strip!
- klass.desc description, task.comment || non_namespaced_name
+ klass.desc description, Rake.application.last_description || non_namespaced_name
+ Rake.application.last_description = nil
klass.send :define_method, non_namespaced_name do |*args|
Rake::Task[task.name.to_sym].invoke(*args)
end
@@ -52,15 +56,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
+
Oops, something went wrong.

0 comments on commit 71f989f

Please sign in to comment.