Piotr Perak
Piotr Perak

Reputation: 11088

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?

Upvotes: 19

Views: 16256

Answers (3)

Marko A.
Marko A.

Reputation: 93

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>

Upvotes: 4

Jacco
Jacco

Reputation: 3272

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.

Upvotes: 7

mthierba
mthierba

Reputation: 5667

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

Upvotes: 21

Related Questions