wheel-picker 1.0.3

Simple cli tool to make and randomly pick from a wheel.
on:
  push:
    branches:
      - main

permissions:
  contents: write

jobs:
  build:
    name: build wheel-picker-${{ matrix.platform.name }}
    if: startsWith(github.event.head_commit.message, 'v')
    runs-on: ${{ matrix.platform.os }}
    strategy:
      matrix:
        platform:
          - os: ubuntu-latest
            name: linux-x86_64
            target: x86_64-unknown-linux-gnu
          - os: ubuntu-latest
            name: linux-arm64
            target: aarch64-unknown-linux-gnu
          - os: macos-latest
            name: macos-x86_64
            target: x86_64-apple-darwin
          - os: macos-latest
            name: macos-arm64
            target: aarch64-apple-darwin
          - os: windows-latest
            name: windows-x86_64
            target: x86_64-pc-windows-msvc
          - os: windows-latest
            name: windows-arm64
            target: aarch64-pc-windows-msvc

    steps:
      - uses: actions/checkout@v4
      - uses: Swatinem/rust-cache@v2
      - uses: dtolnay/rust-toolchain@nightly

      - name: Install UPX (linux)
        if: matrix.platform.os == 'ubuntu-latest'
        run: |
          sudo apt-get update
          sudo apt-get install -y upx

      # upx is currently unsupported on macos and i dont feel like risking the --force-macos flag
      # - name: Install UPX (macos)
      #   if: matrix.platform.os == 'macos-latest'
      #   run: |
      #     brew install upx

      - name: Install UPX (windows)
        if: matrix.platform.os == 'windows-latest'
        run: |
          Invoke-WebRequest -Uri "https://github.com/upx/upx/releases/download/v5.0.2/upx-5.0.2-win64.zip" -OutFile "upx.zip"
          Expand-Archive upx.zip -DestinationPath "C:\UPX"
          echo "C:\UPX" | Out-File -Append -FilePath $env:GITHUB_PATH

      - name: Build
        run: cargo release --locked

      - name: Package (linux)
        if: matrix.platform.os == 'ubuntu-latest'
        run: |
          mkdir -p dist
          bin_name="wheel-picker"
          cp target/release/${bin_name} dist/
          cd dist

          upx --best --lzma ${bin_name}
          zip ../wheel-picker-${{ matrix.platform.name }}.zip ${bin_name}

      - name: Package (macos)
        if: matrix.platform.os == 'macos-latest'
        run: |
          mkdir -p dist
          bin_name="wheel-picker"
          cp target/release/${bin_name} dist/
          cd dist

          zip ../wheel-picker-${{ matrix.platform.name }}.zip ${bin_name}

      - name: Package (windows)
        if: matrix.platform.os == 'windows-latest'
        run: |
          mkdir dist
          $bin_name = "wheel-picker.exe"
          Copy-Item "target/release/$bin_name" dist/
          Compress-Archive -Path "dist/$bin_name" -DestinationPath "wheel-picker-${{ matrix.platform.name }}.zip"

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: wheel-picker-${{ matrix.platform.name }}
          path: wheel-picker-${{ matrix.platform.name }}.zip

  release:
    if: startsWith(github.event.head_commit.message, 'v')
    needs: build
    runs-on: ubuntu-latest

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

      - name: Get commit version and body
        id: commit
        run: |
          full_msg='${{ github.event.head_commit.message }}'
          version_num="$(echo "$full_msg" | head -n1)"
          body="$(echo "$full_msg" | tail -n +2)"

          {
            echo "version_num<<EOF"
            echo "${version_num:1}"
            echo "EOF"
          } >> "$GITHUB_OUTPUT"

          {
            echo "body<<EOF"
            echo "$body"
            echo "EOF"
          } >> "$GITHUB_OUTPUT"

      - name: Create release
        uses: softprops/action-gh-release@v2
        with:
          tag_name: v${{ steps.commit.outputs.version_num }}
          body: |
            [crates.io release](https://crates.io/crates/wheel-picker/${{ steps.commit.outputs.version_num }})

            ${{ steps.commit.outputs.body }}
          files: dist/**/*.zip

  publish:
    if: startsWith(github.event.head_commit.message, 'v')
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: Swatinem/rust-cache@v2
      - uses: dtolnay/rust-toolchain@nightly

      - name: Check if version already exists on crates.io
        id: check
        run: |
          crate_name=$(cargo metadata --no-deps --format-version 1 | jq -r '.packages[0].name')
          version=$(cargo metadata --no-deps --format-version 1 | jq -r '.packages[0].version')
          cratesio_version=$(curl -s "https://crates.io/api/v1/crates/$crate_name" | jq -r '.crate.max_version')
          if [[ $version == $cratesio_version ]]; then
            echo "exists=true" >> $GITHUB_OUTPUT
          else
            echo "exists=false" >> $GITHUB_OUTPUT
          fi

      - name: Publish to crates.io
        if: steps.check.outputs.exists == 'false'
        run: cargo publish
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}