Skip to content

Commit

Permalink
Add Get-Package (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasNieto authored Dec 10, 2024
1 parent e11fe9e commit 9a31bb9
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 7 deletions.
3 changes: 2 additions & 1 deletion .cspell.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"Authenticode",
"netstandard",
"Nieto",
"runneradmin"
"runneradmin",
"Runspace"
]
}
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ jobs:
name: module
path: C:\Users\runneradmin\Documents\PowerShell\Modules\AnyPackage.Msi\

- name: Install AnyPackage module
run: Install-Module AnyPackage -Force -AllowClobber
- name: Install AnyPackage.Programs module
run: Install-Module AnyPackage.Programs -Force -AllowClobber

- name: Test with Pester
run: |
Expand Down Expand Up @@ -117,8 +117,8 @@ jobs:
name: module-signed
path: '~/.local/share/powershell/Modules/AnyPackage.Msi'

- name: Install AnyPackage module
run: Install-Module AnyPackage -Force -AllowClobber
- name: Install AnyPackage.Programs module
run: Install-Module AnyPackage.Programs -Force -AllowClobber

- name: Publish Module
env:
Expand Down
3 changes: 2 additions & 1 deletion src/AnyPackage.Msi.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
Description = 'Msi provider for AnyPackage.'
PowerShellVersion = '5.1'
RequiredModules = @(
@{ ModuleName = 'AnyPackage'; ModuleVersion = '0.8.0' })
@{ ModuleName = 'AnyPackage'; ModuleVersion = '0.8.0' }
'AnyPackage.Programs')
FunctionsToExport = @()
CmdletsToExport = @()
AliasesToExport = @()
Expand Down
13 changes: 13 additions & 0 deletions src/code/GetPackageDynamicParameters.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) Thomas Nieto - All Rights Reserved
// You may use, distribute and modify this code under the
// terms of the MIT license.

using System.Management.Automation;

namespace AnyPackage.Provider.Msi;

public class GetPackageDynamicParameters
{
[Parameter]
public SwitchParameter SystemComponent { get; set; }
}
60 changes: 59 additions & 1 deletion src/code/MsiProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
// 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;

[PackageProvider("Msi", PackageByName = false, FileExtensions = [".msi", ".msp"])]
public class MsiProvider : PackageProvider, IFindPackage
public class MsiProvider : PackageProvider, IFindPackage, IGetPackage
{
public void FindPackage(PackageRequest request)
{
Expand All @@ -25,6 +27,53 @@ public void FindPackage(PackageRequest request)
request.WritePackage(package);
}

public void GetPackage(PackageRequest request)
{
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)
{
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)
{
source = new PackageSourceInfo(result.Source.Name, result.Source.Location, ProviderInfo);
}
else
{
source = null;
}

var package = new PackageInfo(result.Name,
result.Version,
source,
result.Description,
dependencies: null,
result.Metadata.ToDictionary(x => x.Key, x => x.Value),
ProviderInfo);

request.WritePackage(package);
}
}

private PackageInfo FindPackageMsi(PackageRequest request)
{
using var database = new Database(request.Path, DatabaseOpenMode.ReadOnly);
Expand Down Expand Up @@ -59,6 +108,15 @@ private PackageInfo FindPackageMsp(PackageRequest request)
return package;
}

protected override object? GetDynamicParameters(string commandName)
{
return commandName switch
{
"Get-Package" => new GetPackageDynamicParameters(),
_ => null,
};
}

private static Dictionary<string, object?> GetMetadataMsi(Database database)
{
var props = database.ExecuteStringQuery("SELECT Property FROM Property");
Expand Down

0 comments on commit 9a31bb9

Please sign in to comment.