NuGet how to apply properties to files - nuget

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?

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 after init.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 in Init.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

Related

Nuget Package Without Lib-Ref-Content Folders

I have downloaded an open source project, made changes to it and then created a nuget package from this new code. The package actually functions as a post-build target. It just runs an exe. That is why it does not contain a lib folder. There is a tools folder and files are copied under this folder. The other folders are: build, buildCrossTargeting, package, _rels. .But when I try to install this local nuget package to a .net 4.6.1 project, I get the following error in Visual Studio:
You are trying to install this package into a project that targets
'.NETFramework,Version=v4.6.1', but the package does not contain any
assembly references or content files that are compatible with that
framework.
When I open and inspect the original nuget package downloaded from nuget.org, I see the same structure. But somehow it installs fine while this locally created one raises error.
What can be done about it?
With the information you provided, I can only guess (please see How to create a Minimal, Reproducible Example). My guess is that you changed the name (id) of the package, but didn't change the name of the target or props files in the build directory. As the docs say, the name of the targets and props file must match the package id exactly, so if you change the package id, you must rename those files.

How can I deploy a PS1 script with a nuget package

I have a PS1 script that I use in all my projects to sign the assemblies. Until now I copied this file over to all my projects. Now I wanted to create a nuget package with the PS1 file.
I created a nuspec file and put the file in "content". Unfortunately nothing happened. Then I tried to put it in lib. Still noting happened. When I restore the package in my project no files where created in this project.
When I analyst the nupkg file with my 7-Zip the file looks OK. The ps1 file was in content, lib respectively.
I didn't found anything to this topic online. Can someone explain to me, how to create such a NuGet-Package?
When a project using packages.config installs a NuGet package, the package's tools\install.ps1 script will run. However, this no longer happens when the project using the package uses PackageReference (such as SDK style projects, used by .NET Core).
Similarly, the files in the content folder of the nupkg are copied into the project on install, but only when the project uses packages.config. PackageReference projects use the contentFiles folder in the nupkg, however the behaviour is different. Those files are copied only on build, not install, for .NET Framework projects and on publish for .NET Core projects. Probably not what you want for signing assemblies.
The feature you probably want to use is including MSBuild props and targets in your package. Note that the props and targets file names must match the package id exactly for NuGet to use them. You probably want to use afterTargets="build" at a guess.

What is the recommended way to copy a .NET exe and its DLLs with a NuGet package

I am creating a NuGet package, which is a library DLL. I did this successfully.
However I would like to have a .NET exe (and its dependent assemblies) also in the target machine. It is completely OK if the .exe and these dlls are under the solution's package folder.
If it is possible I do not want to create a standard Windows installer and run it silently.
Thanks in advance
You'll have to edit your .nuspec file. You can include a element to include additional files in your package. Look at what else you can do by looking at the specs.
You can run your exe by creating a powershell script during package installation.
Starting from NuGet 2.0 a package can contain a "tools" folder. Here you can put exe, scripts and so on.
http://docs.nuget.org/docs/creating-packages/creating-and-publishing-a-package#Framework_Version_Folder_Structure
Binary under this folder will not be referenced, just copied on the machine.
Hope it helps.

Copy tools from nuget package to bin folder

I have some automated tests that are using Selenium WebDriver. Tests should run on Chrome so I need to have ChromeDriver.exe. I am using NuGet to download the libraries and found that the ChromeDriver could be downloaded as a package too (http://www.nuget.org/packages/WebDriver.ChromeDriver).
However, this package contains only ChromeDriver.exe in the Tools folder. I need to copy this file into the bin folder because tests need to find it. Is there some way to do the copy in a generic way (without specific paths), because in the folder where the ChromeDriver.exe is unpacked from package contains version number so I would need to change the paths when the new version will be used. Also I would need to do similar thing with the NUnit.Runner package.
You could probably use a post-build event. Then inside the event use copy/xcopy and move it into the bin folder after the project has successfully built.

NuGet and nUnit automation

I have a VS project and in the project properties under the Debug tab I set:
Start External Program: D:\SolutionName\packages\NUnit.2.5.10.11092\tools\nunit.exe
Command Arguments: projectname.dll
This lets me start nUnit and run the nunits tests dll and when I start debugging the project.
Is there a better way? We use TFS and not everyone installs the solution to d: and the version number in the path where NuGet installs it changes periodically.
Was hoping to some how grab the text of the nunit.exe path from the path in the VS: Project : References section that was placed there by NuGet. This way I wouldn't have to change it for nUnit version changes and other TFS users wouldn't have to change it either.
Any ideas?
You might want to take a look at this:
http://lostechies.com/joshuaflanagan/2011/06/24/how-to-use-a-tool-installed-by-nuget-in-your-build-scripts/
If you're using NUnit in NuGet, then the runner will be in packages\NUnit(version)\, so you could probably use $(SolutionDir)packages\NUnit(blah) in the External Program command to run the version pulled from the NuGet package.
As Danny mentioned, install it to a relative (to your source code) tools folder via NuGet, ie
./tools/nuget.exe install Nunit.Runners -o ./tools
Then in your project configuration, just use the relative path.
I ran into the same issue. After a great deal of searching I found this question: Get NuGet package folder in MSBuild
Basically, you can create a project item containing a sort-of "wildcard" in the path name in place of the specific version number and then tell MSBuild to retrieve the relative path directory.