task icon indicating copy to clipboard operation
task copied to clipboard

Feature: matrix support

Open nikaro opened this issue 1 year ago • 5 comments

Hello,

It would be nice to be able to do something like this:

env:
  APP: myapp
  CGO_ENABLED: 0
  GOARCH:
    sh: go env GOARCH
  GOOS:
    sh: go env GOOS

tasks:
  build:
    desc: Build application
    cmd: go build -o build/${APP}-${GOOS}-${GOARCH} .

  build:all:
    desc: Build for all targets
    matrix:
      ARCH: [amd64, arm64]
      OS: [linux, darwin, windows]
    env:
      GOARCH: "{{.matrix.ARCH}}"
      GOOS: "{{.matrix.OS}}"
    cmds:
      - task: build

Previous issue on the subject: https://github.com/go-task/task/issues/675

Current workaround:

  build:all:
    desc: Build for all targets
    cmds:
      - for: [linux/amd64, linux/arm64, darwin/amd64, darwin/arm64, windows/amd64, windows/arm64]
        cmd: >-
          export GOOS=$(echo {{.ITEM}} | cut -d/ -f1);
          export GOARCH=$(echo {{.ITEM}} | cut -d/ -f2);
          go build -o build/${APP}-${GOOS}-${GOARCH} .

nikaro avatar Aug 17 '24 11:08 nikaro

Hey @nikaro,

We already have for: so you can loop though variables. That would sove your use case.

https://taskfile.dev/usage/#looping-over-values

andreynering avatar Aug 19 '24 13:08 andreynering

Hello @andreynering,

You mean something like this?

  build:all:
    desc: Build for all targets
    vars:
      matrix:
        - os: linux
          arch: arm64
        - os: linux
          arch: amd64
        - os: darwin
          arch: arm64
        - os: darwin
          arch: darwin
        - os: windows
          arch: arm64
        - os: windows
          arch: amd64
    cmds:
      - for:
          var: matrix
        cmd: go build -o build/${APP}-{{.ITEM.os}}-{{.ITEM.arch}} .

This is a bit cleaner (as it avoids the | cut -d hack), but still a proper matrix support would be better in terms of readability :-)

nikaro avatar Aug 19 '24 13:08 nikaro

@andreynering I'm inclined to agree that there is a valid use-case here even if it is only to reduce a bit of repetition. Can I propose the following syntax:

  build:all:
    desc: Build for all targets
    cmds:
      - for:
          matrix:
            ARCH: [amd64, arm64]
            OS: [linux, darwin, windows]
        cmd: go build -o build/${APP}-{{.ITEM.OS}}-{{.ITEM.ARCH}} .

This way it is any extension to the existing for functionality rather than another looping keyword. This should be relatively simple to plug into the existing code.

Edit: Also related: https://github.com/go-task/task/issues/675

pd93 avatar Aug 19 '24 14:08 pd93

Yeah, thinking in it as an extension to for: makes more sense 👍

andreynering avatar Aug 19 '24 16:08 andreynering

I totally agree. I would add this is implemented like that in Github action and it works well

vmaerten avatar Aug 19 '24 17:08 vmaerten