# based on: https://github.com/skanehira/rust-cli-template/blob/main/.github/workflows/release.yaml (LICENSE: The MIT License (MIT) - Copyright (c) 2025 skanehira https://github.com/skanehira/rust-cli-template/blob/main/LICENSE)

name: Release
on:
  push:
    tags:
      - "v*"
  workflow_dispatch:

permissions:
  contents: write

jobs:
  build:
    name: Build - ${{matrix.target}}
    strategy:
      matrix:
        include:
          - target: x86_64-unknown-linux-gnu
            runner: ubuntu-latest
            os: Linux
            arch: x86_64
            ext: tar.gz
          - target: aarch64-unknown-linux-gnu
            runner: ubuntu-24.04-arm
            os: Linux
            arch: arm64
            ext: tar.gz
          - target: x86_64-apple-darwin
            runner: macos-15-intel
            os: Darwin
            arch: x86_64
            ext: tar.gz
          - target: aarch64-apple-darwin
            runner: macos-latest
            os: Darwin
            arch: arm64
            ext: tar.gz
          - target: x86_64-pc-windows-msvc
            runner: windows-latest
            os: Windows
            arch: x86_64
            ext: zip
    runs-on: ${{ matrix.runner }}
    steps:
      - name: Checkout
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2

      - name: Setup sccache
        uses: mozilla-actions/sccache-action@7d986dd989559c6ecdb630a3fd2557667be217ad # v0.0.9

      - name: Setup environment variables for sccache
        shell: bash
        run: |
          echo "SCCACHE_GHA_ENABLED=true" >> "$GITHUB_ENV"
          echo "RUSTC_WRAPPER=sccache" >> "$GITHUB_ENV"

      - name: Setup Rust
        uses: actions-rust-lang/setup-rust-toolchain@a0b538fa0b742a6aa35d6e2c169b4bd06d225a98 # v1.15.3
        with:
          rustflags: ""

      - name: Get project name
        id: project
        shell: bash
        run: |
          name=$(cargo metadata --format-version 1 --no-deps | jq -r '.packages[0].name')
          echo "name=$name" >> "$GITHUB_OUTPUT"

      - name: Build
        run: cargo build --release

      - name: Create archive (Unix)
        if: matrix.os != 'Windows'
        shell: bash
        run: |
          name="${{ steps.project.outputs.name }}"
          archive_name="${name}_${{ matrix.target}}.tar.gz"
          tar -czvf "$archive_name" -C target/release "$name"
          echo "archive_name=$archive_name" >> "$GITHUB_ENV"

      - name: Create archive (Windows)
        if: matrix.os == 'Windows'
        shell: pwsh
        run: |
          $name = "${{ steps.project.outputs.name }}"
          $archiveName = "${name}_${{ matrix.target}}.zip"
          Compress-Archive -Path "target/release/${name}.exe" -DestinationPath $archiveName
          echo "archive_name=$archiveName" >> $env:GITHUB_ENV

      - name: Upload artifact
        uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f # v7.0.0
        with:
          name: ${{ steps.project.outputs.name }}_${{ matrix.os }}_${{ matrix.arch }}
          path: ${{ env.archive_name }}
          if-no-files-found: error

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

      - name: Download all artifacts
        uses: actions/download-artifact@70fc10c6e5e1ce46ad2ea6f2b72d43f7d47b13c3 # v8.0.0
        with:
          path: artifacts
          merge-multiple: true

      - name: Generate changelog
        id: changelog
        run: |
          # Get the previous tag
          prev_tag=$(git describe --tags --abbrev=0 HEAD^ 2>/dev/null || echo "")

          if [ -n "$prev_tag" ]; then
            echo "## Changes since $prev_tag" > changelog.md
            echo "" >> changelog.md
            git log --pretty=format:"- %s" "$prev_tag"..HEAD >> changelog.md
          else
            echo "## Initial Release" > changelog.md
          fi

      - name: Create release
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          gh release create "${{ github.ref_name }}" \
            --title "${{ github.ref_name }}" \
            --notes-file changelog.md \
            artifacts/*
