# Release workflow
#
# We parallelize builds, dump all the artifacts into a release, and then publish the release
# This guarantees everything is properly built and cached in case anything goes wrong
#
# The artifacts also need to get pushed to the various places
# - the CLI goes to the releases page for binstall
# - the extension goes to the marketplace
# - the docs go to the website
#
# We need to be aware of the channel we're releasing
# - prerelease is master
# - stable is whatever the latest stable release is (ie 0.4 or 0.5 or 0.6 etc)
#
# It's intended that this workflow is run manually, and only when we're ready to release

name: Publish CLI
on:
  workflow_dispatch:
    inputs:
      post:
        name: "Release Post"
        required: true
        description: Choose the release post to publish with. Must be a tag (eg v0.4.0)
        type: string
      channel:
        name: "CLI Binary Version"
        required: true
        description: Choose the version number to publish with. Must be a tag (ie v0.4.0)
        type: string

env:
  # make sure we have the right version
  # main is always a prepatch until we hit 1.0, and then this script needs to be updated
  # note that we need to promote the prepatch to a minor bump when we actually do a release
  # this means the version in git will always be one minor bump ahead of the actual release - basically meaning once
  # we release a version, it's fair game to merge breaking changes to main since all semver-compatible changes will be
  # backported automatically
  # SEMVER: ${{ github.event.inputs.channel == 'main' && 'prerelease' || 'patch' }}
  # PRERELEASE_TAG: ${{ github.event.inputs.channel == 'main' && '-pre' || '' }}
  RELEASE_TAG: ${{ github.event.inputs.channel }}
  RELEASE_POST: ${{ github.event.inputs.post }}

jobs:
  release-cli:
    permissions:
      contents: write
    runs-on: ${{ matrix.platform.os }}
    strategy:
      matrix:
        platform:
          - target: x86_64-pc-windows-msvc
            os: windows-latest
          - target: aarch64-pc-windows-msvc
            os: windows-latest
          - target: x86_64-apple-darwin
            os: macos-13
          - target: aarch64-apple-darwin
            os: macos-latest
          - target: x86_64-unknown-linux-gnu
            os: ubuntu-24.04
          - target: aarch64-unknown-linux-gnu
            os: ubuntu-24.04-arm
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Install openssl on macos
        if: matrix.platform.os == 'macos-latest'
        run: brew install openssl

      - name: Install nasm for windows (tls)
        if: ${{ matrix.platform.target == 'x86_64-pc-windows-msvc' }}
        uses: ilammy/setup-nasm@v1

      - name: Free Disk Space
        if: ${{ matrix.platform.os == 'ubuntu-24.04' || matrix.platform.os == 'ubuntu-24.04-arm' }}
        uses: ./.github/actions/free-disk-space

      - uses: awalsh128/cache-apt-pkgs-action@latest
        if: ${{ matrix.platform.os == 'ubuntu-24.04' || matrix.platform.os == 'ubuntu-24.04-arm' }}
        with:
          packages: libwebkit2gtk-4.1-dev libgtk-3-dev libayatana-appindicator3-dev libxdo-dev
          version: 1.0

      - name: Install stable
        uses: dtolnay/rust-toolchain@master
        with:
          toolchain: "1.86.0"
          targets: ${{ matrix.platform.target }}

      - uses: Swatinem/rust-cache@v2
        with:
          cache-all-crates: "true"
          save-if: ${{ github.ref == 'refs/heads/main' }}

      - name: Free Disk Space
        uses: jlumbroso/free-disk-space@v1.3.1
        with: # speed things up a bit
          large-packages: false
          docker-images: false
          swap-storage: false

      # Todo: we want `cargo install dx` to actually just use a prebuilt binary instead of building it
      - name: Build and upload CLI binaries
        uses: taiki-e/upload-rust-binary-action@v1
        with:
          bin: dx
          token: ${{ secrets.GITHUB_TOKEN }}
          target: ${{ matrix.platform.target }}
          archive: $bin-$target
          checksum: sha256
          manifest_path: packages/cli/Cargo.toml
          ref: refs/tags/${{ env.RELEASE_POST }}
          features: optimizations
          zip: "all"

  # todo: these things
  # Run benchmarks, which we'll use to display on the website
  # release-benchmarks:
  # Build the vscode extension, uploading the artifact to the marketplace
  # release-extension:

  # First, run checks (clippy, tests, etc) and then publish the crates to crates.io
  # release-crates:
  #   steps:
  #     # Checkout the right branch, and the nightly stuff
  #     - uses: actions/checkout@v4
  #       ref: ${{ github.event.inputs.channel }}
  #     - run: sudo apt-get update
  #     - run: sudo apt install libwebkit2gtk-4.1-dev libgtk-3-dev libayatana-appindicator3-dev libxdo-dev
  #     - uses: dtolnay/rust-toolchain@nightly
  #       with:
  #         toolchain: nightly-2024-02-01
  #     - uses: Swatinem/rust-cache@v2
  #       with:
  #         cache-all-crates: "true"

  #     - name: Free Disk Space (Ubuntu)
  #       uses: jlumbroso/free-disk-space@v1.3.1
  #       with: # speed things up a bit
  #         large-packages: false
  #         docker-images: false
  #         swap-storage: false

  #     # Just make sure clippy is happy before doing anything else
  #     # Don't publish versions with clippy errors!
  #     - name: Clippy
  #       run: cargo clippy --workspace --all --examples --tests --all-features --all-targets -- -D warnings

  #     # Build the docs here too before publishing, to ensure they're up to date
  #     - name: cargo doc
  #       run: RUSTDOCFLAGS="--cfg docsrs" cargo doc --no-deps --workspace --all-features

  #     - name: Publish to crates.io
  #       run: |
  #         git config --global user.email "github-actions[bot]@users.noreply.github.com"
  #         git config --global user.name "github-actions[bot]"
  #         cargo workspaces version -y ${{ env.SEMVER }} --pre-id rc --no-git-commit

  #         # todo: actually just publish!
  #         # cargo workspaces publish -y ${{ github.event.inputs.semver }}

  # this will be more useful when we publish the website with updated docs
  # Build the docs.rs docs and publish them to the website under the right folder
  # v0.4.x -> docs/0.4
  # v0.5.x -> docs/0.5 etc
  # main -> docs/nightly
  # strip the v from the channel, and the .x from the end, and replace main with nightly
  # - name: determine docs folder by channel
  #   id: determine_docs_folder
  #   run: echo "::set-output name=folder::$(echo ${{ github.event.inputs.channel }} | sed 's/v//g' | sed 's/\.x//g' | sed 's/main/nightly/g')"
