Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add order only dependency. #269

Merged
merged 1 commit into from
Aug 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion lib/rake/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ class Task
# List of prerequisites for a task.
attr_reader :prerequisites

# List of order only prerequisites for a task.
attr_reader :order_only_prerequisites

# List of actions attached to a task.
attr_reader :actions

Expand Down Expand Up @@ -55,7 +58,7 @@ def sources

# List of prerequisite tasks
def prerequisite_tasks
prerequisites.map { |pre| lookup_prerequisite(pre) }
(prerequisites + order_only_prerequisites).map { |pre| lookup_prerequisite(pre) }
end

def lookup_prerequisite(prerequisite_name) # :nodoc:
Expand Down Expand Up @@ -104,6 +107,7 @@ def initialize(task_name, app)
@arg_names = nil
@locations = []
@invocation_exception = nil
@order_only_prerequisites = []
end

# Enhance a task with prerequisites or actions. Returns self.
Expand Down Expand Up @@ -358,6 +362,18 @@ def investigation
return result
end

# Format dependencies parameter to pass to task.
def self.format_deps(deps)
deps = [deps] unless deps.respond_to?(:to_ary)
deps.map { |d| Rake.from_pathname(d).to_s }
end

# Add order only dependencies.
def |(deps)
@order_only_prerequisites |= Task.format_deps(deps) - @prerequisites
self
end

# ----------------------------------------------------------------
# Rake Module Methods
#
Expand Down
24 changes: 14 additions & 10 deletions lib/rake/task_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ def initialize # :nodoc:
end

def create_rule(*args, &block) # :nodoc:
pattern, args, deps = resolve_args(args)
pattern, args, deps, order_only = resolve_args(args)
pattern = Regexp.new(Regexp.quote(pattern) + "$") if String === pattern
@rules << [pattern, args, deps, block]
@rules << [pattern, args, deps, order_only, block]
end

def define_task(task_class, *args, &block) # :nodoc:
task_name, arg_names, deps = resolve_args(args)
task_name, arg_names, deps, order_only = resolve_args(args)

original_scope = @scope
if String === task_name and
Expand All @@ -31,15 +31,15 @@ def define_task(task_class, *args, &block) # :nodoc:
end

task_name = task_class.scope_name(@scope, task_name)
deps = [deps] unless deps.respond_to?(:to_ary)
deps = deps.map { |d| Rake.from_pathname(d).to_s }
task = intern(task_class, task_name)
task.set_arg_names(arg_names) unless arg_names.empty?
if Rake::TaskManager.record_task_metadata
add_location(task)
task.add_description(get_description(task))
end
task.enhance(deps, &block)
task.enhance(Task.format_deps(deps), &block)
task | order_only unless order_only.nil?
task
ensure
@scope = original_scope
end
Expand Down Expand Up @@ -108,7 +108,7 @@ def resolve_args_without_dependencies(args)
else
arg_names = args
end
[task_name, arg_names, []]
[task_name, arg_names, [], nil]
end
private :resolve_args_without_dependencies

Expand All @@ -121,7 +121,10 @@ def resolve_args_without_dependencies(args)
# task :t, [a] => [:d]
#
def resolve_args_with_dependencies(args, hash) # :nodoc:
fail "Task Argument Error" if hash.size != 1
fail "Task Argument Error" if
hash.size != 1 &&
(hash.size != 2 || !hash.key?(:order_only))
order_only = hash.delete(:order_only)
key, value = hash.map { |k, v| [k, v] }.first
if args.empty?
task_name = key
Expand All @@ -133,7 +136,7 @@ def resolve_args_with_dependencies(args, hash) # :nodoc:
deps = value
end
deps = [deps] unless deps.respond_to?(:to_ary)
[task_name, arg_names, deps]
[task_name, arg_names, deps, order_only]
end
private :resolve_args_with_dependencies

Expand All @@ -144,9 +147,10 @@ def resolve_args_with_dependencies(args, hash) # :nodoc:
def enhance_with_matching_rule(task_name, level=0)
fail Rake::RuleRecursionOverflowError,
"Rule Recursion Too Deep" if level >= 16
@rules.each do |pattern, args, extensions, block|
@rules.each do |pattern, args, extensions, order_only, block|
if pattern && pattern.match(task_name)
task = attempt_rule(task_name, pattern, args, extensions, block, level)
task | order_only unless order_only.nil?
return task if task
end
end
Expand Down
12 changes: 6 additions & 6 deletions test/test_rake_task_manager_argument_resolution.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
class TestRakeTaskManagerArgumentResolution < Rake::TestCase

def test_good_arg_patterns
assert_equal [:t, [], []], task(:t)
assert_equal [:t, [], [:x]], task(t: :x)
assert_equal [:t, [], [:x, :y]], task(t: [:x, :y])
assert_equal [:t, [], [], nil], task(:t)
assert_equal [:t, [], [:x], nil], task(t: :x)
assert_equal [:t, [], [:x, :y], nil], task(t: [: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])
assert_equal [:t, [:a, :b], [], nil], task(:t, [:a, :b])
assert_equal [:t, [:a, :b], [:x], nil], task(:t, [:a, :b] => :x)
assert_equal [:t, [:a, :b], [:x, :y], nil], task(:t, [:a, :b] => [:x, :y])
end

def task(*args)
Expand Down
18 changes: 18 additions & 0 deletions test/test_rake_test_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,22 @@ def test_task_prerequisites_deps
task = Rake::Task[:child]
assert_includes task.prerequisites, "parent"
end

def test_task_order_only_prerequisites
t = task(a: 'b') {
:aaa
} | 'c'
b, c = task('b'), task('c')
assert_equal ['b'], t.prerequisites
assert_equal ['c'], t.order_only_prerequisites
assert_equal [b, c], t.prerequisite_tasks
end

def test_task_order_only_prerequisites_key
t = task 'a' => 'b', order_only: ['c']
b, c = task('b'), task('c')
assert_equal ['b'], t.prerequisites
assert_equal ['c'], t.order_only_prerequisites
assert_equal [b, c], t.prerequisite_tasks
end
end