# 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]*'
    paths-ignore:
      - '**.md'
      - '.*'
      - '**/.settings/**'
      - 'flatlaf-core/svg/**'
      - 'flatlaf-testing/dumps/**'
      - 'flatlaf-testing/misc/**'
      - 'images/**'

jobs:
  build:
    name: build (11)
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

      - uses: gradle/actions/wrapper-validation@v4

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

      - name: Check with Error Prone
        run: ./gradlew errorprone clean

      - name: Build with Gradle
        run: ./gradlew build

      - name: Upload artifacts
        uses: actions/upload-artifact@v4
        with:
          name: FlatLaf-build-artifacts
          path: |
            flatlaf-*/build/libs
            flatlaf-*/flatlaf-*/build/libs
            !**/*-javadoc.jar
            !**/*-sources.jar


  build-on:
    runs-on: ubuntu-latest
    needs: build
    if: github.repository == 'JFormDesigner/FlatLaf'

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

    steps:
      - uses: actions/checkout@v4

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

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


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

    steps:
      - uses: actions/checkout@v4

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

      - name: Publish snapshot to Sonatype Central
        run: ./gradlew publish -PskipFonts -Dorg.gradle.internal.publish.checksums.insecure=true -Dorg.gradle.parallel=false
        env:
          SONATYPE_USERNAME: ${{ secrets.SONATYPE_USERNAME }}
          SONATYPE_PASSWORD: ${{ secrets.SONATYPE_PASSWORD }}


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

    steps:
      - uses: actions/checkout@v4

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

      - name: Release a new stable version to Maven Central
        run: ./gradlew publishToSonatype closeSonatypeStagingRepository :flatlaf-demo:build :flatlaf-theme-editor:build -PskipFonts -Prelease -Dorg.gradle.parallel=false
        env:
          SONATYPE_USERNAME: ${{ secrets.SONATYPE_USERNAME }}
          SONATYPE_PASSWORD: ${{ secrets.SONATYPE_PASSWORD }}
          SIGNING_KEY:      ${{ secrets.SIGNING_KEY }}
          SIGNING_PASSWORD: ${{ secrets.SIGNING_PASSWORD }}

      - name: Install lftp
        run: sudo apt-get -y install lftp

      - name: Upload demo and theme editor
        run: >
          lftp -c "set ftp:ssl-force true;
            open -u ${{ secrets.FTP_USERNAME }},${{ secrets.FTP_PASSWORD }} ${{ secrets.FTP_SERVER }};
            mput flatlaf-demo/build/libs/flatlaf-demo-*.jar;
            mput flatlaf-theme-editor/build/libs/flatlaf-theme-editor-*.jar"
