From 0e13e269d0ec816a6528ea0df715a09ba762d5ae Mon Sep 17 00:00:00 2001
From: Thomas Hilscher <thomas.hilscher@gmail.com>
Date: Mon, 21 Aug 2023 19:43:29 +0200
Subject: [PATCH] Moved building MSI to Installer and created Bootstrapper in
 Setup

---
 Elwig.sln                                     | 20 +++++-
 {Setup => Installer}/BuildFilesTransform.xslt |  0
 .../DocumentTemplateComponents.wxs            |  2 +-
 .../DocumentTemplatesTransform.xslt           |  0
 {Setup => Installer}/Files/config.ini         |  0
 {Setup => Installer}/Folders.wxs              |  0
 Installer/Installer.wixproj                   | 66 +++++++++++++++++++
 {Setup => Installer}/MainComponents.wxs       |  2 +-
 {Setup => Installer}/Package.de-at.wxl        |  0
 {Setup => Installer}/Package.wxs              |  8 +--
 {Setup => Installer}/ShortcutComponents.wxs   |  0
 Setup/Bundle.wxs                              | 25 +++++++
 Setup/Setup.wixproj                           | 54 ++++-----------
 13 files changed, 129 insertions(+), 48 deletions(-)
 rename {Setup => Installer}/BuildFilesTransform.xslt (100%)
 rename {Setup => Installer}/DocumentTemplateComponents.wxs (82%)
 rename {Setup => Installer}/DocumentTemplatesTransform.xslt (100%)
 rename {Setup => Installer}/Files/config.ini (100%)
 rename {Setup => Installer}/Folders.wxs (100%)
 create mode 100644 Installer/Installer.wixproj
 rename {Setup => Installer}/MainComponents.wxs (85%)
 rename {Setup => Installer}/Package.de-at.wxl (100%)
 rename {Setup => Installer}/Package.wxs (58%)
 rename {Setup => Installer}/ShortcutComponents.wxs (100%)
 create mode 100644 Setup/Bundle.wxs

diff --git a/Elwig.sln b/Elwig.sln
index 142f9ae..da8a5d8 100644
--- a/Elwig.sln
+++ b/Elwig.sln
@@ -6,7 +6,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Elwig", "Elwig\Elwig.csproj
 EndProject
 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tests", "Tests\Tests.csproj", "{30D7700A-7B0A-4E5D-B839-B4C1D95E307E}"
 EndProject
-Project("{B7DD6F7E-DEF8-4E67-B5B7-07EF123DB6F0}") = "Setup", "Setup\Setup.wixproj", "{4A82C9C0-EB6D-409C-B0B2-09A5F727D16F}"
+Project("{B7DD6F7E-DEF8-4E67-B5B7-07EF123DB6F0}") = "Installer", "Installer\Installer.wixproj", "{4A82C9C0-EB6D-409C-B0B2-09A5F727D16F}"
+EndProject
+Project("{B7DD6F7E-DEF8-4E67-B5B7-07EF123DB6F0}") = "Setup", "Setup\Setup.wixproj", "{952E309C-2090-4978-8996-65900D8E8FA4}"
 EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -68,6 +70,22 @@ Global
 		{4A82C9C0-EB6D-409C-B0B2-09A5F727D16F}.Release|x64.Build.0 = Release|x64
 		{4A82C9C0-EB6D-409C-B0B2-09A5F727D16F}.Release|x86.ActiveCfg = Release|x86
 		{4A82C9C0-EB6D-409C-B0B2-09A5F727D16F}.Release|x86.Build.0 = Release|x86
+		{952E309C-2090-4978-8996-65900D8E8FA4}.Debug|Any CPU.ActiveCfg = Debug|x64
+		{952E309C-2090-4978-8996-65900D8E8FA4}.Debug|Any CPU.Build.0 = Debug|x64
+		{952E309C-2090-4978-8996-65900D8E8FA4}.Debug|ARM64.ActiveCfg = Debug|ARM64
+		{952E309C-2090-4978-8996-65900D8E8FA4}.Debug|ARM64.Build.0 = Debug|ARM64
+		{952E309C-2090-4978-8996-65900D8E8FA4}.Debug|x64.ActiveCfg = Debug|x64
+		{952E309C-2090-4978-8996-65900D8E8FA4}.Debug|x64.Build.0 = Debug|x64
+		{952E309C-2090-4978-8996-65900D8E8FA4}.Debug|x86.ActiveCfg = Debug|x86
+		{952E309C-2090-4978-8996-65900D8E8FA4}.Debug|x86.Build.0 = Debug|x86
+		{952E309C-2090-4978-8996-65900D8E8FA4}.Release|Any CPU.ActiveCfg = Release|x64
+		{952E309C-2090-4978-8996-65900D8E8FA4}.Release|Any CPU.Build.0 = Release|x64
+		{952E309C-2090-4978-8996-65900D8E8FA4}.Release|ARM64.ActiveCfg = Release|ARM64
+		{952E309C-2090-4978-8996-65900D8E8FA4}.Release|ARM64.Build.0 = Release|ARM64
+		{952E309C-2090-4978-8996-65900D8E8FA4}.Release|x64.ActiveCfg = Release|x64
+		{952E309C-2090-4978-8996-65900D8E8FA4}.Release|x64.Build.0 = Release|x64
+		{952E309C-2090-4978-8996-65900D8E8FA4}.Release|x86.ActiveCfg = Release|x86
+		{952E309C-2090-4978-8996-65900D8E8FA4}.Release|x86.Build.0 = Release|x86
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE
diff --git a/Setup/BuildFilesTransform.xslt b/Installer/BuildFilesTransform.xslt
similarity index 100%
rename from Setup/BuildFilesTransform.xslt
rename to Installer/BuildFilesTransform.xslt
diff --git a/Setup/DocumentTemplateComponents.wxs b/Installer/DocumentTemplateComponents.wxs
similarity index 82%
rename from Setup/DocumentTemplateComponents.wxs
rename to Installer/DocumentTemplateComponents.wxs
index 3de3119..a77dcec 100644
--- a/Setup/DocumentTemplateComponents.wxs
+++ b/Installer/DocumentTemplateComponents.wxs
@@ -5,7 +5,7 @@
 				<File Source="$(TargetDir)\paged.polyfill.js" />
 			</Component>
 			<Component>
-				<File Source="$(var.Elwig.ProjectDir)\Documents\style.css" />
+				<File Source="$(var.ElwigProjectDir)\Documents\style.css" />
 			</Component>
 		</ComponentGroup>
 	</Fragment>
diff --git a/Setup/DocumentTemplatesTransform.xslt b/Installer/DocumentTemplatesTransform.xslt
similarity index 100%
rename from Setup/DocumentTemplatesTransform.xslt
rename to Installer/DocumentTemplatesTransform.xslt
diff --git a/Setup/Files/config.ini b/Installer/Files/config.ini
similarity index 100%
rename from Setup/Files/config.ini
rename to Installer/Files/config.ini
diff --git a/Setup/Folders.wxs b/Installer/Folders.wxs
similarity index 100%
rename from Setup/Folders.wxs
rename to Installer/Folders.wxs
diff --git a/Installer/Installer.wixproj b/Installer/Installer.wixproj
new file mode 100644
index 0000000..3d54723
--- /dev/null
+++ b/Installer/Installer.wixproj
@@ -0,0 +1,66 @@
+<Project Sdk="WixToolset.Sdk/4.0.1">
+  <PropertyGroup>
+    <HarvestFileSuppressUniqueIds>false</HarvestFileSuppressUniqueIds>
+    <HarvestFileGenerateGuidsNow>true</HarvestFileGenerateGuidsNow>
+    <HarvestDirectorySuppressRegistry>false</HarvestDirectorySuppressRegistry>
+    <HarvestDirectoryVerboseOutput>true</HarvestDirectoryVerboseOutput>
+    <HarvestProjectsDirectoryIds>InstallFolder</HarvestProjectsDirectoryIds>
+    <CabinetCachePath>$(OutputPath)bin\cabcache\</CabinetCachePath>
+    <ReuseCabinetCache>True</ReuseCabinetCache>
+    <SuppressValidation>True</SuppressValidation>
+    <BuildProjectReferences>False</BuildProjectReferences>
+    <OutputName>Elwig</OutputName>
+    <Cultures>de-AT</Cultures>
+  </PropertyGroup>
+  <UsingTask TaskName="GetFileVersion" TaskFactory="RoslynCodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll">
+    <ParameterGroup>
+      <AssemblyPath ParameterType="System.String" Required="true" />
+      <Version ParameterType="System.String" Output="true" />
+    </ParameterGroup>
+    <Task>
+      <Using Namespace="System.Diagnostics" />
+      <Code Type="Fragment" Language="cs"><![CDATA[
+          this.Version = FileVersionInfo.GetVersionInfo(this.AssemblyPath).ProductVersion;  
+        ]]></Code>
+    </Task>
+  </UsingTask>
+  <Target Name="CustomBeforeBuild" BeforeTargets="BeforeBuild">
+    <Exec Command="curl -s -L &quot;https://unpkg.com/pagedjs/dist/paged.polyfill.js&quot; -o &quot;$(TargetDir)paged.polyfill.js&quot;" />
+    <Exec Command="curl -s &quot;http://www.columbia.edu/~em36/PDFtoPrinter.exe&quot; -z &quot;$(TargetDir)PDFtoPrinter.exe&quot; -o &quot;$(TargetDir)PDFtoPrinter.exe&quot;" />
+    <Exec Command="dotnet publish &quot;$(SolutionDir)Elwig\Elwig.csproj&quot; &quot;/p:PublishProfile=$(SolutionDir)\Elwig\Properties\PublishProfiles\FolderProfile.pubxml&quot;" />
+    <GetFileVersion AssemblyPath="..\Elwig\bin\Publish\Elwig.exe">
+      <Output TaskParameter="Version" PropertyName="ElwigFileVersion" />
+    </GetFileVersion>
+    <PropertyGroup>
+      <DefineConstants>ProductVersion=$(ElwigFileVersion);BuildPath=..\Elwig\bin\Publish;DocumentTemplatesPath=..\Elwig\Documents;ElwigProjectDir=..\Elwig</DefineConstants>
+    </PropertyGroup>
+  </Target>
+  <ItemGroup>
+    <HarvestDirectory Include="../Elwig/bin/Publish">
+      <ComponentGroupName>BuildFiles</ComponentGroupName>
+      <DirectoryRefId>InstallFolder</DirectoryRefId>
+      <SuppressRootDirectory>true</SuppressRootDirectory>
+      <PreprocessorVariable>BuildPath</PreprocessorVariable>
+      <Transforms>BuildFilesTransform.xslt</Transforms>
+    </HarvestDirectory>
+    <BindPath BindName="BuildBindPath" Include="../Elwig/bin/Publish" />
+  </ItemGroup>
+  <ItemGroup>
+    <HarvestDirectory Include="../Elwig/Documents">
+      <ComponentGroupName>DocumentTemplates</ComponentGroupName>
+      <DirectoryRefId>ConfigFolderResources</DirectoryRefId>
+      <SuppressRootDirectory>true</SuppressRootDirectory>
+      <PreprocessorVariable>DocumentTemplatesPath</PreprocessorVariable>
+      <Transforms>DocumentTemplatesTransform.xslt</Transforms>
+    </HarvestDirectory>
+    <BindPath BindName="DocumentTemplateBindPath" Include="../Elwig/Documents" />
+  </ItemGroup>
+  <ItemGroup>
+    <None Include="DocumentTemplatesTransform.xslt" />
+    <None Include="BuildFilesTransform.xslt" />
+    <None Include="Files\config.ini" />
+  </ItemGroup>
+  <ItemGroup>
+    <PackageReference Include="WixToolset.Heat" Version="4.0.1" />
+  </ItemGroup>
+</Project>
\ No newline at end of file
diff --git a/Setup/MainComponents.wxs b/Installer/MainComponents.wxs
similarity index 85%
rename from Setup/MainComponents.wxs
rename to Installer/MainComponents.wxs
index 168f519..e400d59 100644
--- a/Setup/MainComponents.wxs
+++ b/Installer/MainComponents.wxs
@@ -2,7 +2,7 @@
   <Fragment>
     <ComponentGroup Id="MainComponents">
       <Component Directory="InstallFolder">
-        <File Source="$(var.Elwig.ProjectDir)\bin\Publish\Elwig.exe" Id="Elwig.exe"/>
+        <File Source="$(var.ElwigProjectDir)\bin\Publish\Elwig.exe" Id="Elwig.exe"/>
       </Component>
       <Component Directory="ConfigFolder" Permanent="true" NeverOverwrite="true">
         <File Source="$(ProjectDir)\Files\config.ini" Id="config.ini"/>
diff --git a/Setup/Package.de-at.wxl b/Installer/Package.de-at.wxl
similarity index 100%
rename from Setup/Package.de-at.wxl
rename to Installer/Package.de-at.wxl
diff --git a/Setup/Package.wxs b/Installer/Package.wxs
similarity index 58%
rename from Setup/Package.wxs
rename to Installer/Package.wxs
index 2c469f4..f5f04bc 100644
--- a/Setup/Package.wxs
+++ b/Installer/Package.wxs
@@ -1,9 +1,9 @@
 <Wix xmlns="http://wixtoolset.org/schemas/v4/wxs">
-  <Package Name="Elwig" Manufacturer="Elwig" Version="!(bind.FileVersion.Elwig.exe)" Language="3079" UpgradeCode="a459416a-756a-4be6-b6f4-40872c8425a7" Compressed="true">
-    <MajorUpgrade DowngradeErrorMessage="!(loc.DowngradeError)" />
-	<MediaTemplate EmbedCab="true"/>
+  <Package Name="Elwig" Manufacturer="Elwig" Version="$(var.ProductVersion)" Language="3079" UpgradeCode="a459416a-756a-4be6-b6f4-40872c8425a7" Compressed="true">
+    <MajorUpgrade DowngradeErrorMessage="!(loc.DowngradeError)" AllowSameVersionUpgrades="no" />
+	<MediaTemplate EmbedCab="true" MaximumUncompressedMediaSize="10"/>
 
-	<Icon Id="icon.ico" SourceFile="$(var.Elwig.ProjectDir)\elwig.ico"/>
+	<Icon Id="icon.ico" SourceFile="$(var.ElwigProjectDir)\elwig.ico"/>
 	<Property Id="ARPPRODUCTICON" Value="icon.ico"/>
 
 	<Feature Id="Main">
diff --git a/Setup/ShortcutComponents.wxs b/Installer/ShortcutComponents.wxs
similarity index 100%
rename from Setup/ShortcutComponents.wxs
rename to Installer/ShortcutComponents.wxs
diff --git a/Setup/Bundle.wxs b/Setup/Bundle.wxs
new file mode 100644
index 0000000..ed298fb
--- /dev/null
+++ b/Setup/Bundle.wxs
@@ -0,0 +1,25 @@
+<Wix xmlns="http://wixtoolset.org/schemas/v4/wxs" xmlns:bal="http://wixtoolset.org/schemas/v4/wxs/bal" xmlns:util="http://wixtoolset.org/schemas/v4/wxs/util">
+  <Bundle Name="Elwig" Manufacturer="Elwig" Version="!(bind.packageVersion.ElwigMsi)" UpgradeCode="f3c8fcab-c37c-43aa-9ab8-e42f4bb518b7" IconSourceFile="$(var.ElwigProjectDir)\elwig.ico" >
+	<BootstrapperApplication>
+      <bal:WixStandardBootstrapperApplication LicenseUrl="" Theme="hyperlinkLicense" LogoFile="$(var.ElwigProjectDir)\elwig.png" SuppressOptionsUI="yes" ShowVersion="yes" />   
+	</BootstrapperApplication>
+	  
+    <util:RegistrySearch Id="VCredistx86" Variable="VCredistx86" Result="exists" Root="HKLM" Key="SOFTWARE\Classes\Installer\Dependencies\VC,redist.x86,x86,14.36,bundle" />
+    <util:RegistrySearch Id="Webview2Machine" Variable="Webview2Machine" Result="exists" Root="HKLM" Key="SOFTWARE\WOW6432Node\Microsoft\EdgeUpdate\Clients\{F3017226-FE2A-4295-8BDF-00C3A9A7E4C5}" />
+    <util:RegistrySearch Id="Webview2User" Variable="Webview2User" Result="exists" Root="HKCU" Key="Software\Microsoft\EdgeUpdate\Clients\{F3017226-FE2A-4295-8BDF-00C3A9A7E4C5}" />
+
+    <Chain>
+      <ExePackage Id="VCredistx86Installer" DisplayName="VC Redist x86 installer" Name="VC_redist.x86.exe" Cache="remove" Compressed="yes" PerMachine="yes" 
+				  Permanent="yes" Vital="yes" SourceFile="$(TargetDir)VC_redist.x86.exe" InstallArguments="/install /passive /norestart" DetectCondition="VCredistx86">
+      </ExePackage>
+
+      <ExePackage Id="MicrosoftEdgeWebview2" DisplayName="Microsoft Edge Webview2 Runtime" Name="MicrosoftEdgeWebview2Setup.exe" Cache="remove"
+				  Compressed="yes" PerMachine="yes" Permanent ="yes" Vital ="no" SourceFile="$(TargetDir)MicrosoftEdgeWebview2Setup.exe" InstallArguments="/silent /install"
+				  UninstallArguments="/silent /uninstall" DetectCondition="Webview2Machine OR Webview2User" >
+      </ExePackage>
+
+      <MsiPackage Id="ElwigMsi" SourceFile="$(var.Installer.TargetDir)\Elwig.msi" Permanent="no" Compressed="yes" Vital="yes"/>
+    </Chain>
+	  
+  </Bundle>
+</Wix>
diff --git a/Setup/Setup.wixproj b/Setup/Setup.wixproj
index 84ef8d2..5ba6252 100644
--- a/Setup/Setup.wixproj
+++ b/Setup/Setup.wixproj
@@ -1,47 +1,19 @@
-<Project Sdk="WixToolset.Sdk/4.0.1">
+<Project Sdk="WixToolset.Sdk/4.0.1">
   <PropertyGroup>
-    <HarvestFileSuppressUniqueIds>false</HarvestFileSuppressUniqueIds>
-    <HarvestFileGenerateGuidsNow>true</HarvestFileGenerateGuidsNow>
-    <HarvestDirectorySuppressRegistry>false</HarvestDirectorySuppressRegistry>
-    <HarvestDirectoryVerboseOutput>true</HarvestDirectoryVerboseOutput>
-    <HarvestProjectsDirectoryIds>InstallFolder</HarvestProjectsDirectoryIds>
-  </PropertyGroup>
-  <PropertyGroup>
-    <DefineConstants>BuildPath=../Elwig/bin/Publish;DocumentTemplatesPath=../Elwig/Documents</DefineConstants>
-    <PreBuildEvent>curl -s -L "https://unpkg.com/pagedjs/dist/paged.polyfill.js" -o "$(TargetDir)paged.polyfill.js" &amp; curl -s "http://www.columbia.edu/~em36/PDFtoPrinter.exe" -z "$(TargetDir)PDFtoPrinter.exe" -o "$(TargetDir)PDFtoPrinter.exe" &amp; dotnet publish "$(SolutionDir)Elwig\Elwig.csproj" "/p:PublishProfile=$(SolutionDir)\Elwig\Properties\PublishProfiles\FolderProfile.pubxml"</PreBuildEvent>
+    <OutputType>Bundle</OutputType>
     <OutputName>Elwig</OutputName>
-  </PropertyGroup>
-  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
     <Cultures>de-AT</Cultures>
-    <OutputPath>bin\</OutputPath>
   </PropertyGroup>
+  <Target Name="CustomBeforeBuild" BeforeTargets="BeforeBuild">
+    <Exec Command='curl -L -s "https://go.microsoft.com/fwlink/p/?LinkId=2124703" -z "$(TargetDir)MicrosoftEdgeWebview2Setup.exe" -o "$(TargetDir)MicrosoftEdgeWebview2Setup.exe"' />
+    <Exec Command='curl -L -s "https://aka.ms/vs/17/release/vc_redist.x86.exe" -z "$(TargetDir)VC_redist.x86.exe" -o "$(TargetDir)VC_redist.x86.exe"' />
+    <PropertyGroup>
+      <DefineConstants>ElwigProjectDir=..\Elwig</DefineConstants>
+    </PropertyGroup>
+  </Target>
   <ItemGroup>
-    <HarvestDirectory Include="../Elwig/bin/Publish">
-      <ComponentGroupName>BuildFiles</ComponentGroupName>
-      <DirectoryRefId>InstallFolder</DirectoryRefId>
-      <SuppressRootDirectory>true</SuppressRootDirectory>
-      <PreprocessorVariable>BuildPath</PreprocessorVariable>
-      <Transforms>BuildFilesTransform.xslt</Transforms>
-    </HarvestDirectory>
-    <BindPath BindName="BuildBindPath" Include="../Elwig/bin/Publish" />
+    <ProjectReference Include="..\Installer\Installer.wixproj" />
+    <PackageReference Include="WixToolset.Bal.wixext" Version="4.0.1" />
+    <PackageReference Include="WixToolset.Util.wixext" Version="4.0.1" />
   </ItemGroup>
-  <ItemGroup>
-    <HarvestDirectory Include="../Elwig/Documents">
-      <ComponentGroupName>DocumentTemplates</ComponentGroupName>
-      <DirectoryRefId>ConfigFolderResources</DirectoryRefId>
-      <SuppressRootDirectory>true</SuppressRootDirectory>
-      <PreprocessorVariable>DocumentTemplatesPath</PreprocessorVariable>
-      <Transforms>DocumentTemplatesTransform.xslt</Transforms>
-    </HarvestDirectory>
-    <BindPath BindName="DocumentTemplateBindPath" Include="../Elwig/Documents" />
-  </ItemGroup>
-  <ItemGroup>
-    <None Include="DocumentTemplatesTransform.xslt" />
-    <None Include="BuildFilesTransform.xslt" />
-    <None Include="Files\config.ini" />
-  </ItemGroup>
-  <ItemGroup>
-    <ProjectReference Include="..\Elwig\Elwig.csproj" PublishProfile="..\Elwig\Properties\PublishProfiles\FolderProfile.pubxml" Publish="true" />
-    <PackageReference Include="WixToolset.Heat" Version="4.0.1" />
-  </ItemGroup>
-</Project>
+</Project>
\ No newline at end of file