task icon indicating copy to clipboard operation
task copied to clipboard

Global preconditions

Open duboiss opened this issue 5 years ago • 9 comments

I'm new with Taskfile. I start to write my first Taskfile.yaml and I realize a serious problem : preconditions. It's a great option but from what I see we can't store a precondition so I have several duplications.

It would be nice if we can have something similar to:

# Global preconditions available for all tasks
preconditions:
    my-first-condition:
        - sh: "[ 1 = 0 ]"
        msg: "One doesn't equal Zero, Halting"

    my-second-condition:
        - sh: "[ 1 = 0 ]"
        msg: "One doesn't equal Zero, Halting"

tasks:
    my-first-task:
        # ...
        preconditions:
            - precondition: my-first-condition
            - precondition: my-second-condition

So like when you reference a task to be executed in a task. Or if you have a better idea...

duboiss avatar Nov 16 '20 19:11 duboiss

Hi @DuboisS,

This sounds like a good idea, but I have no plans to implement that soon...

andreynering avatar Dec 27 '20 14:12 andreynering

I will check this one out to see if i can add it with the code in the repo now.

@andreynering Will you accept a PR for this or would that be planned later and you can't accept a PR even if there was one?

Is so then you can assign me :)

DanyHenriquez avatar Jan 19 '21 11:01 DanyHenriquez

Hi @DanyHenriquez,

I'm not sure. This is not trivial to implement so it may need some thought first. Also, there are other priorities for now.

andreynering avatar Feb 20 '21 20:02 andreynering

@andreynering @DuboisS Based on my case, I have a project that use a lot of Taskfiles under Taskfile.d for command like task service:list ..., task project:list ..., task acount:list ..., etc and the problem is that we can't share the preconditions or even tasks between (namespaces).

So I suggest, if there will be PR for that to consider a more global preconditions like global:precondition-1, global:precondition-2 etc

Example:

version: '3'

tasks:
  build:
    deps: 
      - global:precondition-1            # << precondition 1
    cmds:
      - go build -v -i main.go
    preconditions:
       - task: global:precondition-2   # << precondition 2

chermed avatar Mar 20 '21 19:03 chermed

@andreynering Hit me up and let me know where you need help

DanyHenriquez avatar Jun 10 '21 22:06 DanyHenriquez

Cleaning up my queue. Unfollowing since i don't know the status or if help is needed. Let me know if this changes 😄

DanyHenriquez avatar Aug 02 '21 19:08 DanyHenriquez

Looks like a duplicate of #294.

marco-m-pix4d avatar Aug 09 '21 14:08 marco-m-pix4d

There is a way to reuse preconditions by the use of YAML anchors:

tasks:

  task1:
    cmds:
      - "echo task1"
    preconditions:
      - &test-precondition
        sh: "[ 1 = 0 ]"
        msg: "One doesn't equal Zero, Halting"

  task2:
    cmds:
      - "echo task2"
    preconditions:
      - *test-precondition

  task3:
    cmds:
      - "echo task3"
    preconditions:
      - *test-precondition

The anchor have to be on the same YAML file. Adding global preconditions (e.g. #294) would only reduse duplications, if they are used on multiple imported taskfiles.

However we could reference YAML anchors in the documentation :+1:

sylv-io avatar Oct 06 '21 08:10 sylv-io

Since we do not use strict YAML unmarshal, even this is possible:

preconditions:
  <<: &test-precondition
    sh: "[ 1 = 0 ]"
    msg: "One doesn't equal Zero, Halting"

tasks:

  task1:
    cmds:
      - "echo task1"
    preconditions:
      - *test-precondition

  task2:
    cmds:
      - "echo task2"
    preconditions:
      - *test-precondition

  task3:
    cmds:
      - "echo task3"
    preconditions:
      - *test-precondition

sylv-io avatar Oct 06 '21 08:10 sylv-io

Duplicate of #294

pd93 avatar Oct 15 '22 02:10 pd93