name: Build and Release

on:
  release:
    types: [created]
  push:
    tags:
      - "v*.*.*"
  workflow_dispatch:

permissions:
  contents: write

jobs:
  build-common-archs:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        include:
          - os: ubuntu-latest
            target: x86_64-unknown-linux-gnu
            filename: downapk-linux-x86_64
          - os: windows-latest
            target: x86_64-pc-windows-msvc
            filename: downapk-windows-x86_64.exe
            extension: .exe
          - os: windows-latest
            target: i686-pc-windows-msvc
            filename: downapk-windows-i686.exe
            extension: .exe
          - os: windows-latest
            target: aarch64-pc-windows-msvc
            filename: downapk-windows-aarch64.exe
            extension: .exe
          - os: macos-latest
            target: x86_64-apple-darwin
            filename: downapk-darwin-x86_64
          - os: macos-latest
            target: aarch64-apple-darwin
            filename: downapk-darwin-aarch64

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Setup Rust
        run: |
          rustup update --no-self-update stable
          rustup target add ${{ matrix.target }}
          rustup default stable

      - name: Setup Cache
        uses: Swatinem/rust-cache@v2
        env:
          cache-name: cache-cargo-${{ matrix.target }}
        with:
          prefix-key: "downapk-v1"
          shared-key: ${{ runner.os }}-cargo-
          key: ${{ runner.os }}-cargo-${{ matrix.target }}-
          cache-directories: |
            C:\Users\runneradmin\.cargo
            /Users/runner/.cargo
      - name: Build for ${{ matrix.target }}
        run: cargo build --release --target ${{ matrix.target }}

      - name: Upload binaries to release
        uses: svenstaro/upload-release-action@v2
        if: contains(matrix.target, 'windows') == true
        with:
          repo_token: ${{ secrets.GITHUB_TOKEN }}
          file: target/${{ matrix.target }}/release/downapk${{ matrix.extension }}
          asset_name: ${{ matrix.filename }}
          tag: ${{ github.ref }}
          file_glob: false

  build-extra-archs:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        include:
          - target: aarch64-unknown-linux-gnu
            filename: downapk-linux-aarch64
          - target: aarch64-linux-android
            filename: downapk-android-aarch64
          - target: armv7-unknown-linux-gnueabihf
            filename: downapk-linux-armv7
          - target: armv7-linux-androideabi
            filename: downapk-android-armv7
          - target: x86_64-linux-android
            filename: downapk-android-x86_64
          - target: i686-unknown-linux-gnu
            filename: downapk-linux-i686

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Setup Rust
        run: |
          rustup update --no-self-update stable
          rustup target add ${{ matrix.target }}

      - name: Setup Cache
        uses: Swatinem/rust-cache@v2
        env:
          cache-name: cache-cargo-${{ matrix.target }}
        with:
          prefix-key: "downapk-v1"
          shared-key: ${{ runner.os }}-cargo-
          key: ${{ runner.os }}-cargo-${{ matrix.target }}-

      - name: Install dependencies
        run: |
          cargo install cross --git https://github.com/cross-rs/cross

      - name: Build ${{ matrix.target }}
        run: cross build --release --target ${{ matrix.target }}

      - name: Upload binaries to release
        uses: svenstaro/upload-release-action@v2
        if: contains(matrix.target, 'windows') == false
        with:
          repo_token: ${{ secrets.GITHUB_TOKEN }}
          file: target/${{ matrix.target }}/release/downapk
          asset_name: ${{ matrix.filename }}
          tag: ${{ github.ref }}
          file_glob: false

  publish:
    needs: [build-common-archs, build-extra-archs]
    runs-on: ubuntu-latest
    name: "Publish to Cargo"
    environment: Cargo

    steps:
      - uses: actions/checkout@master

      # Use caching to speed up your build
      - name: Setup Cache
        uses: Swatinem/rust-cache@v2
        env:
          cache-name: cache-cargo
        with:
          prefix-key: "downapk-v1"
          shared-key: ${{ runner.os }}-cargo-
          key: ${{ runner.os }}-cargo-

      - name: Setup cargo
        run: |
          rustup update --no-self-update stable
          
      - name: Cargo publish
        run: cargo publish
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
        
