From 449766fb9abbfb9d8365ff824952cd1aa9067683 Mon Sep 17 00:00:00 2001 From: take-cheeze Date: Thu, 21 Jun 2018 20:12:09 +0900 Subject: [PATCH] Add order only dependency. --- lib/rake/task.rb | 18 +++++++++++++- lib/rake/task_manager.rb | 24 +++++++++++-------- ...t_rake_task_manager_argument_resolution.rb | 12 +++++----- test/test_rake_test_task.rb | 18 ++++++++++++++ 4 files changed, 55 insertions(+), 17 deletions(-) diff --git a/lib/rake/task.rb b/lib/rake/task.rb index c7e0a1d9e..cde41c636 100644 --- a/lib/rake/task.rb +++ b/lib/rake/task.rb @@ -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 @@ -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: @@ -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. @@ -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 # diff --git a/lib/rake/task_manager.rb b/lib/rake/task_manager.rb index c1e60b95e..88a2e6bb3 100644 --- a/lib/rake/task_manager.rb +++ b/lib/rake/task_manager.rb @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/test/test_rake_task_manager_argument_resolution.rb b/test/test_rake_task_manager_argument_resolution.rb index c07be6f5e..bc4943613 100644 --- a/test/test_rake_task_manager_argument_resolution.rb +++ b/test/test_rake_task_manager_argument_resolution.rb @@ -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) diff --git a/test/test_rake_test_task.rb b/test/test_rake_test_task.rb index 396e05924..6a8dd2a4a 100644 --- a/test/test_rake_test_task.rb +++ b/test/test_rake_test_task.rb @@ -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