From dc30c8f251f904c33236c66037afadcc7595417e Mon Sep 17 00:00:00 2001 From: Oleg Shilo Date: Sat, 20 Jul 2024 18:18:57 +1000 Subject: [PATCH] - VSTemplates update. Added warning message about `project.GUID`. Triggered by #1597 --- .../WixSharp (.NET Core)/Program.cs | 3 ++ .../WixSharp Managed Setup (WiX3)/Program.cs | 1 + .../WixSharp Managed Setup (WiX4)/Program.cs | 1 + .../Program.cs | 1 + .../Program.cs | 3 +- .../Program.cs | 1 + .../Program.cs | 3 +- .../Program.cs | 3 +- .../Program.cs | 3 +- .../WixSharp Setup (WiX3)/Program.cs | 1 + .../WixSharp Setup (WiX4)/Program.cs | 3 +- .../Program.cs | 1 + .../Program.cs | 1 + .../Program.cs | 1 + .../Program.cs | 1 + .../Wix# Samples/Features/setup.cs | 6 +-- .../Managed Setup/SetupEvents/setup.cs | 1 + Source/src/WixSharp/CommonTasks.cs | 15 +----- Source/src/WixSharp/Compiler.cs | 47 +++++++++++++++++++ Source/src/WixSharp/ProjectValidator.cs | 5 ++ Source/src/WixSharp/Utilities/Utils.cs | 28 +++++++++++ 21 files changed, 108 insertions(+), 21 deletions(-) diff --git a/Source/Templates.2022/ProjectTemplates/WixSharp (.NET Core)/Program.cs b/Source/Templates.2022/ProjectTemplates/WixSharp (.NET Core)/Program.cs index 34927b3c..ba56e850 100644 --- a/Source/Templates.2022/ProjectTemplates/WixSharp (.NET Core)/Program.cs +++ b/Source/Templates.2022/ProjectTemplates/WixSharp (.NET Core)/Program.cs @@ -13,6 +13,9 @@ // project.AddUIProject("$safeprojectname$.UI"); // name of the 'Custom UI Library' project in the solution +#warning "DON'T FORGET to replace this with a freshly generated GUID and remove this `#warning` statement." +project.GUID = new Guid("6fe30b47-2577-43ad-9095-1861ba25889b"); + project.Load += (e) => { Native.MessageBox("OnLoad", "WixSharp - .NET8"); diff --git a/Source/Templates.2022/ProjectTemplates/WixSharp Managed Setup (WiX3)/Program.cs b/Source/Templates.2022/ProjectTemplates/WixSharp Managed Setup (WiX3)/Program.cs index 726d568e..627b3010 100644 --- a/Source/Templates.2022/ProjectTemplates/WixSharp Managed Setup (WiX3)/Program.cs +++ b/Source/Templates.2022/ProjectTemplates/WixSharp Managed Setup (WiX3)/Program.cs @@ -13,6 +13,7 @@ static void Main() new Dir(@"%ProgramFiles%\My Company\My Product", new File("Program.cs"))); + #warning "DON'T FORGET to replace this with a freshly generated GUID and remove this `#warning` statement." project.GUID = new Guid("6fe30b47-2577-43ad-9095-1861ba25889b"); project.ManagedUI = ManagedUI.Empty; //no standard UI dialogs diff --git a/Source/Templates.2022/ProjectTemplates/WixSharp Managed Setup (WiX4)/Program.cs b/Source/Templates.2022/ProjectTemplates/WixSharp Managed Setup (WiX4)/Program.cs index 726d568e..627b3010 100644 --- a/Source/Templates.2022/ProjectTemplates/WixSharp Managed Setup (WiX4)/Program.cs +++ b/Source/Templates.2022/ProjectTemplates/WixSharp Managed Setup (WiX4)/Program.cs @@ -13,6 +13,7 @@ static void Main() new Dir(@"%ProgramFiles%\My Company\My Product", new File("Program.cs"))); + #warning "DON'T FORGET to replace this with a freshly generated GUID and remove this `#warning` statement." project.GUID = new Guid("6fe30b47-2577-43ad-9095-1861ba25889b"); project.ManagedUI = ManagedUI.Empty; //no standard UI dialogs diff --git a/Source/Templates.2022/ProjectTemplates/WixSharp Managed Setup - Cusom UI (WiX3)/Program.cs b/Source/Templates.2022/ProjectTemplates/WixSharp Managed Setup - Cusom UI (WiX3)/Program.cs index 0902e810..02b1a4d3 100644 --- a/Source/Templates.2022/ProjectTemplates/WixSharp Managed Setup - Cusom UI (WiX3)/Program.cs +++ b/Source/Templates.2022/ProjectTemplates/WixSharp Managed Setup - Cusom UI (WiX3)/Program.cs @@ -12,6 +12,7 @@ static void Main() new Dir(@"%ProgramFiles%\My Company\My Product", new File("Program.cs"))); + #warning "DON'T FORGET to replace this with a freshly generated GUID and remove this `#warning` statement." project.GUID = new Guid("6fe30b47-2577-43ad-9095-1861ba25889b"); //custom set of standard UI dialogs diff --git a/Source/Templates.2022/ProjectTemplates/WixSharp Managed Setup - Cusom UI (WiX4)/Program.cs b/Source/Templates.2022/ProjectTemplates/WixSharp Managed Setup - Cusom UI (WiX4)/Program.cs index 992a971d..d21d4b9b 100644 --- a/Source/Templates.2022/ProjectTemplates/WixSharp Managed Setup - Cusom UI (WiX4)/Program.cs +++ b/Source/Templates.2022/ProjectTemplates/WixSharp Managed Setup - Cusom UI (WiX4)/Program.cs @@ -11,7 +11,8 @@ static void Main() var project = new ManagedProject("MyProduct", new Dir(@"%ProgramFiles%\My Company\My Product", new File("Program.cs"))); - + + #warning "DON'T FORGET to replace this with a freshly generated GUID and remove this `#warning` statement." project.GUID = new Guid("6fe30b47-2577-43ad-9095-1861ba25889b"); //custom set of standard UI dialogs diff --git a/Source/Templates.2022/ProjectTemplates/WixSharp Managed Setup - Custom Dialog (WiX3)/Program.cs b/Source/Templates.2022/ProjectTemplates/WixSharp Managed Setup - Custom Dialog (WiX3)/Program.cs index 89c332ba..1168899c 100644 --- a/Source/Templates.2022/ProjectTemplates/WixSharp Managed Setup - Custom Dialog (WiX3)/Program.cs +++ b/Source/Templates.2022/ProjectTemplates/WixSharp Managed Setup - Custom Dialog (WiX3)/Program.cs @@ -15,6 +15,7 @@ static void Main() new Dir(@"%ProgramFiles%\My Company\My Product", new File("Program.cs"))); + #warning "DON'T FORGET to replace this with a freshly generated GUID and remove this `#warning` statement." project.GUID = new Guid("6fe30b47-2577-43ad-9095-1861ba25889b"); //project.ManagedUI = ManagedUI.Empty; //no standard UI dialogs diff --git a/Source/Templates.2022/ProjectTemplates/WixSharp Managed Setup - Custom Dialog (WiX4)/Program.cs b/Source/Templates.2022/ProjectTemplates/WixSharp Managed Setup - Custom Dialog (WiX4)/Program.cs index c7bc54c0..7224089b 100644 --- a/Source/Templates.2022/ProjectTemplates/WixSharp Managed Setup - Custom Dialog (WiX4)/Program.cs +++ b/Source/Templates.2022/ProjectTemplates/WixSharp Managed Setup - Custom Dialog (WiX4)/Program.cs @@ -13,7 +13,8 @@ static void Main() var project = new ManagedProject("MyProduct", new Dir(@"%ProgramFiles%\My Company\My Product", new File("Program.cs"))); - + + #warning "DON'T FORGET to replace this with a freshly generated GUID and remove this `#warning` statement." project.GUID = new Guid("6fe30b47-2577-43ad-9095-1861ba25889b"); //project.ManagedUI = ManagedUI.Empty; //no standard UI dialogs diff --git a/Source/Templates.2022/ProjectTemplates/WixSharp Managed Setup - Custom WPF UI (WiX3)/Program.cs b/Source/Templates.2022/ProjectTemplates/WixSharp Managed Setup - Custom WPF UI (WiX3)/Program.cs index 124f7be3..825d654b 100644 --- a/Source/Templates.2022/ProjectTemplates/WixSharp Managed Setup - Custom WPF UI (WiX3)/Program.cs +++ b/Source/Templates.2022/ProjectTemplates/WixSharp Managed Setup - Custom WPF UI (WiX3)/Program.cs @@ -12,7 +12,8 @@ static void Main() var project = new ManagedProject("MyProduct", new Dir(@"%ProgramFiles%\My Company\My Product", new File("Program.cs"))); - + + #warning "DON'T FORGET to replace this with a freshly generated GUID and remove this `#warning` statement." project.GUID = new Guid("6fe30b47-2577-43ad-9095-1861ba25889b"); // project.ManagedUI = ManagedUI.DefaultWpf; // all stock UI dialogs diff --git a/Source/Templates.2022/ProjectTemplates/WixSharp Managed Setup - Custom WPF UI (WiX4)/Program.cs b/Source/Templates.2022/ProjectTemplates/WixSharp Managed Setup - Custom WPF UI (WiX4)/Program.cs index 124f7be3..825d654b 100644 --- a/Source/Templates.2022/ProjectTemplates/WixSharp Managed Setup - Custom WPF UI (WiX4)/Program.cs +++ b/Source/Templates.2022/ProjectTemplates/WixSharp Managed Setup - Custom WPF UI (WiX4)/Program.cs @@ -12,7 +12,8 @@ static void Main() var project = new ManagedProject("MyProduct", new Dir(@"%ProgramFiles%\My Company\My Product", new File("Program.cs"))); - + + #warning "DON'T FORGET to replace this with a freshly generated GUID and remove this `#warning` statement." project.GUID = new Guid("6fe30b47-2577-43ad-9095-1861ba25889b"); // project.ManagedUI = ManagedUI.DefaultWpf; // all stock UI dialogs diff --git a/Source/Templates.2022/ProjectTemplates/WixSharp Setup (WiX3)/Program.cs b/Source/Templates.2022/ProjectTemplates/WixSharp Setup (WiX3)/Program.cs index a069beb3..344fd0a4 100644 --- a/Source/Templates.2022/ProjectTemplates/WixSharp Setup (WiX3)/Program.cs +++ b/Source/Templates.2022/ProjectTemplates/WixSharp Setup (WiX3)/Program.cs @@ -11,6 +11,7 @@ static void Main() new Dir(@"%ProgramFiles%\My Company\My Product", new File("Program.cs"))); + #warning "DON'T FORGET to replace this with a freshly generated GUID and remove this `#warning` statement." project.GUID = new Guid("6fe30b47-2577-43ad-9095-1861ba25889b"); //project.SourceBaseDir = ""; //project.OutDir = ""; diff --git a/Source/Templates.2022/ProjectTemplates/WixSharp Setup (WiX4)/Program.cs b/Source/Templates.2022/ProjectTemplates/WixSharp Setup (WiX4)/Program.cs index a069beb3..b98b4f8a 100644 --- a/Source/Templates.2022/ProjectTemplates/WixSharp Setup (WiX4)/Program.cs +++ b/Source/Templates.2022/ProjectTemplates/WixSharp Setup (WiX4)/Program.cs @@ -10,7 +10,8 @@ static void Main() var project = new Project("MyProduct", new Dir(@"%ProgramFiles%\My Company\My Product", new File("Program.cs"))); - + + #warning "DON'T FORGET to replace this with a freshly generated GUID and remove this `#warning` statement." project.GUID = new Guid("6fe30b47-2577-43ad-9095-1861ba25889b"); //project.SourceBaseDir = ""; //project.OutDir = ""; diff --git a/Source/Templates.2022/ProjectTemplates/WixSharp Setup - Bootstrapper (WiX3)/Program.cs b/Source/Templates.2022/ProjectTemplates/WixSharp Setup - Bootstrapper (WiX3)/Program.cs index 8f6414d9..256c6882 100644 --- a/Source/Templates.2022/ProjectTemplates/WixSharp Setup - Bootstrapper (WiX3)/Program.cs +++ b/Source/Templates.2022/ProjectTemplates/WixSharp Setup - Bootstrapper (WiX3)/Program.cs @@ -16,6 +16,7 @@ static void Main() new MsiPackage(productMsi) { DisplayInternalUI = true }); bootstrapper.Version = new Version("1.0.0.0"); + #warning "DON'T FORGET to replace this with a freshly generated GUID and remove this `#warning` statement." bootstrapper.UpgradeCode = new Guid("6f330b47-2577-43ad-9095-1861bb25844b"); // bootstrapper.Application = new SilentBootstrapperApplication(); // bootstrapper.PreserveTempFiles = true; diff --git a/Source/Templates.2022/ProjectTemplates/WixSharp Setup - Bootstrapper (WiX4)/Program.cs b/Source/Templates.2022/ProjectTemplates/WixSharp Setup - Bootstrapper (WiX4)/Program.cs index 02a6eae2..1fcc2cbe 100644 --- a/Source/Templates.2022/ProjectTemplates/WixSharp Setup - Bootstrapper (WiX4)/Program.cs +++ b/Source/Templates.2022/ProjectTemplates/WixSharp Setup - Bootstrapper (WiX4)/Program.cs @@ -16,6 +16,7 @@ static void Main() new MsiPackage(productMsi) { DisplayInternalUI = true }); bootstrapper.Version = new Version("1.0.0.0"); + #warning "DON'T FORGET to replace this with a freshly generated GUID and remove this `#warning` statement." bootstrapper.UpgradeCode = new Guid("6f330b47-2577-43ad-9095-1861bb25844b"); // bootstrapper.PreserveTempFiles = true; diff --git a/Source/Templates.2022/ProjectTemplates/WixSharp Setup - Bootstrapper Custom BA (WiX3)/Program.cs b/Source/Templates.2022/ProjectTemplates/WixSharp Setup - Bootstrapper Custom BA (WiX3)/Program.cs index ab9e718c..b2cba77c 100644 --- a/Source/Templates.2022/ProjectTemplates/WixSharp Setup - Bootstrapper Custom BA (WiX3)/Program.cs +++ b/Source/Templates.2022/ProjectTemplates/WixSharp Setup - Bootstrapper Custom BA (WiX3)/Program.cs @@ -19,6 +19,7 @@ static void Main() }); bootstrapper.Version = new Version("1.0.0.0"); + #warning "DON'T FORGET to replace this with a freshly generated GUID and remove this `#warning` statement." bootstrapper.UpgradeCode = new Guid("6f330b47-2577-43ad-9095-1861bb25844b"); bootstrapper.Application = new ManagedBootstrapperApplication("%this%"); diff --git a/Source/Templates.2022/ProjectTemplates/WixSharp Setup - Bootstrapper Custom BA (WiX4)/Program.cs b/Source/Templates.2022/ProjectTemplates/WixSharp Setup - Bootstrapper Custom BA (WiX4)/Program.cs index ab9e718c..b2cba77c 100644 --- a/Source/Templates.2022/ProjectTemplates/WixSharp Setup - Bootstrapper Custom BA (WiX4)/Program.cs +++ b/Source/Templates.2022/ProjectTemplates/WixSharp Setup - Bootstrapper Custom BA (WiX4)/Program.cs @@ -19,6 +19,7 @@ static void Main() }); bootstrapper.Version = new Version("1.0.0.0"); + #warning "DON'T FORGET to replace this with a freshly generated GUID and remove this `#warning` statement." bootstrapper.UpgradeCode = new Guid("6f330b47-2577-43ad-9095-1861bb25844b"); bootstrapper.Application = new ManagedBootstrapperApplication("%this%"); diff --git a/Source/src/WixSharp.Samples/Wix# Samples/Features/setup.cs b/Source/src/WixSharp.Samples/Wix# Samples/Features/setup.cs index 9c78021f..88529f63 100644 --- a/Source/src/WixSharp.Samples/Wix# Samples/Features/setup.cs +++ b/Source/src/WixSharp.Samples/Wix# Samples/Features/setup.cs @@ -2,6 +2,8 @@ //css_ref Wix_bin\WixToolset.Dtf.WindowsInstaller.dll; //css_ref System.Core.dll; using System; +using System.Linq; + // using System.IO; using System.Windows.Forms; using WixSharp; @@ -40,8 +42,6 @@ static public void Main() project.UI = WUI.WixUI_FeatureTree; project.DefaultFeature = binaries; //this line is optional - project.UnelevateAfterInstallEvent(); - project.DefaultDeferredProperties += ",FEATURE_INSTALL_PATH2"; project.AfterInstall += (SetupEventArgs e) => @@ -56,7 +56,7 @@ static public void Main() } }; - + project.AddCustomActionRefAssembliesOf(typeof(Script)); // project.PreserveTempFiles = true; diff --git a/Source/src/WixSharp.Samples/Wix# Samples/Managed Setup/SetupEvents/setup.cs b/Source/src/WixSharp.Samples/Wix# Samples/Managed Setup/SetupEvents/setup.cs index fb2255e1..eeced8e9 100644 --- a/Source/src/WixSharp.Samples/Wix# Samples/Managed Setup/SetupEvents/setup.cs +++ b/Source/src/WixSharp.Samples/Wix# Samples/Managed Setup/SetupEvents/setup.cs @@ -80,6 +80,7 @@ static public void Main() }; // project.UnelevateAfterInstallEvent(); // just for demo purposes + Compiler.AutoGeneration.AddManagedCustomActionDependencies = true; project.GUID = new Guid("6f330b47-2577-43ad-9095-1861ba25889b"); // project.PreserveTempFiles = true; diff --git a/Source/src/WixSharp/CommonTasks.cs b/Source/src/WixSharp/CommonTasks.cs index 94b224e0..1fa16c77 100644 --- a/Source/src/WixSharp/CommonTasks.cs +++ b/Source/src/WixSharp/CommonTasks.cs @@ -1100,19 +1100,7 @@ static public void Wxl3_to_Wxl4(this string source, string destination) /// The Type from the managed CA. public static void AddCustomActionRefAssembliesOf(this Project project, Type type) { - var dependencies = type.Assembly - .GetReferencedAssemblies() - .Where(x => !x.Name.StartsWith("System")) - .Select(x => - { - try - { - return System.Reflection.Assembly.Load(x).Location; - } - catch { return null; } - }) - .Where(x => x.IsNotEmpty() && !x.StartsWith(Environment.SpecialFolder.Windows.GetPath(), true)) - .ToArray(); + var dependencies = type.Assembly.GetRefAssemblies(); project.DefaultRefAssemblies.AddRange(dependencies); } @@ -2020,6 +2008,7 @@ public static ManagedProject MapAsDeferredProperty(this ManagedProject project, /// /// The project. /// + [Obsolete("This method is obsolete. Use `project.AfterInstallEventExecution = EventExecution.MsiSessionScopeImmediate;` instead")] public static ManagedProject UnelevateAfterInstallEvent(this ManagedProject project) { project.WixSourceGenerated += diff --git a/Source/src/WixSharp/Compiler.cs b/Source/src/WixSharp/Compiler.cs index c5403466..5ae69928 100644 --- a/Source/src/WixSharp/Compiler.cs +++ b/Source/src/WixSharp/Compiler.cs @@ -37,6 +37,7 @@ THE SOFTWARE. using System.Text; using System.Text.RegularExpressions; using System.Threading; +using System.Windows.Forms; using System.Xml; using System.Xml.Linq; using WixSharp.Bootstrapper; @@ -130,6 +131,16 @@ internal object WxsGenerationSynchObject /// public bool ForceComponentIdUniqueness = false; + /// + /// The option for adding managed custom action dependencies automatically. + ///

This option is a convenient way to add all the dependencies automatically setting them manually. + /// The compiler analyzes the CA assembly dependencies and adds them to the CA binary that is packaged + /// with "WixToolset.Dtf.MakeSfxCA.exe".

+ ///

Note, this method may unnecessarily increase the size of the msi as not all CA dependency assemblies + /// may be required at runtime (during the installation).

+ ///
+ public bool AddManagedCustomActionDependencies = false; + /// /// Remove media if no files are included /// @@ -3319,6 +3330,42 @@ static void PackageManagedAsm(string asm, string nativeDll, string[] refAssembli var requiredAsms = new List(refAssemblies); + + if (Compiler.AutoGeneration.AddManagedCustomActionDependencies) + { +#if !NETCORE + string[] dependencies = (string[])Utils.ExecuteInTempDomain(asmReflector => asmReflector.GetRefAssemblies(asmFile)); + + // dependencies are loaded in the temp domain and their location may not be original (preferred). IE in the temp folder + // so we need to resolve them to the original locations if possible + var localAssemblies = AppDomain.CurrentDomain.GetAssemblies() + .Select(x => + { + try + { + return x.Location; + } + catch + { + return null; + } + }) + .Where(x => x.IsNotEmpty()); + + for (int i = 0; i < dependencies.Length; i++) + { + var localAsmPath = localAssemblies.Where(x => x.PathGetFileName() == dependencies[i].PathGetFileName()).FirstOrDefault(); + if (localAsmPath != null) + dependencies[i] = localAsmPath; + } + + dependencies.Where(x => !requiredAsms.Contains(x)) + .ForEach(x => requiredAsms.Add(x)); +#else + throw new NotImplementedException($"The option {nameof(Compiler.AutoGeneration.AddManagedCustomActionDependencies)} is not implemented on .NET Core"); +#endif + } + if (refAssemblies.Any(x => x.PathGetFileName() == "WixSharp.UI.dll") && !refAssemblies.Any(x => x.PathGetFileName() == "WixToolset.Mba.Core.dll")) { diff --git a/Source/src/WixSharp/ProjectValidator.cs b/Source/src/WixSharp/ProjectValidator.cs index cac4449e..3fcce0a0 100644 --- a/Source/src/WixSharp/ProjectValidator.cs +++ b/Source/src/WixSharp/ProjectValidator.cs @@ -237,6 +237,11 @@ public bool ValidateCustomBaAssembly(string assembly) return valid; } + public string[] GetRefAssemblies(string file) + { + return ReflectionExtensions.GetRefAssembliesOf(file); + } + public void ValidateCAAssembly(string file, string dtfAsm) { // `ValidateCAAssemblyImpl` will load assembly from `file` for validation. Though for this to happen diff --git a/Source/src/WixSharp/Utilities/Utils.cs b/Source/src/WixSharp/Utilities/Utils.cs index 7be4b73c..03b920b0 100644 --- a/Source/src/WixSharp/Utilities/Utils.cs +++ b/Source/src/WixSharp/Utilities/Utils.cs @@ -41,6 +41,34 @@ public static Session Create() static class ReflectionExtensions { + public static string[] GetRefAssembliesOf(string assembly) + { + try + { + return GetRefAssemblies(System.Reflection.Assembly.LoadFrom(assembly)); + } + catch { return new string[0]; } + } + + public static string[] GetRefAssemblies(this System.Reflection.Assembly assembly) + { + var dependencies = assembly + .GetReferencedAssemblies() + .Where(x => !x.Name.StartsWith("System")) + .Select(x => + { + try + { + return System.Reflection.Assembly.ReflectionOnlyLoad(x.FullName).Location; + } + catch { return null; } + }) + .Where(x => x.IsNotEmpty() && !x.StartsWith(Environment.SpecialFolder.Windows.GetPath(), true)) + .ToArray(); + + return dependencies; + } + public static object Call(this MethodInfo method, params object[] args) { object instance = method.IsStatic ? null : Activator.CreateInstance(method.DeclaringType);