Simon
Simon

Reputation: 34880

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?

Upvotes: 6

Views: 2747

Answers (2)

Eric Eskildsen
Eric Eskildsen

Reputation: 4769

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.

Upvotes: 5

cezarypiatek
cezarypiatek

Reputation: 1124

It's old question but maybe it helps somebody. To run script during package installation instead of console initialization put your code inside install.ps1 file.

Upvotes: -1

Related Questions