# https://github.com/actions-rs/example/blob/23ffb1bf0016f41999902ba7542b4f1bb1a89c48/.github/workflows/quickstart.yml#L4
name: CI
on:
  push:
    branches:
      - main
  pull_request:

env:
  CARGO_TERM_COLOR: always

jobs:
  check:
    name: Check
    runs-on: ubuntu-latest
    steps:
      - name: Checkout sources
        uses: actions/checkout@v2

      - name: Install stable toolchain
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          override: true

      - name: Run cargo check
        uses: actions-rs/cargo@v1
        with:
          command: check
  test:
    name: Test Suite
    runs-on: ubuntu-latest
    steps:
      - name: Checkout sources
        uses: actions/checkout@v2

      - name: Install stable toolchain
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          override: true

      - name: Run cargo test
        uses: actions-rs/cargo@v1
        with:
          command: test
  lints:
    name: Lints
    runs-on: ubuntu-latest
    steps:
      - name: Checkout sources
        uses: actions/checkout@v2

      - name: Install stable toolchain
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          override: true
          components: rustfmt, clippy

      - name: Run cargo fmt
        uses: actions-rs/cargo@v1
        with:
          command: fmt
          args: --all -- --check

      - name: Run cargo clippy
        uses: actions-rs/cargo@v1
        with:
          command: clippy
          args: -- -D warnings

  release:
    runs-on: macos-latest
    needs:
      - test
      - lints
      - check
    if: github.ref == 'refs/heads/main'
    steps:
      - uses: actions/checkout@v3
      - name: Update local toolchain
        run: |
          rustup update
          rustup component add clippy
          rustup target add aarch64-apple-darwin
          rustup target add x86_64-apple-darwin
      - name: Toolchain info
        run: |
          cargo --version --verbose
          rustc --version
          cargo clippy --version
      - name: Build
        run: cargo build --release --target aarch64-apple-darwin --target x86_64-apple-darwin
      - name: Set sha_short Variable
        id: sha-short-var
        run: echo "sha_short=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
      - name: Create GitHub Release
        run: |
          lipo -create -output target/packs target/aarch64-apple-darwin/release/packs target/x86_64-apple-darwin/release/packs
          gh release create "${{ steps.sha-short-var.outputs.sha_short }}" --target "${{ github.sha }}" -t "${{ steps.sha-short-var.outputs.sha_short }}" --latest
          gh release upload "${{ steps.sha-short-var.outputs.sha_short }}" target/packs
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      - name: Cargo publish
        run: |
          cargo_search_output=$(cargo search pks)
          published_version=$(echo "$cargo_search_output" | grep -o 'pks = "[^"]*"' | cut -d'"' -f2)
          package_version=$(awk -F '"' '/^name/ {pkg = $2} /^version/ && pkg == "pks" {print $2}' Cargo.toml)

          if [[ "$published_version" == "$package_version" ]]; then
            echo "Package 'pks' version $package_version has already been published."
          else
            cargo publish
          fi
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
