# This workflow will build a Java project with Maven
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven

name: CI

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

jobs:
  # TODO: add format/checkstyle
  build:
    name: Java ${{ matrix.jdk }} / ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [ ubuntu-latest ]
        #        os: [ ubuntu-latest, macos-latest, windows-latest ]
        jdk: [ 17 ]
    #        include:
    #          - os: ubuntu-latest
    #            jdk: 16
    runs-on: ${{ matrix.os }}
    timeout-minutes: 5
    steps:
      - uses: actions/checkout@v2
      - name: Set up JDK ${{ matrix.jdk }}
        uses: actions/setup-java@v2
        with:
          java-version: ${{ matrix.jdk }}
          distribution: 'temurin'
          cache: 'maven'
      - name: Build with mvnw (linux/mac)
        if: ${{ !contains(matrix.os, 'windows') }}
        run: ./mvnw --batch-mode -no-transfer-progress package jib:buildTar --file pom.xml
      - name: Build with mvnw.cmd (windows)
        if: ${{ contains(matrix.os, 'windows') }}
        run: mvnw.cmd --batch-mode -no-transfer-progress package jib:buildTar --file pom.xml
        shell: cmd

  examples:
    name: Example project
    runs-on: ubuntu-latest
    timeout-minutes: 5
    continue-on-error: true
    steps:
      - uses: actions/checkout@v2
      - name: Set up JDK 17
        uses: actions/setup-java@v2
        with:
          java-version: 17
          distribution: 'temurin'
          # do not cache to ensure we can resolve dependencies
          server-id: 'github-flatmap'
          server-username: MAVEN_USERNAME
          server-password: MAVEN_PASSWORD
      - name: Build and test
        run: mvn --batch-mode -no-transfer-progress package --file pom.xml
        working-directory: flatmap-examples
        # workaround for github maven packages not supporting anonymous access (https://github.community/t/download-from-github-package-registry-without-authentication/14407/111)
        env:
          MAVEN_USERNAME: 'flatmapbot'
          MAVEN_PASSWORD: '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'
      - name: Find jar
        run: mv target/*with-deps.jar ./run.jar
        working-directory: flatmap-examples
      - name: Run
        run: java -jar run.jar --osm-path=../flatmap-core/src/test/resources/monaco-latest.osm.pbf --mbtiles=data/out.mbtiles
        working-directory: flatmap-examples
      - name: Verify
        run: java -cp run.jar com.onthegomap.flatmap.mbtiles.Verify data/out.mbtiles
        working-directory: flatmap-examples

  run:
    name: Build / Run
    runs-on: ubuntu-latest
    timeout-minutes: 5
    steps:
      - uses: actions/checkout@v2
      - name: Cache data/sources
        uses: ./.github/cache-sources-action
      - name: Set up JDK
        uses: actions/setup-java@v2
        with:
          java-version: 17
          distribution: 'temurin'
          cache: 'maven'

      - name: Build this branch
        run: ./mvnw -DskipTests -Dimage.version=CI_ONLY --batch-mode -no-transfer-progress package jib:dockerBuild --file pom.xml

      - name: Download data (java)
        run: java -jar flatmap-dist/target/*with-deps.jar --only-download --area=monaco

      - name: Download wikidata (java)
        run: java -jar flatmap-dist/target/*with-deps.jar --only-fetch-wikidata --area=monaco

      - name: Verify build
        run: ./scripts/test-release.sh CI_ONLY
        env:
          SKIP_EXAMPLE_PROJECT: true
