name: Build and upload CLI

on:
  push:
    tags:
      - 'v*'
  workflow_dispatch:

jobs:
  cli:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [macos-11.0, ubuntu-18.04, windows-2019, self-hosted-linux-arm64, self-hosted-macos-arm64]
        include:
          - os: macos-11.0
            ARCH: macos-x86_64
            MIME: application/octet-stream
            EXT: ''
          - os: ubuntu-18.04
            ARCH: linux-x86_64
            MIME: application/octet-stream
            EXT: ''
          - os: windows-2019
            ARCH: windows-x86_64
            MIME: application/vnd.microsoft.portable-executable
            EXT: '.exe'
          - os: self-hosted-linux-arm64
            ARCH: linux-arm64
            MIME: application/octet-stream
            EXT: ''
          - os: self-hosted-macos-arm64
            ARCH: macos-arm64
            MIME: application/octet-stream
            EXT: ''
    steps:
      - uses: actions/checkout@v1

      - name: Get version
        id: version
        shell: bash
        run: echo ::set-output name=VERSION::"$([[ "$GITHUB_REF" == refs/tags/v* ]] && echo ${GITHUB_REF#refs/tags/v} || echo '0.0.0')"

      - name: Get file name
        id: file
        shell: bash
        run: echo ::set-output name=FILE::${{ steps.version.outputs.VERSION }}-${{ matrix.ARCH }}${{ matrix.EXT }}

      - name: Set up Rust
        if: runner.name != 'macos-arm64'
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
          profile: minimal
          default: true

      - name: Run prebuild steps
        shell: bash
        run: bash ./prebuild.sh

      - name: Build CLI
        working-directory: ./cli
        run: cargo build --release -vvv

      - name: Upload to B2
        uses: wilsonzlin/b2-upload-action@v1.0.0
        with:
          bucket: ${{ secrets.CICD_CLI_B2_BUCKET_NAME }}
          uploadKey: minify-html/bin/${{ steps.file.outputs.FILE }}
          keyId: ${{ secrets.CICD_CLI_B2_KEY_ID }}
          applicationKey: ${{ secrets.CICD_CLI_B2_APPLICATION_KEY }}
          file: ./cli/target/release/minify-html-cli${{ matrix.EXT }}
          contentType: ${{ matrix.MIME }}
