name: Native Image - Snapshot

on:
  workflow_dispatch:
#  push:
#    branches:
#      - master
#    paths-ignore:
#      - '**.md'
#      - 'docs/**'

jobs:
  clean_release:
    name: 'Clean the snapshot-native pre-release'
    runs-on: ubuntu-latest
    steps:
      - name: Checkout the repository
        uses: actions/checkout@v4
 
      - name: Clean release and tag
        run: |
          gh release delete snapshot-native --cleanup-tag -y || true
          echo "::notice title=release snapshot-native::snapshot - Native Image at ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/releases/tag/snapshot-native"
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

  build_images:
    name: 'Build Native Image ${{ matrix.platform }}'
    needs: clean_release
    strategy:
      matrix:
        os: [ macos-latest, windows-latest, ubuntu-latest ]
        include:
          - os: 'ubuntu-latest'
            platform: 'linux-amd64'
          - os: 'macos-latest'
            platform: 'darwin-arm64'
          - os: 'macos-13'
            platform: 'darwin-amd64'
          - os: 'windows-latest'
            platform: 'win-amd64'
    runs-on: ${{matrix.os}}
    steps:
      - name: Checkout the repository
        uses: actions/checkout@v4

      - uses: graalvm/setup-graalvm@v1
        with:
          java-version: '21'
          github-token: ${{ secrets.GITHUB_TOKEN }}
          distribution: liberica
          cache: gradle

      - name: Set the release version
        run: echo "VERSION=$(grep 'version =' gradle.properties | cut -d' ' -f 3)" >> $GITHUB_ENV
        shell: bash

      - name: Set the DateTime
        shell: bash
        run: echo "DATETIME=$(date -u +'%F at %T (UTC)')" >> $GITHUB_ENV

      - name: Build GPLv2 native image
        shell: bash
        run: ./gradlew :plantuml-gplv2:nativeFullCompile :plantuml-gplv2:nativeHeadlessCompile -x test

      - name: Archive Full Release
        uses: thedoctor0/zip-release@0.7.6
        with:
          type: 'zip'
          filename: "plantuml-full-${{ matrix.platform }}-${{ env.VERSION }}.zip"
          directory: plantuml-gplv2/build/native/nativeFullCompile/

      - name: Archive Headless Release
        uses: thedoctor0/zip-release@0.7.6
        with:
          type: 'zip'
          filename: "plantuml-headless-${{ matrix.platform }}-${{ env.VERSION }}.zip"
          directory: plantuml-gplv2/build/native/nativeHeadlessCompile/

      - name: Upload binaries to release
        uses: svenstaro/upload-release-action@v2
        with:
          repo_token: ${{ secrets.GITHUB_TOKEN }}
          file: "plantuml-gplv2/build/native/native*/plantuml-*-${{ matrix. platform }}-${{ env.VERSION }}.zip"
          file_glob: true
          tag: snapshot-native
          release_name: snapshot - Native Image (~v${{ env.VERSION }})
          overwrite: true
          make_latest: false
          prerelease: true
          body: |
              ## Version ~v${{ env.VERSION }} of the ${{ env.DATETIME }}
              This is a [Native Image](https://www.graalvm.org/latest/reference-manual/native-image/) pre-release of [the latest development work](https://github.com/plantuml/plantuml/commits/).
              ⚠️  **It is not ready for general use** ⚠️
              ⏱  _Snapshot taken the ${{ env.DATETIME }}_
