NuGet init.ps1 executing twice - powershell

I am following the solution found at "Create a NuGet package that shows update notifications" to get a notification of a nuget package update.
However the init.ps1 script is executing twice.
I stripped all the code out so that only the following is in init.ps1.
param($installPath, $toolsPath, $package, $project)
if ($project -eq $null) {
$project = Get-Project
}
Write-Host "Hello, I'm running inside of init.ps1"
When I close the solution and reopen it, in the output window the text is there twice.
I am using VS 2012, NuGet 2.2.31210
I checked the packages.config file and there is only one entry for my package.
Why is it running twice and is there any way to get it to only run once?
Thanks,
Joe

In init.ps1 script, Get-Project will return a random project in the solution but not the particular project the package has been installed.
Use $projects = Get-Project -All to get the list of projects in solution, loop through the projects and search the packages.config to check if the current project has the package installed.

Related

Nuget package init.ps1 script and PackageReference modification

I want to have referenced dll(s) in my package in "copy local = false" mode. In other words, I don't want that the dll(s) my package ships is/are copied into a output directory after the build.
The way this can be done in "packages.config" scenario is by having a following script in the tools/install.ps1 file.
param($installPath, $toolsPath, $package, $project)
$asms = $package.AssemblyReferences | %{$_.Name}
foreach ($reference in $project.Object.References)
{
if ($asms -contains $reference.Name + ".dll")
{
$reference.CopyLocal = $false;
}
}
I know that the newest Nuget version with PackageReference functionality uses only init.ps1 script. But I don't have any clue what so ever, what shall I write into that script.
I also know that the package user can add...
<ExcludeAssets>Runtime</ExcludeAssets>
to make this happen. But as a package author, I really would like to enforce that behaviour. So would it be possible to add that in the script?

Update all packages except one

is there any way to update all packages except one?
For instance when I created new project I need to update all packages except jQuery. It should stay at same version.
You could write a custom script for updating the packages like the following and execute it in the package manager console
$list = Get-package -project {Add project name here}
for($i=0; $i -lt $list.Length;$i ++ ) { if($list[$i].Id -ne "jquery") { Update-Package -project {Add project name here} $list[$i].Id } }
no there is no direct way of doing that. May be you can update all, then uninstall JQuery and install the version of jquery you want.

How to delete files a dependency installed after your package is installed

I'm working on a nuget package that depends on the Unity and Unity.Mvc4 packages. My nuspec file has them listed as dependencies. Everything works, but when my package is installed the dependencies are installed first.
The Unity dependencies deploy files to the root of the project that my package has moved to a different location and customized, as such I don't want those files to exist in the root after my package is installed.
Is there any way to override dependency files in nuspec, or run a powershell script after install to clean them up?
You might add a Powershell script that move those files created by Unity to your actual project root.
For information about executing Powershell scripts during Nuget package installation, see the Nuget documentation. Note that these scripts must be placed within the tools folder of your Nuget package to be automatically executed.
Here's the code in my install.ps1 that did the trick if anyone wants it for reference.
# Runs every time a package is installed in a project
param($installPath, $toolsPath, $package, $project)
# $installPath is the path to the folder where the package is installed.
# $toolsPath is the path to the tools directory in the folder where the package is installed.
# $package is a reference to the package object.
# $project is a reference to the project the package was installed to.
#EnvDTE Project Reference
#https://msdn.microsoft.com/en-us/library/envdte.project.projectitems.aspx
function deleteProjectItem($fileName) {
$file = $null
foreach ($item in $project.ProjectItems) {
if ($item.Name.Equals($fileName, "InvariantCultureIgnoreCase")) {
$file = $item
break
}
}
if ($file -ne $null) {
Write-Host "Deleting: $($item.Name) ..." -NoNewline
$item.Delete()
Write-Host "Deleted!"
}
}
deleteProjectItem "BootStrapper.cs"
deleteProjectItem "job_scheduling_data_2_0.xsd"
deleteProjectItem "Unity.Mvc4.README.txt"

$project.Properties is null in Init.ps1 (Visual Studio 2012)

I created a simple NuGet package that includes an Init.ps1 script in the Tools folder. The script starts as follows:
param($installPath, $toolsPath, $package, $project)
$postBuildEvent = $project.Properties.Item("PostBuildEvent").Value
When installing the package in a Visual Studio 2012 project I get an error on $project.Properties.Item, because $project.Properties is null: "You cannot call a method on a null-valued expression". I checked if $project.Properties is null and it is.
Question is, why it is null. I just try to alter the post build event (like described here). Does anyone have clues?
Edit:
If the code is inside Install.ps1 it works. It seems that the project properties are not available when first installing a package.
The parameter list for init.ps1 is:
param($installPath, $toolsPath, $package)
init.ps1 does not have the $project parameter because it is executed at the solution level and is not dependent on project.
See here for more information: http://docs.nuget.org/docs/creating-packages/creating-and-publishing-a-package

how to output debugging messages from install.ps1 in NuGet

I am developing a NuGet package, including an install.ps1 script which runs during the package installation. I would like to be able to output messages from my script and also output the results of running .bat files from within my sript.
Here is my install.ps1:
param($installPath, $toolsPath, $package, $project)
Write-Output "Running install.ps1 for MyPkg"
Set-Location $toolsPath
.\helper.bat | Write-Output
When I install my package in Visual Studio, then I look in the Package Manager option in the Output page, I see:
Executing script file 'C:\Test\packages\MyPkg.1\tools\install.ps1'.
and it seems the script is working (I can tell in other ways that helper.bat ran), but I don't see any of the output. How can I get the output working?
I could not get the output when installing from the NuGet Package Manager Dialog, I'll dig a bit later to see where it's going.
But you should be able to see it when installing from the Nuget console (Tools->Library Package Manager->Package Manager Console). The output went directly in the console. Example :
PM> uninstall-package samplepackage
hello from unninstal.ps1
Successfully removed 'samplepackage 1.0.0' from WebApplication24.
unninstal.ps1 :
param($installPath, $toolsPath, $package, $project)
Write-Host "hello from unninstal.ps1"