name: Release

on:
  push:
    tags:
      - "*"

defaults:
  run:
    # make sure to work on Windows
    shell: bash

jobs:
  release-pypi:
    name: release-pypi
    runs-on: ubuntu-latest
    permissions:
      id-token: write
      contents: write

    steps:
      - uses: actions/checkout@v6.0.2

      - uses: actions/setup-python@v6
        with:
          python-version: "3.11"
          cache: pip

      - name: Check prerelease
        id: check_version
        run: |
          if [[ "${{ github.ref }}" =~ ^refs/tags/[0-9.]+$ ]]; then
            echo "PRERELEASE=false" >> $GITHUB_OUTPUT
          else
            echo "PRERELEASE=true" >> $GITHUB_OUTPUT
          fi

      - name: Build artifacts
        run: |
          pipx run build

      - name: Upload artifacts
        uses: actions/upload-artifact@v7.0.1
        with:
          name: pdm-wheel
          path: dist/*.whl
          if-no-files-found: error
          retention-days: 15

      - name: Test Build
        run: |
          python -m pip install "pdm[locked] @ file://$(ls ${GITHUB_WORKSPACE}/dist/*.whl)"
          pdm --help

      - name: Publish package distributions to PyPI
        run: pdm publish --no-build

      - name: Get Changelog
        id: get-changelog
        run: |
          awk '/## Release/{if (flag==1)exit;else;flag=1;next} flag' CHANGELOG.md > .changelog.md

      - name: Create Release
        uses: actions/create-release@main
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ github.ref }}
          release_name: v${{ github.ref }}
          body_path: .changelog.md
          draft: false
          prerelease: ${{ steps.check_version.outputs.PRERELEASE }}

      - name: Trigger Bucket Update
        uses: benc-uk/workflow-dispatch@v1.3.2
        with:
          workflow: Excavator
          repo: frostming/scoop-frostming
          token: ${{ secrets.G_T }}
          ref: master

  binary:
    needs: release-pypi
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os:
          [
            "ubuntu-24.04",
            "ubuntu-24.04-arm",
            "windows-2025",
            "macos-15-intel",
            "macos-15",
          ]

    env:
      PYAPP_REPO: pyapp
      PYAPP_VERSION: "0.27.0"
      PYAPP_PROJECT_NAME: pdm
      PYAPP_PROJECT_VERSION: ${{ github.ref_name }}
      PYAPP_SELF_COMMAND: app # since `self` has been taken in `pdm`
      PYAPP_DISTRIBUTION_EMBED: true
      PYAPP_PROJECT_FEATURES: locked
      SOURCE_FILE: ${{ matrix.os != 'windows-2025' && 'pyapp' || 'pyapp.exe' }}
      TARGET_FILE: ${{ matrix.os != 'windows-2025' && 'pdm' || 'pdm.exe' }}

    steps:
      - name: Checkout
        uses: actions/checkout@v6.0.2

      - name: Fetch PyApp
        run: >-
          mkdir $PYAPP_REPO && curl -L
          https://github.com/ofek/pyapp/releases/download/v$PYAPP_VERSION/source.tar.gz
          |
          tar --strip-components=1 -xzf - -C $PYAPP_REPO

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

      - name: Run sccache-cache
        uses: mozilla-actions/sccache-action@v0.0.10
        if: matrix.os != 'macos-15-intel'

      - name: Set sccache env
        if: matrix.os != 'macos-15-intel'
        run: |
          echo "SCCACHE_GHA_ENABLED=true" >> $GITHUB_ENV
          echo "RUSTC_WRAPPER=sccache" >> $GITHUB_ENV

      - name: Download artifacts
        uses: actions/download-artifact@v8.0.1
        with:
          name: pdm-wheel
          path: dist

      - name: Configure embedded wheel
        run: |
          cd dist
          wheel="$(echo *.whl)"
          mv $wheel ../$PYAPP_REPO
          echo "PYAPP_PROJECT_PATH=$wheel" >> $GITHUB_ENV
          echo "TARGET_TRIPLE=$(rustc --version --verbose | grep "host" | awk '{print $2}')" >> $GITHUB_ENV

      - name: Build
        run: |
          cd $PYAPP_REPO
          cargo build --release
          mv target/release/$SOURCE_FILE ../$TARGET_FILE

      - name: Upload Assets
        uses: actions/upload-artifact@v7.0.1
        with:
          name: pdm-${{ github.ref_name }}-${{ env.TARGET_TRIPLE }}
          path: ${{ env.TARGET_FILE }}
          if-no-files-found: error
          retention-days: 15

      - name: Create and Upload Archive with Checksum
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          UPLOAD_FILE: pdm-${{ github.ref_name }}-${{ env.TARGET_TRIPLE }}.tar.gz
          CHECKSUM_FILE: pdm-${{ github.ref_name }}-${{ env.TARGET_TRIPLE }}.tar.gz.sha256
        run: |
          tar -czf $UPLOAD_FILE $TARGET_FILE
          if command -v sha256sum &> /dev/null; then
            sha256sum $UPLOAD_FILE > $CHECKSUM_FILE
          else
            shasum -a 256 $UPLOAD_FILE > $CHECKSUM_FILE
          fi
          gh release upload ${{ github.ref_name }} $UPLOAD_FILE $CHECKSUM_FILE --clobber
