ruby / ruby/rake

How to determine whether any prerequisite tasks needed to run at the time they were defined?

Open
#270 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Ruby
Stars
2.5k
Forks
650
Avg merge
6m
Merged PRs (30d)
3

Description

I have a use-case in which I need to run a task if and only if at least one of its prerequisite tasks either (1) currently needs to run or (2) needed to run at the time the prerequisite task was defined. Here are some attempts at a Rake::Task subclass:

class MyTask < Rake::Task
  def needed?
    prerequisite_tasks.any?(&:needed?)
  end
end

This approach does not work, since at the time MyTask#needed? is called, prerequisite tasks may have run and therefore return false for #needed?.

class MyTask < Rake::Task
  def needed?
    prerequisite_tasks.any? { |p| p.needed? or p.already_invoked }
  end
end

This also doesn't work, since #already_invoked can return true [even if the task's #execute method was never called]:

          @already_invoked = true


          invoke_prerequisites(task_args, new_chain)
          execute(task_args) if needed?

What ended up working was:

class MyTask < Rake::Task
  def initialize(*)
    @needed = false
    super
  end
  
  def enhance(*)
    super.tap { @needed |= prerequisite_tasks.any?(&:needed?) }
  end

  def needed?
    @needed
  end
end

But that just feels... wrong. Is there something I'm missing here? I'd be more than happy to open a PR implementing what I'm looking for (maybe an #already_executed method?) if this functionality doesn't already exist.

Thanks!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Rake::Task entry points shown in the issue: #needed?, #already_invoked, #execute, #enhance, and invoke_prerequisites. Trace when each state changes and determine how to distinguish a prerequisite that was actually executed from one that was only invoked. Done means the behavior is specified and covered by tests for both cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
build-system
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.