1

I'm creating a package for nuget and I would like to edit the project.csproj file and replace some codes as part of the package installation process.

Can this be done with the nuspec file?

Or should you use a PowerShell script? If possible give an example with PowerShell

GaryEmery
  • 278
  • 2
  • 13
  • The question is confusing. You're creating a NuGet package, but you're installing it, then you want to edit the packaged version after installing it? – canton7 Nov 14 '19 at 12:59
  • no, i want to install my nuget package to a wpf project or any other project then after installing package i want to edit that wpf project csproj file –  Nov 14 '19 at 13:02
  • More details of what exactly you're trying to edit in the `.csproj` might reveal a better solution – Barry O'Kane Nov 14 '19 at 13:16
  • Well, given that the project has already been compiled, changing the csproj is rather closing the stable door after the horse has bolted, no? – canton7 Nov 14 '19 at 13:49
  • I think what you mean is that as part of the installation process of your package, you want to edit the .csproj where your package is being installed. This can be done with Powershell, but installation scripts are being deprecated and will not work when someone is using PackageReference (see: https://learn.microsoft.com/en-us/nuget/consume-packages/migrate-packages-config-to-package-reference ). However, I don't know what you're supposed to do as a replacement. – howcheng Nov 14 '19 at 22:24

1 Answers1

2

You're looking for the MSBuild props/targets extensibility.

Whatever changes you want to make to the project file you put in either a props or targets file (props are imported before the csproj is evaluated, targets afterwards). You need to use the correct naming convention, but NuGet 5.3 (included in .NET Core SDK 3.0, if you use dotnet pack) has a warning now if you don't get it right. Then, when the project uses your package, NuGet tells the rest of the build system to import your packages props/targets file.

zivkan
  • 12,793
  • 2
  • 34
  • 51
  • That documentation refers to the old format of using a nuspec file. How do we do this with the newer projects where the Nuget metadata is defined in the proj file? – gregsdennis May 03 '21 at 09:51
  • Our [docs on including content in a package](https://learn.microsoft.com/en-us/nuget/reference/msbuild-targets#including-content-in-a-package) explains it. Basically in an ItemGroup, you should be able to do something like `` – zivkan May 04 '21 at 23:20