Skip to content

Instantly share code, notes, and snippets.

@ycaroafonso
Created September 15, 2022 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ycaroafonso/bd5dc6be09237b746f014637bdebe397 to your computer and use it in GitHub Desktop.
Save ycaroafonso/bd5dc6be09237b746f014637bdebe397 to your computer and use it in GitHub Desktop.
delete unused package directories
# 20220915 Ycaro Afonso
$dirPackages = Join-Path -Path (Get-Location) -ChildPath 'packages'
[String[]]$dirXml = @()
foreach ($arqSln in (Get-ChildItem -Path .\* -Include *.sln)){
$sln = Get-Content $arqSln
[String[]]$dirXml += [regex]::matches($sln, '"([^"]*).csproj"').value.replace('"','')
}
$diretoriosExistentes = Get-ChildItem -Path $dirPackages | Foreach-Object {$_.Name}
[String[]]$packagesUtilizados = @()
foreach ($arqXml in $dirXml)
{
[xml]$xml = Get-Content $arqXml
[array]$service_arr = $xml.Project.ItemGroup.Reference
foreach ($service in $service_arr)
{
if($service.HintPath){
if ($packagesUtilizados -NotContains $service.HintPath.Split("\\")[2]) {
[String[]]$packagesUtilizados += $service.HintPath.Split("\\")[2]
}
}
}
}
$diretoriosExistentes | ForEach-Object {
if ($packagesUtilizados -NotContains $PSItem) {
"Item: [$PSItem]"
Remove-Item (Join-Path -Path $dirPackages -ChildPath $PSItem) -Recurse
}
}
#Remove-Item .\packagesUtilizados.txt
#Remove-Item .\diretoriosExistentes.txt
#$packagesUtilizados | Out-File -Append .\packagesUtilizados.txt
#$diretoriosExistentes | Out-File -Append .\diretoriosExistentes.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment