name: Snapshot

on: [push, pull_request]

concurrency:
  group: ${{ github.ref }}
  cancel-in-progress: true

jobs:
  build:
    name: Build
    strategy:
      matrix:
        config:
          - { os: macos-latest, GOOS: darwin }
          - { os: ubuntu-latest, GOOS: linux }
          - { os: ubuntu-latest, GOOS: windows }
          - { os: ubuntu-latest, GOOS: freebsd }
          - { os: ubuntu-latest, GOOS: netbsd }
          - { os: ubuntu-latest, GOOS: openbsd }
          - { os: ubuntu-latest, GOOS: dragonfly }
    runs-on: ${{ matrix.config.os }}
    env:
      GOOS: ${{ matrix.config.GOOS }}
    steps:
      - name: Checkout
        uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - name: Set up Go
        uses: ./.github/actions/setup-go

      - shell: bash
        run: |
          echo "sha_short=$(git rev-parse --short HEAD)" >> $GITHUB_ENV

      - id: cache
        uses: actions/cache@v5
        with:
          path: dist/${{ matrix.config.GOOS }}
          key: "snapshot-${{ matrix.config.GOOS }}-${{ env.sha_short }}"

      - name: Run GoReleaser
        uses: ./.github/actions/goreleaser
        with:
          args: release --split --snapshot --clean
        env:
          GORELEASER_KEY: ${{ secrets.GORELEASER_KEY }}

  release:
    name: Release
    needs: build
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - name: Set up Go
        uses: ./.github/actions/setup-go

      - name: Test
        run: go test ./...

      # copy cache from macos job
      - shell: bash
        run: |
          echo "sha_short=$(git rev-parse --short HEAD)" >> $GITHUB_ENV

      - uses: actions/cache@v5
        with: { path: dist/darwin, key: "snapshot-darwin-${{ env.sha_short }}" }
      - uses: actions/cache@v5
        with: { path: dist/linux, key: "snapshot-linux-${{ env.sha_short }}" }
      - uses: actions/cache@v5
        with: { path: dist/windows, key: "snapshot-windows-${{ env.sha_short }}" }
      - uses: actions/cache@v5
        with: { path: dist/freebsd, key: "snapshot-freebsd-${{ env.sha_short }}" }
      - uses: actions/cache@v5
        with: { path: dist/netbsd, key: "snapshot-netbsd-${{ env.sha_short }}" }
      - uses: actions/cache@v5
        with: { path: dist/openbsd, key: "snapshot-openbsd-${{ env.sha_short }}" }
      - uses: actions/cache@v5
        with: { path: dist/dragonfly, key: "snapshot-dragonfly-${{ env.sha_short }}" }

      - name: Run GoReleaser
        uses: ./.github/actions/goreleaser
        with:
          args: continue --merge
        env:
          GORELEASER_KEY: ${{ secrets.GORELEASER_KEY }}
          GITHUB_TOKEN: ${{ secrets.RELEASE_GH_TOKEN }}

      - name: Flatten artifacts
        run: |
          for d in dist/{darwin,linux,windows,freebsd,netbsd,openbsd,dragonfly}; do
            mv $d/* dist/
            rmdir $d
          done

      - name: Get name (push)
        if: github.event_name == 'push'
        run: |
          echo -n ${GITHUB_REF#refs/heads/} | tr / - > dist/NAME
          echo -n ${GITHUB_SHA} > dist/SHA

      - name: Get name (pull request)
        if: github.event_name == 'pull_request'
        run: |
          echo -n pr-${{ github.event.number }} > dist/NAME
          echo -n ${{ github.event.number }} > dist/PR
          echo -n ${{ github.event.pull_request.head.sha }} > dist/SHA

      - name: Upload artifacts
        uses: actions/upload-artifact@v6
        with:
          name: dist
          path: dist/
