---
name: Release
on:
  workflow_dispatch:
  pull_request:
    branches:
      - main
    paths:
      - version.bzl
      - .github/workflows/release.yaml
  push:
    branches:
      - main
    paths:
      - version.bzl

defaults:
  run:
    shell: bash

env:
  BAZEL_STARTUP_FLAGS: --bazelrc=${{ github.workspace }}/.github/github.bazelrc

jobs:
  validation:
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@v4
      - name: Ensure release does not already exist
        if: startsWith(github.ref, 'refs/heads/main')
        run: |
          git fetch origin &> /dev/null
          version="$(grep 'VERSION =' ${{ github.workspace }}/version.bzl | sed 's/VERSION = "//' | sed 's/"//')"
          if [[ -n "$(git tag -l ${version})" ]]; then
            echo "A release '${version}' already exists." >&2
            exit 1
          else
            echo "Tag '${version}' will be created"
          fi
  builds:
    needs: validation
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        # Create a job for each target triple
        include:
          - os: macOS-14
            env:
              TARGET: "aarch64-apple-darwin"
          - os: ubuntu-22.04
            env:
              TARGET: "aarch64-unknown-linux-gnu"
          - os: ubuntu-22.04
            env:
              TARGET: "aarch64-unknown-linux-musl"
          - os: windows-2022
            env:
              TARGET: "aarch64-pc-windows-msvc"
          - os: macOS-13
            env:
              TARGET: "x86_64-apple-darwin"
          - os: ubuntu-22.04
            env:
              TARGET: "x86_64-pc-windows-gnu"
          - os: windows-2022
            env:
              TARGET: "x86_64-pc-windows-msvc"
          - os: ubuntu-22.04
            env:
              TARGET: "x86_64-unknown-linux-gnu"
          - os: ubuntu-22.04
            env:
              TARGET: "x86_64-unknown-linux-musl"
    steps:
      - uses: actions/checkout@v4
      - name: Install rust toolchains for host
        run: |
          # Detect the current version of rust
          version="$(grep 'DEFAULT_RUST_VERSION =' ./rust/private/common.bzl | grep -o '[[:digit:].]\+')"
          rustup override set "${version}"
          rustup target add ${TARGET}
          rustup update stable --no-self-update --force-non-host
          rustup default stable
        env:
          TARGET: "${{ matrix.env.TARGET }}"
      - name: Setup macos build tooling
        run: |
          sudo xcode-select -s /Applications/Xcode_15.2.0.app/Contents/Developer/
          # Set SDK environment variables
          echo "SDKROOT=$(xcrun -sdk macosx14.2 --show-sdk-path)" >> $GITHUB_ENV
          MACOSX_DEPLOYMENT_TARGET="$(xcrun -sdk macosx14.2 --show-sdk-platform-version)"
          if [[ -n "${MACOSX_DEPLOYMENT_TARGET}" ]]; then
            echo "MACOSX_DEPLOYMENT_TARGET=${MACOSX_DEPLOYMENT_TARGET}" >> $GITHUB_ENV
          fi
        if: startswith(matrix.os, 'macos')
      - name: Setup Windows Bazelrc
        run: |
          echo "startup --output_user_root=C:/tmp" > ./user.bazelrc
          echo "startup --windows_enable_symlinks" > ./user.bazelrc
          echo "build --enable_runfiles" > ./user.bazelrc
        if: startswith(matrix.os, 'Windows')
      - name: Build cargo-bazel binaries
        run: |
          # Build binaries
          if [[ "${RUNNER_OS}" == "Windows" ]]; then
            OUTPUT_PATH="$(cygpath "${{ github.workspace }}/crate_universe/target/artifacts" -m)"
          else
            OUTPUT_PATH="${{ github.workspace }}/crate_universe/target/artifacts"
          fi
            bazel ${BAZEL_STARTUP_FLAGS[@]} run //crate_universe/tools/cross_installer -- --target=${TARGET} --output="${OUTPUT_PATH}"
        env:
          TARGET: "${{ matrix.env.TARGET }}"
      - uses: actions/upload-artifact@v4
        with:
          name: "${{ matrix.env.TARGET }}"
          path: ${{ github.workspace }}/crate_universe/target/artifacts/${{ matrix.env.TARGET }}
          if-no-files-found: error
  release:
    if: startsWith(github.ref, 'refs/heads/main')
    needs: builds
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@v4
      - uses: actions/download-artifact@v4
        with:
          path: ${{ github.workspace }}/crate_universe/target/artifacts
      - name: Detect the current version
        run: |
          version="$(grep 'VERSION =' ${{ github.workspace }}/version.bzl | grep -o '[[:digit:].]\+')"
          echo "RELEASE_VERSION=${version}" >> $GITHUB_ENV
      - name: Create the rules archive
        run: |
          # Update urls and sha256 values
          bazel ${BAZEL_STARTUP_FLAGS[@]} run //crate_universe/tools/urls_generator \
            -- --artifacts-dir="${ARTIFACTS_DIR}" --url-prefix="${URL_PREFIX}"

          bazel clean

          # Build an archive of the repo contents.
          # `examples/hello_world` is included for the BCR presubmit; it must appear before --exclude="examples"
          tar -czf ${{ github.workspace }}/.github/rules_rust.tar.gz \
            -C ${{ github.workspace }} \
            --exclude=".git" \
            --exclude=".github" \
            --exclude="crate_universe/target" \
            examples/hello_world \
            --exclude="examples" \
            .

          # Save the sha256 checksum of the distro archive to the environment
          sha256_base64="$(shasum --algorithm 256 ${{ github.workspace }}/.github/rules_rust.tar.gz | awk '{ print $1 }' | xxd -r -p | base64)"
          echo "ARCHIVE_SHA256_BASE64=${sha256_base64}" >> $GITHUB_ENV
        env:
          CARGO_BAZEL_GENERATOR_URL: file://${{ github.workspace }}/crate_universe/target/artifacts/x86_64-unknown-linux-gnu/cargo-bazel
          ARTIFACTS_DIR: ${{ github.workspace }}/crate_universe/target/artifacts
          URL_PREFIX: https://github.com/${{ github.repository_owner }}/rules_rust/releases/download/${{ env.RELEASE_VERSION }}

      # Upload the artifact in case creating a release fails so all artifacts can then be manually recovered.
      - uses: actions/upload-artifact@v4
        with:
          name: "rules_rust.tar.gz"
          path: ${{ github.workspace }}/.github/rules_rust.tar.gz
          if-no-files-found: error
      - name: Generate release notes
        run: |
          # Generate the release notes
          sed 's#{version}#${{ env.RELEASE_VERSION }}#g' ${{ github.workspace }}/.github/release_notes.template \
          | sed 's#{sha256_base64}#${{ env.ARCHIVE_SHA256_BASE64 }}#g' \
          > ${{ github.workspace }}/.github/release_notes.txt
      - name: Create release
        uses: softprops/action-gh-release@v1
        id: rules_rust_release
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          generate_release_notes: true
          tag_name: ${{ env.RELEASE_VERSION }}
          body_path: ${{ github.workspace }}/.github/release_notes.txt
          target_commitish: ${{ github.base_ref }}

      - name: "Upload the rules archive"
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.rules_rust_release.outputs.upload_url }}
          asset_name: rules_rust-${{ env.RELEASE_VERSION }}.tar.gz
          asset_path: ${{ github.workspace }}/.github/rules_rust.tar.gz
          asset_content_type: application/gzip

      # There must be a upload action for each platform triple we create
      - name: "Upload aarch64-apple-darwin"
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.rules_rust_release.outputs.upload_url }}
          asset_name: cargo-bazel-aarch64-apple-darwin
          asset_path: ${{ github.workspace }}/crate_universe/target/artifacts/aarch64-apple-darwin/cargo-bazel
          asset_content_type: application/octet-stream
      - name: "Upload aarch64-pc-windows-msvc"
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.rules_rust_release.outputs.upload_url }}
          asset_name: cargo-bazel-aarch64-pc-windows-msvc.exe
          asset_path: ${{ github.workspace }}/crate_universe/target/artifacts/aarch64-pc-windows-msvc/cargo-bazel.exe
          asset_content_type: application/octet-stream
      - name: "Upload aarch64-unknown-linux-gnu"
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.rules_rust_release.outputs.upload_url }}
          asset_name: cargo-bazel-aarch64-unknown-linux-gnu
          asset_path: ${{ github.workspace }}/crate_universe/target/artifacts/aarch64-unknown-linux-gnu/cargo-bazel
          asset_content_type: application/octet-stream
      - name: "Upload x86_64-apple-darwin"
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.rules_rust_release.outputs.upload_url }}
          asset_name: cargo-bazel-x86_64-apple-darwin
          asset_path: ${{ github.workspace }}/crate_universe/target/artifacts/x86_64-apple-darwin/cargo-bazel
          asset_content_type: application/octet-stream
      - name: "Upload x86_64-pc-windows-gnu"
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.rules_rust_release.outputs.upload_url }}
          asset_name: cargo-bazel-x86_64-pc-windows-gnu.exe
          asset_path: ${{ github.workspace }}/crate_universe/target/artifacts/x86_64-pc-windows-gnu/cargo-bazel.exe
          asset_content_type: application/octet-stream
      - name: "Upload x86_64-pc-windows-msvc"
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.rules_rust_release.outputs.upload_url }}
          asset_name: cargo-bazel-x86_64-pc-windows-msvc.exe
          asset_path: ${{ github.workspace }}/crate_universe/target/artifacts/x86_64-pc-windows-msvc/cargo-bazel.exe
          asset_content_type: application/octet-stream
      - name: "Upload x86_64-unknown-linux-gnu"
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.rules_rust_release.outputs.upload_url }}
          asset_name: cargo-bazel-x86_64-unknown-linux-gnu
          asset_path: ${{ github.workspace }}/crate_universe/target/artifacts/x86_64-unknown-linux-gnu/cargo-bazel
          asset_content_type: application/octet-stream
      - name: "Upload x86_64-unknown-linux-musl"
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.rules_rust_release.outputs.upload_url }}
          asset_name: cargo-bazel-x86_64-unknown-linux-musl
          asset_path: ${{ github.workspace }}/crate_universe/target/artifacts/x86_64-unknown-linux-musl/cargo-bazel
          asset_content_type: application/octet-stream
      - name: "Upload aarch64-unknown-linux-musl"
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.rules_rust_release.outputs.upload_url }}
          asset_name: cargo-bazel-aarch64-unknown-linux-musl
          asset_path: ${{ github.workspace }}/crate_universe/target/artifacts/aarch64-unknown-linux-musl/cargo-bazel
          asset_content_type: application/octet-stream
