PowerShell / PSResourceGet

PSResourceGet is the package manager for PowerShell
https://www.powershellgallery.com/packages/Microsoft.PowerShell.PSResourceGet
MIT License
483 stars 92 forks source link

Support for platform-specific binary dependencies? #138

Open markm77 opened 4 years ago

markm77 commented 4 years ago

See e.g. https://stackoverflow.com/questions/51732417/how-can-i-publish-a-powershell-gallery-module-that-depends-on-a-platform-specifi.

I also have the same issue as e.g. on Windows need to generate my binary module using dotnet publish -r win10-x64 rather than dotnet publish to make it work locally (I believe the issue is to do with the SQLite driver for EF Core).

This makes it a bit tricky to publish a cross-platform binary module via PowerShell Gallery....

markm77 commented 4 years ago

FYI more info on my particular unwanted platform dependency in case of interest.... https://github.com/dotnet/efcore/issues/9505#issuecomment-626216453 😊

JustinGrote commented 4 years ago

@markm77 this isn't really powershellget specific. A common technique is in your module .psm1 file to detect the platform (using $isWindows or $isLinux as one potential method) and load the appropriate assemblies/binaries for the platform, if you can't use Powershell Standard to create cross-platform assemblies.

markm77 commented 4 years ago

Thanks @JustinGrote.

Here's how I'm interpreting what you suggest. I could do a dotnet publish for each supported platform to generate the platform-specific DLL and dependencies in a platform-specific folder. Then I could publish all of these folders plus the .psm1 to PowerShell gallery and use the psm1 to point to the correct folder at run-time?

Is this possible and is this what you mean?

JustinGrote commented 4 years ago

Correct and use add-type to load the .dlls, and assign a variable to the commands you want to run if they are binary '.exe' files.

See pester for an example: https://github.com/JustinGrote/Pester/blob/v5.0/src/Pester.Types.ps1

Also these are good articles on the subject: https://docs.microsoft.com/en-us/powershell/scripting/learn/writing-portable-modules?view=powershell-7 https://devblogs.microsoft.com/powershell/depending-on-the-right-powershell-nuget-package-in-your-net-project/

markm77 commented 4 years ago

Thanks for your links. Since I don't need to support pre-v7 PowerShell, looking at this indicates actually I just need to use correct subfolders for the DLL (and hopefully dependencies??). I will need to try this, hopefully this is all compatible with output of dotnet build and dotnet publish with mulitple target frameworks. That would be nice.

I have moved away from add-type and back to import-module due to various issues (see here). Hopefully import-module respects the subfolder structure above (auto-selects correct subfolder) in PowerShell v7.

Let me test all this which seems a perfectly reasonable way to support platform-specific binary dependencies in a single PowerShell gallery package with no help required from PowerShellGet. If it works, I'll close this.