# https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions
# https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-gradle

name: CI

on:
  push:
    branches:
      - '*'
    tags:
      - '[0-9]*'

jobs:
  build:
    runs-on: ubuntu-latest

    strategy:
      matrix:
        # test against
        # - Java 8 (minimum requirement)
        # - Java LTS versions (11, 17, ...)
        # - lastest Java version(s)
        java:
          - 8
          - 11  # LTS
          - 17  # LTS
        toolchain: [""]
        include:
          - java: 17
            toolchain: 19  # latest

    steps:
      - uses: actions/checkout@v3

      - uses: gradle/wrapper-validation-action@v1
        if: matrix.java == '8'

      - name: Setup Java ${{ matrix.java }}
        uses: actions/setup-java@v3
        with:
          java-version: ${{ matrix.java }}
          distribution: adopt  # Java 8 and 11 are pre-installed on ubuntu-latest
          cache: gradle

      - name: Build with Gradle
        run: ./gradlew build -Dtoolchain=${{ matrix.toolchain }}

      - name: Upload artifacts
        uses: actions/upload-artifact@v3
        if: matrix.java == '11'
        with:
          name: FlatLaf-build-artifacts
          path: |
            flatlaf-*/build/libs
            flatlaf-*/flatlaf-*/build/libs
            !**/*-javadoc.jar
            !**/*-sources.jar


  snapshot:
    runs-on: ubuntu-latest
    needs: build
    if: |
      github.event_name == 'push' &&
      (github.ref == 'refs/heads/main' || startsWith( github.ref, 'refs/heads/develop-' )) &&
      github.repository == 'JFormDesigner/FlatLaf'

    steps:
      - uses: actions/checkout@v3

      - name: Setup Java 11
        uses: actions/setup-java@v3
        with:
          java-version: 11
          distribution: adopt  # pre-installed on ubuntu-latest
          cache: gradle

      - name: Publish snapshot to oss.sonatype.org
        run: ./gradlew publish :flatlaf-theme-editor:build -PskipFonts -Dorg.gradle.internal.publish.checksums.insecure=true
        env:
          OSSRH_USERNAME: ${{ secrets.OSSRH_USERNAME }}
          OSSRH_PASSWORD: ${{ secrets.OSSRH_PASSWORD }}

      - name: Upload theme editor
        uses: sebastianpopp/ftp-action@releases/v2
        with:
          host: ${{ secrets.FTP_SERVER }}
          user: ${{ secrets.FTP_USERNAME }}
          password: ${{ secrets.FTP_PASSWORD }}
          forceSsl: true
          localDir: "flatlaf-theme-editor/build/libs"
          remoteDir: "snapshots"
          options: "--only-newer --no-recursion --verbose=1"


  release:
    runs-on: ubuntu-latest
    needs: build
    if: |
      github.event_name == 'push' &&
      startsWith( github.ref, 'refs/tags/' ) &&
      github.repository == 'JFormDesigner/FlatLaf'

    steps:
      - uses: actions/checkout@v3

      - name: Setup Java 11
        uses: actions/setup-java@v3
        with:
          java-version: 11
          distribution: adopt  # pre-installed on ubuntu-latest
          cache: gradle

      - name: Release a new stable version to Maven Central
        run: ./gradlew publish :flatlaf-demo:build :flatlaf-theme-editor:build -PskipFonts -Prelease
        env:
          OSSRH_USERNAME: ${{ secrets.OSSRH_USERNAME }}
          OSSRH_PASSWORD: ${{ secrets.OSSRH_PASSWORD }}
          SIGNING_KEY:      ${{ secrets.SIGNING_KEY }}
          SIGNING_PASSWORD: ${{ secrets.SIGNING_PASSWORD }}

      - name: Upload demo
        uses: sebastianpopp/ftp-action@releases/v2
        with:
          host: ${{ secrets.FTP_SERVER }}
          user: ${{ secrets.FTP_USERNAME }}
          password: ${{ secrets.FTP_PASSWORD }}
          forceSsl: true
          localDir: "flatlaf-demo/build/libs"
          remoteDir: "."
          options: "--only-newer --no-recursion --verbose=1"

      - name: Upload theme editor
        uses: sebastianpopp/ftp-action@releases/v2
        with:
          host: ${{ secrets.FTP_SERVER }}
          user: ${{ secrets.FTP_USERNAME }}
          password: ${{ secrets.FTP_PASSWORD }}
          forceSsl: true
          localDir: "flatlaf-theme-editor/build/libs"
          remoteDir: "."
          options: "--only-newer --no-recursion --verbose=1"
