hpip 0.1.0

Host These Things Please - a modern async HTTP file server
name: Release

permissions:
  contents: write

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

jobs:
  publish-crate:
    name: Publish to crates.io
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: dtolnay/rust-toolchain@stable

      - name: Get releasing version
        run: echo NEXT_VERSION=$(sed -nE 's/^\s*version = "(.*?)"/\1/p' Cargo.toml | head -1) >> $GITHUB_ENV

      - name: Check published version
        run: echo PREV_VERSION=$(cargo search hpip --limit 1 | sed -nE 's/^[^"]*"//; s/".*//1p' -) >> $GITHUB_ENV

      - name: Cargo login
        if: env.NEXT_VERSION != env.PREV_VERSION
        run: cargo login ${{ secrets.CRATES_TOKEN }}

      - name: Cargo package
        if: env.NEXT_VERSION != env.PREV_VERSION
        run: |
          echo "Releasing version: $NEXT_VERSION"
          echo "Published version: $PREV_VERSION"
          cargo package

      - name: Cargo publish
        if: env.NEXT_VERSION != env.PREV_VERSION
        run: |
          cargo publish
          echo "New version $NEXT_VERSION has been published"

  build-artifacts:
    name: Build ${{ matrix.target }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - target: x86_64-unknown-linux-gnu
            os: ubuntu-latest
            artifact: hpip
          - target: x86_64-apple-darwin
            os: macos-latest
            artifact: hpip
          - target: aarch64-apple-darwin
            os: macos-latest
            artifact: hpip
          - target: x86_64-pc-windows-msvc
            os: windows-latest
            artifact: hpip.exe

    steps:
      - uses: actions/checkout@v6
      - uses: dtolnay/rust-toolchain@stable
        with:
          targets: ${{ matrix.target }}

      - name: Build release
        run: cargo build --release --target ${{ matrix.target }}

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: hpip-${{ matrix.target }}
          path: target/${{ matrix.target }}/release/${{ matrix.artifact }}

  create-release:
    name: Create GitHub Release
    runs-on: ubuntu-latest
    needs: [build-artifacts]
    steps:
      - uses: actions/checkout@v6

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

      - name: Package artifacts
        run: |
          mkdir -p release
          for dir in artifacts/hpip-*/; do
            target=$(basename "$dir" | sed 's/^hpip-//')
            if [[ "$target" == *"windows"* ]]; then
              zip -j "release/hpip-${target}.zip" "$dir"/*
            else
              tar czf "release/hpip-${target}.tar.gz" -C "$dir" .
            fi
          done

      - name: Create release
        uses: softprops/action-gh-release@v2
        with:
          generate_release_notes: true
          files: release/*