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

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

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?

NuGet init.ps1 executing twice

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.

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"

NuGet - install.ps1 does not get called

I'm trying to create my first NuGet package. I don't know why my install.ps1 script does not get called. This is directory structure
--Package
|
- MyPackage.nuspec
- tools
|
- Install.ps1
- some_xml_file
I build package using this command line
nuget.exe pack MyPackage.nuspec
When I Install-Package from VS Package Manager Console install.ps1 does not get called.
I thought that maybe I had some errors in script and that's the reason so I commented out everything but
param($installPath, $toolsPath, $package, $project)
"ECHO"
But I don't see ECHO appearing in Package Manager Console. What can be wrong?
Install.ps will only be invoked if there is something in the \lib and/or \content folder, not for a "tools only" package, though. See here:
The package must have files in the content or lib folder for Install.ps1 to run. Just having something in the tools folder will not kick this off.
Use the Init.ps1 instead (however, this will be executed every time the solution is opened).
Install.ps1 (and Uninstall.ps1) are no longer called in v3, but you can use Init.ps1. See here:
Powershell script support was modified to no longer execute install
and uninstall scripts, but init scripts are still executed. Some of
the reasoning for this is the inability to determine which package
scripts need to be run when not all packages are directly referenced
by a project.
An alternative to the install script can sometimes be a package targets file. This targets file is automatically weaved into the project file (csproj, ...) and gets called with a build.
To allow Nuget to find this targets file and to weave it in, these two things are mandatory:
the name of the targets file must be <package-name>.targets
it must be saved in the folder build at the top level of the package
If you like to copy something to the output folder (e.g. some additional binaries, like native DLLs) you can put these binaries into the package under folder binaries and use this fragment in the targets file for the copying:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="CopyBinaries" BeforeTargets="BeforeBuild">
<CreateItem Include="$(MSBuildThisFileDirectory)..\binaries\**\*.*">
<Output TaskParameter="Include" ItemName="PackageBinaries" />
</CreateItem>
<Copy SourceFiles="#(PackageBinaries)"
DestinationFolder="$(OutputPath)\%(RecursiveDir)"
SkipUnchangedFiles="true"
OverwriteReadOnlyFiles="true"
/>
</Target>
</Project>

NUnit assembly not found

I've used NUnit before, but not in a while, and never on this machine. I unzipped version 2.4.8 under Program Files, and I keep getting this error when trying to load my tests.
Could not load file or assembly 'nunit.framework, Version=2.4.8.0, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77' or one of its dependencies. The system cannot find the file specified**
In order to simplify the problem, I've compiled the most basic possible test file.
using NUnit.Framework;
namespace test
{
[TestFixture]
public class Tester
{
[Test]
public void ATest()
{
Assert.IsTrue(false, "At least the test ran!");
}
}
}
I've added "C:\Program Files\NUnit-2.4.8-net-2.0\bin" to my PATH (and rebooted). Note that if I copy the test assembly into that folder, then
C:\Program Files\NUnit-2.4.8-net-2.0\bin>nunit-console test.dll
works, but
C:\Program Files\NUnit-2.4.8-net-2.0\bin>nunit-console c:\dev\nunit_test\test.dll
and
C:\dev\nunit_test>nunit_console test.dll
fail with the above error.
Presumably I could get around this by copying the NUnit.Framework DLL file into my project's bin folder, but I don't remember having to do this in the past. Moreover, I get the same error in the GUI. Shouldn't the GUI know where the framework is located (that is, in the same folder)?
I'm not using Visual Studio. I use the following line to compile the test project.
%windir%\Microsoft.NET\Framework\v2.0.50727\csc.exe /r:"C:\Program Files\NUnit-2.4.8-net-2.0\bin\nunit.framework.dll" /t:library /out:test.dll test.cs
I tried both the .msi and the .zip file with the same result.
Make sure you have added a reference to nunit.framework. If you have, then make sure the properties of that reference have the copy local property set to true.
I had the same problem, and I had installed using NUnit-2.4.8-net-2.0.msi. Expanding on the "add to the GAC" comment, here's what I did:
Open your "Visual Studio command prompt (generally: make sure gacutil is in your path) and: cd "C:\Program Files\NUnit 2.4.8\bin"
Unregister your NUnit entries from the GAC. You can do this by finding the NUnit entries registered in the GAC:
gacutil /l | find /i "nunit" > temp.bat && notepad temp.bat
Prepend the nunit.core and nunit.framework lines with "gacutil /uf", i.e.:
gacutil /uf nunit.core,Version=2.4.2.0,Culture=neutral,PublicKeyToken=96d09a1eb7f44a77
gacutil /uf nunit.framework,Version=2.4.2.0,Culture=neutral,PublicKeyToken=96d09a1eb7f44a77
Run your .bat file to remove them:
temp.bat
Register the NUnit DLL files you need:
gacutil /i nunit.core.dll
gacutil /i nunit.framework.dll
If you install using NUnit-2.4.8-net-2.0.msi, the NUnit assemblies are added to the GAC.
You can also reinstall manually by running gacutil from the Visual Studio 2005 command prompt.
Note that the current NUnit installation (2.5.10) doesn't register itself automatically in the GAC.
If you must use GAC, register it via gacutil /i <nunitframeworkpath> where nunitframeworkpath is usually %Program Files%\NUnit\net-2.0\framework\nunit-framework.
I got this error message today when I tried to add a new test assembly to an existing NUnit test project. It seems that my test projects had multiple path references to identical nunit.framework.dll assemblies.
If you have more than one test assembly in your NUnit project, you may want to verify the Path property of the nunit.framework reference in your test projects. Once I made them match, the error message went away.