.github/workflows/rust-release.yml

GitHub
name: Rust Release

on:
  push:
    tags:
      - "v*"

permissions:
  contents: write
  id-token: write

env:
  CARGO_TERM_COLOR: always

jobs:
  build:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        include:
          - os: ubuntu-latest
            target: x86_64-unknown-linux-gnu
            artifact: wakaru
            archive: wakaru-linux-x64.tar.gz
            npm-pkg: cli-linux-x64
          - os: ubuntu-24.04-arm
            target: aarch64-unknown-linux-gnu
            artifact: wakaru
            archive: wakaru-linux-arm64.tar.gz
            npm-pkg: cli-linux-arm64
          - os: macos-latest
            target: aarch64-apple-darwin
            artifact: wakaru
            archive: wakaru-darwin-arm64.tar.gz
            npm-pkg: cli-darwin-arm64
          - os: windows-latest
            target: x86_64-pc-windows-msvc
            artifact: wakaru.exe
            archive: wakaru-win32-x64.zip
            npm-pkg: cli-win32-x64
    steps:
      - uses: actions/checkout@v6
      - name: Install pinned Rust toolchain
        run: rustc --version
      - name: Install release target
        run: rustup target add ${{ matrix.target }}
      - uses: Swatinem/rust-cache@v2

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

      - name: Package (unix)
        if: runner.os != 'Windows'
        run: |
          cd target/${{ matrix.target }}/release
          tar czf ${{ matrix.archive }} ${{ matrix.artifact }}
          mv ${{ matrix.archive }} ${{ github.workspace }}/

      - name: Package (windows)
        if: runner.os == 'Windows'
        shell: pwsh
        run: |
          Compress-Archive -Path "target/${{ matrix.target }}/release/${{ matrix.artifact }}" -DestinationPath "${{ matrix.archive }}"

      - name: Prepare platform npm package
        shell: bash
        run: |
          cp "target/${{ matrix.target }}/release/${{ matrix.artifact }}" \
             "npm/platforms/${{ matrix.npm-pkg }}/"

      - uses: actions/upload-artifact@v7
        with:
          name: ${{ matrix.npm-pkg }}
          path: npm/platforms/${{ matrix.npm-pkg }}/

      - uses: actions/upload-artifact@v7
        with:
          name: ${{ matrix.archive }}
          path: ${{ matrix.archive }}

  release:
    needs: build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/download-artifact@v7
        with:
          path: artifacts

      - name: Create GitHub Release
        uses: softprops/action-gh-release@v2
        with:
          files: |
            artifacts/**/*.tar.gz
            artifacts/**/*.zip
          generate_release_notes: true

  npm-publish:
    needs: build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: actions/setup-node@v6
        with:
          node-version: 24
          registry-url: https://registry.npmjs.org

      - uses: actions/download-artifact@v7
        with:
          path: artifacts

      - name: Publish platform packages
        run: |
          VERSION="${GITHUB_REF_NAME#v}"
          for pkg in cli-darwin-arm64 cli-linux-arm64 cli-linux-x64 cli-win32-x64; do
            PKG_DIR="npm/platforms/$pkg"
            cp artifacts/$pkg/* "$PKG_DIR/"
            cd "$PKG_DIR"
            npm version "$VERSION" --no-git-tag-version --allow-same-version
            npm publish --tag latest --access public
            cd "$GITHUB_WORKSPACE"
          done

      - name: Publish main package
        run: |
          VERSION="${GITHUB_REF_NAME#v}"
          cd npm
          # Update version and optionalDependencies versions
          node -e "
            const pkg = require('./package.json');
            pkg.version = '$VERSION';
            for (const dep of Object.keys(pkg.optionalDependencies)) {
              pkg.optionalDependencies[dep] = '$VERSION';
            }
            require('fs').writeFileSync('package.json', JSON.stringify(pkg, null, 2) + '\n');
          "
          npm publish --tag latest --access public