# https://taskfile.dev

version: "3"

env:
  CGO_ENABLED: 0
  GOEXPERIMENT: greenteagc

tasks:
  lint:install:
    desc: Install golangci-lint
    cmds:
      - go install github.com/golangci/golangci-lint/v2/cmd/golangci-lint@latest
    env:
      GOTOOLCHAIN: go1.25.0

  lint:
    desc: Run base linters
    cmds:
      - golangci-lint run --path-mode=abs --config=".golangci.yml" --timeout=5m
    env:
      GOEXPERIMENT: null

  lint-fix:
    desc: Run base linters and fix issues
    cmds:
      - golangci-lint run --path-mode=abs --config=".golangci.yml" --timeout=5m --fix
    env:
      GOEXPERIMENT: null

  build:
    desc: Run build
    cmds:
      - go build .
    generates:
      - crush

  run:
    desc: Run build
    cmds:
      - go run .

  test:
    desc: Run tests
    cmds:
      - go test ./... {{.CLI_ARGS}}

  fmt:
    desc: Run gofumpt
    cmds:
      - gofumpt -w .

  dev:
    desc: Run with profiling enabled
    env:
      CRUSH_PROFILE: true
    cmds:
      - go run .

  install:
    desc: Install the application
    cmds:
      - go install -v .

  profile:cpu:
    desc: 10s CPU profile
    cmds:
      - go tool pprof -http :6061 'http://localhost:6060/debug/pprof/profile?seconds=10'

  profile:heap:
    desc: Heap profile
    cmds:
      - go tool pprof -http :6061 'http://localhost:6060/debug/pprof/heap'

  profile:allocs:
    desc: Allocations profile
    cmds:
      - go tool pprof -http :6061 'http://localhost:6060/debug/pprof/allocs'

  schema:
    desc: Generate JSON schema for configuration
    cmds:
      - go run main.go schema > schema.json
      - echo "Generated schema.json"
    generates:
      - schema.json

  release:
    desc: Create and push a new tag following semver
    vars:
      NEXT:
        sh: go run github.com/caarlos0/svu/v3@latest next --always
    prompt: "This will release {{.NEXT}}. Please make sure you've fetch tags. Continue?"
    preconditions:
      - sh: '[ $(git symbolic-ref --short HEAD) = "main" ]'
        msg: Not on main branch
      - sh: "[ $(git status --porcelain=2 | wc -l) = 0 ]"
        msg: "Git is dirty"
    cmds:
      - git commit --allow-empty -m "{{.NEXT}}"
      - git tag -d nightly
      - git tag --annotate --sign {{.NEXT}} {{.CLI_ARGS}}
      - echo "Pushing {{.NEXT}}..."
      - git push origin --tags
