Skip to content

Commit

Permalink
Merge pull request #269 from take-cheeze/order_only
Browse files Browse the repository at this point in the history
Add order only dependency.
  • Loading branch information
hsbt authored Aug 17, 2019
2 parents 7eff2ab + 449766f commit 5e6c6f4
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 17 deletions.
18 changes: 17 additions & 1 deletion lib/rake/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ class Task
attr_reader :prerequisites
alias prereqs 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 @@ -56,7 +59,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 @@ -105,6 +108,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 @@ -360,6 +364,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 @@ -109,7 +109,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 @@ -122,7 +122,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 @@ -134,7 +137,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 @@ -145,9 +148,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 # :nodoc:

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

0 comments on commit 5e6c6f4

Please sign in to comment.