So init.ps1 https://docs.nuget.org/create/creating-and-publishing-a-package#automatically-running-powershell-scripts-during-package-installation-and-removal runs in two circumstance. When you install a package for the first time and when you open the nuget powershell console as part of opening a solution.
Init.ps1 runs the first time a package is installed in a solution.... The script also runs every time the solution is opened
So inside Init.ps1 how do you determine in which context it is running?
Init.ps1 runs the first time a package is installed in a solution.... The script also runs every time the solution is opened.
Open your project or solution in Visual Studio, and select Tools > NuGet Package Manager > Package Manager Console to open the Package Manager Console window.
To open the console in Visual Studio, go to the main menu and select Tools > NuGet Package Manager > Package Manager Console command.
The NuGet Package Manager Console uses NuGet PowerShell commands to find, install, uninstall, restore, and update NuGet packages. The console is built into Visual Studio on Windows.
There isn't a built-in way—Init.ps1
is meant to be called each time Package Manager Console is initialized, so there's purposefully no context parameter. As Yishai Galatzer puts it in no uncertain terms on GitHub:
That's not the intent of init.ps1, and we will not enable it. NuGet is not an automation entry point into visual studio, and please do not use it as such.
I do find this a bit unfortunate since support for Install.ps1
and Uninstall.ps1
was removed in NuGet 3 back in 2015, which means we can no longer run custom code after installation, even simple stuff like showing an HTML readme. The reasoning makes sense, but it would be nice if there were an alternative for package installs specifically inside Visual Studio.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With