Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In a nuget init.ps1 how do you detect if running as install or as console initialization

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?

like image 661
Simon Avatar asked Aug 20 '15 04:08

Simon


People also ask

What is init ps1?

Init.ps1 runs the first time a package is installed in a solution.... The script also runs every time the solution is opened.

How do I get NuGet console?

Open your project or solution in Visual Studio, and select Tools > NuGet Package Manager > Package Manager Console to open the Package Manager Console window.

How do I use NuGet console?

To open the console in Visual Studio, go to the main menu and select Tools > NuGet Package Manager > Package Manager Console command.

What is Package Manager Console?

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.


1 Answers

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.

like image 146
Eric Eskildsen Avatar answered Sep 24 '22 00:09

Eric Eskildsen