Skip to content

Commit

Permalink
introduced the new args format
Browse files Browse the repository at this point in the history
  • Loading branch information
jimweirich committed Sep 1, 2008
1 parent 5af487a commit f0df74a
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 13 deletions.
68 changes: 55 additions & 13 deletions lib/rake.rb
Expand Up @@ -1696,23 +1696,65 @@ def synthesize_file_task(task_name)
# Resolve the arguments for a task/rule. Returns a triplet of
# [task_name, arg_name_list, prerequisites].
def resolve_args(args)
if args.last.is_a?(Hash)
deps = args.pop
resolve_args_with_dependencies(args, deps)
else
resolve_args_without_dependencies(args)
end
end

# Resolve task arguments for a task or rule when there are no
# dependencies declared.
#
# The patterns recognized by this argument resolving function are:
#
# task :t
# task :t, [:a]
# task :t, :a (deprecated)
#
def resolve_args_without_dependencies(args)
task_name = args.shift
arg_names = args #.map { |a| a.to_sym }
needs = []
if task_name.is_a?(Hash)
hash = task_name
task_name = hash.keys[0]
needs = hash[task_name]
if args.size == 1 && args.first.respond_to?(:to_ary)
arg_names = args.first.to_ary
else
arg_names = args
end
if arg_names.last.is_a?(Hash)
hash = arg_names.pop
needs = hash[:needs]
fail "Unrecognized keys in task hash: #{hash.keys.inspect}" if hash.size > 1
[task_name, arg_names, []]
end
private :resolve_args_without_dependencies

# Resolve task arguments for a task or rule when there are
# dependencies declared.
#
# The patterns recognized by this argument resolving function are:
#
# task :t => [:d]
# task :t, [a] => [:d]
# task :t, :needs => [:d] (deprecated)
# task :t, :a, :needs => [:d] (deprecated)
#
def resolve_args_with_dependencies(args, hash) # :nodoc:
fail "Task Argument Error" if hash.size != 1
key, value = hash.map { |k, v| [k,v] }.first
if args.empty?
task_name = key
arg_names = []
deps = value
elsif key == :needs
task_name = args.shift
arg_names = args
deps = value
else
task_name = args.shift
arg_names = key
deps = value
end
needs = [needs] unless needs.respond_to?(:to_ary)
[task_name, arg_names, needs]
deps = [deps] unless deps.respond_to?(:to_ary)
[task_name, arg_names, deps]
end

private :resolve_args_with_dependencies

# If a rule can be found that matches the task name, enhance the
# task with the prerequisites and actions from the rule. Set the
# source attribute of the task appropriately for the rule. Return
Expand Down
22 changes: 22 additions & 0 deletions test/test_task_manager.rb
Expand Up @@ -146,3 +146,25 @@ def test_correctly_scoped_prerequisites_are_invoked
end

end

class TestTaskManagerArgumentResolution < Test::Unit::TestCase
def test_args
assert_equal [:t, [], []], task(:t)
assert_equal [:t, [], [:x]], task(:t => :x)
assert_equal [:t, [], [:x, :y]], task(:t => [:x, :y])

assert_equal [:t, [:a, :b], []], task(:t, :a, :b)
assert_equal [:t, [], [:x]], task(:t, :needs => :x)
assert_equal [:t, [:a, :b], [:x]], task(:t, :a, :b, :needs => :x)
assert_equal [:t, [:a, :b], [:x, :y]], task(:t, :a, :b, :needs => [:x, :y])

assert_equal [:t, [:a, :b], []], task(:t, [:a, :b])
assert_equal [:t, [:a, :b], [:x]], task(:t, [:a, :b] => :x)
assert_equal [:t, [:a, :b], [:x, :y]], task(:t, [:a, :b] => [:x, :y])
end

def task(*args)
tm = TaskManager.new
tm.resolve_args(args)
end
end

0 comments on commit f0df74a

Please sign in to comment.