New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make using NuGet packages installed with Install-Package easier to use - make Add-Type support NuGet packages #6724

Open
mklement0 opened this Issue Apr 24, 2018 · 8 comments

Comments

Projects
None yet
4 participants
@mklement0
Contributor

mklement0 commented Apr 24, 2018

#6679 (comment) shows how hard it is to use a NuGet package installed via Install-Package in Powershell, for two reasons:

  • You must manually determine the platform-appropriate *.dll file in the package's folder subtree and pass its full path to Add-Type -Path (e.g., runtimes/linux-x64/... on a 64-bit Intel Linux system)

  • Even that by itself is not enough, if the package contains native helper libraries, because Add-Type -Path doesn't find them - see link above (whereas creating a C#-based .NET Core console application based on the same package installed with dotnet add package does).

Therefore, it would be great if Add-Type had built-in support for resolving both issues above, say with the following syntax:

# Install a NuGet package from the NuGet Gallery and add its types to the session.
Install-Package -Provider nuget Mono.Posix.NETStandard  | Add-Type

# Add the types of an already installed NuGet package to the session.
Add-Type -Package Mono.Posix.NETStandard 

# Equivalent, via Get-Package.
Get-Package Mono.Posix.NETStandard | Add-Type

That is, when given a NuGet package [name], Add-Type would load the platform-appropriate assemblies and all their dependencies to make their types available to the current session.

Environment data

Written as of:

PowerShell Core v6.0.2
@thnk2wn

This comment has been minimized.

thnk2wn commented May 20, 2018

Would love this. Coming from Cake Build this was so easy with https://cakebuild.net/docs/fundamentals/preprocessor-directives. Given the widespread usage of nuget and likely need to leverage that from PS Core scripts it would seem this would be supported as a first class citizen.

@iSazonov

This comment has been minimized.

Collaborator

iSazonov commented Jul 30, 2018

How it correlates with Install/Uninstall-Module?

@mklement0

This comment has been minimized.

Contributor

mklement0 commented Jul 31, 2018

@iSazonov:

Install-Module is used for PowerShell modules from the PowerShell Gallery, whereas NuGet packages are .NET packages from the NuGet Gallery.

While PSGallery modules can be installed with Install-Package too, because the *-Package cmdlets are an abstraction over all galleries / package sources, the inverse is not true: NuGet packages cannot be installed with Install-Module.

Is that what you were asking?

@iSazonov

This comment has been minimized.

Collaborator

iSazonov commented Jul 31, 2018

Thanks!
Seems this can resolve #7259 too:

Install-Package "SomePackage"  | Add-Type -PassThru | Import-Module
@mklement0

This comment has been minimized.

Contributor

mklement0 commented Jul 31, 2018

@iSazonov:

While #7259 has the same goal in the abstract - making use of NuGet package easier - it is a competing proposal in terms of implementation.

  • #7259 suggests the use of Import-Module to load NuGet packages.

  • This issue suggest use of Add-Type only.

I don't think it's appropriate to integrate the functionality into Import-Module, because NuGet packages are .NET assemblies, not PowerShell modules; hence the suggestion to use Add-Type, which is the cmdlet already used for loading assemblies.

In short: My vote is to implement this issue only, instead of #7259.

@iSazonov

This comment has been minimized.

Collaborator

iSazonov commented Jul 31, 2018

.NET assemblies can contain cmdlets. We use this in our tests.

@mklement0

This comment has been minimized.

Contributor

mklement0 commented Jul 31, 2018

@iSazonov:

Yes, all compiled cmdlets come as part of assemblies, but they're typically packaged as part of a PS module.
However, good point about the need to use Import-Module on an assembly containing a cmdlet that isn't packaged as part of a module in order to import such cmdlets; I assume you meant:

(Install-Package "SomePackage" | Add-Type -PassThru).Assembly | Import-Module

But it sounds like we're in agreement then, correct?:

No changes to Import-Module are required; it is sufficient to make Add-Type understand NuGet packages.

@mklement0

This comment has been minimized.

Contributor

mklement0 commented Jul 31, 2018

I've started a conversation about which proposal to consider (or perhaps find a synthesis) at #7259 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment