NuGet restore


NuGet is a package manager like NPM for node.js or Gem for Ruby, and it is a dominant one in .NET.

You can handle dependency packages in two ways. You download them from NuGet server but also store them in your source control. Or, you just remember what packages you use in your project, and restore them on build. Node and Ruby guys always go for the 2nd option. .NET people used to do the first one, but now prefer the 2nd option. I agree to the 2nd option, restoring packages on build, rather than storing them in git repository. Git is for source code, NuGet repository is for packages.

The Visual Studio on my work machine isn't set up for NuGet restore. "Restore" option is disabled, and when I download and build an open-source project, it can't restore the packages and can't build the solution.

I've checked if there's work-around. It seems that the latest NuGet.exe can restore packages in command-line, regardless of your VS settings, which makes sense to me, as NuGet shouldn't depend on the settings of your IDE. So update NuGet.exe to the latest.

[sourcecode language="powershell"]
exec { .\Tools\NuGet\NuGet.exe update -self }
[/sourcecode]

Create an environmental variable, EnableNuGetPackageRestore, to true, but just for Process, not for Machine, not to impact other projects.

[sourcecode language="powershell"]
[Environment]::SetEnvironmentVariable("EnableNuGetPackageRestore", "true", "Process")
[/sourcecode]

Then, you can safely restore your packages in command-line.

[sourcecode language="powershell"]
exec { .\Tools\NuGet\NuGet.exe restore ".\Src\$name.sln" | Out-Default } "Error restoring $name"
[/sourcecode]

So, all the code in one place

[sourcecode language="powershell"]
Write-Host "Restoring"
[Environment]::SetEnvironmentVariable("EnableNuGetPackageRestore", "true", "Process")
exec { .\Tools\NuGet\NuGet.exe update -self }
exec { .\Tools\NuGet\NuGet.exe restore ".\Src\$name.sln" | Out-Default } "Error restoring $name"
[/sourcecode]