From 72f3dceefa2e3893b061e074380794bc60b67b6f Mon Sep 17 00:00:00 2001 From: Sean Hall Date: Sun, 3 Feb 2019 13:20:43 -0600 Subject: [PATCH] Import code from old v4 repo --- src/ca/dllmain.cpp | 27 + src/ca/precomp.h | 18 + src/ca/wixdepca.cpp | 516 ++++++++++++++++ src/ca/wixdepca.def | 8 + src/ca/wixdepca.vcxproj | 57 ++ src/ca/wixdepca.vcxproj.filters | 40 ++ src/wixext/Dependency.xsd | 226 +++++++ src/wixext/DependencyBinder.cs | 169 ++++++ src/wixext/DependencyCommon.cs | 26 + src/wixext/DependencyCompiler.cs | 615 ++++++++++++++++++++ src/wixext/DependencyDecompiler.cs | 345 +++++++++++ src/wixext/DependencyExtension.csproj | 50 ++ src/wixext/DependencyExtensionData.cs | 64 ++ src/wixext/messages.xml | 60 ++ src/wixext/tables.xml | 38 ++ src/wixlib/DependencyExtension.wixproj | 27 + src/wixlib/DependencyExtension.wxs | 22 + src/wixlib/DependencyExtension_Platform.wxi | 26 + src/wixlib/DependencyExtension_x86.wxs | 8 + src/wixlib/en-us.wxl | 8 + 20 files changed, 2350 insertions(+) create mode 100644 src/ca/dllmain.cpp create mode 100644 src/ca/precomp.h create mode 100644 src/ca/wixdepca.cpp create mode 100644 src/ca/wixdepca.def create mode 100644 src/ca/wixdepca.vcxproj create mode 100644 src/ca/wixdepca.vcxproj.filters create mode 100644 src/wixext/Dependency.xsd create mode 100644 src/wixext/DependencyBinder.cs create mode 100644 src/wixext/DependencyCommon.cs create mode 100644 src/wixext/DependencyCompiler.cs create mode 100644 src/wixext/DependencyDecompiler.cs create mode 100644 src/wixext/DependencyExtension.csproj create mode 100644 src/wixext/DependencyExtensionData.cs create mode 100644 src/wixext/messages.xml create mode 100644 src/wixext/tables.xml create mode 100644 src/wixlib/DependencyExtension.wixproj create mode 100644 src/wixlib/DependencyExtension.wxs create mode 100644 src/wixlib/DependencyExtension_Platform.wxi create mode 100644 src/wixlib/DependencyExtension_x86.wxs create mode 100644 src/wixlib/en-us.wxl diff --git a/src/ca/dllmain.cpp b/src/ca/dllmain.cpp new file mode 100644 index 0000000..7d299fe --- /dev/null +++ b/src/ca/dllmain.cpp @@ -0,0 +1,27 @@ +// 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. + +#include "precomp.h" + +/******************************************************************** +DllMain - standard entry point for all WiX custom actions. + +********************************************************************/ +extern "C" BOOL WINAPI DllMain( + IN HINSTANCE hInstance, + IN ULONG ulReason, + IN LPVOID) +{ + switch(ulReason) + { + case DLL_PROCESS_ATTACH: + WcaGlobalInitialize(hInstance); + ::DisableThreadLibraryCalls(hInstance); + break; + + case DLL_PROCESS_DETACH: + WcaGlobalFinalize(); + break; + } + + return TRUE; +} diff --git a/src/ca/precomp.h b/src/ca/precomp.h new file mode 100644 index 0000000..5fd06cf --- /dev/null +++ b/src/ca/precomp.h @@ -0,0 +1,18 @@ +#pragma once +// 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. + + +#include +#include +#include +#include + +#include "wcautil.h" +#include "fileutil.h" +#include "strutil.h" +#include "memutil.h" +#include "regutil.h" +#include "dictutil.h" +#include "deputil.h" + +#include "CustomMsiErrors.h" diff --git a/src/ca/wixdepca.cpp b/src/ca/wixdepca.cpp new file mode 100644 index 0000000..154b73f --- /dev/null +++ b/src/ca/wixdepca.cpp @@ -0,0 +1,516 @@ +// 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. + +#include "precomp.h" + +#define IDNOACTION 0 +#define INITIAL_STRINGDICT_SIZE 4 + +LPCWSTR vcsDependencyProviderQuery = + L"SELECT `WixDependencyProvider`.`WixDependencyProvider`, `WixDependencyProvider`.`Component_`, `WixDependencyProvider`.`ProviderKey`, `WixDependencyProvider`.`Attributes` " + L"FROM `WixDependencyProvider`"; +enum eDependencyProviderQuery { dpqId = 1, dpqComponent, dpqProviderKey, dpqAttributes }; + +LPCWSTR vcsDependencyQuery = + L"SELECT `WixDependency`.`WixDependency`, `WixDependencyProvider`.`Component_`, `WixDependency`.`ProviderKey`, `WixDependency`.`MinVersion`, `WixDependency`.`MaxVersion`, `WixDependency`.`Attributes` " + L"FROM `WixDependencyProvider`, `WixDependency`, `WixDependencyRef` " + L"WHERE `WixDependency`.`WixDependency` = `WixDependencyRef`.`WixDependency_` AND `WixDependencyProvider`.`WixDependencyProvider` = `WixDependencyRef`.`WixDependencyProvider_`"; +enum eDependencyComponentQuery { dqId = 1, dqComponent, dqProviderKey, dqMinVersion, dqMaxVersion, dqAttributes }; + +static HRESULT EnsureRequiredDependencies( + __in MSIHANDLE hInstall, + __in BOOL fMachineContext + ); + +static HRESULT EnsureAbsentDependents( + __in MSIHANDLE hInstall, + __in BOOL fMachineContext + ); + +static HRESULT SplitIgnoredDependents( + __deref_inout STRINGDICT_HANDLE* psdIgnoredDependents + ); + +static HRESULT CreateDependencyRecord( + __in int iMessageId, + __in_ecount(cDependencies) const DEPENDENCY* rgDependencies, + __in UINT cDependencies, + __out MSIHANDLE *phRecord + ); + +static LPCWSTR LogDependencyName( + __in_z LPCWSTR wzName + ); + +/*************************************************************************** + WixDependencyRequire - Checks that all required dependencies are installed. + +***************************************************************************/ +extern "C" UINT __stdcall WixDependencyRequire( + __in MSIHANDLE hInstall + ) +{ + HRESULT hr = S_OK; + UINT er = ERROR_SUCCESS; + BOOL fMachineContext = FALSE; + + hr = WcaInitialize(hInstall, "WixDependencyRequire"); + ExitOnFailure(hr, "Failed to initialize."); + + hr = RegInitialize(); + ExitOnFailure(hr, "Failed to initialize the registry functions."); + + // Determine whether we're installing per-user or per-machine. + fMachineContext = WcaIsPropertySet("ALLUSERS"); + + // Check for any provider components being (re)installed that their requirements are already installed. + hr = EnsureRequiredDependencies(hInstall, fMachineContext); + ExitOnFailure(hr, "Failed to ensure required dependencies for (re)installing components."); + +LExit: + RegUninitialize(); + + er = FAILED(hr) ? ERROR_INSTALL_FAILURE : ERROR_SUCCESS; + return WcaFinalize(er); +} + +/*************************************************************************** + WixDependencyCheck - Check dependencies based on component state. + + Note: may return ERROR_NO_MORE_ITEMS to terminate the session early. +***************************************************************************/ +extern "C" UINT __stdcall WixDependencyCheck( + __in MSIHANDLE hInstall + ) +{ + HRESULT hr = S_OK; + UINT er = ERROR_SUCCESS; + BOOL fMachineContext = FALSE; + + hr = WcaInitialize(hInstall, "WixDependencyCheck"); + ExitOnFailure(hr, "Failed to initialize."); + + hr = RegInitialize(); + ExitOnFailure(hr, "Failed to initialize the registry functions."); + + // Determine whether we're installing per-user or per-machine. + fMachineContext = WcaIsPropertySet("ALLUSERS"); + + // Check for any dependents of provider components being uninstalled. + hr = EnsureAbsentDependents(hInstall, fMachineContext); + ExitOnFailure(hr, "Failed to ensure absent dependents for uninstalling components."); + +LExit: + RegUninitialize(); + + er = FAILED(hr) ? ERROR_INSTALL_FAILURE : ERROR_SUCCESS; + return WcaFinalize(er); +} + +/*************************************************************************** + EnsureRequiredDependencies - Check that dependencies are installed for + any provider component that is being installed or reinstalled. + + Note: Skipped if DISABLEDEPENDENCYCHECK is set. +***************************************************************************/ +static HRESULT EnsureRequiredDependencies( + __in MSIHANDLE /*hInstall*/, + __in BOOL fMachineContext + ) +{ + HRESULT hr = S_OK; + DWORD er = ERROR_SUCCESS; + STRINGDICT_HANDLE sdDependencies = NULL; + HKEY hkHive = NULL; + PMSIHANDLE hView = NULL; + PMSIHANDLE hRec = NULL; + LPWSTR sczId = NULL; + LPWSTR sczComponent = NULL; + LPWSTR sczProviderKey = NULL; + LPWSTR sczMinVersion = NULL; + LPWSTR sczMaxVersion = NULL; + int iAttributes = 0; + WCA_TODO tComponentAction = WCA_TODO_UNKNOWN; + DEPENDENCY* rgDependencies = NULL; + UINT cDependencies = 0; + PMSIHANDLE hDependencyRec = NULL; + + // Skip the dependency check if the WixDependency table is missing (no dependencies to check for). + hr = WcaTableExists(L"WixDependency"); + if (S_FALSE == hr) + { + WcaLog(LOGMSG_STANDARD, "Skipping the dependency check since no dependencies are authored."); + ExitFunction1(hr = S_OK); + } + + // If the table exists but not the others, the database was not authored correctly. + ExitOnFailure(hr, "Failed to check if the WixDependency table exists."); + + // Initialize the dictionary to keep track of unique dependency keys. + hr = DictCreateStringList(&sdDependencies, INITIAL_STRINGDICT_SIZE, DICT_FLAG_CASEINSENSITIVE); + ExitOnFailure(hr, "Failed to initialize the unique dependency string list."); + + // Set the registry hive to use depending on install context. + hkHive = fMachineContext ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER; + + // Loop over the provider components. + hr = WcaOpenExecuteView(vcsDependencyQuery, &hView); + ExitOnFailure(hr, "Failed to open the query view for dependencies."); + + while (S_OK == (hr = WcaFetchRecord(hView, &hRec))) + { + hr = WcaGetRecordString(hRec, dqId, &sczId); + ExitOnFailure(hr, "Failed to get WixDependency.WixDependency."); + + hr = WcaGetRecordString(hRec, dqComponent, &sczComponent); + ExitOnFailure(hr, "Failed to get WixDependencyProvider.Component_."); + + // Skip the current component if its not being installed or reinstalled. + tComponentAction = WcaGetComponentToDo(sczComponent); + if (WCA_TODO_INSTALL != tComponentAction && WCA_TODO_REINSTALL != tComponentAction) + { + WcaLog(LOGMSG_STANDARD, "Skipping dependency check for %ls because the component %ls is not being (re)installed.", sczId, sczComponent); + continue; + } + + hr = WcaGetRecordString(hRec, dqProviderKey, &sczProviderKey); + ExitOnFailure(hr, "Failed to get WixDependency.ProviderKey."); + + hr = WcaGetRecordString(hRec, dqMinVersion, &sczMinVersion); + ExitOnFailure(hr, "Failed to get WixDependency.MinVersion."); + + hr = WcaGetRecordString(hRec, dqMaxVersion, &sczMaxVersion); + ExitOnFailure(hr, "Failed to get WixDependency.MaxVersion."); + + hr = WcaGetRecordInteger(hRec, dqAttributes, &iAttributes); + ExitOnFailure(hr, "Failed to get WixDependency.Attributes."); + + // Check the registry to see if the required providers (dependencies) exist. + hr = DepCheckDependency(hkHive, sczProviderKey, sczMinVersion, sczMaxVersion, iAttributes, sdDependencies, &rgDependencies, &cDependencies); + if (E_NOTFOUND != hr) + { + ExitOnFailure(hr, "Failed dependency check for %ls.", sczId); + } + } + + if (E_NOMOREITEMS != hr) + { + ExitOnFailure(hr, "Failed to enumerate all of the rows in the dependency query view."); + } + else + { + hr = S_OK; + } + + // If we collected any dependencies in the previous check, pump a message and prompt the user. + if (0 < cDependencies) + { + hr = CreateDependencyRecord(msierrDependencyMissingDependencies, rgDependencies, cDependencies, &hDependencyRec); + ExitOnFailure(hr, "Failed to create the dependency record for message %d.", msierrDependencyMissingDependencies); + + // Send a yes/no message with a warning icon since continuing could be detrimental. + // This is sent as a USER message to better detect whether a user or dependency-aware bootstrapper is responding + // or if Windows Installer or a dependency-unaware boostrapper is returning a typical default response. + er = WcaProcessMessage(static_cast(INSTALLMESSAGE_USER | MB_ICONWARNING | MB_YESNO | MB_DEFBUTTON2), hDependencyRec); + switch (er) + { + // Only a user or dependency-aware bootstrapper that prompted the user should return IDYES to continue anyway. + case IDYES: + ExitFunction1(hr = S_OK); + + // Only a user or dependency-aware bootstrapper that prompted the user should return IDNO to terminate the operation. + case IDNO: + WcaSetReturnValue(ERROR_INSTALL_USEREXIT); + ExitFunction1(hr = S_OK); + + // A dependency-aware bootstrapper should return IDCANCEL if running silently and the operation should be canceled. + case IDCANCEL: + __fallthrough; + + // Bootstrappers which are not dependency-aware may return IDOK for unhandled messages. + case IDOK: + __fallthrough; + + // Windows Installer returns 0 for USER messages when silent or passive, or when a bootstrapper does not handle the message. + case IDNOACTION: + WcaSetReturnValue(ERROR_INSTALL_FAILURE); + ExitFunction1(hr = S_OK); + + default: + ExitOnFailure(hr = E_UNEXPECTED, "Unexpected message response %d from user or bootstrapper application.", er); + } + } + +LExit: + ReleaseDependencyArray(rgDependencies, cDependencies); + ReleaseStr(sczId); + ReleaseStr(sczComponent); + ReleaseStr(sczProviderKey); + ReleaseStr(sczMinVersion); + ReleaseStr(sczMaxVersion); + ReleaseDict(sdDependencies); + + return hr; +} + +/*************************************************************************** + EnsureAbsentDependents - Checks that there are no dependents + registered for providers that are being uninstalled. + + Note: Skipped if UPGRADINGPRODUCTCODE is set. +***************************************************************************/ +static HRESULT EnsureAbsentDependents( + __in MSIHANDLE /*hInstall*/, + __in BOOL fMachineContext + ) +{ + HRESULT hr = S_OK; + DWORD er = ERROR_SUCCESS; + STRINGDICT_HANDLE sdIgnoredDependents = NULL; + HKEY hkHive = NULL; + PMSIHANDLE hView = NULL; + PMSIHANDLE hRec = NULL; + LPWSTR sczId = NULL; + LPWSTR sczComponent = NULL; + LPWSTR sczProviderKey = NULL; + int iAttributes = 0; + WCA_TODO tComponentAction = WCA_TODO_UNKNOWN; + DEPENDENCY* rgDependents = NULL; + UINT cDependents = 0; + PMSIHANDLE hDependencyRec = NULL; + + // Split the IGNOREDEPENDENCIES property for use below if set. If it is "ALL", then quit now. + hr = SplitIgnoredDependents(&sdIgnoredDependents); + ExitOnFailure(hr, "Failed to get the ignored dependents."); + + hr = DictKeyExists(sdIgnoredDependents, L"ALL"); + if (E_NOTFOUND != hr) + { + ExitOnFailure(hr, "Failed to check if \"ALL\" was set in IGNOREDEPENDENCIES."); + + // Otherwise... + WcaLog(LOGMSG_STANDARD, "Skipping the dependencies check since IGNOREDEPENDENCIES contains \"ALL\"."); + ExitFunction(); + } + else + { + // Key was not found, so proceed. + hr = S_OK; + } + + // Skip the dependent check if the WixDependencyProvider table is missing (no dependency providers). + hr = WcaTableExists(L"WixDependencyProvider"); + if (S_FALSE == hr) + { + WcaLog(LOGMSG_STANDARD, "Skipping the dependents check since no dependency providers are authored."); + ExitFunction(); + } + + ExitOnFailure(hr, "Failed to check if the WixDependencyProvider table exists."); + + // Set the registry hive to use depending on install context. + hkHive = fMachineContext ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER; + + // Loop over the provider components. + hr = WcaOpenExecuteView(vcsDependencyProviderQuery, &hView); + ExitOnFailure(hr, "Failed to open the query view for dependency providers."); + + while (S_OK == (hr = WcaFetchRecord(hView, &hRec))) + { + hr = WcaGetRecordString(hRec, dpqId, &sczId); + ExitOnFailure(hr, "Failed to get WixDependencyProvider.WixDependencyProvider."); + + hr = WcaGetRecordString(hRec, dpqComponent, &sczComponent); + ExitOnFailure(hr, "Failed to get WixDependencyProvider.Component."); + + // Skip the current component if its not being uninstalled. + tComponentAction = WcaGetComponentToDo(sczComponent); + if (WCA_TODO_UNINSTALL != tComponentAction) + { + WcaLog(LOGMSG_STANDARD, "Skipping dependents check for %ls because the component %ls is not being uninstalled.", sczId, sczComponent); + continue; + } + + hr = WcaGetRecordString(hRec, dpqProviderKey, &sczProviderKey); + ExitOnFailure(hr, "Failed to get WixDependencyProvider.ProviderKey."); + + hr = WcaGetRecordInteger(hRec, dpqAttributes, &iAttributes); + ExitOnFailure(hr, "Failed to get WixDependencyProvider.Attributes."); + + // Check the registry to see if the provider has any dependents registered. + hr = DepCheckDependents(hkHive, sczProviderKey, iAttributes, sdIgnoredDependents, &rgDependents, &cDependents); + ExitOnFailure(hr, "Failed dependents check for %ls.", sczId); + } + + if (E_NOMOREITEMS != hr) + { + ExitOnFailure(hr, "Failed to enumerate all of the rows in the dependency provider query view."); + } + else + { + hr = S_OK; + } + + // If we collected any providers with dependents in the previous check, pump a message and prompt the user. + if (0 < cDependents) + { + hr = CreateDependencyRecord(msierrDependencyHasDependents, rgDependents, cDependents, &hDependencyRec); + ExitOnFailure(hr, "Failed to create the dependency record for message %d.", msierrDependencyHasDependents); + + // Send a yes/no message with a warning icon since continuing could be detrimental. + // This is sent as a USER message to better detect whether a user or dependency-aware bootstrapper is responding + // or if Windows Installer or a dependency-unaware boostrapper is returning a typical default response. + er = WcaProcessMessage(static_cast(INSTALLMESSAGE_USER | MB_ICONWARNING | MB_YESNO | MB_DEFBUTTON2), hDependencyRec); + switch (er) + { + // Only a user or dependency-aware bootstrapper that prompted the user should return IDYES to continue anyway. + case IDYES: + ExitFunction1(hr = S_OK); + + // Only a user or dependency-aware bootstrapper that prompted the user should return IDNO to terminate the operation. + case IDNO: + __fallthrough; + + // Bootstrappers which are not dependency-aware may return IDOK for unhandled messages. + case IDOK: + __fallthrough; + + // Windows Installer returns 0 for USER messages when silent or passive, or when a bootstrapper does not handle the message. + case IDNOACTION: + WcaSetReturnValue(ERROR_NO_MORE_ITEMS); + ExitFunction1(hr = S_OK); + + // A dependency-aware bootstrapper should return IDCANCEL if running silently and the operation should be canceled. + case IDCANCEL: + WcaSetReturnValue(ERROR_INSTALL_FAILURE); + ExitFunction1(hr = S_OK); + + default: + hr = E_UNEXPECTED; + ExitOnFailure(hr, "Unexpected message response %d from user or bootstrapper application.", er); + } + } + +LExit: + ReleaseDependencyArray(rgDependents, cDependents); + ReleaseStr(sczId); + ReleaseStr(sczComponent); + ReleaseStr(sczProviderKey); + + return hr; +} + +/*************************************************************************** + SplitIgnoredDependents - Splits the IGNOREDEPENDENCIES property into a map. + +***************************************************************************/ +static HRESULT SplitIgnoredDependents( + __deref_inout STRINGDICT_HANDLE* psdIgnoredDependents + ) +{ + HRESULT hr = S_OK; + LPWSTR sczIgnoreDependencies = NULL; + LPCWSTR wzDelim = L";"; + LPWSTR wzContext = NULL; + + hr = WcaGetProperty(L"IGNOREDEPENDENCIES", &sczIgnoreDependencies); + ExitOnFailure(hr, "Failed to get the string value of the IGNOREDEPENDENCIES property."); + + hr = DictCreateStringList(psdIgnoredDependents, INITIAL_STRINGDICT_SIZE, DICT_FLAG_CASEINSENSITIVE); + ExitOnFailure(hr, "Failed to create the string dictionary."); + + // Parse through the semicolon-delimited tokens and add to the string dictionary. + for (LPCWSTR wzToken = ::wcstok_s(sczIgnoreDependencies, wzDelim, &wzContext); wzToken; wzToken = ::wcstok_s(NULL, wzDelim, &wzContext)) + { + hr = DictAddKey(*psdIgnoredDependents, wzToken); + ExitOnFailure(hr, "Failed to ignored dependency \"%ls\" to the string dictionary.", wzToken); + } + +LExit: + ReleaseStr(sczIgnoreDependencies); + + return hr; +} + +/*************************************************************************** + CreateDependencyRecord - Creates a record containing the message template + and records to send to the UI handler. + + Notes: Callers should call WcaProcessMessage and handle return codes. +***************************************************************************/ +static HRESULT CreateDependencyRecord( + __in int iMessageId, + __in_ecount(cDependencies) const DEPENDENCY* rgDependencies, + __in UINT cDependencies, + __out MSIHANDLE *phRecord + ) +{ + HRESULT hr = S_OK; + UINT er = ERROR_SUCCESS; + UINT cParams = 0; + UINT iParam = 0; + + // Should not be PMSIHANDLE. + MSIHANDLE hRec = NULL; + + // Calculate the number of parameters based on the format: + // msgId, count, key1, name1, key2, name2, etc. + cParams = 2 + 2 * cDependencies; + + hRec = ::MsiCreateRecord(cParams); + ExitOnNull(hRec, hr, E_OUTOFMEMORY, "Not enough memory to create the message record."); + + er = ::MsiRecordSetInteger(hRec, ++iParam, iMessageId); + ExitOnFailure(hr = HRESULT_FROM_WIN32(er), "Failed to set the message identifier into the message record."); + + er = ::MsiRecordSetInteger(hRec, ++iParam, cDependencies); + ExitOnFailure(hr = HRESULT_FROM_WIN32(er), "Failed to set the number of dependencies into the message record."); + + // Now loop through each dependency and add the key and name to the record. + for (UINT i = 0; i < cDependencies; i++) + { + const DEPENDENCY* pDependency = &rgDependencies[i]; + + // Log message type-specific information. + switch (iMessageId) + { + // Send a user message when installing a component that is missing some dependencies. + case msierrDependencyMissingDependencies: + WcaLog(LOGMSG_VERBOSE, "The dependency \"%ls\" is missing or is not the required version.", pDependency->sczKey); + break; + + // Send a user message when uninstalling a component that still has registered dependents. + case msierrDependencyHasDependents: + WcaLog(LOGMSG_VERBOSE, "Found dependent \"%ls\", name: \"%ls\".", pDependency->sczKey, LogDependencyName(pDependency->sczName)); + break; + } + + er = ::MsiRecordSetStringW(hRec, ++iParam, pDependency->sczKey); + ExitOnFailure(hr = HRESULT_FROM_WIN32(er), "Failed to set the dependency key \"%ls\" into the message record.", pDependency->sczKey); + + er = ::MsiRecordSetStringW(hRec, ++iParam, pDependency->sczName); + ExitOnFailure(hr = HRESULT_FROM_WIN32(er), "Failed to set the dependency name \"%ls\" into the message record.", pDependency->sczName); + } + + // Only assign the out parameter if successful to this point. + *phRecord = hRec; + hRec = NULL; + +LExit: + if (hRec) + { + ::MsiCloseHandle(hRec); + } + + return hr; +} + +/*************************************************************************** + LogDependencyName - Returns the dependency name or "Unknown" if null. + +***************************************************************************/ +static LPCWSTR LogDependencyName( + __in_z LPCWSTR wzName + ) +{ + return wzName ? wzName : L"Unknown"; +} diff --git a/src/ca/wixdepca.def b/src/ca/wixdepca.def new file mode 100644 index 0000000..df50e99 --- /dev/null +++ b/src/ca/wixdepca.def @@ -0,0 +1,8 @@ +; 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. + + +LIBRARY "wixdepca" + +EXPORTS + WixDependencyRequire + WixDependencyCheck diff --git a/src/ca/wixdepca.vcxproj b/src/ca/wixdepca.vcxproj new file mode 100644 index 0000000..b757a35 --- /dev/null +++ b/src/ca/wixdepca.vcxproj @@ -0,0 +1,57 @@ + + + + + + + + Debug + Win32 + + + Release + Win32 + + + + + Debug + ARM + + + Release + ARM + + + + + {B86AF46C-0F90-49CC-923F-A800B088D015} + DynamicLibrary + Unicode + WixDepCA + wixdepca.def + + + + + + $(WixRoot)src\libs\dutil\inc;$(WixRoot)src\libs\wcautil;$(WixRoot)src\libs\deputil\inc + msi.lib;dutil.lib;deputil.lib;wcautil.lib + + + + + + + + + + + + + + + + + + diff --git a/src/ca/wixdepca.vcxproj.filters b/src/ca/wixdepca.vcxproj.filters new file mode 100644 index 0000000..1fdb023 --- /dev/null +++ b/src/ca/wixdepca.vcxproj.filters @@ -0,0 +1,40 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + Source Files + + + + + Header Files + + + + + Resource Files + + + + + Source Files + + + \ No newline at end of file diff --git a/src/wixext/Dependency.xsd b/src/wixext/Dependency.xsd new file mode 100644 index 0000000..0c36cb8 --- /dev/null +++ b/src/wixext/Dependency.xsd @@ -0,0 +1,226 @@ + + + + + + + + The source code schema for the WiX Toolset Dependency Extension. + + + + + + Describes the information for this product or feature that serves as a dependency of other products or features. + + + + + + + + + + This element is required for any product, feature, or bundle that will use the Dependency feature to properly reference count + other products or features. It should be authored into a component that is always installed and removed with the + product or features that contain it. This guarantees that product dependencies are not removed before those products that + depend on them. + + + The @Key attribute should identify a version range for your product that you guarantee will be backward compatible. + This key is designed to persist throughout compatible upgrades so that dependent products do not have to be reinstalled + and will not prevent your product from being upgraded. If this attribute is not authored, the value is the ProductCode + and will not automatically support upgrades. + + + By default this uses the Product/@Id attribute value, which may be automatically generated. + + + How To: Author product dependencies + + + + + + + + + + + Dependency provider identity. If this attribute is not specified, an identifier will be generated automatically. + + + + + + + Optional unique registry key name that identifies a product version range on which other products can depend. + This attribute is required in package authoring, but optional for components. + + + + + + + The version of the package. For MSI packages, the ProductVersion will be used by default + and this attribute should not be specified. + + + + + + + Optional display name of the package. For MSI packages, the ProductName will be used by default. + + + + + + + + + Describes a dependency on a provider for the current component or package. + + + + + + + + + This element declares a dependency on any product that uses the Provides element. If that product is uninstalled + before a product that requires it, the uninstall will err or warn the user that other products are installed + which depend on that product. This behavior can be modified by changing the attribute values on the Requires element. + + + If you do not nest this element under a Provides element, you must specify the @Id attribute + so that it can be referenced by a RequiresRef element nested under a Provides element. + + + + How To: Author product dependencies + + + + + + + Dependency requirement identity. If this attribute is not specified, an identifier will be generated automatically. + If this element is not authored under a Provides element, this attribute is required. + + + + + + + The unique registry key name for the dependency provider to require during installation of this product. + + + + + + + The minimum version of the dependency provider required to be installed. The default is unbound. + + + + + + + The maximum version of the dependency provider required to be installed. The default is unbound. + + + + + + + Set to "yes" to make the range of dependency provider versions required include the value specified in Minimum. + + + + + + + Set to "yes" to make the range of dependency provider versions required include the value specified in Maximum. + + + + + + + + + References existing authoring for a dependency on a provider for the current component or package. + + + + + This element references a dependency on any product that uses the Provides element. If that product is uninstalled + before a product that requires it, the uninstall will err or warn the user that other products are installed + which depend on that product. This behavior can be modified by changing the attribute values on the Requires element. + + + + How To: Author product dependencies + + + + + + + The identifier of the Requires element to reference. + + + + + + + + + Optional attribute to explicitly author the provider key for the entire bundle. + + + + + + This provider key is designed to persist throughout compatible upgrades so that dependent bundles do not have to be reinstalled + and will not prevent your product from being upgraded. If this attribute is not authored, the value is the + automatically-generated bundle ID and will not automatically support upgrades. + + + Only a single provider key is supported for bundles. To author that your bundle provides additional features via + packages, author different provider keys for your packages. + + + + + + + + + + Values of this type will look like: "x.x.x.x" where x is an integer from 0 to 65534. + This can also be a preprocessor, binder, or WiX variable. + + + + + + + + + + Values of this type will either be "yes" or "no". + + + + + + + + diff --git a/src/wixext/DependencyBinder.cs b/src/wixext/DependencyBinder.cs new file mode 100644 index 0000000..13fea20 --- /dev/null +++ b/src/wixext/DependencyBinder.cs @@ -0,0 +1,169 @@ +// 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.Extensions +{ + using System; + using System.Collections.ObjectModel; + using System.Globalization; + using WixToolset.Data; + using WixToolset.Extensibility; + + /// + /// The compiler for the WiX toolset dependency extension. + /// + public sealed class DependencyBinder : BinderExtension + { + private Output output; + + /// + /// Called after all output changes occur and right before the output is bound into its final format. + /// + public override void Finish(Output output) + { + // Only process MSI packages. + if (OutputType.Product != output.Type) + { + return; + } + + this.output = output; + + Table wixDependencyTable = output.Tables["WixDependency"]; + Table wixDependencyProviderTable = output.Tables["WixDependencyProvider"]; + Table wixDependencyRefTable = output.Tables["WixDependencyRef"]; + + // Make sure there's something to do. + if (null != wixDependencyRefTable) + { + KeyedRowCollection wixDependencyRows = new KeyedRowCollection(wixDependencyTable); + KeyedRowCollection wixDependencyProviderRows = new KeyedRowCollection(wixDependencyProviderTable); + + // For each relationship, get the provides and requires rows to generate registry values. + foreach (Row wixDependencyRefRow in wixDependencyRefTable.Rows) + { + string providesId = (string)wixDependencyRefRow[0]; + string requiresId = (string)wixDependencyRefRow[1]; + + Row wixDependencyRow = null; + if (wixDependencyRows.Contains(requiresId)) + { + wixDependencyRow = wixDependencyRows[requiresId]; + } + + Row wixDependencyProviderRow = null; + if (wixDependencyProviderRows.Contains(providesId)) + { + wixDependencyProviderRow = wixDependencyProviderRows[providesId]; + } + + // If we found both rows, generate the registry values. + if (null != wixDependencyRow && null != wixDependencyProviderRow) + { + // Format the root registry key using the required provider key and the current provider key. + string requiresKey = (string)wixDependencyRow[1]; + string providesKey = (string)wixDependencyProviderRow[2]; + string keyRequires = String.Format(@"{0}{1}\{2}\{3}", DependencyCommon.RegistryRoot, requiresKey, DependencyCommon.RegistryDependents, providesKey); + + // Get the component ID from the provider. + string componentId = (string)wixDependencyProviderRow[1]; + + Row row = this.CreateRegistryRow(wixDependencyRow); + row[0] = this.Core.CreateIdentifier("reg", providesId, requiresId, "(Default)"); + row[1] = -1; + row[2] = keyRequires; + row[3] = "*"; + row[4] = null; + row[5] = componentId; + + string minVersion = (string)wixDependencyRow[2]; + if (!String.IsNullOrEmpty(minVersion)) + { + row = this.CreateRegistryRow(wixDependencyRow); + row[0] = this.Core.CreateIdentifier("reg", providesId, requiresId, "MinVersion"); + row[1] = -1; + row[2] = keyRequires; + row[3] = "MinVersion"; + row[4] = minVersion; + row[5] = componentId; + } + + string maxVersion = (string)wixDependencyRow[3]; + if (!String.IsNullOrEmpty(minVersion)) + { + row = this.CreateRegistryRow(wixDependencyRow); + row[0] = this.Core.CreateIdentifier("reg", providesId, requiresId, "MaxVersion"); + row[1] = -1; + row[2] = keyRequires; + row[3] = "MaxVersion"; + row[4] = maxVersion; + row[5] = componentId; + } + + if (null != wixDependencyRow[4]) + { + int attributes = (int)wixDependencyRow[4]; + + row = this.CreateRegistryRow(wixDependencyRow); + row[0] = this.Core.CreateIdentifier("reg", providesId, requiresId, "Attributes"); + row[1] = -1; + row[2] = keyRequires; + row[3] = "Attributes"; + row[4] = String.Concat("#", attributes.ToString(CultureInfo.InvariantCulture.NumberFormat)); + row[5] = componentId; + } + } + } + } + } + + /// + /// Creates a registry row using source information from the given . + /// + /// The from which the section and source line information are retrieved. + /// A new Registry row. + private Row CreateRegistryRow(Row referenceRow) + { + TableDefinition tableDefinition = this.Core.TableDefinitions["Registry"]; + + // Create the row from the main tables, which were populated during link anyway. + // We still associate the table with the dependency row's section to maintain servicing. + Table table = this.output.EnsureTable(tableDefinition, referenceRow.Table.Section); + Row row = table.CreateRow(referenceRow.SourceLineNumbers); + + // Set the section ID for patching and return the new row. + row.SectionId = referenceRow.SectionId; + return row; + } + + /// + /// A keyed collection of instances for O(1) lookup. + /// + private sealed class KeyedRowCollection : KeyedCollection + { + /// + /// Initializes the class with all rows from the specified . + /// + /// The containing rows to index. + internal KeyedRowCollection(Table table) + { + if (null != table) + { + foreach (Row row in table.Rows) + { + this.Add(row); + } + } + } + + /// + /// Gets the primary key for the . + /// + /// The to index. + /// The primary key for the . + protected override string GetKeyForItem(Row row) + { + return row.GetPrimaryKey('/'); + } + } + } +} diff --git a/src/wixext/DependencyCommon.cs b/src/wixext/DependencyCommon.cs new file mode 100644 index 0000000..4826d8b --- /dev/null +++ b/src/wixext/DependencyCommon.cs @@ -0,0 +1,26 @@ +// 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.Extensions +{ + using System; + using WixToolset; + + internal static class DependencyCommon + { + // Bundle attributes are in the upper 32-bits. + internal const int ProvidesAttributesBundle = 0x10000; + + // Same values as for the Upgrade table in Windows Installer. + internal const int RequiresAttributesMinVersionInclusive = 256; + internal const int RequiresAttributesMaxVersionInclusive = 512; + + // The root registry key for the dependency extension. We write to Software\Classes explicitly + // based on the current security context instead of HKCR. See + // http://msdn.microsoft.com/en-us/library/ms724475(VS.85).aspx for more information. + internal static readonly string RegistryRoot = @"Software\Classes\Installer\Dependencies\"; + internal static readonly string RegistryDependents = "Dependents"; + + // The following characters cannot be used in a provider key. + internal static readonly char[] InvalidCharacters = new char[] { ' ', '\"', ';', '\\' }; + } +} diff --git a/src/wixext/DependencyCompiler.cs b/src/wixext/DependencyCompiler.cs new file mode 100644 index 0000000..a138c04 --- /dev/null +++ b/src/wixext/DependencyCompiler.cs @@ -0,0 +1,615 @@ +// 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.Extensions +{ + using System; + using System.Collections.Generic; + using System.Globalization; + using System.Text; + using System.Xml.Linq; + using WixToolset.Data; + using WixToolset.Extensibility; + + /// + /// The compiler for the WiX toolset dependency extension. + /// + public sealed class DependencyCompiler : CompilerExtension + { + /// + /// Package type when parsing the Provides element. + /// + private enum PackageType + { + None, + ExePackage, + MsiPackage, + MspPackage, + MsuPackage + } + + public DependencyCompiler() + { + this.Namespace = "http://wixtoolset.org/schemas/v4/wxs/dependency"; + } + + /// + /// Processes an attribute for the Compiler. + /// + /// Source line number for the parent element. + /// Parent element of attribute. + /// Attribute to process. + public override void ParseAttribute(XElement parentElement, XAttribute attribute, IDictionary context) + { + SourceLineNumber sourceLineNumbers = Preprocessor.GetSourceLineNumbers(parentElement); + switch (parentElement.Name.LocalName) + { + case "Bundle": + switch (attribute.Name.LocalName) + { + case "ProviderKey": + this.ParseProviderKeyAttribute(sourceLineNumbers, parentElement, attribute); + break; + default: + this.Core.UnexpectedAttribute(parentElement, attribute); + break; + } + break; + default: + this.Core.UnexpectedAttribute(parentElement, attribute); + break; + } + } + + /// + /// Processes an element for the Compiler. + /// + /// Source line number for the parent element. + /// Parent element of element to process. + /// Element to process. + /// Extra information about the context in which this element is being parsed. + public override void ParseElement(XElement parentElement, XElement element, IDictionary context) + { + PackageType packageType = PackageType.None; + + switch (parentElement.Name.LocalName) + { + case "Bundle": + case "Fragment": + case "Module": + case "Product": + switch (element.Name.LocalName) + { + case "Requires": + this.ParseRequiresElement(element, null, false); + break; + default: + this.Core.UnexpectedElement(parentElement, element); + break; + } + break; + case "ExePackage": + packageType = PackageType.ExePackage; + break; + case "MsiPackage": + packageType = PackageType.MsiPackage; + break; + case "MspPackage": + packageType = PackageType.MspPackage; + break; + case "MsuPackage": + packageType = PackageType.MsuPackage; + break; + default: + this.Core.UnexpectedElement(parentElement, element); + break; + } + + if (PackageType.None != packageType) + { + string packageId = context["PackageId"]; + + switch (element.Name.LocalName) + { + case "Provides": + this.ParseProvidesElement(element, packageType, packageId); + break; + default: + this.Core.UnexpectedElement(parentElement, element); + break; + } + } + } + + /// + /// Processes a child element of a Component for the Compiler. + /// + /// Parent element of element to process. + /// Element to process. + /// Extra information about the context in which this element is being parsed. + /// The component key path type if set. + public override ComponentKeyPath ParsePossibleKeyPathElement(XElement parentElement, XElement element, IDictionary context) + { + SourceLineNumber sourceLineNumbers = Preprocessor.GetSourceLineNumbers(parentElement); + ComponentKeyPath keyPath = null; + + switch (parentElement.Name.LocalName) + { + case "Component": + string componentId = context["ComponentId"]; + + // 64-bit components may cause issues downlevel. + bool win64 = false; + Boolean.TryParse(context["Win64"], out win64); + + switch (element.Name.LocalName) + { + case "Provides": + if (win64) + { + this.Core.OnMessage(DependencyWarnings.Win64Component(sourceLineNumbers, componentId)); + } + + keyPath = this.ParseProvidesElement(element, PackageType.None, componentId); + break; + default: + this.Core.UnexpectedElement(parentElement, element); + break; + } + break; + default: + this.Core.UnexpectedElement(parentElement, element); + break; + } + + return keyPath; + } + + /// + /// Processes the ProviderKey bundle attribute. + /// + /// Source line number for the parent element. + /// Parent element of attribute. + /// The XML attribute for the ProviderKey attribute. + private void ParseProviderKeyAttribute(SourceLineNumber sourceLineNumbers, XElement parentElement, XAttribute attribute) + { + Identifier id = null; + string providerKey = null; + int illegalChar = -1; + + switch (attribute.Name.LocalName) + { + case "ProviderKey": + providerKey = this.Core.GetAttributeValue(sourceLineNumbers, attribute); + break; + default: + this.Core.UnexpectedAttribute(parentElement, attribute); + break; + } + + // Make sure the key does not contain any illegal characters or values. + if (String.IsNullOrEmpty(providerKey)) + { + this.Core.OnMessage(WixErrors.IllegalEmptyAttributeValue(sourceLineNumbers, parentElement.Name.LocalName, attribute.Name.LocalName)); + } + else if (0 <= (illegalChar = providerKey.IndexOfAny(DependencyCommon.InvalidCharacters))) + { + StringBuilder sb = new StringBuilder(DependencyCommon.InvalidCharacters.Length * 2); + Array.ForEach(DependencyCommon.InvalidCharacters, c => sb.Append(c).Append(" ")); + + this.Core.OnMessage(DependencyErrors.IllegalCharactersInProvider(sourceLineNumbers, "ProviderKey", providerKey[illegalChar], sb.ToString())); + } + else if ("ALL" == providerKey) + { + this.Core.OnMessage(DependencyErrors.ReservedValue(sourceLineNumbers, parentElement.Name.LocalName, "ProviderKey", providerKey)); + } + + // Generate the primary key for the row. + id = this.Core.CreateIdentifier("dep", attribute.Name.LocalName, providerKey); + + if (!this.Core.EncounteredError) + { + // Create the provider row for the bundle. The Component_ field is required + // in the table definition but unused for bundles, so just set it to the valid ID. + Row row = this.Core.CreateRow(sourceLineNumbers, "WixDependencyProvider", id); + row[1] = id.Id; + row[2] = providerKey; + row[5] = DependencyCommon.ProvidesAttributesBundle; + } + } + + /// + /// Processes the Provides element. + /// + /// The XML node for the Provides element. + /// The type of the package being chained into a bundle, or "None" if building an MSI package. + /// Explicit key path. + /// The identifier of the parent component or package. + /// The type of key path if set. + private ComponentKeyPath ParseProvidesElement(XElement node, PackageType packageType, string parentId) + { + SourceLineNumber sourceLineNumbers = Preprocessor.GetSourceLineNumbers(node); + ComponentKeyPath keyPath = null; + Identifier id = null; + string key = null; + string version = null; + string displayName = null; + int attributes = 0; + int illegalChar = -1; + + foreach (XAttribute attrib in node.Attributes()) + { + if (String.IsNullOrEmpty(attrib.Name.NamespaceName) || this.Namespace == attrib.Name.Namespace) + { + switch (attrib.Name.LocalName) + { + case "Id": + id = this.Core.GetAttributeIdentifier(sourceLineNumbers, attrib); + break; + case "Key": + key = this.Core.GetAttributeValue(sourceLineNumbers, attrib); + break; + case "Version": + version = this.Core.GetAttributeVersionValue(sourceLineNumbers, attrib); + break; + case "DisplayName": + displayName = this.Core.GetAttributeValue(sourceLineNumbers, attrib); + break; + default: + this.Core.UnexpectedAttribute(node, attrib); + break; + } + } + else + { + this.Core.ParseExtensionAttribute(node, attrib); + } + } + + // Make sure the key is valid. The key will default to the ProductCode for MSI packages + // and the package code for MSP packages in the binder if not specified. + if (!String.IsNullOrEmpty(key)) + { + // Make sure the key does not contain any illegal characters or values. + if (0 <= (illegalChar = key.IndexOfAny(DependencyCommon.InvalidCharacters))) + { + StringBuilder sb = new StringBuilder(DependencyCommon.InvalidCharacters.Length * 2); + Array.ForEach(DependencyCommon.InvalidCharacters, c => sb.Append(c).Append(" ")); + + this.Core.OnMessage(DependencyErrors.IllegalCharactersInProvider(sourceLineNumbers, "Key", key[illegalChar], sb.ToString())); + } + else if ("ALL" == key) + { + this.Core.OnMessage(DependencyErrors.ReservedValue(sourceLineNumbers, node.Name.LocalName, "Key", key)); + } + } + else if (PackageType.ExePackage == packageType || PackageType.MsuPackage == packageType) + { + // Must specify the provider key when authored for a package. + this.Core.OnMessage(WixErrors.ExpectedAttribute(sourceLineNumbers, node.Name.LocalName, "Key")); + } + else if (PackageType.None == packageType) + { + // Make sure the ProductCode is authored and set the key. + this.Core.CreateSimpleReference(sourceLineNumbers, "Property", "ProductCode"); + key = "!(bind.property.ProductCode)"; + } + + // The Version attribute should not be authored in or for an MSI package. + if (!String.IsNullOrEmpty(version)) + { + switch (packageType) + { + case PackageType.None: + this.Core.OnMessage(DependencyWarnings.DiscouragedVersionAttribute(sourceLineNumbers)); + break; + case PackageType.MsiPackage: + this.Core.OnMessage(DependencyWarnings.DiscouragedVersionAttribute(sourceLineNumbers, parentId)); + break; + } + } + else if (PackageType.MspPackage == packageType || PackageType.MsuPackage == packageType) + { + // Must specify the Version when authored for packages that do not contain a version. + this.Core.OnMessage(WixErrors.ExpectedAttribute(sourceLineNumbers, node.Name.LocalName, "Version")); + } + + // Need the element ID for child element processing, so generate now if not authored. + if (null == id) + { + id = this.Core.CreateIdentifier("dep", node.Name.LocalName, parentId, key); + } + + foreach (XElement child in node.Elements()) + { + if (this.Namespace == child.Name.Namespace) + { + switch (child.Name.LocalName) + { + case "Requires": + this.ParseRequiresElement(child, id.Id, PackageType.None == packageType); + break; + case "RequiresRef": + this.ParseRequiresRefElement(child, id.Id, PackageType.None == packageType); + break; + default: + this.Core.UnexpectedElement(node, child); + break; + } + } + else + { + this.Core.ParseExtensionElement(node, child); + } + } + + if (!this.Core.EncounteredError) + { + // Create the row in the provider table. + Row row = this.Core.CreateRow(sourceLineNumbers, "WixDependencyProvider", id); + row[1] = parentId; + row[2] = key; + + if (!String.IsNullOrEmpty(version)) + { + row[3] = version; + } + + if (!String.IsNullOrEmpty(displayName)) + { + row[4] = displayName; + } + + if (0 != attributes) + { + row[5] = attributes; + } + + if (PackageType.None == packageType) + { + // Reference the Check custom action to check for dependencies on the current provider. + if (Platform.ARM == this.Core.CurrentPlatform) + { + // Ensure the ARM version of the CA is referenced. + this.Core.CreateSimpleReference(sourceLineNumbers, "CustomAction", "WixDependencyCheck_ARM"); + } + else + { + // All other supported platforms use x86. + this.Core.CreateSimpleReference(sourceLineNumbers, "CustomAction", "WixDependencyCheck"); + } + + // Generate registry rows for the provider using binder properties. + string keyProvides = String.Concat(DependencyCommon.RegistryRoot, key); + + row = this.Core.CreateRow(sourceLineNumbers, "Registry", this.Core.CreateIdentifier("reg", id.Id, "(Default)")); + row[1] = -1; + row[2] = keyProvides; + row[3] = null; + row[4] = "[ProductCode]"; + row[5] = parentId; + + // Use the Version registry value and use that as a potential key path. + Identifier idVersion = this.Core.CreateIdentifier("reg", id.Id, "Version"); + keyPath = new ComponentKeyPath() { Id = idVersion.Id, Explicit = false, Type = ComponentKeyPathType.Registry }; + + row = this.Core.CreateRow(sourceLineNumbers, "Registry", idVersion); + row[1] = -1; + row[2] = keyProvides; + row[3] = "Version"; + row[4] = !String.IsNullOrEmpty(version) ? version : "[ProductVersion]"; + row[5] = parentId; + + row = this.Core.CreateRow(sourceLineNumbers, "Registry", this.Core.CreateIdentifier("reg", id.Id, "DisplayName")); + row[1] = -1; + row[2] = keyProvides; + row[3] = "DisplayName"; + row[4] = !String.IsNullOrEmpty(displayName) ? displayName : "[ProductName]"; + row[5] = parentId; + + if (0 != attributes) + { + row = this.Core.CreateRow(sourceLineNumbers, "Registry", this.Core.CreateIdentifier("reg", id.Id, "Attributes")); + row[1] = -1; + row[2] = keyProvides; + row[3] = "Attributes"; + row[4] = String.Concat("#", attributes.ToString(CultureInfo.InvariantCulture.NumberFormat)); + row[5] = parentId; + } + } + } + + return keyPath; + } + + /// + /// Processes the Requires element. + /// + /// The XML node for the Requires element. + /// The parent provider identifier. + /// Whether the Requires custom action should be referenced. + private void ParseRequiresElement(XElement node, string providerId, bool requiresAction) + { + SourceLineNumber sourceLineNumbers = Preprocessor.GetSourceLineNumbers(node); + Identifier id = null; + string providerKey = null; + string minVersion = null; + string maxVersion = null; + int attributes = 0; + int illegalChar = -1; + + foreach (XAttribute attrib in node.Attributes()) + { + if (String.IsNullOrEmpty(attrib.Name.NamespaceName) || this.Namespace == attrib.Name.Namespace) + { + switch (attrib.Name.LocalName) + { + case "Id": + id = this.Core.GetAttributeIdentifier(sourceLineNumbers, attrib); + break; + case "ProviderKey": + providerKey = this.Core.GetAttributeValue(sourceLineNumbers, attrib); + break; + case "Minimum": + minVersion = this.Core.GetAttributeVersionValue(sourceLineNumbers, attrib); + break; + case "Maximum": + maxVersion = this.Core.GetAttributeVersionValue(sourceLineNumbers, attrib); + break; + case "IncludeMinimum": + if (YesNoType.Yes == this.Core.GetAttributeYesNoValue(sourceLineNumbers, attrib)) + { + attributes |= DependencyCommon.RequiresAttributesMinVersionInclusive; + } + break; + case "IncludeMaximum": + if (YesNoType.Yes == this.Core.GetAttributeYesNoValue(sourceLineNumbers, attrib)) + { + attributes |= DependencyCommon.RequiresAttributesMaxVersionInclusive; + } + break; + default: + this.Core.UnexpectedAttribute(node, attrib); + break; + } + } + else + { + this.Core.ParseExtensionAttribute(node, attrib); + } + } + + this.Core.ParseForExtensionElements(node); + + if (null == id) + { + // Generate an ID only if this element is authored under a Provides element; otherwise, a RequiresRef + // element will be necessary and the Id attribute will be required. + if (!String.IsNullOrEmpty(providerId)) + { + id = this.Core.CreateIdentifier("dep", node.Name.LocalName, providerKey); + } + else + { + this.Core.OnMessage(WixErrors.ExpectedAttributeWhenElementNotUnderElement(sourceLineNumbers, node.Name.LocalName, "Id", "Provides")); + id = Identifier.Invalid; + } + } + + if (String.IsNullOrEmpty(providerKey)) + { + this.Core.OnMessage(WixErrors.ExpectedAttribute(sourceLineNumbers, node.Name.LocalName, "ProviderKey")); + } + // Make sure the key does not contain any illegal characters. + else if (0 <= (illegalChar = providerKey.IndexOfAny(DependencyCommon.InvalidCharacters))) + { + StringBuilder sb = new StringBuilder(DependencyCommon.InvalidCharacters.Length * 2); + Array.ForEach(DependencyCommon.InvalidCharacters, c => sb.Append(c).Append(" ")); + + this.Core.OnMessage(DependencyErrors.IllegalCharactersInProvider(sourceLineNumbers, "ProviderKey", providerKey[illegalChar], sb.ToString())); + } + + + if (!this.Core.EncounteredError) + { + // Reference the Require custom action if required. + if (requiresAction) + { + if (Platform.ARM == this.Core.CurrentPlatform) + { + // Ensure the ARM version of the CA is referenced. + this.Core.CreateSimpleReference(sourceLineNumbers, "CustomAction", "WixDependencyRequire_ARM"); + } + else + { + // All other supported platforms use x86. + this.Core.CreateSimpleReference(sourceLineNumbers, "CustomAction", "WixDependencyRequire"); + } + } + + Row row = this.Core.CreateRow(sourceLineNumbers, "WixDependency", id); + row[1] = providerKey; + row[2] = minVersion; + row[3] = maxVersion; + + if (0 != attributes) + { + row[4] = attributes; + } + + // Create the relationship between this WixDependency row and the WixDependencyProvider row. + if (!String.IsNullOrEmpty(providerId)) + { + // Create the relationship between the WixDependency row and the parent WixDependencyProvider row. + row = this.Core.CreateRow(sourceLineNumbers, "WixDependencyRef"); + row[0] = providerId; + row[1] = id.Id; + } + } + } + + /// + /// Processes the RequiresRef element. + /// + /// The XML node for the RequiresRef element. + /// The parent provider identifier. + /// Whether the Requires custom action should be referenced. + private void ParseRequiresRefElement(XElement node, string providerId, bool requiresAction) + { + SourceLineNumber sourceLineNumbers = Preprocessor.GetSourceLineNumbers(node); + string id = null; + + foreach (XAttribute attrib in node.Attributes()) + { + if (String.IsNullOrEmpty(attrib.Name.NamespaceName) || this.Namespace == attrib.Name.Namespace) + { + switch (attrib.Name.LocalName) + { + case "Id": + id = this.Core.GetAttributeIdentifierValue(sourceLineNumbers, attrib); + break; + default: + this.Core.UnexpectedAttribute(node, attrib); + break; + } + } + else + { + this.Core.ParseExtensionAttribute(node, attrib); + } + } + + this.Core.ParseForExtensionElements(node); + + if (String.IsNullOrEmpty(id)) + { + this.Core.OnMessage(WixErrors.ExpectedAttribute(sourceLineNumbers, node.Name.LocalName, "Id")); + } + + if (!this.Core.EncounteredError) + { + // Reference the Require custom action if required. + if (requiresAction) + { + if (Platform.ARM == this.Core.CurrentPlatform) + { + // Ensure the ARM version of the CA is referenced. + this.Core.CreateSimpleReference(sourceLineNumbers, "CustomAction", "WixDependencyRequire_ARM"); + } + else + { + // All other supported platforms use x86. + this.Core.CreateSimpleReference(sourceLineNumbers, "CustomAction", "WixDependencyRequire"); + } + } + + // Create a link dependency on the row that contains information we'll need during bind. + this.Core.CreateSimpleReference(sourceLineNumbers, "WixDependency", id); + + // Create the relationship between the WixDependency row and the parent WixDependencyProvider row. + Row row = this.Core.CreateRow(sourceLineNumbers, "WixDependencyRef"); + row[0] = providerId; + row[1] = id; + } + } + } +} diff --git a/src/wixext/DependencyDecompiler.cs b/src/wixext/DependencyDecompiler.cs new file mode 100644 index 0000000..3013cf7 --- /dev/null +++ b/src/wixext/DependencyDecompiler.cs @@ -0,0 +1,345 @@ +// 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.Extensions +{ + using System; + using System.Collections.Generic; + using System.Collections.ObjectModel; + using WixToolset; + using WixToolset.Data; + using WixToolset.Extensibility; + using WixToolset.Extensions.Serialize.Dependency; + using Dependency = WixToolset.Extensions.Serialize.Dependency; + using Wix = WixToolset.Data.Serialize; + + /// + /// The decompiler for the WiX toolset dependency extension. + /// + public sealed class DependencyDecompiler : DecompilerExtension + { + private RegistryKeyValueCollection registryValues; + private Dictionary keyCache; + + /// + /// Creates a new instance of the class. + /// + public DependencyDecompiler() + { + this.registryValues = new RegistryKeyValueCollection(); + this.keyCache = new Dictionary(); + + this.TableDefinitions = DependencyExtensionData.GetExtensionTableDefinitions(); + } + + /// + /// Get the extensions library to be removed. + /// + /// Table definitions for library. + /// Library to remove from decompiled output. + public override Library GetLibraryToRemove(TableDefinitionCollection tableDefinitions) + { + return DependencyExtensionData.GetExtensionLibrary(tableDefinitions); + } + + /// + /// Decompiles an extension table. + /// + /// The table to decompile. + public override void DecompileTable(Table table) + { + switch (table.Name) + { + case "WixDependencyProvider": + this.DecompileWixDependencyProviderTable(table); + break; + + case "WixDependency": + this.DecompileWixDependencyTable(table); + break; + + case "WixDependencyRef": + this.DecompileWixDependencyRefTable(table); + break; + + default: + base.DecompileTable(table); + break; + } + } + + /// + /// Finalize decompilation by removing registry values that the compiler writes. + /// + /// The collection of all tables. + public override void Finish(TableIndexedCollection tables) + { + // Remove generated registry rows. + this.FinalizeRegistryTable(tables); + + // Remove extension properties. + this.FinalizeProperties(); + } + + /// + /// Decompiles the WixDependencyProvider table. + /// + /// The table to decompile. + private void DecompileWixDependencyProviderTable(Table table) + { + foreach (Row row in table.Rows) + { + Provides provides = new Provides(); + + provides.Id = (string)row[0]; + provides.Key = (string)row[2]; + + if (null != row[3]) + { + provides.Version = (string)row[3]; + } + + if (null != row[4]) + { + provides.DisplayName = (string)row[4]; + } + + // Nothing to parse for attributes currently. + + Wix.Component component = (Wix.Component)this.Core.GetIndexedElement("Component", (string)row[1]); + if (null != component) + { + component.AddChild(provides); + } + else + { + this.Core.OnMessage(WixWarnings.ExpectedForeignRow(row.SourceLineNumbers, table.Name, row.GetPrimaryKey(DecompilerConstants.PrimaryKeyDelimiter), "Component_", (string)row[1], "Component")); + } + + // Index the provider to parent the RequiresRef elements. + this.Core.IndexElement(row, provides); + + // Add the provider-specific registry keys to be removed during finalization. + // Only remove specific keys that the compiler writes. + string keyProvides = String.Concat(DependencyCommon.RegistryRoot, provides.Key); + + this.registryValues.Add(keyProvides, null); + this.registryValues.Add(keyProvides, "Version"); + this.registryValues.Add(keyProvides, "DisplayName"); + this.registryValues.Add(keyProvides, "Attributes"); + + // Cache the provider key. + this.keyCache[provides.Id] = provides.Key; + } + } + + /// + /// Decompiles the WixDependency table. + /// + /// The table to decompile. + private void DecompileWixDependencyTable(Table table) + { + foreach (Row row in table.Rows) + { + Requires requires = new Requires(); + + requires.Id = (string)row[0]; + requires.ProviderKey = (string)row[1]; + + if (null != row[2]) + { + requires.Minimum = (string)row[2]; + } + + if (null != row[3]) + { + requires.Maximum = (string)row[3]; + } + + if (null != row[4]) + { + int attributes = (int)row[4]; + + if (0 != (attributes & DependencyCommon.RequiresAttributesMinVersionInclusive)) + { + requires.IncludeMinimum = Dependency.YesNoType.yes; + } + + if (0 != (attributes & DependencyCommon.RequiresAttributesMaxVersionInclusive)) + { + requires.IncludeMaximum = Dependency.YesNoType.yes; + } + } + + this.Core.RootElement.AddChild(requires); + + // Cache the requires key. + this.keyCache[requires.Id] = requires.ProviderKey; + } + } + + /// + /// Decompiles the WixDependencyRef table. + /// + /// The table to decompile. + private void DecompileWixDependencyRefTable(Table table) + { + foreach (Row row in table.Rows) + { + RequiresRef requiresRef = new RequiresRef(); + + requiresRef.Id = (string)row[1]; + + Provides provides = (Provides)this.Core.GetIndexedElement("WixDependencyProvider", (string)row[0]); + if (null != provides) + { + provides.AddChild(requiresRef); + } + else + { + this.Core.OnMessage(WixWarnings.ExpectedForeignRow(row.SourceLineNumbers, table.Name, row.GetPrimaryKey(DecompilerConstants.PrimaryKeyDelimiter), "WixDependencyProvider_", (string)row[0], "WixDependencyProvider")); + } + + // Get the cached keys for the provider and dependency IDs and generate registry rows. + string providesKey = null; + string requiresKey = null; + + if (null != provides && this.keyCache.ContainsKey(provides.Id)) + { + providesKey = this.keyCache[provides.Id]; + } + else + { + this.Core.OnMessage(DependencyWarnings.ProvidesKeyNotFound(row.SourceLineNumbers, provides.Id)); + } + + if (this.keyCache.ContainsKey(requiresRef.Id)) + { + requiresKey = this.keyCache[requiresRef.Id]; + } + else + { + this.Core.OnMessage(DependencyWarnings.RequiresKeyNotFound(row.SourceLineNumbers, requiresRef.Id)); + } + + if (!this.Core.EncounteredError) + { + // Add the dependency-specific registry keys to be removed during finalization. + // Only remove specific keys that the compiler writes. + string keyRequires = String.Format(@"{0}{1}\{2}\{3}", DependencyCommon.RegistryRoot, requiresKey, DependencyCommon.RegistryDependents, providesKey); + + this.registryValues.Add(keyRequires, "*"); + this.registryValues.Add(keyRequires, "MinVersion"); + this.registryValues.Add(keyRequires, "MaxVersion"); + this.registryValues.Add(keyRequires, "Attributes"); + } + } + } + + /// + /// Removes rows from the Registry table that are generated by this extension. + /// + /// The collection of tables. + private void FinalizeRegistryTable(TableIndexedCollection tables) + { + Table registryTable = tables["Registry"]; + if (null != registryTable) + { + foreach (Row registryRow in registryTable.Rows) + { + // Check if the compiler writes this registry value; if so, it should be removed. + if (this.registryValues.Contains(registryRow)) + { + Wix.ISchemaElement elem = this.Core.GetIndexedElement(registryRow); + + // If the registry row was found, remove it from its parent. + if (null != elem && null != elem.ParentElement) + { + Wix.IParentElement elemParent = elem.ParentElement as Wix.IParentElement; + if (null != elemParent) + { + elemParent.RemoveChild(elem); + } + } + } + } + } + } + + /// + /// Removes properties defined by this extension. + /// + /// The collection of tables. + private void FinalizeProperties() + { + string[] properties = new string[] { "DISABLEDEPENDENCYCHECK", "IGNOREDEPENDENCIES" }; + foreach (string property in properties) + { + Wix.Property elem = this.Core.GetIndexedElement("Property", property) as Wix.Property; + if (null != elem) + { + // If a value is defined, log a warning we're removing it. + if (!String.IsNullOrEmpty(elem.Value)) + { + this.Core.OnMessage(DependencyWarnings.PropertyRemoved(elem.Id)); + } + + // If the property row was found, remove it from its parent. + if (null != elem.ParentElement) + { + Wix.IParentElement elemParent = elem.ParentElement as Wix.IParentElement; + if (null != elemParent) + { + elemParent.RemoveChild(elem); + } + } + } + } + } + + /// + /// Provides an O(1) lookup for registry key and value name pairs for use in the decompiler. + /// + private sealed class RegistryKeyValueCollection : KeyedCollection> + { + /// + /// Adds the registry key and value name pair to the collection if it doesn't already exist. + /// + /// The registry key to add. + /// The registry value name to add. + internal void Add(string key, string name) + { + KeyValuePair pair = new KeyValuePair(key, name); + if (!this.Contains(pair)) + { + this.Add(pair); + } + } + + /// + /// Returns whether the collection contains the registry key and value name pair from the . + /// + /// The registry to search for. + /// True if the collection contains the registry key and value name pair from the ; otherwise, false. + internal bool Contains(Row row) + { + if (null == row) + { + return false; + } + + KeyValuePair pair = new KeyValuePair((string)row[2], (string)row[3]); + return this.Contains(pair); + } + + /// + /// Return the hash code of the key and value pair concatenated with a colon as a delimiter. + /// + /// The registry key and value name pair. + /// + protected override int GetKeyForItem(KeyValuePair pair) + { + return String.Concat(pair.Key, ":", pair.Value).GetHashCode(); + } + } + } +} diff --git a/src/wixext/DependencyExtension.csproj b/src/wixext/DependencyExtension.csproj new file mode 100644 index 0000000..050e866 --- /dev/null +++ b/src/wixext/DependencyExtension.csproj @@ -0,0 +1,50 @@ + + + + + + + {A0B6D3F1-AE5E-423B-BA92-60C9926CA498} + WixDependencyExtension + Library + WixToolset.Extensions + + + + + + + + + + $(RootNamespace).Data.tables.xml + PreserveNewest + + + $(RootNamespace).Data.Messages.resources + + + $(RootNamespace).Xsd.Dependency.xsd + PreserveNewest + + + WixToolset.Data.Serialize + WixToolset.Extensions.Serialize.Dependency + + + Data\Dependency.wixlib + + + + + + + + + + + false + + + + diff --git a/src/wixext/DependencyExtensionData.cs b/src/wixext/DependencyExtensionData.cs new file mode 100644 index 0000000..da2215c --- /dev/null +++ b/src/wixext/DependencyExtensionData.cs @@ -0,0 +1,64 @@ +// 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.Extensions +{ + using System; + using System.Reflection; + using WixToolset.Data; + using WixToolset.Extensibility; + + /// + /// The WiX toolset dependency extension. + /// + public sealed class DependencyExtensionData : ExtensionData + { + /// + /// Gets the default culture. + /// + /// The default culture. + public override string DefaultCulture + { + get { return "en-us"; } + } + + /// + /// Gets the optional table definitions for this extension. + /// + /// The optional table definitions for this extension. + public override TableDefinitionCollection TableDefinitions + { + get + { + return DependencyExtensionData.GetExtensionTableDefinitions(); + } + } + + /// + /// Gets the library associated with this extension. + /// + /// The table definitions to use while loading the library. + /// The loaded library. + public override Library GetLibrary(TableDefinitionCollection tableDefinitions) + { + return DependencyExtensionData.GetExtensionLibrary(tableDefinitions); + } + + /// + /// Internal mechanism to access the extension's table definitions. + /// + /// Extension's table definitions. + internal static TableDefinitionCollection GetExtensionTableDefinitions() + { + return ExtensionData.LoadTableDefinitionHelper(Assembly.GetExecutingAssembly(), "WixToolset.Extensions.Data.tables.xml"); + } + + /// + /// Internal mechanism to access the extension's library. + /// + /// Extension's library. + internal static Library GetExtensionLibrary(TableDefinitionCollection tableDefinitions) + { + return ExtensionData.LoadLibraryHelper(Assembly.GetExecutingAssembly(), "WixToolset.Extensions.Data.Dependency.wixlib", tableDefinitions); + } + } +} diff --git a/src/wixext/messages.xml b/src/wixext/messages.xml new file mode 100644 index 0000000..bd6eb60 --- /dev/null +++ b/src/wixext/messages.xml @@ -0,0 +1,60 @@ + + + + + + + + + The provider key authored into the {0} attribute contains an illegal character, '{1}'. Please author the provider key without any of the following characters: {2} + + + + + + + + The {0}/@{1} attribute value '{2}' is reserved and cannot be used here. Please choose a different value. + + + + + + + + + + The provider key with identifier {0} was not found in the WixDependencyProvider table. Related registry rows will not be removed from authoring. + + + + + + The dependency key with identifier {0} was not found in the WixDependency table. Related registry rows will not be removed from authoring. + + + + + + The property {0} was authored in the package with a value and will be removed. The property should not be authored. + + + + + + The Provides/@Version attribute should not be specified in an MSI package. The ProductVersion will be used by default. + + + The Provides/@Version attribute should not be specified for MSI package {0}. The ProductVersion will be used by default. + + + + + + The Provides element should not be authored in the 64-bit component with identifier {0}. The dependency feature may not work if installing this package on 64-bit Windows operating systems prior to Windows 7 and Windows Server 2008 R2. Set the Component/@Win64 attribute to "no" to make sure the dependency feature works correctly on all supported operating systems. + + + + + + diff --git a/src/wixext/tables.xml b/src/wixext/tables.xml new file mode 100644 index 0000000..03c9f26 --- /dev/null +++ b/src/wixext/tables.xml @@ -0,0 +1,38 @@ + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/wixlib/DependencyExtension.wixproj b/src/wixlib/DependencyExtension.wixproj new file mode 100644 index 0000000..e52218e --- /dev/null +++ b/src/wixlib/DependencyExtension.wixproj @@ -0,0 +1,27 @@ + + + + + + + {58ED0EC8-73F8-4EE1-8664-A53486D38EC8} + dependency + Library + true + true + 1086 + en-us + + + + + + + + + + + + + + diff --git a/src/wixlib/DependencyExtension.wxs b/src/wixlib/DependencyExtension.wxs new file mode 100644 index 0000000..21f863d --- /dev/null +++ b/src/wixlib/DependencyExtension.wxs @@ -0,0 +1,22 @@ + + + + + + + + + + !(loc.msierrDependencyMissingDependencies) + !(loc.msierrDependencyHasDependents) + + + + + + + + + + + diff --git a/src/wixlib/DependencyExtension_Platform.wxi b/src/wixlib/DependencyExtension_Platform.wxi new file mode 100644 index 0000000..d06b005 --- /dev/null +++ b/src/wixlib/DependencyExtension_Platform.wxi @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/wixlib/DependencyExtension_x86.wxs b/src/wixlib/DependencyExtension_x86.wxs new file mode 100644 index 0000000..715eba0 --- /dev/null +++ b/src/wixlib/DependencyExtension_x86.wxs @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/src/wixlib/en-us.wxl b/src/wixlib/en-us.wxl new file mode 100644 index 0000000..ce29a15 --- /dev/null +++ b/src/wixlib/en-us.wxl @@ -0,0 +1,8 @@ + + + + + + If you continue with this install, the product may not work properly because [2] or more dependencies are missing. Do you want to continue with this install anyway? + If you continue with this uninstall, [2] or more products may stop working properly. Do you want to continue with this uninstall anyway? +