# GitHub Actions Workflow created for testing and preparing the plugin release in following steps:
# - validate Gradle Wrapper,
# - run test and verifyPlugin tasks,
# - run buildPlugin task and prepare artifact for the further tests,
# - run IntelliJ Plugin Verifier,
# - create a draft release.
#
# Workflow is triggered on push and pull_request events.
#
# Docs:
# - GitHub Actions: https://help.github.com/en/actions
# - IntelliJ Plugin Verifier GitHub Action: https://github.com/ChrisCarini/intellij-platform-plugin-verifier-action
#

name: Build
on:
  pull_request:
    branches:
      - master
jobs:

  # Run Gradle Wrapper Validation Action to verify the wrapper's checksum
  gradleValidation:
    name: Gradle Wrapper
    runs-on: ubuntu-latest
    steps:

      - name: Fetch Sources
        uses: actions/checkout@v3

      - name: Gradle Wrapper Validation
        uses: gradle/wrapper-validation-action@v1.0.5

  # Run verifyPlugin and test Gradle tasks
  test:
    name: Test
    needs: gradleValidation
    runs-on: ubuntu-latest
    steps:

      - name: Setup Java
        uses: actions/setup-java@v3
        with:
          java-version: 11
          distribution: 'zulu'
          
      - name: Fetch Sources
        uses: actions/checkout@v3

      - name: Setup Gradle Dependencies Cache
        uses: actions/cache@v3.2.3
        with:
          path: ~/.gradle/caches
          key: ${{ runner.os }}-gradle-caches-${{ hashFiles('**/*.gradle', '**/*.gradle.kts', 'gradle.properties') }}

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

      - name: Run Linters and Test
        run: ./gradlew check

      - name: Verify Plugin
        run: ./gradlew verifyPlugin

  # Build plugin with buildPlugin Gradle task and provide the artifact for the next workflow jobs
  # Requires test job to be passed
  build:
    name: Build
    needs: test
    runs-on: ubuntu-latest
    outputs:
      name: ${{ steps.properties.outputs.name }}
      version: ${{ steps.properties.outputs.version }}
      artifact: ${{ steps.properties.outputs.artifact }}
    steps:

      - name: Setup Java
        uses: actions/setup-java@v3
        with:
          java-version: 11
          distribution: 'zulu'

      - name: Fetch Sources
        uses: actions/checkout@v3

      - name: Setup Gradle Dependencies Cache
        uses: actions/cache@v3.2.3
        with:
          path: ~/.gradle/caches
          key: ${{ runner.os }}-gradle-caches-${{ hashFiles('**/*.gradle', '**/*.gradle.kts', 'gradle.properties') }}

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

      - name: Set environment variables
        id: properties
        shell: bash
        run: |
          PROPERTIES="$(./gradlew properties --console=plain -q)"
          VERSION="$(echo "$PROPERTIES" | grep "^version:" | cut -f2- -d ' ')"
          NAME="$(echo "$PROPERTIES" | grep "^pluginName_:" | cut -f2- -d ' ')"
          ARTIFACT="${NAME}-${VERSION}.zip"

          echo "::set-output name=version::$VERSION"
          echo "::set-output name=name::$NAME"
          echo "::set-output name=artifact::$ARTIFACT"

      - name: Build Plugin
        run: ./gradlew buildPlugin

      # Upload plugin artifact to make it available in the next jobs
      - name: Upload artifact
        uses: actions/upload-artifact@v3.1.2
        with:
          name: plugin-artifact
          path: ./build/distributions/${{ needs.build.outputs.artifact }}

  # Verify built plugin using IntelliJ Plugin Verifier tool
  # Requires build job to be passed
  verify:
    name: Verify
    needs: build
    runs-on: ubuntu-latest
    steps:

      - name: Setup Java
        uses: actions/setup-java@v3
        with:
          java-version: 11
          distribution: 'zulu'

      - name: Fetch Sources
        uses: actions/checkout@v3

      - name: Setup Gradle Dependencies Cache
        uses: actions/cache@v3.2.3
        with:
          path: ~/.gradle/caches
          key: ${{ runner.os }}-gradle-caches-${{ hashFiles('**/*.gradle', '**/*.gradle.kts', 'gradle.properties') }}

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

      # Set environment variables
      - name: Export Properties
        id: properties
        shell: bash
        run: |
          PROPERTIES="$(./gradlew properties --console=plain -q)"
          IDE_VERSIONS="$(echo "$PROPERTIES" | grep "^pluginVerifierIdeVersions:" | base64)"

          echo "::set-output name=ideVersions::$IDE_VERSIONS"
          echo "::set-output name=pluginVerifierHomeDir::~/.pluginVerifier"

      # Cache Plugin Verifier IDEs
      - name: Setup Plugin Verifier IDEs Cache
        uses: actions/cache@v3.2.3
        with:
          path: ${{ steps.properties.outputs.pluginVerifierHomeDir }}/ides
          key: ${{ runner.os }}-plugin-verifier-${{ steps.properties.outputs.ideVersions }}

      # Run IntelliJ Plugin Verifier action using GitHub Action
      - name: Verify Plugin
        run: ./gradlew runPluginVerifier -Pplugin.verifier.home.dir=${{ steps.properties.outputs.pluginVerifierHomeDir }}
