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

name: SNAPSHOT Build

on:
  push:
    branches: [ master ]

jobs:
  linux:
    if: "!contains(github.event.head_commit.message, 'cd skip')"
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2
        with:
          submodules: recursive
      - name: Setup Eclipse Temurin OpenJDK 17
        uses: actions/setup-java@v2
        with:
          distribution: 'temurin'
          java-version: 17
      - name: Add build info
        env:
          WORKFLOW_RUN: ${{ github.run_number }}
        run: |
          echo "build=$WORKFLOW_RUN" >> assets/configs/app.properties
      - name: Grant execute permission for gradlew
        run: chmod +x gradlew
      - name: Build JAR dist files
        env:
          WORKFLOW_RUN: ${{ github.run_number }}
        run: ./gradlew dist -PSNAPSHOT=true
      - name: Build deb package
        env:
          WORKFLOW_RUN: ${{ github.run_number }}
        run: ./gradlew jpackage -PSNAPSHOT=true
      - name: Fix deb file names
        run: mv build/jpackage/*.deb Linux.HyperLap2D-SNAPSHOT.deb
      - name: Upload deb artifact
        uses: actions/upload-artifact@v2
        with:
          name: Linux.HyperLap2D-SNAPSHOT.deb
          path: Linux.HyperLap2D-SNAPSHOT.deb
      - name: Sync files with production server
        uses: garygrossgarten/github-action-scp@release
        with:
          host: ${{ secrets.DEPLOY_SSH_HOST }}
          username: ${{ secrets.DEPLOY_SSH_USERNAME }}
          password: ${{ secrets.DEPLOY_SSH_PASSWORD }}
          port: ${{ secrets.DEPLOY_SSH_PORT }}
          local: "Linux.HyperLap2D-SNAPSHOT.deb"
          remote: ${{ format('{0}/{1}', secrets.DEPLOY_SSH_TARGET_PATH, 'Linux.HyperLap2D-SNAPSHOT.deb') }}
      - name: Generate build info
        env:
          WORKFLOW_RUN: ${{ github.run_number }}
        run: |
          echo "{ \"build\": $WORKFLOW_RUN }" > snapshot.json
      - name: Sync build info with production server
        uses: garygrossgarten/github-action-scp@release
        with:
          host: ${{ secrets.DEPLOY_SSH_HOST }}
          username: ${{ secrets.DEPLOY_SSH_USERNAME }}
          password: ${{ secrets.DEPLOY_SSH_PASSWORD }}
          port: ${{ secrets.DEPLOY_SSH_PORT }}
          local: "snapshot.json"
          remote: ${{ format('{0}/{1}', secrets.DEPLOY_SSH_TARGET_PATH, 'snapshot.json') }}
      - name: Publish SNAPSHOT artifacts
        if: "!contains(github.event.head_commit.message, 'editor only')"
        env:
          NEXUS_USERNAME: ${{ secrets.NEXUS_USERNAME }}
          NEXUS_PASSWORD: ${{ secrets.NEXUS_PASSWORD }}
        run: ./gradlew build publish -PSNAPSHOT=true

  windows:
    if: "!contains(github.event.head_commit.message, 'cd skip')"
    runs-on: windows-latest

    steps:
      - uses: actions/checkout@v2
        with:
          submodules: recursive
      - name: Setup Eclipse Temurin OpenJDK 17
        uses: actions/setup-java@v2
        with:
          distribution: 'temurin'
          java-version: 17
      - name: Add build info
        env:
          WORKFLOW_RUN: ${{ github.run_number }}
        run: |
          echo "build=$Env:WORKFLOW_RUN" >> assets/configs/app.properties
      - name: Build JAR dist files
        env:
          WORKFLOW_RUN: ${{ github.run_number }}
        run: ./gradlew.bat dist -PSNAPSHOT=true
      - name: Build exe package
        env:
          WORKFLOW_RUN: ${{ github.run_number }}
        run: ./gradlew.bat jpackage -PSNAPSHOT=true
      - name: Fix file names
        run: mv build/jpackage/*.exe Windows.HyperLap2D-SNAPSHOT.exe
      - name: Upload exe artifact
        uses: actions/upload-artifact@v2
        with:
          name: Windows.HyperLap2D-SNAPSHOT.exe
          path: Windows.HyperLap2D-SNAPSHOT.exe
      - name: Sync files with server
        uses: garygrossgarten/github-action-scp@release
        with:
          host: ${{ secrets.DEPLOY_SSH_HOST }}
          username: ${{ secrets.DEPLOY_SSH_USERNAME }}
          password: ${{ secrets.DEPLOY_SSH_PASSWORD }}
          port: ${{ secrets.DEPLOY_SSH_PORT }}
          local: "Windows.HyperLap2D-SNAPSHOT.exe"
          remote: ${{ format('{0}/{1}', secrets.DEPLOY_SSH_TARGET_PATH, 'Windows.HyperLap2D-SNAPSHOT.exe') }}

  macOS:
    if: "!contains(github.event.head_commit.message, 'cd skip')"
    runs-on: macOS-latest

    steps:
      - uses: actions/checkout@v2
        with:
          submodules: recursive
      - name: Setup Eclipse Temurin OpenJDK 17
        uses: actions/setup-java@v2
        with:
          distribution: 'temurin'
          java-version: 17
      - name: Add build info
        env:
          WORKFLOW_RUN: ${{ github.run_number }}
        run: |
          echo "build=$WORKFLOW_RUN" >> assets/configs/app.properties
      - name: Grant execute permission for gradlew
        run: chmod +x gradlew
      - name: Build JAR dist files
        env:
          WORKFLOW_RUN: ${{ github.run_number }}
        run: ./gradlew dist -PSNAPSHOT=true
      - name: Build dmg package
        env:
          WORKFLOW_RUN: ${{ github.run_number }}
        run: ./gradlew jpackage -PSNAPSHOT=true
      - name: Fix file names
        run: mv build/jpackage/*.dmg macOS.HyperLap2D-SNAPSHOT.dmg
      - name: Upload dmg artifact
        uses: actions/upload-artifact@v2
        with:
          name: macOS.HyperLap2D-SNAPSHOT.dmg
          path: macOS.HyperLap2D-SNAPSHOT.dmg
      - name: Sync files with server
        uses: garygrossgarten/github-action-scp@release
        with:
          host: ${{ secrets.DEPLOY_SSH_HOST }}
          username: ${{ secrets.DEPLOY_SSH_USERNAME }}
          password: ${{ secrets.DEPLOY_SSH_PASSWORD }}
          port: ${{ secrets.DEPLOY_SSH_PORT }}
          local: "macOS.HyperLap2D-SNAPSHOT.dmg"
          remote: ${{ format('{0}/{1}', secrets.DEPLOY_SSH_TARGET_PATH, 'macOS.HyperLap2D-SNAPSHOT.dmg') }}