Instantly share code, notes, and snippets.

Embed
What would you like to do?
Disable the NuGet Package Restore functionality in a Visual Studio solution.
# Usage: .\DisableNuGetPackageRestore.ps1 C:\Path\To\Solution.sln
# Get the path that the user specified when calling the script
$solution = $args[0]
$solutionPath = Split-Path $solution -Parent
$solutionName = Split-Path $solution -Leaf
# Delete the .nuget directory and all contents
Remove-Item (Join-Path $solutionPath ".nuget") -Force -Recurse -ErrorAction 0
# Create a backup of the solution file
Copy-Item $solution $("$solution.bak")
# Clear out the solution file
Clear-Content $solution
# Flag field used to denote what part of the solution file we are in
# 0: Not inside section
# 1: Beginning and middle of section
# 2: Last line of the section
$foundNuGetSection = 0
# Create the new contents of the solution file without the NuGet section
(Get-Content $("$solution.bak")) |
Foreach-Object {
# Parsing the solution file layout is no fun...
if ($_ -match "^Project.*nuget.*") {
$foundNuGetSection = 1
} elseif ($_ -match "^EndProject$" -and $foundNuGetSection -eq 1) {
$foundNuGetSection = 2
} elseif ($foundNuGetSection -eq 2) {
$foundNuGetSection = 0
}
if ($foundNuGetSection -eq 0) {
Add-Content -Encoding UTF8 $solution $_
}
}
# Remove the temporary solution backup file
Remove-Item $("$solution.bak")
# Find every .csproj file and remove the reference to the NuGet.targets file
# Example: <Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
Get-ChildItem $solutionPath *.csproj -recurse |
Foreach-Object {
(Get-Content $_.PSPath) | Where-Object {$_ -notmatch 'NuGet.targets'} | Set-Content -Encoding UTF8 $_.PSPath
}
@mkchandler

This comment has been minimized.

Owner

mkchandler commented Feb 7, 2014

You can call this script like this: .\DisableNuGetPackageRestore.ps1 C:\Path\To\Solution.sln

@paulbrown79

This comment has been minimized.

paulbrown79 commented May 19, 2014

Hey

Just used this...very useful!

Thanks Paul

@PilotBob

This comment has been minimized.

PilotBob commented Jun 25, 2014

Nice. What about vbproj files?

@mkchandler

This comment has been minimized.

Owner

mkchandler commented Aug 9, 2014

@PilotBob I don't have any experience with .vbproj files, but I would guess that it is very similar (possibly just changing line 45) to search for that extension.

@ghost

This comment has been minimized.

ghost commented Sep 6, 2014

Thanks for the share. Much appreciated.

@ghost

This comment has been minimized.

ghost commented Sep 6, 2014

I had hoped that this script would do all the steps of the migration guide (http://docs.nuget.org/docs/workflows/migrating-to-automatic-package-restore). It did remove the ".nuget" folder from .sln and disk, and also removed the

<Import Project="$(SolutionDir)\.nuget\nuget.targets" />  

section, but did not remove the:

<RestorePackages>true</RestorePackages>

and

  <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
    <PropertyGroup>
      <ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them.  For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
    </PropertyGroup>
  </Target>

sections for me. Perhaps i skipped/missed something?

@mkchandler

This comment has been minimized.

Owner

mkchandler commented Sep 17, 2014

@automatonic I have not seen those sections before, which is why they are not accounted for in the script. Should be easy to add though.

@PCHEdgar

This comment has been minimized.

PCHEdgar commented May 20, 2016

Hi, thanks for sharing! I found a problem when OctoPack installed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment