From ba7bab476501c16e437b0aee71c1be02c3dda176 Mon Sep 17 00:00:00 2001 From: Rob Mensching Date: Mon, 3 May 2021 15:55:48 -0700 Subject: Move Bal.wixext into ext --- .../BootstrapperApplicationFactory.cs | 86 ++++++++++++ .../BootstrapperSectionGroup.cs | 29 +++++ src/ext/Bal/WixToolset.Mba.Host/Exceptions.cs | 145 +++++++++++++++++++++ src/ext/Bal/WixToolset.Mba.Host/HostSection.cs | 47 +++++++ src/ext/Bal/WixToolset.Mba.Host/NativeMethods.cs | 18 +++ .../SupportedFrameworkElement.cs | 47 +++++++ .../SupportedFrameworkElementCollection.cs | 36 +++++ .../WixToolset.Mba.Host/WixToolset.Mba.Host.config | 26 ++++ .../WixToolset.Mba.Host/WixToolset.Mba.Host.csproj | 54 ++++++++ .../WixToolset.Mba.Host/WixToolset.Mba.Host.nuspec | 23 ++++ 10 files changed, 511 insertions(+) create mode 100644 src/ext/Bal/WixToolset.Mba.Host/BootstrapperApplicationFactory.cs create mode 100644 src/ext/Bal/WixToolset.Mba.Host/BootstrapperSectionGroup.cs create mode 100644 src/ext/Bal/WixToolset.Mba.Host/Exceptions.cs create mode 100644 src/ext/Bal/WixToolset.Mba.Host/HostSection.cs create mode 100644 src/ext/Bal/WixToolset.Mba.Host/NativeMethods.cs create mode 100644 src/ext/Bal/WixToolset.Mba.Host/SupportedFrameworkElement.cs create mode 100644 src/ext/Bal/WixToolset.Mba.Host/SupportedFrameworkElementCollection.cs create mode 100644 src/ext/Bal/WixToolset.Mba.Host/WixToolset.Mba.Host.config create mode 100644 src/ext/Bal/WixToolset.Mba.Host/WixToolset.Mba.Host.csproj create mode 100644 src/ext/Bal/WixToolset.Mba.Host/WixToolset.Mba.Host.nuspec (limited to 'src/ext/Bal/WixToolset.Mba.Host') diff --git a/src/ext/Bal/WixToolset.Mba.Host/BootstrapperApplicationFactory.cs b/src/ext/Bal/WixToolset.Mba.Host/BootstrapperApplicationFactory.cs new file mode 100644 index 00000000..78e68bd9 --- /dev/null +++ b/src/ext/Bal/WixToolset.Mba.Host/BootstrapperApplicationFactory.cs @@ -0,0 +1,86 @@ +// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. + +namespace WixToolset.Mba.Host +{ + using System; + using System.Configuration; + using System.Reflection; + using System.Runtime.InteropServices; + using WixToolset.Mba.Core; + + /// + /// Entry point for the managed host to create and return the BA to the engine. + /// + [ClassInterface(ClassInterfaceType.None)] + public sealed class BootstrapperApplicationFactory : MarshalByRefObject, IBootstrapperApplicationFactory + { + /// + /// Creates a new instance of the class. + /// Entry point for the MBA host. + /// + public BootstrapperApplicationFactory() + { + } + + /// + /// Loads the bootstrapper application assembly and calls its IBootstrapperApplicationFactory.Create method. + /// + /// Pointer to BOOTSTRAPPER_CREATE_ARGS struct. + /// Pointer to BOOTSTRAPPER_CREATE_RESULTS struct. + /// The bootstrapper application assembly + /// does not define the . + public void Create(IntPtr pArgs, IntPtr pResults) + { + // Get the wix.boostrapper section group to get the name of the bootstrapper application assembly to host. + var section = ConfigurationManager.GetSection("wix.bootstrapper/host") as HostSection; + if (null == section) + { + throw new MissingAttributeException(); // TODO: throw a more specific exception than this. + } + + // Load the BA's IBootstrapperApplicationFactory. + var baFactoryType = BootstrapperApplicationFactory.GetBAFactoryTypeFromAssembly(section.AssemblyName); + var baFactory = (IBootstrapperApplicationFactory)Activator.CreateInstance(baFactoryType); + if (null == baFactory) + { + throw new InvalidBootstrapperApplicationFactoryException(); + } + + baFactory.Create(pArgs, pResults); + } + + /// + /// Locates the and returns the specified type. + /// + /// The assembly that defines the IBootstrapperApplicationFactory implementation. + /// The bootstrapper application factory . + private static Type GetBAFactoryTypeFromAssembly(string assemblyName) + { + Type baFactoryType = null; + + // Load the requested assembly. + Assembly asm = AppDomain.CurrentDomain.Load(assemblyName); + + // If an assembly was loaded and is not the current assembly, check for the required attribute. + // This is done to avoid using the BootstrapperApplicationFactoryAttribute which we use at build time + // to specify the BootstrapperApplicationFactory assembly in the manifest. + if (!Assembly.GetExecutingAssembly().Equals(asm)) + { + // There must be one and only one BootstrapperApplicationFactoryAttribute. + // The attribute prevents multiple declarations already. + var attrs = (BootstrapperApplicationFactoryAttribute[])asm.GetCustomAttributes(typeof(BootstrapperApplicationFactoryAttribute), false); + if (null != attrs) + { + baFactoryType = attrs[0].BootstrapperApplicationFactoryType; + } + } + + if (null == baFactoryType) + { + throw new MissingAttributeException(); + } + + return baFactoryType; + } + } +} diff --git a/src/ext/Bal/WixToolset.Mba.Host/BootstrapperSectionGroup.cs b/src/ext/Bal/WixToolset.Mba.Host/BootstrapperSectionGroup.cs new file mode 100644 index 00000000..5cf1bc9c --- /dev/null +++ b/src/ext/Bal/WixToolset.Mba.Host/BootstrapperSectionGroup.cs @@ -0,0 +1,29 @@ +// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. + +namespace WixToolset.Mba.Host +{ + using System; + using System.Configuration; + + /// + /// Handler for the wix.bootstrapper configuration section group. + /// + public class BootstrapperSectionGroup : ConfigurationSectionGroup + { + /// + /// Creates a new instance of the class. + /// + public BootstrapperSectionGroup() + { + } + + /// + /// Gets the handler for the mba configuration section. + /// + [ConfigurationProperty("host")] + public HostSection Host + { + get { return (HostSection)base.Sections["host"]; } + } + } +} diff --git a/src/ext/Bal/WixToolset.Mba.Host/Exceptions.cs b/src/ext/Bal/WixToolset.Mba.Host/Exceptions.cs new file mode 100644 index 00000000..c68951f0 --- /dev/null +++ b/src/ext/Bal/WixToolset.Mba.Host/Exceptions.cs @@ -0,0 +1,145 @@ +// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. + +namespace WixToolset.Mba.Host +{ + using System; + using System.Runtime.Serialization; + + /// + /// Base class for exception returned to the bootstrapper application host. + /// + [Serializable] + public abstract class BootstrapperException : Exception + { + /// + /// Creates an instance of the base class with the given HRESULT. + /// + /// The HRESULT for the exception that is used by the bootstrapper application host. + public BootstrapperException(int hr) + { + this.HResult = hr; + } + + /// + /// Initializes a new instance of the class. + /// + /// Exception message. + public BootstrapperException(string message) + : base(message) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// Exception message + /// Inner exception associated with this one + public BootstrapperException(string message, Exception innerException) + : base(message, innerException) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// Serialization information for this exception + /// Streaming context to serialize to + protected BootstrapperException(SerializationInfo info, StreamingContext context) + : base(info, context) + { + } + } + + /// + /// The bootstrapper application assembly loaded by the host does not contain exactly one instance of the + /// class. + /// + /// + [Serializable] + public class MissingAttributeException : BootstrapperException + { + /// + /// Creates a new instance of the class. + /// + public MissingAttributeException() + : base(NativeMethods.E_NOTFOUND) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// Exception message. + public MissingAttributeException(string message) + : base(message) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// Exception message + /// Inner exception associated with this one + public MissingAttributeException(string message, Exception innerException) + : base(message, innerException) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// Serialization information for this exception + /// Streaming context to serialize to + protected MissingAttributeException(SerializationInfo info, StreamingContext context) + : base(info, context) + { + } + } + + /// + /// The bootstrapper application factory specified by the + /// does not extend the base class. + /// + /// + /// + [Serializable] + public class InvalidBootstrapperApplicationFactoryException : BootstrapperException + { + /// + /// Creates a new instance of the class. + /// + public InvalidBootstrapperApplicationFactoryException() + : base(NativeMethods.E_UNEXPECTED) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// Exception message. + public InvalidBootstrapperApplicationFactoryException(string message) + : base(message) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// Exception message + /// Inner exception associated with this one + public InvalidBootstrapperApplicationFactoryException(string message, Exception innerException) + : base(message, innerException) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// Serialization information for this exception + /// Streaming context to serialize to + protected InvalidBootstrapperApplicationFactoryException(SerializationInfo info, StreamingContext context) + : base(info, context) + { + } + } +} diff --git a/src/ext/Bal/WixToolset.Mba.Host/HostSection.cs b/src/ext/Bal/WixToolset.Mba.Host/HostSection.cs new file mode 100644 index 00000000..632025c7 --- /dev/null +++ b/src/ext/Bal/WixToolset.Mba.Host/HostSection.cs @@ -0,0 +1,47 @@ +// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. + +namespace WixToolset.Mba.Host +{ + using System; + using System.Configuration; + + /// + /// Handler for the Host configuration section. + /// + public sealed class HostSection : ConfigurationSection + { + private static readonly ConfigurationProperty assemblyNameProperty = new ConfigurationProperty("assemblyName", typeof(string), null, ConfigurationPropertyOptions.IsRequired); + private static readonly ConfigurationProperty supportedFrameworksProperty = new ConfigurationProperty("", typeof(SupportedFrameworkElementCollection), null, ConfigurationPropertyOptions.IsDefaultCollection); + + /// + /// Creates a new instance of the class. + /// + public HostSection() + { + } + + /// + /// Gets the name of the assembly that contians the child class. + /// + /// + /// The assembly specified by this name must contain the to identify + /// the type of the child class. + /// + [ConfigurationProperty("assemblyName", IsRequired = true)] + public string AssemblyName + { + get { return (string)base[assemblyNameProperty]; } + set { base[assemblyNameProperty] = value; } + } + + /// + /// Gets the of supported frameworks for the host configuration. + /// + [ConfigurationProperty("", IsDefaultCollection = true)] + [ConfigurationCollection(typeof(SupportedFrameworkElement))] + public SupportedFrameworkElementCollection SupportedFrameworks + { + get { return (SupportedFrameworkElementCollection)base[supportedFrameworksProperty]; } + } + } +} diff --git a/src/ext/Bal/WixToolset.Mba.Host/NativeMethods.cs b/src/ext/Bal/WixToolset.Mba.Host/NativeMethods.cs new file mode 100644 index 00000000..b9fc85a0 --- /dev/null +++ b/src/ext/Bal/WixToolset.Mba.Host/NativeMethods.cs @@ -0,0 +1,18 @@ +// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. + +namespace WixToolset.Mba.Host +{ + using System; + using System.Runtime.InteropServices; + + /// + /// Contains native constants, functions, and structures for this assembly. + /// + internal static class NativeMethods + { + #region Error Constants + internal const int E_NOTFOUND = unchecked((int)0x80070490); + internal const int E_UNEXPECTED = unchecked((int)0x8000ffff); + #endregion + } +} diff --git a/src/ext/Bal/WixToolset.Mba.Host/SupportedFrameworkElement.cs b/src/ext/Bal/WixToolset.Mba.Host/SupportedFrameworkElement.cs new file mode 100644 index 00000000..fe7fd2eb --- /dev/null +++ b/src/ext/Bal/WixToolset.Mba.Host/SupportedFrameworkElement.cs @@ -0,0 +1,47 @@ +// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. + +namespace WixToolset.Mba.Host +{ + using System; + using System.Configuration; + + /// + /// Handler for the supportedFramework configuration section. + /// + public sealed class SupportedFrameworkElement : ConfigurationElement + { + private static readonly ConfigurationProperty versionProperty = new ConfigurationProperty("version", typeof(string), null, ConfigurationPropertyOptions.IsRequired); + private static readonly ConfigurationProperty runtimeVersionProperty = new ConfigurationProperty("runtimeVersion", typeof(string)); + + /// + /// Creates a new instance of the class. + /// + public SupportedFrameworkElement() + { + } + + /// + /// Gets the version of the supported framework. + /// + /// + /// The assembly specified by this name must contain a value matching the NETFX version registry key under + /// "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP". + /// + [ConfigurationProperty("version", IsRequired = true)] + public string Version + { + get { return (string)base[versionProperty]; } + set { base[versionProperty] = value; } + } + + /// + /// Gets the runtime version required by this supported framework. + /// + [ConfigurationProperty("runtimeVersion", IsRequired = false)] + public string RuntimeVersion + { + get { return (string)base[runtimeVersionProperty]; } + set { base[runtimeVersionProperty] = value; } + } + } +} diff --git a/src/ext/Bal/WixToolset.Mba.Host/SupportedFrameworkElementCollection.cs b/src/ext/Bal/WixToolset.Mba.Host/SupportedFrameworkElementCollection.cs new file mode 100644 index 00000000..12c7cf3e --- /dev/null +++ b/src/ext/Bal/WixToolset.Mba.Host/SupportedFrameworkElementCollection.cs @@ -0,0 +1,36 @@ +// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. + +namespace WixToolset.Mba.Host +{ + using System; + using System.Configuration; + using System.Diagnostics.CodeAnalysis; + + /// + /// Handler for the supportedFramework collection. + /// + [SuppressMessage("Microsoft.Design", "CA1010:CollectionsShouldImplementGenericInterface")] + [ConfigurationCollection(typeof(SupportedFrameworkElement), AddItemName = "supportedFramework", CollectionType = ConfigurationElementCollectionType.BasicMap)] + public sealed class SupportedFrameworkElementCollection : ConfigurationElementCollection + { + public override ConfigurationElementCollectionType CollectionType + { + get { return ConfigurationElementCollectionType.BasicMap; } + } + + protected override string ElementName + { + get { return "supportedFramework"; } + } + + protected override ConfigurationElement CreateNewElement() + { + return new SupportedFrameworkElement(); + } + + protected override object GetElementKey(ConfigurationElement element) + { + return (element as SupportedFrameworkElement).Version; + } + } +} diff --git a/src/ext/Bal/WixToolset.Mba.Host/WixToolset.Mba.Host.config b/src/ext/Bal/WixToolset.Mba.Host/WixToolset.Mba.Host.config new file mode 100644 index 00000000..a19b66f1 --- /dev/null +++ b/src/ext/Bal/WixToolset.Mba.Host/WixToolset.Mba.Host.config @@ -0,0 +1,26 @@ + + + + + + + +
+ + + + + + + + + + + + + + diff --git a/src/ext/Bal/WixToolset.Mba.Host/WixToolset.Mba.Host.csproj b/src/ext/Bal/WixToolset.Mba.Host/WixToolset.Mba.Host.csproj new file mode 100644 index 00000000..3ee0ad1e --- /dev/null +++ b/src/ext/Bal/WixToolset.Mba.Host/WixToolset.Mba.Host.csproj @@ -0,0 +1,54 @@ + + + + + + WixToolset.Mba.Host + WixToolset.Mba.Host + net20 + Managed Bootstrapper Application entry point + embedded + $(MSBuildThisFileName).nuspec + AnyCPU + + + + + + + + + + + + + + + + False + + + + + + + + + + + + + + + + + + + + + + $(OutputPath) + Id=$(AssemblyName);Version=$(BuildVersionSimple);Authors=$(Authors);Copyright=$(Copyright);Description=$(Description);RepositoryCommit=$(SourceRevisionId);RepositoryType=$(RepositoryType);RepositoryUrl=$(PrivateRepositoryUrl) + + + \ No newline at end of file diff --git a/src/ext/Bal/WixToolset.Mba.Host/WixToolset.Mba.Host.nuspec b/src/ext/Bal/WixToolset.Mba.Host/WixToolset.Mba.Host.nuspec new file mode 100644 index 00000000..00fd21ac --- /dev/null +++ b/src/ext/Bal/WixToolset.Mba.Host/WixToolset.Mba.Host.nuspec @@ -0,0 +1,23 @@ + + + + $id$ + $version$ + WiX Toolset Team + WiX Toolset Team + MS-RL + https://github.com/wixtoolset/Bal.wixext + false + $description$ + $copyright$ + + + + + + + + + + + -- cgit v1.2.3-55-g6feb