神刀安全网

Disable NuGet Package Restore for a .Net Poject

If you have decided on Checking in Package Dependencies into Source Control for an existing project that uses Nuget Packages then this post is for you

When using NuGet package references that are not included in the source control, these packages gets restored during build time. There are multiple ways that NuGet supports restore these dependencies at build time

  • Automatic Package Restore is the current recommended approach (within Visual Studio), which is available from NuGet 2.7.
  • Command-line package restore on build servers
  • MSBuild-integrated package restore approach is the original Package Restore implementation and is still used in many projects.

Depending on the type to of restore the project uses, NuGet has different configuration entries in the csproj files and .nuget folder in the solution root. So when choosing to check in package dependencies into the source control, it is a good idea to remove all these generated configurations and files that are not required any more. The below script does this for you!

WARNING!The script deletes the .nuget folder (if it exists), updates the .csproj files. Please make sure that the project folder is under source control or you have a backup of the folder. After running the script make sure that all the changes that you see are expected as explained here and the project builds and runs as before.

The PowerShell script does the below for a given solution directory folder (mandatory)

  • For each of the csproj file in the given folder, the script removes the
    • RestorePackages node
    • NugetPackageImportStamp node
    • nuget target import from the solution root .nuget folder
    • EnsureNuGetPackageBuildImports node
  • Removes .nuget folder from the solution root if it exists.

The script leaves blank lines in the csproj files in place of the removed nodes.

Remove NuGet Restore link
param([Parameter(Mandatory=$true)][string]$solutionDirectory)   $importNugetTargetsTag= [regex]::escape(@' <Import Project="$(SolutionDir)/.nuget/NuGet.targets" Condition="Exists('$(SolutionDir)/.nuget/NuGet.targets')" /> '@)  $restorePackagesTag = '<RestorePackages>.*?</RestorePackages>' $nuGetPackageImportStamp = '<NuGetPackageImportStamp>.*?</NuGetPackageImportStamp>'  $EnsureNuGetPackageBuildImportsTargetTag = '(?smi)<Target Name="EnsureNuGetPackageBuildImports".*?</Target>'  foreach ($f in Get-ChildItem -Recurse -Path $solutionDirectory -Filter *.csproj | sort-object) {     $text = Get-Content $f.FullName -Raw     $text `         -replace $importNugetTargetsTag, "" `         -replace $nuGetPackageImportStamp, "" `         -replace $restorePackagesTag, "" `         -replace $EnsureNuGetPackageBuildImportsTargetTag, "" `         | set-content $f.FullName }  Get-ChildItem -Path $solutionDirectory -include .nuget -Recurse | foreach ($_) { remove-item $_.fullname -Force -Recurse } 

Any similarity with the scripts here is intended as that was my starting place. To explicitly opt out of the Automatic Package Restore on Visual Studio add a Nuget.config in the solution root.

<?xml version="1.0" encoding="utf-8"?> <configuration>   <packageRestore>     <!-- Opts out of both Automatic Package Restore and MSBuild-Integrated Package Restore -->     <add key="enabled" value="False" />      <!-- Opts out of Automatic Package Restore in Visual Studio -->     <add key="automatic" value="False" />   </packageRestore> </configuration> 

Hope this helps you to move away from NuGet restore at build time.

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » Disable NuGet Package Restore for a .Net Poject

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址