Skip to content

Commit

Permalink
Remove dependency on AnyPackage.Programs (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasNieto authored Dec 12, 2024
1 parent 7d22e61 commit b6d82df
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 44 deletions.
4 changes: 1 addition & 3 deletions src/AnyPackage.Msi.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -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 = @()
Expand Down
96 changes: 55 additions & 41 deletions src/code/MsiProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<PackageInfo> GetPackageImpl(PackageRequest request)
private IEnumerable<PackageInfo> 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<PackageInfo>())
{
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<string, object?>
{
{ "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);
}
}

Expand Down

0 comments on commit b6d82df

Please sign in to comment.