name: Java CI Build
env:
  junit_platform_version: '1.7.2'
  # ANT_HOME is also our ant version
  ANT_HOME: 'apache-ant-1.10.11'
  WIX_HOME: 'wix3'
  WIX_URL: 'https://github.com/wixtoolset/wix3/releases/download/wix3112rtm/wix311-binaries.zip'
on:
  push:
    branches: [master]

defaults:
  run:
    shell: bash

jobs:

  createrelease:
    name: Create Release
    runs-on: ubuntu-latest
    env:
      LANG: en_US.UTF-8
    outputs:
      upload_url: ${{ steps.create_release.outputs.upload_url }}
      josm_revision: ${{ steps.create_revision.outputs.josm_revision }}
      josm_prerelease: ${{ steps.create_revision.outputs.josm_prerelease }}
      josm_release_tag: ${{ steps.create_revision.outputs.josm_release_tag }}
    steps:
      - name: Checkout
        uses: actions/checkout@v2
        with:
          fetch-depth: 256

      - name: Set revision env variable
        id: create_revision
        # grab josm revision from last git-svn-id
        run: |
          josm_revision="$(git log -1 --grep 'git-svn-id: https://josm.openstreetmap.de/svn/trunk@' --pretty=format:%B | tail -1 | sed -n 's%git-svn-id: https://josm.openstreetmap.de/svn/trunk@\([0-9]*\) [-0-9a-f]*%\1%p')"
          if [[ "$josm_revision" == "$(curl --silent https://josm.openstreetmap.de/tested)" ]]; then
            echo "josm_prerelease=false" >> $GITHUB_ENV
            echo "::set-output name=josm_prerelease::false"
            josm_release_tag=$josm_revision-tested
          else
            echo "josm_prerelease=true" >> $GITHUB_ENV
            echo "::set-output name=josm_prerelease::true"
            josm_release_tag=$josm_revision
          fi
          echo "josm_revision=$josm_revision" >> $GITHUB_ENV
          echo "::set-output name=josm_revision::$josm_revision"
          echo "josm_release_tag=$josm_release_tag" >> $GITHUB_ENV
          echo "::set-output name=josm_release_tag::$josm_release_tag"

      - name: Create release
        id: create_release
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # This token is provided by Actions, you do not need to create your own token
        with:
          tag_name: ${{ env.josm_release_tag }}
          release_name: JOSM release ${{ env.josm_release_tag }}
          body: |
            JOSM release ${{ env.josm_release_tag }}
          draft: false
          prerelease: ${{ env.josm_prerelease }}

  build:
    needs: createrelease
    runs-on: ${{ matrix.os }}
    env:
      LANG: en_US.UTF-8
    strategy:
      fail-fast: false
      matrix:
        # test against latest update of each major Java version, as well as specific updates of LTS versions:
        java: [8, 11, 16, 17-ea, 18-ea]
        os: [ubuntu-latest, macos-latest, windows-latest]
        exclude:
          - java: 8
            os: macos-latest
          - java: 11
            os: macos-latest
    name: Java ${{ matrix.java }} on ${{ matrix.os }}
    steps:
      - name: Checkout
        uses: actions/checkout@v2
        with:
          fetch-depth: 256

      - name: Cache
        uses: actions/cache@v2.0.0
        with:
          path: |
            ~/.ivy2/cache/
            ~/work/josm/josm/tools/
          key: ${{ runner.os }}-ivy2-${{ hashFiles('build.xml', 'ivy.xml', 'tools/ivy.xml') }}

      - name: Setup Java ${{ matrix.java }}
        uses: actions/setup-java@v2
        with:
          distribution: 'zulu'
          java-version: ${{ matrix.java }}

      - name: Install Ant ${{ env.ANT_HOME }}
        run: |
          if [ ! -f tools/${{ env.ANT_HOME }}-bin.tar.gz ]; then
            curl -o tools/${{ env.ANT_HOME }}-bin.tar.gz https://downloads.apache.org/ant/binaries/${{ env.ANT_HOME }}-bin.tar.gz
          fi
          tar zxf tools/${{ env.ANT_HOME }}-bin.tar.gz

      - name: Ant diagnostics
        run: ${{ env.ANT_HOME }}/bin/ant -diagnostics

      - name: Optimise images
        if: ${{ runner.os == 'macos' && always() }}
        run: |
          brew install --cask imageoptim
          defaults write net.pornel.ImageOptim SvgoEnabled 1
          defaults write net.pornel.ImageOptim PngCrush2Enabled 1
          defaults write net.pornel.ImageOptim PngOutEnabled 1
          /Applications/ImageOptim.app/Contents/MacOS/ImageOptim resources/images

      - name: Build with Ant
        # Calls ant with -Dreleasebuild=true if we're a 'tested' build
        run: |
          if [ "${{ needs.createrelease.outputs.josm_prerelease }}" == "true" ]; then
            ANT="${{ env.ANT_HOME }}/bin/ant"
          else
            ANT="${{ env.ANT_HOME }}/bin/ant -Dreleasebuild=true"
          fi
          $ANT dist

      - name: Upload jar
        if: ${{ always() }}
        id: upload-jar
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        uses: actions/upload-release-asset@v1
        with:
          upload_url: ${{ needs.createrelease.outputs.upload_url }} # This pulls from the CREATE RELEASE job above, referencing its ID to get its outputs object, which include a `upload_url`.
          asset_path: dist/josm-custom.jar
          asset_name: JOSM-${{ runner.os}}-java${{ matrix.java }}-${{ needs.createrelease.outputs.josm_revision }}.jar
          asset_content_type: application/java-archive

      - name: Build and package for macOS
        if: ${{ runner.os == 'macos' && always() }}
        env:
          CERT_MACOS_P12: ${{ secrets.CERT_MACOS_P12 }}
          CERT_MACOS_PW: ${{ secrets.CERT_MACOS_PW }}
          APPLE_ID_PW: ${{ secrets.APPLE_ID_PW }}
        run: |
          ./native/macosx/macos-jpackage.sh ${{ needs.createrelease.outputs.josm_revision }}

      - name: Build and package for Windows
        if: ${{ runner.os == 'windows' && always() }}
        run: |
          if [ ! -f tools/${{ env.WIX_HOME }}-binaries.zip ]; then
            curl -o tools/${{ env.WIX_HOME }}-binaries.zip ${{ env.WIX_URL }}
          fi
          Expand-Archive -Path tools/${{ env.WIX_HOME }}-binaries.zip -DestinationPath ${{ env.WIX_HOME }}
          echo "${{ env.WIX_HOME }}" >> $GITHUB_PATH
          ./native/windows/win-jpackage.sh ${{ needs.createrelease.outputs.josm_revision }}

      - name: Upload macOS app
        if: ${{ runner.os == 'macos' && always() }}
        id: upload-app
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ needs.createrelease.outputs.upload_url }} # This pulls from the CREATE RELEASE job above, referencing its ID to get its outputs object, which include a `upload_url`.
          asset_path: app/JOSM.zip
          asset_name: JOSM-${{ runner.os}}-java${{ matrix.java }}.zip
          asset_content_type: application/zip

      - name: Upload Windows Installer package
        if: ${{ runner.os == 'windows' && always() }}
        id: upload-msi
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ needs.createrelease.outputs.upload_url }} # This pulls from the CREATE RELEASE job above, referencing its ID to get its outputs object, which include a `upload_url`.
          asset_path: app/JOSM.msi
          asset_name: JOSM-${{ runner.os}}-java${{ matrix.java }}.msi
          asset_content_type: application/x-ole-storage
