From b6d82dfd08694d73eb69ce4d0efad8cd378341f8 Mon Sep 17 00:00:00 2001 From: Thomas Nieto <38873752+ThomasNieto@users.noreply.github.com> Date: Wed, 11 Dec 2024 23:12:24 -0600 Subject: [PATCH] Remove dependency on AnyPackage.Programs (#5) --- src/AnyPackage.Msi.psd1 | 4 +- src/code/MsiProvider.cs | 96 +++++++++++++++++++++++------------------ 2 files changed, 56 insertions(+), 44 deletions(-) diff --git a/src/AnyPackage.Msi.psd1 b/src/AnyPackage.Msi.psd1 index 32a302c..0a49e5e 100644 --- a/src/AnyPackage.Msi.psd1 +++ b/src/AnyPackage.Msi.psd1 @@ -7,9 +7,7 @@ Copyright = '(c) 2024 Thomas Nieto. All rights reserved.' Description = 'Msi provider for AnyPackage.' PowerShellVersion = '5.1' - RequiredModules = @( - @{ ModuleName = 'AnyPackage'; ModuleVersion = '0.8.0' } - 'AnyPackage.Programs') + RequiredModules = @(@{ ModuleName = 'AnyPackage'; ModuleVersion = '0.8.0' }) FunctionsToExport = @() CmdletsToExport = @() AliasesToExport = @() diff --git a/src/code/MsiProvider.cs b/src/code/MsiProvider.cs index bfa4dad..50f6220 100644 --- a/src/code/MsiProvider.cs +++ b/src/code/MsiProvider.cs @@ -2,8 +2,6 @@ // You may use, distribute and modify this code under the // terms of the MIT license. -using System.Management.Automation; - using WixToolset.Dtf.WindowsInstaller; namespace AnyPackage.Provider.Msi; @@ -29,54 +27,70 @@ public void FindPackage(PackageRequest request) public void GetPackage(PackageRequest request) { - foreach (var package in GetPackageImpl(request)) + foreach (var package in GetPackages()) { - request.WritePackage(package); + if (request.IsMatch(package.Name, package.Version!)) + { + request.WritePackage(package); + } } } - private IEnumerable GetPackageImpl(PackageRequest request) + private IEnumerable GetPackages() { - using var powershell = PowerShell.Create(RunspaceMode.CurrentRunspace); - powershell.AddCommand("Get-Package") - .AddParameter("Name", request.Name) - .AddParameter("Provider", @"AnyPackage.Programs\Programs"); - - if (request.Version is not null) + foreach (ProductInstallation? product in ProductInstallation.AllProducts) { - powershell.AddParameter("Version", request.Version); - } - - if (request.DynamicParameters is GetPackageDynamicParameters dynamicParameters - && dynamicParameters.SystemComponent) - { - powershell.AddParameter("SystemComponent"); - } - - var scriptBlock = ScriptBlock.Create("$_.Metadata['UninstallString'] -like 'MsiExec.exe*' "); - - powershell.AddCommand("Where-Object") - .AddParameter("FilterScript", scriptBlock); - - foreach (var result in powershell.Invoke()) - { - PackageSourceInfo? source; - if (result.Source is not null) + if (string.IsNullOrWhiteSpace(product.ProductName)) { - source = new PackageSourceInfo(result.Source.Name, result.Source.Location, ProviderInfo); - } - else - { - source = null; + continue; } - yield return new PackageInfo(result.Name, - result.Version, - source, - result.Description, - dependencies: null, - result.Metadata.ToDictionary(x => x.Key, x => x.Value), - ProviderInfo); + var metadata = new Dictionary + { + { "Features", product.Features }, + { "ProductCode", product.ProductCode }, + { "IsInstalled", product.IsInstalled }, + { "IsAdvertised", product.IsAdvertised }, + { "IsElevated", product.IsElevated }, + { "SourceList", product.SourceList }, + { "HelpLink", product.HelpLink }, + { "HelpTelephone", product.HelpTelephone }, + { "InstallDate", product.InstallDate }, + { "ProductName", product.ProductName }, + { "InstallLocation", product.InstallLocation }, + { "InstallSource", product.InstallSource }, + { "LocalPackage", product.LocalPackage }, + { "Publisher", product.Publisher }, + { "UrlInfoAbout", product.UrlInfoAbout }, + { "UrlUpdateInfo", product.UrlUpdateInfo }, + { "ProductVersion", product.ProductVersion }, + { "ProductId", product.ProductId }, + { "RegCompany", product.RegCompany }, + { "RegOwner", product.RegOwner }, + { "AdvertisedTransforms", product.AdvertisedTransforms }, + { "AdvertisedLanguage", product.AdvertisedLanguage }, + { "AdvertisedProductName", product.AdvertisedProductName }, + { "AdvertisedPerMachine", product.AdvertisedPerMachine }, + { "AdvertisedPackageCode", product.AdvertisedPackageCode }, + { "AdvertisedVersion", product.AdvertisedVersion }, + { "AdvertisedProductIcon", product.AdvertisedProductIcon }, + { "AdvertisedPackageName", product.AdvertisedPackageName }, + { "PrivilegedPatchingAuthorized", product.PrivilegedPatchingAuthorized }, + { "UserSid", product.UserSid }, + { "Context", product.Context } + }; + + var source = new PackageSourceInfo(product.LocalPackage, + product.LocalPackage, + ProviderInfo); + + yield return new PackageInfo(product.ProductName, + product.ProductVersion, + source, + description: "", + dependencies: null, + metadata, + ProviderInfo); } }