From 39ccd0b4a7cf59d2520590edf457b722c77f8eae Mon Sep 17 00:00:00 2001
From: Ronan Burke <burkuscat@gmail.com>
Date: Thu, 22 Feb 2024 21:12:37 +0000
Subject: [PATCH] Working actions for Windows

---
 .../weather-twenty-one-windows-packaged.yml   | 72 +++++++++++++++++++
 .../weather-twenty-one-windows-unpackaged.yml | 50 +++++++++++++
 .../Platforms/Windows/Package.appxmanifest    |  2 +-
 .../WeatherTwentyOne/WeatherTwentyOne.csproj  | 14 ++--
 4 files changed, 128 insertions(+), 10 deletions(-)
 create mode 100644 .github/workflows/weather-twenty-one-windows-packaged.yml
 create mode 100644 .github/workflows/weather-twenty-one-windows-unpackaged.yml

diff --git a/.github/workflows/weather-twenty-one-windows-packaged.yml b/.github/workflows/weather-twenty-one-windows-packaged.yml
new file mode 100644
index 000000000..ad57cbe7c
--- /dev/null
+++ b/.github/workflows/weather-twenty-one-windows-packaged.yml
@@ -0,0 +1,72 @@
+name: Build Windows Packaged Release
+
+on:
+  push:
+    branches: [ main, actions ]
+
+env:
+    DOTNETVERSION: 8.0.201
+    CONFIGURATION: Release # Values: Debug, Release
+    PLATFORM: x64
+    VERSIONNUMBER: 1.${{ github.run_number }}.${{ github.run_attempt }}
+    
+jobs:
+  buildWindowsPackaged:
+    runs-on: windows-latest
+
+    steps:
+      - uses: actions/checkout@v3
+
+      - name: Setup .NET SDK ${{env.DOTNETVERSION}}
+        uses: actions/setup-dotnet@v2
+        with:
+          dotnet-version: '${{env.DOTNETVERSION}}'
+
+      - name: List installed .NET info
+        shell: pwsh
+        run: dotnet --info
+
+      - name: Install MAUI Workload
+        run: dotnet workload install maui --ignore-failed-sources
+
+      - uses: managedcode/MAUIAppVersion@v1
+        with:
+          csproj: '8.0/Apps/WeatherTwentyOne/src/WeatherTwentyOne/WeatherTwentyOne.csproj'
+          version: 0 # Revision number must be 0 on Windows to be allowed on MS Store https://learn.microsoft.com/en-us/windows/apps/publish/publish-your-app/package-version-numbering?pivots=store-installer-msix#version-numbering-for-windows10-packages
+          displayVersion: '${{ env.VERSIONNUMBER }}'
+          printFile: true # optional
+
+      # https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/certutil
+      # commandline util to encode to base64 on windows
+      # openssl enc -base64 -a -in WinSignCert.pfx -out WinSignCert_base64.txt
+      - name: Create signing pfx file from secrets
+        shell: pwsh
+        id: secret-file
+        env:
+          SECRET_DATA1: ${{ secrets.WIN_SIGN_CERT }}
+        run: |
+          $secretFile = "WinSignCert.pfx"; 
+          $encodedBytes = [System.Convert]::FromBase64String($env:SECRET_DATA1); 
+          Set-Content $secretFile -Value $encodedBytes -AsByteStream;
+          Write-Output "::set-output name=SECRET_FILE::$secretFile";
+          Write-Output " ";
+          Write-Output "CHECK FILE EXISTS";
+          Write-Output " ";
+          ls
+
+      - name: Add Cert to Store
+        run: |
+          certutil -user -q -p ${{ secrets.WIN_CERT_PASSWORD }} -importpfx WinSignCert.pfx NoRoot
+
+      # https://docs.microsoft.com/en-us/dotnet/maui/windows/deployment/overview
+      - name: Build Windows MSIX
+        run: |
+          cd .\8.0\Apps\WeatherTwentyOne\src\WeatherTwentyOne\
+          dotnet publish -f net8.0-windows10.0.19041.0 -c:${{ env.CONFIGURATION }} -p:PackageCertificateThumbprint=${{ secrets.WIN_CERT_THUMBPRINT }} -p:AppxPackageSigningEnabled=true -p:WindowsPackageType=MSIX --self-contained
+
+      - name: Upload Windows Artifact
+        uses: actions/upload-artifact@v4
+        with:
+          name: artifacts-windows
+          path: |
+            8.0\Apps\WeatherTwentyOne\src\WeatherTwentyOne\bin\${{ env.PLATFORM }}\${{ env.CONFIGURATION }}\net8.0-windows10.0.19041.0\win10-x64\AppPackages\**\*
diff --git a/.github/workflows/weather-twenty-one-windows-unpackaged.yml b/.github/workflows/weather-twenty-one-windows-unpackaged.yml
new file mode 100644
index 000000000..ea028ab00
--- /dev/null
+++ b/.github/workflows/weather-twenty-one-windows-unpackaged.yml
@@ -0,0 +1,50 @@
+name: Build Windows Unpackaged Release
+
+on:
+  push:
+    branches: [ main, actions ]
+
+env:
+    DOTNETVERSION: 8.0.201
+    CONFIGURATION: Release # Values: Debug, Release
+    PLATFORM: x64
+    VERSIONNUMBER: 1.${{ github.run_number }}.${{ github.run_attempt }}
+    
+jobs:
+  buildWindowsPackaged:
+    runs-on: windows-latest
+
+    steps:
+      - uses: actions/checkout@v3
+
+      - name: Setup .NET SDK ${{env.DOTNETVERSION}}
+        uses: actions/setup-dotnet@v2
+        with:
+          dotnet-version: '${{env.DOTNETVERSION}}'
+
+      - name: List installed .NET info
+        shell: pwsh
+        run: dotnet --info
+
+      - name: Install MAUI Workload
+        run: dotnet workload install maui --ignore-failed-sources
+
+      - uses: managedcode/MAUIAppVersion@v1
+        with:
+          csproj: '8.0/Apps/WeatherTwentyOne/src/WeatherTwentyOne/WeatherTwentyOne.csproj'
+          version: 0 # Revision number must be 0 on Windows to be allowed on MS Store https://learn.microsoft.com/en-us/windows/apps/publish/publish-your-app/package-version-numbering?pivots=store-installer-msix#version-numbering-for-windows10-packages
+          displayVersion: '${{ env.VERSIONNUMBER }}'
+          printFile: true # optional
+
+      # https://docs.microsoft.com/en-us/dotnet/maui/windows/deployment/overview
+      - name: Build Windows EXE
+        run: |
+          cd .\8.0\Apps\WeatherTwentyOne\src\WeatherTwentyOne\
+          dotnet publish -f net8.0-windows10.0.19041.0 -c:${{ env.CONFIGURATION }} -p:AppxPackageSigningEnabled=false -p:WindowsPackageType=None --self-contained
+
+      - name: Upload Windows Artifact
+        uses: actions/upload-artifact@v4
+        with:
+          name: artifacts-windows
+          path: |
+            8.0\Apps\WeatherTwentyOne\src\WeatherTwentyOne\bin\${{ env.PLATFORM }}\${{ env.CONFIGURATION }}\net8.0-windows10.0.19041.0\win10-x64\publish\**\*
diff --git a/8.0/Apps/WeatherTwentyOne/src/WeatherTwentyOne/Platforms/Windows/Package.appxmanifest b/8.0/Apps/WeatherTwentyOne/src/WeatherTwentyOne/Platforms/Windows/Package.appxmanifest
index abc19352b..77582de3f 100644
--- a/8.0/Apps/WeatherTwentyOne/src/WeatherTwentyOne/Platforms/Windows/Package.appxmanifest
+++ b/8.0/Apps/WeatherTwentyOne/src/WeatherTwentyOne/Platforms/Windows/Package.appxmanifest
@@ -9,7 +9,7 @@
   <Identity
     Name="BA0275D4-73C9-4F0C-BF53-CAC4FAD28D12"
     Publisher="CN=User Name"
-    Version="1.0.0.0" />
+    Version="0.0.0.0" />
 
   <Properties>
     <DisplayName>WeatherTwentyOne</DisplayName>
diff --git a/8.0/Apps/WeatherTwentyOne/src/WeatherTwentyOne/WeatherTwentyOne.csproj b/8.0/Apps/WeatherTwentyOne/src/WeatherTwentyOne/WeatherTwentyOne.csproj
index 3d2b6adbf..172057633 100644
--- a/8.0/Apps/WeatherTwentyOne/src/WeatherTwentyOne/WeatherTwentyOne.csproj
+++ b/8.0/Apps/WeatherTwentyOne/src/WeatherTwentyOne/WeatherTwentyOne.csproj
@@ -1,4 +1,4 @@
-<Project Sdk="Microsoft.NET.Sdk">
+<Project Sdk="Microsoft.NET.Sdk">
 
   <PropertyGroup>
     <TargetFrameworks>net8.0-ios;net8.0-maccatalyst;net8.0-android</TargetFrameworks>
@@ -19,10 +19,13 @@
     <ApplicationId>com.companyname.WeatherTwentyOne</ApplicationId>
     <EnablePreviewMsixTooling>true</EnablePreviewMsixTooling>
     <SingleProject>true</SingleProject>
+
+    <!-- Versions -->
+    <ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
     <ApplicationVersion>1</ApplicationVersion>
 
 	<!-- (Optional) Pin to a specific version -->
-	<!--<MauiVersion>6.0.101-preview.11.2349</MauiVersion>-->
+	<MauiVersion>8.0.7</MauiVersion>
 
     <!-- Required for C# Hot Reload -->
 	<UseInterpreter Condition="'$(Configuration)' == 'Debug'">True</UseInterpreter>
@@ -35,13 +38,6 @@
   <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'tizen'">6.5</SupportedOSPlatformVersion>
 
   </PropertyGroup>
-
-	<!-- To generate MSIX as part of dotnet publish -->
-	<PropertyGroup Condition="$(TargetFramework.Contains('-windows')) and '$(Configuration)' == 'Release'">
-		<GenerateAppxPackageOnBuild>true</GenerateAppxPackageOnBuild>
-		<!-- <AppxPackageSigningEnabled>true</AppxPackageSigningEnabled> -->
-	</PropertyGroup>
-
 	<!-- For iOS publish-->
 	<PropertyGroup Condition="$(TargetFramework.Contains('-ios')) and '$(Configuration)' == 'Release'">
 		<RuntimeIdentifier>ios-arm64</RuntimeIdentifier>