name: Windows

on: [ push, pull_request ]

env:
  JBR_MAJOR: 21.0.8
  JBR_PATCH: b1138.52

jobs:
  build:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ windows-11-arm, windows-2022 ]
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 1

      - name: Setup MSbuild
        uses: microsoft/setup-msbuild@v2

      - name: Set architecture
        id: set-arch
        run: |
          if ($env:PROCESSOR_ARCHITECTURE -eq "ARM64") {
            echo "ARCH=aarch64" >> $env:GITHUB_ENV
          } else {
            echo "ARCH=x64" >> $env:GITHUB_ENV
          }

      - name: Find MakeAppx
        shell: pwsh
        run: |
          $installedRootsKey = "HKLM:\SOFTWARE\Microsoft\Windows Kits\Installed Roots"
          $kitsRoot = (Get-ItemProperty $installedRootsKey).KitsRoot10
          $versions = Get-ChildItem -Path $installedRootsKey | Select-Object -ExpandProperty PSChildName
          $maxVersion = $versions | ForEach-Object { [version]$_ } | Sort-Object -Descending | Select-Object -First 1
          $arch = if ($env:ARCH -eq "aarch64") { "arm64" } else { "x64" }
          $makeAppXPath = Join-Path -Path $kitsRoot -ChildPath "bin\$maxVersion\$arch\makeappx.exe"
          Write-Output "MakeAppx.exe path: $makeAppXPath"
          
          if (Test-Path $makeAppXPath) {
            "MAKEAPPX_PATH=$makeAppXPath" | Out-File -FilePath $Env:GITHUB_ENV -Encoding utf8 -Append
          } else {
            Write-Output "MakeAppx.exe not found!"
            exit 1
          }

      - name: Install zip
        run: |
          $system32 = [System.Environment]::GetEnvironmentVariable("WINDIR") + "\System32"
          Invoke-WebRequest -Uri "http://stahlworks.com/dev/zip.exe" -OutFile "$system32\zip.exe"
          Invoke-WebRequest -Uri "http://stahlworks.com/dev/unzip.exe" -OutFile "$system32\unzip.exe"

      - name: Install 7z
        uses: milliewalky/setup-7-zip@v2

      - name: Installing Java
        run: |
          $zipPath = "${{ runner.temp }}\java_package.zip"
          $extractDir = "${{ runner.temp }}\jbr"
          $url = "https://cache-redirector.jetbrains.com/intellij-jbr/jbrsdk-${{ env.JBR_MAJOR }}-windows-${{ env.ARCH }}-${{ env.JBR_PATCH }}.zip"
          curl -s --output $zipPath -L $url
          unzip -q $zipPath -d $extractDir
          $jbrDir = Get-ChildItem $extractDir | Select-Object -First 1
          echo "JAVA_HOME=$($jbrDir.FullName)" >> $env:GITHUB_ENV

      - uses: actions/cache@v4
        with:
          path: |
            ~/.gradle/caches
            ~/.gradle/wrapper
          key: ${{ runner.os }}-${{ runner.arch }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
          restore-keys: |
            ${{ runner.os }}-${{ runner.arch }}-gradle-

      - name: Compile
        run: .\gradlew :check-license && .\gradlew classes -x test

      - name: JLink
        run: .\gradlew :jar :copy-dependencies :plugins:migration:build :jlink

      - name: Package
        run: .\gradlew :jpackage && .\gradlew :dist

      - name: MSIX
        env:
          TERMORA_TYPE: appx
        run: |
          .\gradlew --stop
          .\gradlew :dist

      - name: Stop Gradle
        run: .\gradlew.bat --stop

      - name: Upload zip artifact
        uses: actions/upload-artifact@v4
        with:
          name: termora-windows-zip-${{ runner.arch }}
          path: |
            build/distributions/*.zip

      - name: Upload exe artifact
        uses: actions/upload-artifact@v4
        with:
          name: termora-windows-exe-${{ runner.arch }}
          path: |
            build/distributions/*.exe

      - name: Upload msix artifact
        uses: actions/upload-artifact@v4
        with:
          name: termora-windows-msix-${{ runner.arch }}
          path: |
            build/distributions/*.msix
