Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create pure powershell Nuget module for PowerShellGet

What I want

I want to publish number of PowerShell scripts as Nuget package to be used on build systems.

I want to use PowerShellGet to do installation work for me and version management.

I don't want those scripts to be part of any Visual Studio solution, but as standalone scripts.

Usage scenario

On any system, with configured Nuget provider user executes:

Install-Module MyModule

From that moment all exports from that module permanently available for this user. Also user can call that command again to update version of those scripts.

What I've done

You can find current state of package here: GitHub

  1. I've added and configured Nuget provider to our local Nuget server

    To do this call Get-PackageProvider -Name NuGet -ForceBootstrap and Set-PSRepository -Name My_Nuget_Repo -SourceLocation http://my-nuget/api -InstallationPolicy Trusted

  2. Created proper module, which can be imported locally by Import-Module

  3. Created and published Nuget package with that module

Problem

I can install that package by Install-Module cmdlet and I can see it later in Get-InstalledModule list.

But, no functions are available.

Also, no matter what, but Install-Module not calling any of scripts from my package:

  • Not calling ScriptsToProcess from MyModule.psd1
  • Not calling Install.ps1 from tools folder
  • Not calling Init.ps1 from tools folder
  • Cmdlets exported by module not available and module can't be imported by Import-Module

(Same package works properly when installed from Visual Studios Install-Package MyModule, scripts are called, PowerShell module is imported).

Investigation

Since PowerShellGet is based on OneGet it seems that problem is in Install-Package cmdlet (which is called inside Install-Module cmdlet).

When I'm executing Install-Package MyModule from Visual Studio Install.ps1 and Init.ps1 are called. But same command from pure PowerShell doing nothing.

like image 391
Anton Avatar asked Oct 30 '15 10:10

Anton


1 Answers

After long reverse engineering I've found the root cause

Technical reason

Magical tag PSModule has to be added to <Tags> in nuspec file.

Real reason

You shouldn't create nuspec file and pack nuget package manually at all. Use Publish-Module cmdlet instead.

How to do it properly

I've updated powershellget-module GitHub with:

  • Example of minimal module which can be published
  • A way how to use local folder as Nuget feed
  • Publishing, installation and usage of that module
  • Reference script with no dependencies which does it all locally, so you can study it

Check it out.

like image 126
Anton Avatar answered Sep 21 '22 06:09

Anton