task icon indicating copy to clipboard operation
task copied to clipboard

Use sources as variable

Open NVolcz opened this issue 2 years ago • 3 comments

For a task, is the list of files specified in sources available for commands so it can be used as a variable? I have a script that that I would like to pass the list of sources available to:

version: '3'

tasks:
  test:
    cmds:
      - echo {{.sources}}
      - ./test/shellcheck.sh {{.sources}}
    sources:
      - *.sh

Alternatively if it was possible to use vars for sources:

version: '3'

tasks:
  test:
    cmds:
      - echo {{.sources}}
      - ./test/shellcheck.sh {{.sources}}
    vars:
      SCRIPTS:
        sh: find . -iname '*.sh' -not -path './cygwin/*' | xargs echo
    sources:
      - {{.BASH_SCRIPTS}}

NVolcz avatar Dec 05 '22 11:12 NVolcz

Hi @NVolcz,

This indeed makes sense, because we already have other special variables like TASK, TIMESTAMP and CHECKSUM.

Implementation note: this variable should probably be an array (slice) because them users will have more flexibility on what to do with it (join with space or ,, loop through it, or other). TIMESTAMP is already of a different type (time.Time).

andreynering avatar Dec 06 '22 02:12 andreynering

I think this might help with a use-case I'm looking at now. I discover go.mod files and put the results into a GO_MODULES variable at the top level of Taskfile.yaml. Then there are tasks using cmds.for.var to execute something in each of the GO_MODULES directories. I would like the tasks to be able to look at sources with globs relative to each of the GO_MODULES items as each iteration of the for loops run.

edit: never mind, a colleague came up with a pattern for what I want to do that works quite well as-is:

  do-thing:singular:
    desc: Run a single thing
    requires:
      vars:
        - THING
    sources:
      - 'subdir/{{.THING}}/**/*.{go,mod,sum}'
    cmds:
      - task: gotest
        vars:
          GOTESTVAR: './subdir/{{.THING}}/...'

  do-thing:plural:
    desc: Run several things
    requires:
      vars:
        - THINGS
    cmds:
      - for: { var: THINGS }
        task: do-thing:singular
        vars:
          THING: '{{.ITEM}}'

jlucktay avatar Oct 04 '23 15:10 jlucktay

Wonder if this is the only possible solution. May I suggest:

  • having a way to define array variables by the same glob mechanism that is now possible with sources, maybe with a suitable template function.
  • having a way to use array variables for sources.

IMHO this would be more flexible since it would let any variable be usable based on filename expansion.

callegar avatar Jun 04 '24 06:06 callegar