Created
February 27, 2017 18:03
-
-
Save Vaccano/529d9e466dcf50d42d17a707ab49d09f to your computer and use it in GitHub Desktop.
Clean up nuget artifacts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# DO NOT CALL THIS DIRECTLY! IT SHOULD ONLY BE CALLED BY THE BUILD SYSTEM. | |
# IF YOU CALL THIS DIRECTLY then the projects will have all of their nuget references removed. | |
# | |
# For the template to be able to install the nuget stuff, it needs it to be in a state as if | |
# it has never been installed. This script removes: | |
# • The packages.config files | |
# • Removes <None Include="packages.config" /> from any csproj files | |
# • Compiler references that are added by nuget | |
# • Removes any references that have a hint path that contains '..\packages\ | |
# Find all packages.config files and delete them | |
get-childitem ./ -include packages.config -recurse | foreach ($_) {remove-item $_.fullname -force} | |
# Find all .csproj files | |
$csProjFiles = get-childitem ./ -include *.csproj -recurse | |
# Remove the packages.config include from the csproj files. | |
$csProjFiles | foreach ($_) {$currentFile = $_; (get-content $_) | Where-Object {$_ -notmatch 'Include="packages.config"'} | Set-Content $currentFile -force} | |
# Remove any compiler references added by the compiler nuget packages. | |
$csProjFiles | foreach ($_) {$currentFile = $_; (get-content $_) | Where-Object {$_ -notmatch 'build\\Microsoft.Net.Compilers.props'} | Set-Content $currentFile -force} | |
$csProjFiles | foreach ($_) {$currentFile = $_; (get-content $_) | Where-Object {$_ -notmatch 'build\\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props'} | Set-Content $currentFile -force} | |
# Remove any references that have a hint path that contains '..\packages\' | |
foreach ($csProjFile in $csProjFiles){ | |
[xml] $csProjFileXml = get-content $csProjFile; | |
foreach ($itemGroup in $csProjFileXml.Project.ItemGroup) { | |
foreach ($reference in $itemGroup.Reference) { | |
foreach ($hintPath in $reference.HintPath){ | |
if ($hintPath -like '*..\packages\*'){ | |
$itemGroup.RemoveChild($reference) | |
} | |
} | |
} | |
} | |
$csProjFileXml.Save($csProjFile) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment