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

[feature] add conditional tasks #1666

Open
piotrkowalczuk opened this issue Sep 29, 2024 · 1 comment
Open

[feature] add conditional tasks #1666

piotrkowalczuk opened this issue Sep 29, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@piotrkowalczuk
Copy link

piotrkowalczuk commented Sep 29, 2024

Is your feature request related to a problem? Please describe.
I want to be able to skip a task if the codebase does not meet the given criteria at the execution time. For example, in Go, one might want to create a task that executes go generate. The output files are known and should be defined. What's unknown is whether the given project has any //go:generate annotation in its codebase.

tasks:
  generate:
    command: 'go generate ./...'
    inputs:
      - '**/*.go'
      - 'go.mod'
      - '/go.mod'
    outputs:
      - '**/*_templ.go'

This task will produce error if run against Go project without any //go:generate templ generate ./... annotation:

Error: task_runner::missing_outputs

  × Task foo-bar:generate defines outputs but after being ran, either none or not all of them exist.
  │ If you require optional outputs, try using glob patterns instead.

Describe the solution you'd like

Having additional property condition in the Task schema. Value of this field could a simple bash script.

Bash

tasks:
  generate:
    condition: |
      if output=$(go generate -n ./... 2>&1 1>/dev/null); then
        if [[ -n "$output" ]]; then
            exit 1
        else
            exit 0
        fi
      fi

    command: 'go generate ./...'
    inputs:
      - '**/*.go'
      - 'go.mod'
      - '/go.mod'
    outputs:
      - '**/*.pb.go'
      - '**/*_templ.go'

Describe alternatives you've considered

Additional context

@piotrkowalczuk piotrkowalczuk added the enhancement New feature or request label Sep 29, 2024
@chadrik
Copy link
Contributor

chadrik commented Dec 27, 2024

Note that go-task has this feature and it's pretty handy: https://taskfile.dev/usage/#using-programmatic-checks-to-indicate-a-task-is-up-to-date

I needed this feature because the output of my task is outside of the repo, so I can't use outputs. When I was using task I wrote a simple existence check for the output directory and the task would be skipped if it existed. With moon, I have to write a wrapper script around the command that I want to execute to do the check and bail early if the output directory exists.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Development

No branches or pull requests

2 participants