How to resolve Nuget Package Version and Path in P

2020-01-30 00:45发布

I have a NuGet Package containing content files that is referenced in my project. When getting latest on a new machine, the build fails because NuGet Restore doesn't copy content files. So what I want to do is this:

In PreBuild
If my Content file doesn't exist,
    run this nuget Command: "Update-Package My.Nuget.Sources -reinstall"

I'm struggling with determining path differences due to versioning of the Nuget files and getting access to nuget. How do I actually generate the SourceOnlyNugetVersion and NugetPath variables below?

if not exist "$(ProjectFolder)App_Packages\My.NuGet.Sources.$(SourceOnlyNugetVersion?)\somefile.cs" (
    "$(NugetPath)nuget.exe" Update-Package My.NuGet.Sources -reinstall        
) 

1条回答
SAY GOODBYE
2楼-- · 2020-01-30 01:20

How to resolve Nuget Package Version and Path in Pre-Build Event of a Project?

If you want to reinstall package automatically in Pre-build event, I am afraid you can`t achieve it currently.

We could use the command Update-Package -Id <package_name> –reinstall to reinstall the packages to your project in the Package Manager Console, but it is impossible to automate that.

If you want to automate it in the build event, you have to call the NuGet CLI rather than Package Manager Console. Because NuGet CLI does not modify a project file or packages.config; in this way it's similar to restore in that it only adds packages to disk but does not change a project's dependencies. See NuGet CLI reference.

The operation Install packages on NuGet CLI:

enter image description here

Conversely, operation Install packages on Package Manager:

Installs a package and its dependencies into a project.

So we could not use NuGet CLI to reinstall NuGet packages for project.

Besides, we could not use the Package manager console powershell outside visual studio, because package manager console is providing is access to visual studio objects.

https://github.com/NuGet/Home/issues/1512

Similarly, we could not use Package manager console in the build event, build events are run by MSBuild so it needs to work when the build is run from the command line.

So it seems impossible to automate reinstall NuGet packages, or the alternative approach would be to write a console app that uses NuGet.Core.dll to do the same thing that the PowerShell script is doing.

查看更多
登录 后发表回答