name: Build_and_Release_mR-NB
on:
  push:
    branches:
      - v1.78.2-dev
      
permissions:
  contents: write
  
jobs:
  NB-Build-and-Release:
    strategy:
      matrix:
        include:
          - runner: windows-latest
            platform: x64
            arch: x64
          - runner: windows-11-arm
            platform: ARM64
            arch: arm64
    runs-on: ${{ matrix.runner }}
    steps:
      - name: (01) Checkout Repository
        uses: actions/checkout@v5
        
      - name: (02) Setup MSBuild
        uses: microsoft/setup-msbuild@v2
        with:
          vs-version: '17.14.12'
          
      - name: (03) Install and run dotnet-t4 to transform T4 templates
        shell: pwsh
        run: |
          dotnet tool install --global dotnet-t4
          # Refresh PATH to include global tools
          $env:PATH += ";$env:USERPROFILE\.dotnet\tools"
          $ttFile = "$env:GITHUB_WORKSPACE\mRemoteNG\Properties\AssemblyInfo.tt"
          # VS Enterprise 2022 assemblies
          $vsPath = "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Common7\IDE\PublicAssemblies"
          Write-Host "Transforming T4 template"
          t4 $ttFile -P platformType=${{ matrix.platform }} -r:"$vsPath\EnvDTE.dll" -r:"$vsPath\Microsoft.VisualStudio.Interop.dll"
        env:
          PLATFORM: '${{ matrix.platform }}'
  
      - name: (04) Cache NuGet Packages
        uses: actions/cache@v4
        with:
          path: ~/.nuget/packages
          key: ${{ runner.os }}-${{ matrix.arch }}-nuget-${{ hashFiles('**/*.csproj') }}
          restore-keys: |
            ${{ runner.os }}-${{ matrix.arch }}-nuget-
            
      - name: (05) Restore NuGet Packages
        shell: pwsh
        run: dotnet restore
        
      - name: (06) Build Release
        shell: pwsh
        run: |
          msbuild "$Env:GITHUB_WORKSPACE\mRemoteNG.sln" -p:Configuration="Release" -p:Platform=${{ matrix.platform }} /verbosity:minimal
        
      - name: (07) Release Information
        id: version
        shell: pwsh
        run: |
          $assemblyInfoPath = "${{ github.workspace }}\mRemoteNG\Properties\AssemblyInfo.cs"
          $line = Get-Content $assemblyInfoPath | Where-Object { $_ -match 'AssemblyVersion\("(.+?)"\)' }
          if ($line -match 'AssemblyVersion\("(?<ver>\d+\.\d+\.\d+)\.(?<build>\d+)"\)') {
            $version = $matches['ver']
            $build = $matches['build']
          } else {
            throw "Could not extract version and build number"
          }
          $date = Get-Date -Format "yyyyMMdd"
          $zipName = "mRemoteNG-$date-v$version-NB-$build-${{ matrix.arch }}.zip"
          $tag = "$date-v$version-NB-($build)"
          $message = git log -1 --pretty=%B
          echo "message=$message" >> $env:GITHUB_OUTPUT
          echo "zipname=$zipName" >> $env:GITHUB_OUTPUT
          echo "version=$version" >> $env:GITHUB_OUTPUT
          echo "build=$build" >> $env:GITHUB_OUTPUT
          echo "tag=$tag" >> $env:GITHUB_OUTPUT
          $version = "${{ steps.version.outputs.version }}"

      - name: (08) Extract Changelog Section
        id: changelog
        shell: pwsh
        run: |
          $changelogPath = "$env:GITHUB_WORKSPACE\CHANGELOG.md"
          $lines = Get-Content $changelogPath
      
          $startIndex = -1
          for ($i = 0; $i -lt $lines.Count; $i++) {
            if ($lines[$i] -match '^## \[') {
              $startIndex = $i
              break
            }
          }
      
          if ($startIndex -eq -1) {
            throw "No version header found in CHANGELOG.md"
          }
      
          $section = @()
          for ($i = $startIndex + 1; $i -lt $lines.Count; $i++) {
            if ($lines[$i] -match '^## ') {
              break
            }
            $section += $lines[$i]
          }
      
          $joined = $section -join "`n"
          echo "log<<EOF" >> $env:GITHUB_OUTPUT
          echo $joined >> $env:GITHUB_OUTPUT
          echo "EOF" >> $env:GITHUB_OUTPUT
      
          echo "log=$escaped"
                
      - name: (09) Create Zip File
        shell: pwsh
        run: |
          $sourceDir = "$Env:GITHUB_WORKSPACE\mRemoteNG\bin\${{ matrix.platform }}\Release"
          Compress-Archive -Path "$sourceDir\*" -DestinationPath ${{ steps.version.outputs.zipname }}
          echo "File: ${{ steps.version.outputs.zipname }}"

      - name: (10) Create release
        id: create_release
        uses: softprops/action-gh-release@v2
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # This token is provided by Actions, you do not need to create your own token
        with:
          tag_name:  ${{ steps.version.outputs.tag }}
          name: "mRemoteNG ${{ steps.version.outputs.version }} NB ${{ steps.version.outputs.build }}"
          files: ${{ steps.version.outputs.zipname }}
          body: |
            Changes in this Release:
            ${{ steps.changelog.outputs.log }}

            Last Commit Message:
            ${{ steps.version.outputs.message }}

          draft: false
          prerelease: true
