name: CD

run-name: CD | ${{ github.event_name }} by ${{ github.actor }} on '${{ github.ref_name }}'

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

permissions:
  contents: write

jobs:
  publish:
    name: Publishing for ${{ matrix.os }} (${{ matrix.architecture }})
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        include:
          - os: macos-latest
            os-name: macos
            target: x86_64-apple-darwin
            architecture: x86_64
            binary-postfix: ""
            binary-name: rivetui
            use-cross: false
          - os: macos-latest
            os-name: macos
            target: aarch64-apple-darwin
            architecture: arm64
            binary-postfix: ""
            use-cross: false
            binary-name: rivetui
          - os: ubuntu-22.04
            os-name: linux
            target: x86_64-unknown-linux-gnu
            architecture: x86_64
            binary-postfix: ""
            use-cross: false
            binary-name: rivetui
          - os: ubuntu-22.04-arm
            os-name: linux
            target: aarch64-unknown-linux-gnu
            architecture: arm64
            binary-postfix: ""
            use-cross: false
            binary-name: rivetui
          - os: windows-latest
            os-name: windows
            target: x86_64-pc-windows-msvc
            architecture: x86_64
            binary-postfix: ".exe"
            use-cross: false
            binary-name: rivetui
          - os: windows-latest
            os-name: windows
            target: aarch64-pc-windows-msvc
            architecture: arm64
            binary-postfix: ".exe"
            use-cross: true
            binary-name: rivetui

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
      - name: Install Rust toolchain
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
          target: ${{ matrix.target }}
          profile: minimal
          override: true
      - uses: Swatinem/rust-cache@v2
      - name: Cargo build
        uses: actions-rs/cargo@v1
        with:
          command: build
          use-cross: ${{ matrix.use-cross }}
          toolchain: stable
          args: --release --target ${{ matrix.target }}
      - name: install strip command
        shell: bash
        run: |
          if [[ ${{ matrix.target }} == aarch64-unknown-linux-gnu ]]; then
              sudo apt update
              sudo apt-get install -y binutils-aarch64-linux-gnu
          fi
      - name: Packaging final binary
        shell: bash
        run: |
          cp emojis.json target/${{ matrix.target }}/release
          cd target/${{ matrix.target }}/release

          ####### reduce binary size by removing debug symbols #######
          BINARY_NAME=${{ matrix.binary-name }}${{ matrix.binary-postfix }}
          if [[ ${{ runner.os }} != "Windows" ]]; then
            if [[ ${{ matrix.target }} == aarch64-unknown-linux-gnu ]]; then
              GCC_PREFIX="aarch64-linux-gnu-"
            else
              GCC_PREFIX=""
            fi
            "$GCC_PREFIX"strip $BINARY_NAME
          fi

          ########## create tar.gz ##########
          RELEASE_NAME=${{ matrix.binary-name }}-${{ github.ref_name }}-${{ matrix.os-name }}-${{ matrix.architecture }}
          tar czvf $RELEASE_NAME.tar.gz $BINARY_NAME emojis.json

          ########## create sha256 ##########
          if [[ ${{ runner.os }} == 'Windows' ]]; then
            certutil -hashfile $RELEASE_NAME.tar.gz sha256 | grep -E [A-Fa-f0-9]{64} > $RELEASE_NAME.sha256
          else
            shasum -a 256 $RELEASE_NAME.tar.gz > $RELEASE_NAME.sha256
          fi
      - name: Releasing assets
        uses: softprops/action-gh-release@v2
        with:
          draft: false
          files: |
            target/${{ matrix.target }}/release/${{ matrix.binary-name }}-*.tar.gz
            target/${{ matrix.target }}/release/${{ matrix.binary-name }}-*.sha256
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  publish-cargo:
    name: Publishing to Cargo
    runs-on: ubuntu-22.04
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable

      - uses: Swatinem/rust-cache@v2

      - name: Install cargo-edit
        run: cargo install cargo-edit

      - name: Extract Git Tag version
        id: version_extract
        run: |
          TAG_NAME=${{ github.ref_name }}
          VERSION_NUMBER=$(echo $TAG_NAME | sed 's/^v//')
          echo "VERSION=$VERSION_NUMBER" >> $GITHUB_OUTPUT
        shell: bash

      - name: Update Cargo.toml version
        run: cargo set-version ${{ steps.version_extract.outputs.VERSION }}

      - name: Configure Git User
        run: |
          git config user.email "servoskull@omnissiah.mars"
          git config user.name "Servo Skull"

      - name: Commit version
        run: |
          git add Cargo.toml
          git commit -m "[RELEASE] Bump bersion to ${{ steps.version_extract.outputs.VERSION }} before publishing"

      - run: cargo publish
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CRATES_TOKEN }}
