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@v5.0.0

      - 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@v5.0.0
        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.2.4
        with:
          workflow: Excavator
          repo: frostming/scoop-frostming
          token: ${{ secrets.G_T }}
          ref: master

  binary:
    needs: release-pypi
    outputs:
      checksums: ${{ steps.collect-checksums.outputs.checksums-json }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os:
          [
            "ubuntu-22.04",
            "ubuntu-22.04-arm",
            "windows-2022",
            "macos-13",
            "macos-14",
          ]

    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
      SCCACHE_GHA_ENABLED: "true"
      RUSTC_WRAPPER: "sccache"
      SOURCE_FILE: ${{ matrix.os != 'windows-2022' && 'pyapp' || 'pyapp.exe' }}
      TARGET_FILE: ${{ matrix.os != 'windows-2022' && 'pdm' || 'pdm.exe' }}

    steps:
      - name: Checkout
        uses: actions/checkout@v5.0.0

      - 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.9

      - name: Download artifacts
        uses: actions/download-artifact@v6.0.0
        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@v5.0.0
        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
          sha256sum $UPLOAD_FILE > $CHECKSUM_FILE
          gh release upload ${{ github.ref_name }} $UPLOAD_FILE $CHECKSUM_FILE --clobber
