# SPDX-FileCopyrightText: Amolith <amolith@secluded.site>
#
# SPDX-License-Identifier: CC0-1.0

version: "3"

vars:
  VERSION:
    sh: git describe --tags --always 2>/dev/null || echo "v0.0.0"
  GOOS:
    sh: go env GOOS
  GOARCH:
    sh: go env GOARCH

env:
  CGO_ENABLED: 0

tasks:
  default:
    desc: Run all checks
    cmds:
      - task: fmt
      - task: lint
      - task: staticcheck
      - task: test
      - task: vuln
      - task: reuse

  fmt:
    desc: Format all Go source code
    cmds:
      - go install mvdan.cc/gofumpt@latest
      - gofumpt -l -w .

  lint:
    desc: Lint Go source code
    cmds:
      - golangci-lint run

  staticcheck:
    desc: Perform static analysis
    cmds:
      - go install honnef.co/go/tools/cmd/staticcheck@latest
      - staticcheck ./...

  test:
    desc: Run tests
    cmds:
      - go test -v ./...

  vuln:
    desc: Check for vulnerabilities
    cmds:
      - go install golang.org/x/vuln/cmd/govulncheck@latest
      - govulncheck ./...

  reuse:
    desc: Lint licenses and copyright headers
    cmds:
      - reuse lint

  build:
    desc: Build formatted-commit
    cmds:
      - go build -o formatted-commit -ldflags "-s -w -X main.version={{.VERSION}}"
    generates:
      - formatted-commit

  install:
    desc: Install formatted-commit
    cmds:
      - go install -ldflags "-s -w -X main.version={{.VERSION}}"

  run:
    desc: Run formatted-commit
    cmds:
      - go run -ldflags "-s -w -X main.version={{.VERSION}}" . {{.CLI_ARGS}}

  pack:
    desc: Pack formatted-commit with UPX
    cmds:
      - upx --best -qo formatted-commit.min formatted-commit
      - mv formatted-commit.min formatted-commit
    sources:
      - formatted-commit

  clean:
    desc: Remove build artifacts
    cmds:
      - rm -rf formatted-commit

  clean-all:
    desc: Remove build artifacts and config.toml
    cmds:
      - rm -rf formatted-commit config.toml

  release:
    desc: Interactive release workflow
    vars:
      BUMP:
        sh: |
          opts="major minor patch prerelease"
          if git describe --tags --abbrev=0 2>/dev/null | grep -qE '\-[a-zA-Z]+\.[0-9]+$'; then
            opts="$opts graduate"
          fi
          gum choose $opts
      NEXT:
        sh: |
          case "{{.BUMP}}" in
            graduate) svu next ;;
            prerelease)
              suffix=$(gum input --placeholder "Suffix (beta, rc, etc.)")
              svu prerelease --pre-release "$suffix" ;;
            *) svu {{.BUMP}} ;;
          esac
      LAST_STABLE:
        sh: git tag -l | grep -v '\-' | sort -V | tail -n 1
    prompt: "Release {{.NEXT}}?"
    preconditions:
      - sh: '[ $(git symbolic-ref --short HEAD) = "main" ] || [ $(git symbolic-ref --short HEAD) = "dev" ]'
        msg: Not on main or dev branch
      - sh: "[ $(git status --porcelain=2 | wc -l) = 0 ]"
        msg: "Git is dirty"
    cmds:
      - |
        if [ "{{.BUMP}}" = "graduate" ]; then
          llm-tag -b {{.LAST_STABLE}} {{.NEXT}}
        else
          llm-tag {{.NEXT}}
        fi
      - git push soft {{.NEXT}}
      - go list -m git.secluded.site/formatted-commit@{{.NEXT}} > /dev/null
      - echo "Released {{.NEXT}} and notified module proxy"
