NuGet how to apply properties to files
-
29-10-2019 - |
Question
I am creating NuGet Package. I have put two files in content
folder, exe and config files that will be added when user installs this package.
In addition I would like to change VS project properties for these two files.
What do I need to do for that when building the package?
Solution
When you say "apply properties change" do you mean file system properties such last modified time, hidden flag, etc.?
If so there are a set of PowerShell scripts that are run (if they exist in your package) you can use to do this. It won't happen when you build the package, but it will run when somebody installs the package - which should have the same effect form the users point of view. You probably want either Init.ps1
or Install.ps1
.
From Automatically Running PowerShell Scripts During Package Installation and Removal in the NuGet docs:
A package can include PowerShell scripts that automatically run when the package is installed or removed. NuGet automatically runs scripts based on their file names using the following conventions:
Init.ps1
runs the first time a package is installed in a solution.
- If the same package is installed into additional projects in the solution, the script is not run during those installations.
- The script also runs every time the solution is opened. For example, if you install a package, close Visual Studio, and then start Visual Studio and open the solution, the
Init.ps1
script runs again.Install.ps1
runs when a package is installed in a project.
- If the same package is installed in multiple projects in a solution, the script runs each time the package is installed.
- 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.- If your package also has an
init.ps1
,install.ps1
runs afterinit.ps1
.Uninstall.ps1
runs every time a package is uninstalled.- These files should be located in the tools directory of your package.
- At the top of your file, add this line:
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 EnvDTE project object and represents the project the package is installed into. Note: This will be null inInit.ps1
. In that case doesn't have a reference to a particular project because it runs at the solution level. The properties of this object are defined in the MSDN documentation.- When you are testing
$project
in the console while creating your scripts, you can set it to$project = Get-Project