name: Build and publish Java artifact

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

jobs:
  build:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [macos-11.0, ubuntu-18.04, windows-2019]
        include:
          - os: macos-11.0
            ARCH: macos-x86_64
            FILE: 'libminify_html_java.dylib'
          - os: ubuntu-18.04
            ARCH: linux-x86_64
            FILE: 'libminify_html_java.so'
          - os: windows-2019
            ARCH: windows-x86_64
            FILE: 'minify_html_java.dll'
    steps:
      - uses: actions/checkout@v1

      - name: Set up Rust
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
          profile: minimal
          default: true

      - name: Set up GCC (Windows)
        if: runner.os == 'Windows'
        run: .\.github\workflows\gcc.ps1

      - name: Set up Go
        uses: actions/setup-go@v2
        with:
          go-version: '^1.14.0'

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

      - name: Build Java native library
        run: cargo build --release
        working-directory: ./java

      - name: Upload built library
        uses: actions/upload-artifact@v1
        with:
          name: ${{ matrix.ARCH }}
          path: ./java/target/rust/release/${{ matrix.FILE }}
  package:
    runs-on: ubuntu-18.04
    needs: build
    steps:
      - uses: actions/checkout@v1

      - name: Set up JDK
        uses: actions/setup-java@v1
        with:
          java-version: '1.7'
          java-package: jdk
          architecture: x64

      - name: Download Linux built library
        uses: actions/download-artifact@v1
        with:
          name: linux-x86_64
          path: java/src/main/resources/linux-x86_64

      - name: Download macOS built library
        uses: actions/download-artifact@v1
        with:
          name: macos-x86_64
          path: java/src/main/resources/macos-x86_64

      - name: Download Windows built library
        uses: actions/download-artifact@v1
        with:
          name: windows-x86_64
          path: java/src/main/resources/windows-x86_64

      - name: Move native library files to correct location
        shell: bash
        working-directory: ./java/src/main/resources
        run: |
          for f in *; do
            mv $f/* "$f.nativelib"
            rmdir "$f"
          done

      - name: Build, pack, and publish JAR
        working-directory: ./java
        env:
          SONATYPE_GPG_PRIVATE_KEY: ${{ secrets.SONATYPE_GPG_PRIVATE_KEY }}
        run: |
          echo "$SONATYPE_GPG_PRIVATE_KEY" | base64 -d > sonatype-gpg-private-key.asc
          gpg --import sonatype-gpg-private-key.asc
          mkdir -p "$HOME/.m2"
          cat << 'EOF' > "$HOME/.m2/settings.xml"
          <settings>
            <servers>
              <server>
                <id>ossrh</id>
                <username>wilsonzlin</username>
                <password>${{ secrets.SONATYPE_PASSWORD }}</password>
              </server>
            </servers>
            <profiles>
              <profile>
                <id>ossrh</id>
                <activation>
                  <activeByDefault>true</activeByDefault>
                </activation>
              </profile>
            </profiles>
          </settings>
          EOF
          if [[ "$GITHUB_REF" == refs/tags/v* ]]; then
            mvn clean deploy
          fi
