17

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?

Piotr Perak
  • 10,718
  • 9
  • 49
  • 86

3 Answers3

21

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).

Jon Galloway
  • 52,327
  • 25
  • 125
  • 193
mthierba
  • 5,587
  • 1
  • 27
  • 29
  • Thanks I didn't notice it on help pages. I changed Install.ps1 to Init.ps1 and it works. But still I see no ECHO. Although I see 'Successfully installed packageName' – Piotr Perak Jul 31 '11 at 20:51
  • Re-open your solution once the package has been installed. Make sure, however, that the Package Manager Console is already visible. You should then see any output from your `Init.ps1` script. – mthierba Jul 31 '11 at 20:57
  • Yeah I see output at VS start. But not on Install-Package. Is this normal? – Piotr Perak Jul 31 '11 at 21:00
  • Yes, I believe it is. At least that's what I did get as well when trying something similar. Feels like a bug, though... – mthierba Jul 31 '11 at 21:16
  • Maybe You also know why Uninstall.ps1 isn't called? I verified under Powershell ISE that it works (deletes folders created during install). – Piotr Perak Jul 31 '11 at 22:07
  • @Peri: I've actually created an issue on the NuGet project site: http://nuget.codeplex.com/workitem/1365. You might want to vote for it to bump up its priority... – mthierba Aug 05 '11 at 11:52
  • @Peri - as-of nuget 2.0 console - Uninstall.ps1 is not invoked for solution-only packages. – Jay Walker Sep 06 '12 at 01:35
7

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.

Jacco
  • 3,251
  • 1
  • 19
  • 29
2

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>
Marko A.
  • 73
  • 9