# https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-gradle

name: CI

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

jobs:
  build:
    runs-on: ubuntu-latest

    strategy:
      matrix:
        # test against
        # - Java 1.8 (minimum requirement)
        # - Java 9 (first version with JPMS)
        # - Java LTS versions (11, 17, ...)
        # - lastest Java version(s)
        java:
          - 1.8
          - 9
          - 11  # LTS
          - 14
          - 15

    steps:
      - uses: actions/checkout@v2

      - name: Setup Java ${{ matrix.java }}
        uses: actions/setup-java@v1
        with:
          java-version: ${{ matrix.java }}

      - name: Cache Gradle wrapper
        uses: actions/cache@v1
        with:
          path: ~/.gradle/wrapper
          key: ${{ runner.os }}-gradle-wrapper-${{ hashFiles('gradle/wrapper/gradle-wrapper.properties') }}

      - name: Cache Gradle cache
        uses: actions/cache@v2
        with:
          path: ~/.gradle/caches
          key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle.kts') }}
          restore-keys: ${{ runner.os }}-gradle

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

      - name: Upload artifacts
        uses: actions/upload-artifact@v2
        if: matrix.java == '11'
        with:
          name: FlatLaf-build-artifacts
          path: |
            flatlaf-core/build/libs
            flatlaf-demo/build/libs
            flatlaf-extras/build/libs
            flatlaf-intellij-themes/build/libs
            flatlaf-jide-oss/build/libs
            flatlaf-swingx/build/libs
            !**/*-javadoc.jar
            !**/*-sources.jar


  snapshot:
    runs-on: ubuntu-latest
    needs: build
    if: |
      github.event_name == 'push' &&
      github.ref == 'refs/heads/master' &&
      github.repository == 'JFormDesigner/FlatLaf'

    steps:
      - uses: actions/checkout@v2

      - name: Setup Java 11
        uses: actions/setup-java@v1
        with:
          java-version: 11

      - name: Cache Gradle wrapper
        uses: actions/cache@v1
        with:
          path: ~/.gradle/wrapper
          key: ${{ runner.os }}-gradle-wrapper-${{ hashFiles('gradle/wrapper/gradle-wrapper.properties') }}

      - name: Cache Gradle cache
        uses: actions/cache@v2
        with:
          path: ~/.gradle/caches
          key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle.kts') }}
          restore-keys: ${{ runner.os }}-gradle

      - name: Publish snapshot to oss.jfrog.org
        run: ./gradlew artifactoryPublish
        env:
          BINTRAY_USER: ${{ secrets.BINTRAY_USER }}
          BINTRAY_KEY:  ${{ secrets.BINTRAY_KEY }}


  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@v2

      - name: Setup Java 11
        uses: actions/setup-java@v1
        with:
          java-version: 11

      - name: Cache Gradle wrapper
        uses: actions/cache@v1
        with:
          path: ~/.gradle/wrapper
          key: ${{ runner.os }}-gradle-wrapper-${{ hashFiles('gradle/wrapper/gradle-wrapper.properties') }}

      - name: Cache Gradle cache
        uses: actions/cache@v2
        with:
          path: ~/.gradle/caches
          key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle.kts') }}
          restore-keys: ${{ runner.os }}-gradle

      - name: Release a new stable version to bintray
        run: ./gradlew bintrayUpload -Drelease=true
        env:
          BINTRAY_USER: ${{ secrets.BINTRAY_USER }}
          BINTRAY_KEY:  ${{ secrets.BINTRAY_KEY }}
