Salta ai contenuti

Task (go-task) - Cheatsheet Modern Task Runner

Task (go-task) - Cheatsheet Modern Task Runner

Task (a.k.a. go-task) è un task runner e build tool che mira ad essere una Make alternativa più semplice. Definisci task in un file YAML Taskfile.yml — con dipendenze, variabili, includes, e up-to-date checks — ed eseguili con task <name>. È scritto in Go e viene fornito come un singolo binario dependency-free, con comportamento cross-platform e built-in file watching. È una scelta forte quando il tuo team preferisce YAML sopra la sintassi di Make.

Installazione

PiattaformaComando
Scriptsh -c "$(curl -sSL https://taskfile.dev/install.sh)"
macOS/Linux (Homebrew)brew install go-task
Arch Linuxsudo pacman -S go-task
Go installgo install github.com/go-task/task/v3/cmd/task@latest
Windows (Scoop)scoop install task
Verificatask --version

Un Taskfile di Base

# Taskfile.yml
version: ''3''

vars:
  BINARY: myapp

tasks:
  build:
    desc: Build il binario
    cmds:
      - go build -o {{.BINARY}} ./cmd/app

  test:
    desc: Esegui test
    cmds:
      - go test ./...

  ci:
    desc: Lint, test, build
    deps: [test]
    cmds:
      - task: build

Esecuzione di Task

ComandoDescrizione
taskEsegui il task default
task buildEsegui un task nominato
task test buildEsegui parecchi task in ordine
task --list (-l)Lista task con descrizioni
task --list-allLista tutti i task (incl. no-desc)
task --summary buildMostra i dettagli di un task
task VAR=value buildPassa una variabile
task -- arg1 arg2Forward CLI args al task ({{.CLI_ARGS}})

Dipendenze & Ordinamento

KeyComportamento
deps: [a, b]Esegui a e b (in parallelo) prima di questo task
cmds: [{task: x}]Chiama un altro task come passo (sequenziale)
deps parallelismLe dipendenze eseguono concorrentemente di default
--parallelEsegui i task dati in parallelo

Up-to-Date Checks (Skip Unneeded Work)

tasks:
  build:
    sources:
      - "**/*.go"
    generates:
      - myapp
    cmds:
      - go build -o myapp ./cmd/app
KeyEffetto
sourcesInput a fingerprint
generatesExpected output
`method: checksumtimestamp
status: [ ... ]Custom shell checks per up-to-date

Se i sources sono unchancged, Task salta il comando.

Variabili & Templating

FeatureEsempio
Task/global varsvars: { NAME: value }
Env varsenv: { CGO_ENABLED: "0" }
Dotenvdotenv: [''.env'']
Templating{{.NAME}}, {{.CLI_ARGS}}, Go template funcs
Dynamic varssh: per computare una var da un comando

Includes & Watching

FeatureCome
Includesincludes: { docs: ./docs/Taskfile.yml }task docs:build
NamespacingI task inclusi vengono chiamati namespace:task
Watch modetask --watch build (rerun su source changes)
-wShort form di --watch

Workflow Comuni

# Scopri cosa il progetto può fare
task --list

# Esegui CI localmente esattamente come la pipeline fa
task ci

# Itera: ricostruisci su ogni source change
task --watch build

# Pass through arguments
task test -- -run TestLogin

Task vs Make vs just

AspettoTaskMakejust
SyntaxYAMLMakefilejust recipes
LanguageGo (single binary)C (ubiquitous)Rust
Up-to-date checksYes (sources/generates)Yes (timestamps)No (command runner)
File watchingBuilt-in (--watch)NoNo (pair con watchexec)
Best forYAML fans, cross-platformClassic build graphsSimple command running

Vedi anche just per un command-runner alternativa e watchexec per standalone file watching.

Risorse