How to update Nuget tools-only packages via command line - visual-studio

Is it possible to update a "tools-only" Nuget package via the command line?
By "tools-only", I mean a package that does not contain a "lib" or "content" folder (according to the definition on the Nuget site), and contains scripts or tools rather than libraries that are referenced by Visual Studio Projects. A typical use is for tools that assist with build processes.
I am able to add tools-only packages in Visual Studio by managing the Nuget packages at the Solution level. That will download the package correctly, and also let me see and install updates to it.
But I've not been able to get the "update" command of the "nuget.exe" command line app to do the package update. It appears to drive all updates from the project files... but the package is not referenced by a project because it's not a code library.
I understand the focus of Nuget is code libraries, but the doco does make mention of tools packages... Given that it can be done within Visual Studio, I'd have thought the same should be possible on the command line.
E.g.
Tool package "A" (no lib, just tools folder)
In VS in project "B", manage Nuget packages on solution and install A
(Will download, and create ".nuget" folder with packages.config)
Release new version of "A"
In this scenario, I can see and install the update within VS, but cannot get the right command line to install the update.

Related

Project references are not resolved after updating NuGet packges through CLI

I have a nightly process running on TeamCity that will loop through my packages on a locally hosted NuGet repo and update all packages in all solutions to the latest prerelease version.
When I pull the latest code the morning after. The solution builds fine, but my project references are not loaded.
Unresolved project references: https://imgur.com/a/v7Klbkm
Unresolved code: https://imgur.com/a/EEzWgUe
I'm using packages.config to manage my NuGet packages.
In order to continue working I have to "reload" my Visual Studio projects by either manually asking the properties of the missing reference, or by changing something in the .csproj file.
That is my little workaround, but for some solutions with 10+ projects this becomes quite cumbersome.
Other means of updating packages work fine, if I try to update the packages through Visual Studio I have no issues. It only fails when using the NuGet.exe command line utility.
Here's the command we use to update the packages through NuGet.exe on TeamCity during a nightly build:
NuGet.exe update <path_to_packages.config> -Id <current_package_to_update> -pre -NonInteractive
This is the version of NuGet we're using:
NuGet Version: 4.8.1.5435
Here's an excerpt of the logging:
found PFW.Utilities with version 2019.9.0-build0009 in file C:\Sources\Xmp\ModXmp\packages.config
http://srvppratonexus.prato.be:8888/nexus/service/rest/repository/browse/nuget-prato-prereleases/PFW.Utilities/2019.9.0-build0009
MSBuild auto-detection: using msbuild version '16.1.76.45076' from 'C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\bin'.
Feeds used:
http://srvppratonexus:8888/nexus/service/local/nuget/nuget-group/
https://api.nuget.org/v3/index.json
C:\Program Files (x86)\Microsoft SDKs\NuGetPackages\
Attempting to gather dependency information for multiple packages with respect to project `C:\Sources\Xmp\ModXmp\ModXmp.csproj`, targeting `.NETFramework,Version=v4.6.1`
Gathering dependency information took 10.79 sec
Attempting to resolve dependencies for multiple packages.
Resolving dependency information took 0 ms
Resolving actions install multiple packages
Found package 'PFW.Utilities 2019.9.0-build0019' in 'C:\Sources\Xmp\packages'.
Removed package 'PFW.Utilities 2019.9.0-build0009' from 'packages.config'
Successfully uninstalled 'PFW.Utilities 2019.9.0-build0009' from ModXmp
Package 'PFW.Utilities.2019.9.0-build0019' already exists in folder 'C:\Sources\Xmp\packages'
Added package 'PFW.Utilities.2019.9.0-build0019' to 'packages.config'
Successfully installed 'PFW.Utilities 2019.9.0-build0019' to ModXmp
Executing nuget actions took 192.26 ms
When I pull the latest code the morning after. The solution builds
fine, but my project references are not loaded.
When you call nuget.exe to update nuget packages for the project using packages.config format outside the VS IDE, it not only modified the packages.config file, but also modified the project file(.csproj). So VS IDE can't load them well unless a reload.
Not sure what's the real solution you have, you can Go Tools => option => Environment => Documents, make sure you've enabled the Detect when file is changed outside the environment. Then you can get reload all dialog when you modify the project file outside VS IDE.
Reload all dialog looks like this:
That is my little workaround, but for some solutions with 10+ projects
this becomes quite cumbersome.
If for some reason that dialog didn't occur, you can manually open one .csproj using notepad, then add one empty line into it and save. VS will detect the change and show the dialog, then click reload all to reload all projects in the solution.(For the solution with many projects)

Test NuGet package Installation on C# project in Azure Pipeline

Is it possible, as part of a CI process for NuGet package creation, to install a newly created package to a project, residing in the repository? So that the installation can be tested.
It's easy to do using Visual Studio UI, but how to do it on a newly created azure pipeline worker automatically?
Install NuGet package on the project in Azure Pipeline
I am afraid it is impossible to install NuGet package on the project in Azure Pipeline.
Because NuGet CLI install command line just Installs a package into the current project but does not modify projects or reference files (packages.config).:
It is like the command line nuget restore, just download the packages not install it.
To install the package to the project, we need modify the project file via access to visual studio objects:
https://github.com/NuGet/Home/issues/1512
So it should be impossible to install NuGet packages out of Visual Studio, check my another thread for some details.
Besides, we also do not recommend to install NuGet package in Azure Pipeline. If we install a newly created package to a project automatically, it will use the scripts to modify our Repos, which is not recommended and safe.
Personally, the correct process is:
Create the new package in the Azure pipeline.
Publish the new package to the Artifacts or any other nuget feed.
Install/Update the new package to the project with Visual Studio and test it.
Update the new package version to the Repos.
Hope this helps.
The NuGet.Client repo has a bunch of tests that install packages into test projects and assert various things. I know of a whole lot of PackageReference tests, but can't remember any packages.config tests. Using the .NET CLI it's easy to script a lot of it, but depending on what you want to do, you might need to write some code to manipulate XML files.
Here's a bunch of useful commands totally written from memory and therefore might not work as is, but it'll get you started:
# create a new .NET Core console app. You'll need to edit the csproj to test different frameworks
dotnet new console
# create nuget.config file
dotnet new nugetconfig
# add a local folder as a package source
nuget sources add -configfile nuget.config -name local -source ..\newPackages
# set the global packages folder to a empty/temporary directory, so the test package
# doesn't pollute the agent's global packages folder
nuget config set -configfile nuget.config globalPackagesFolder gpf
# add the latest version of the package to the project in the current directory.
# use --version to specify a version
dotnet add package MyTestPackage
Since SDK style projects are so short and simple, you may be better off just hardcoding the contents in your code and write them to disk for your tests. It's what we (NuGet.Client) do.
We have plans to eventually move the config options to the dotnet cli so that you won't need to download nuget.exe, but it's really low priority since it's so easy to workaround. nuget.exe works on mono on Linux and Mac, or just hardcode the conents of the config in a string in your test and write it at runtime.
This will only be useful for you if the things you want to test are not impacted by package compatibility issues with PackageReference vs packages.config. However, given the future of .NET is SDK style projects, and SDK style projects don't support packages.config, you can try justifying it by saying it's the future.

How reproduce VS2017 Package Manager command "Update-Package -reinstall" with Nuget CLI

I've tried with Nuget Update, Nuget restore, but they are actually not the same and here's why:
I've created a Nuget pkg only to distribute an executable file. When I install the Nuget the executable is added to my project so I can reference it and run it within my application.
Now, I don't want that every developer has to install the Nuget to have the executable in their project, but I just want the .exe to be added when the project is built. And of course, the reference to the package in the config file is present.
If I run Update-Package -Id -reinstall(using the Package Manager of VS) a folder with the .exe is added to the project root, but if I do nuget update or nuget restore (using the nuget CLI) it only re-add the .exe into the packages folder.
The reason why I need to run the nuget CLI is because I'm using TeamCity and I want to add the .exe when the automatic build runs.
I hope it makes sense.
How reproduce VS2017 Package Manager command “Update-Package -reinstall” with Nuget CLI
I am afraid you can NOT do such things with NuGet CLI. That because NuGet CLI does not modify a project file or packages.config. When we modify the project file, we need to do it by NuGet API in Visual Studio, but only package manager console can provide access to visual studio objects:
https://github.com/NuGet/Home/issues/1512
That is the reason why I said the easiest way is using the command line Update-Package -Id -reinstall in your previous thread. Then I also gave you another way to resolve this issue from the root cause, using .targets file to copy the executable file to the the project root, please check my update answer.
Of course, there is another simple idea to resolve this issue, just add a pre-build event in your project to copy the executable file to the the project root:
xcopy /Y "$(SolutionDir)packages\MyToolPackage.1.0.0\Tools\wkhtmltopdf.exe" "$(ProjectDir)"
Hope this helps.

NuGet Command-line options for uninstalling/reinstalling packages for TFS/VS2013 and NuGet 3.4.3

I modified a solution and deleted one of the .csproj files and instead packaged the .dlls into a NuGet package. Then I added the package to the two other .csproj files that reference the .dlls. All was good - it builds locally, but I can't get it to build on the build server. When I look at the code gotten out of TFS for the build I see that the packages.config for both projects have the correct reference to the NuGet package, but when I open the solution in VS the references have little yellow exclamation marks next to them because they're broken references. The only way I can get it to build is to open the NuGet CMI and execute Uninstall-package package-name and the Install-Package package-name. Then the references are good. When I look in the packages.config of the main project it contains the correct reference to the Package. So I've given up on getting TFS to correctly grab the package, but since Install-Package and Uninstall-Package are CMI commands only I can't automate that (or can I)? Does anyone know if a way that I could automate that to happen after the source is pulled from TFS but before the build?
The yellow exclamation marks issue should be related to the reference path. When you download the source from TFS to another location, the system cannot find the references as the original reference path changed.
So, you need to reinstall the package, you can use the NuGet command line in the Package Manager Console:
Update-Package -reinstall
since Install-Package and Uninstall-Package are CMI commands only I can't automate that (or can I)? Does anyone know if a way that I could automate that to happen after the source is pulled from TFS but before the build?
The simple answer is you can not automate that. You can use the command Install-Package and Uninstall-Package to reinstall the packages to your project in the Package Manager Console, but it seems impossible to automate that. Please forgive me for the lengthy explanation below.
First, we need to know the different the operation Install packages between NuGet CLI and Package Manager, although NuGet CLI and Package Manager both support the operation Install packages.
The operation Install packages on NuGet CLI:
Obviously, NuGet will not reinstall the references when you using the Install-packages operation on the NuGet CLI, just download the package to the packages folder. See NuGet CLI reference:
The install command 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.
Conversely, operation Install packages on Package Manager:
Installs a package and its dependencies into a project.
If you want to automate that, you have to do this operations via NuGet CLI. Since Install package on NuGet CLI will not modify the reference of project, so we could not automate the operation install package to update the reference of the project.
Besides, we also do not recommend you automate that. Once you have automate that, NuGet execute the uninstall/install operations every time before you build the project. We only need to do an uninstall/install the operation after get the project from TFS. Even we do not need to do this operation if the references of the project are not broken after NuGet restore. So according to the reference of the project to determine whether or not to use an command:
Update-Package -reinstall
in the Package Manager Console should be the best choice.

How to update nuget packages without package manager of visual studio?

How to update nuget packages without package manager of visual studio, if the projects referenced in solution have different nuget package folders, then updating nuget packages from solution becomes tedious job. You need to update nuget packages per project. Is there a way to update nuget packages without using visual studio?
To update the nuget packages in solution, you can create a batch file with multiple commands or can execute command like mentioned below using command prompt:
Open command prompt and change the folder where Nuget.Exe is present.
NuGet.Exe Update [Solution File] -Id [Nuget package Id]
There are more switches to Nuget.Exe Update switch, like you can specify the nuget source. Make changes as required and you can successfully update nuget packages without opening the visual studio.

Resources