name: Release

on:
  push:
    tags:
      - "v[0-9]+.[0-9]+.[0-9]+"

permissions:
  contents: write

env:
  CARGO_TERM_COLOR: always

jobs:
  build-linux:
    name: Build ${{ matrix.target }}
    runs-on: self-hosted

    strategy:
      matrix:
        include:
          - target: x86_64-unknown-linux-musl
            asset_name: folk-builder-linux-x86_64
          - target: aarch64-unknown-linux-musl
            asset_name: folk-builder-linux-aarch64

    steps:
      - uses: actions/checkout@v4
        with:
          clean: true

      - name: Clean target
        run: rm -rf /home/deploy/cargo-target target

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable

      - name: Build with cross
        run: cross build --release --target ${{ matrix.target }}
        env:
          CARGO_TARGET_DIR: target

      - name: Package
        run: |
          VERSION=${GITHUB_REF_NAME}
          ARCHIVE=${{ matrix.asset_name }}-${VERSION}.tar.gz
          tar -czf "${ARCHIVE}" -C target/${{ matrix.target }}/release folk-builder
          echo "ARCHIVE=${ARCHIVE}" >> $GITHUB_ENV

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.asset_name }}
          path: ${{ env.ARCHIVE }}

  build-macos:
    name: Build aarch64-apple-darwin
    runs-on: macos-latest

    steps:
      - uses: actions/checkout@v4

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          targets: aarch64-apple-darwin

      - name: Build
        run: cargo build --release --target aarch64-apple-darwin

      - name: Package
        run: |
          VERSION=${GITHUB_REF_NAME}
          ARCHIVE=folk-builder-darwin-aarch64-${VERSION}.tar.gz
          tar -czf "${ARCHIVE}" -C target/aarch64-apple-darwin/release folk-builder
          echo "ARCHIVE=${ARCHIVE}" >> $GITHUB_ENV

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: folk-builder-darwin-aarch64
          path: ${{ env.ARCHIVE }}

  release:
    name: Create GitHub Release
    needs: [build-linux, build-macos]
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

      - name: Download all artifacts
        uses: actions/download-artifact@v4
        with:
          path: dist/

      - name: Create release
        uses: softprops/action-gh-release@v2
        with:
          files: dist/**/*.tar.gz
          generate_release_notes: true
          body: |
            ## folk-builder ${{ github.ref_name }}

            Download for your platform and place in `$PATH`. Requires Rust 1.85+ installed on the build machine.

            ### Install

            **Linux x86_64:**
            ```bash
            curl -L https://github.com/Folk-Project/folk-builder/releases/download/${{ github.ref_name }}/folk-builder-linux-x86_64-${{ github.ref_name }}.tar.gz | tar xz
            sudo mv folk-builder /usr/local/bin/
            ```

            **Linux ARM64:**
            ```bash
            curl -L https://github.com/Folk-Project/folk-builder/releases/download/${{ github.ref_name }}/folk-builder-linux-aarch64-${{ github.ref_name }}.tar.gz | tar xz
            sudo mv folk-builder /usr/local/bin/
            ```

            **macOS (Apple Silicon):**
            ```bash
            curl -L https://github.com/Folk-Project/folk-builder/releases/download/${{ github.ref_name }}/folk-builder-darwin-aarch64-${{ github.ref_name }}.tar.gz | tar xz
            sudo mv folk-builder /usr/local/bin/
            ```

            ### Quick start

            ```toml
            [build]
            output = "folk-server"

            [[plugin]]
            crate_name = "folk-plugin-http"
            version = "0.1"
            config_key = "http"

            [[plugin]]
            crate_name = "folk-plugin-jobs"
            version = "0.1.5"
            config_key = "jobs"

            [[plugin]]
            crate_name = "folk-plugin-grpc"
            version = "0.1.8"
            config_key = "grpc"

            [[plugin]]
            crate_name = "folk-plugin-metrics"
            version = "0.1"
            config_key = "metrics"

            [[plugin]]
            crate_name = "folk-plugin-process"
            version = "0.1"
            config_key = "process"
            ```

            ```bash
            folk-builder build
            ./folk-server
            ```
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
