Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transforming .csproj using nuget package

I know it is possible to transform source code files and config files using a custom Nuget package. Unfortunately I cannot find anywhere on how to be able to append to or change a .csproj file. Is this possible?

like image 297
w4ymo Avatar asked Jan 26 '16 15:01

w4ymo


People also ask

How do I use a Nupkg file?

Copy the . nupkg file to a local folder. Add the folder to your package sources using the nuget sources add -name <name> -source <path> command (see nuget sources). Note that you need only set this local source once on any given computer.

How do I reference an existing NuGet package from a new project?

NET or . NET Core project. After you install a NuGet package, you can then make a reference to it in your code with the using <namespace> statement, where <namespace> is the name of package you're using. After you've made a reference, you can then call the package through its API.


1 Answers

The only file modifications that are directly supported in NuGet without resorting to PowerShell are:

  • web.config and app.config transforms.

You can add files from your NuGet package into the project, but they will overwrite the existing files. The files added can also be transformed if they are .pp files.

Note that there is a move away from PowerShell scripts in NuGet 3 so you may want to see if you get away with not using PowerShell. Other alternatives are custom MSBuild .props and .targets files you can include in your NuGet package which can be used to indirectly modify the project files. You can basically add anything you want to the existing project in your own custom MSBuild .targets file since this is imported into the project.

PowerShell scripts can be used to append or change a .csproj. There are some example NuGet packages that do this, such as the older Bcl Build NuGet package which adds an import element to the project. Note that adding an import is directly supported in the latest NuGet.

Transforming existing code files with PowerShell is more difficult but you can modify code files using the Visual Studio object model.

like image 78
Matt Ward Avatar answered Sep 28 '22 13:09

Matt Ward