Exporting/Using Microsoft.WinGet.Client
as a C# NuGet Package
#5483
Unanswered
JulianPritchard
asked this question in
Ideas
Replies: 1 comment 2 replies
-
We have a sample project for how to consume the COM API package. It should be pretty straightforward without needing to deal with interop. Unless you want to use Configuration or run it inside your own process, which would need some magic in your |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Ask:
When interacting with the winget-cli programmatically there are two recommended approaches: ComInterop, and PowerShell
Generally, when working in a .Net context the answer would be ComInterop
However, I'm making something that could be used as PowerShell binary module
In this flow I'd like to wrap/extend some functionality of the Microsoft.WinGet.Client, by using the PowerShell.SDK to add new commands which use the existing module's API
In order to achieve the above: a NuGet package which has transitive design time build references would be needed
PowerShell Package Content has the
ref/net8.0/...
with 4 pairs of dll/xml files which are an example of such referencesUnlike the PowerShell SDK, there wouldn't be a need to include runtime assets in the package as those would be loaded be PowerShell itself when it loads the WinGet.Client module
In essence I think what I'm asking for is somewhat the reverse of ReferenceEmbeddedCsWinRTProject.targets @JohnMcPMS added to have the .winmd not referenced, but included in the output
Though there might be a simpler way of achieving this?
Original Ask
Would it be possible to have an export of the [PowerShell Winget Client Module](https://www.powershellgallery.com/packages/Microsoft.WinGet.Client/1.10.340) that could be consumed as a NuGet package for a .Net project?
Background:
I'm currently building a tool which is similar to the DSCv3, in that it can be used to bootstrap a dev's machine
Unfortunately, DSCv3 doesn't fully meet our needs, as we have legacy dependencies and esoteric environment configurations
Due to the above I've built a flow around the PowerShell SDK. With the WinGet bootstrap being similar to that for WSB
I was hoping to just use the PowerShell modules, and avoid the COM Interop route
This approach worked quite well, and I noticed that when debugging the WinGet.Client module was being loaded from
%USERPROFILE%\Documents\PowerShell\Modules\Microsoft.WinGet.Client\1.10.340\net8.0-windows10.0.22000.0\Microsoft.WinGet.Client.Cmdlets.dll
Issue:
The issue is that while the library loads fine at runtime it currently isn't available as a development dependency that can be used for typing/static analysis during development as follow
Details
The above code sample will run in a .Net8 Console Application, with top level statements enabled, and the repacking of the PowerShell module described below
I managed to achieve the above by getting the .nupkg from PowerShellGallery, extracting it as a zip, rather clumsily reorganized the files and folders, added a bit to the .nuspec file, and used
nuget pack .\microsoft.winget.client.repack.nuspec
to repack the module as a regular .Net package that can be used in a projectWhile the above works, I really don't believe how I've managed to get it to work is good practice, or something I'd recommend doing. The PowerShell Module supports both (".Net48", ".Net8") TFMs, and ("arm64", "x64", "x86") CPUs which I've had to narrow to .Net8-x64 due to the application now trying to load the dependencies from the package1
Outcomes:
Microsoft.WinGet.Client
nupkg for regular .Net librariesEdit:
Updated ask with some clarification in response to florelis's answer, original ask now in details block
Footnotes
There's likely a way to have the client assembly as a development dependency and then use
AssemblyLoadContext.GetLoadContext
to ensure that PowerShell module is loaded appropriately at runtime. ↩Beta Was this translation helpful? Give feedback.
All reactions