1
+ function Get-FoldersToRemove {
2
+ [CmdletBinding ()]
3
+ param (
4
+ [Parameter (Mandatory = $true , Position = 0 )]
5
+ [string ]$StartPath
6
+ )
7
+
8
+ begin {
9
+ Write-Verbose " Cmdlet Get-FoldersToRemove - Begin"
10
+ $foldersToRemove = @ ()
11
+ }
12
+
13
+ process {
14
+ Write-Verbose " Cmdlet Get-FoldersToRemove - Process"
15
+ Get-ChildItem - Path $StartPath - Recurse - ErrorAction SilentlyContinue | ? { $_.Extension -eq " .csproj" } | % {
16
+ Write-Verbose " [CSPROJ]: $ ( $_.FullName ) "
17
+
18
+ $binPath = " $ ( $_.Directory ) /bin"
19
+ if (Test-Path $binPath ) {
20
+ Write-Verbose " `t [BIN]:`t`t $binPath "
21
+ $binFolder = Get-Item - Path $binPath
22
+ $foldersToRemove += $binFolder
23
+ }
24
+
25
+ $objPath = " $ ( $_.Directory ) /obj"
26
+ if (Test-Path $objPath ) {
27
+ Write-Verbose " `t [OBJ]:`t`t $objPath "
28
+ $objFolder = Get-Item - Path $objPath
29
+ $foldersToRemove += $objFolder
30
+ }
31
+
32
+ [xml ]$csproj = Get-Content - Path $_.FullName
33
+ if ($csproj.Project.ItemGroup.None.Include | ? { $_ -eq " packages.config" }) {
34
+ $prjcRelativePath = $csproj.Project.ItemGroup.Reference.HintPath | ? { $_ -ne $null -and $_.Length -gt 0 } | ? { $_.Contains (" \packages\" ) } | Select-Object - First 1
35
+ if ($prjcRelativePath -ne $null ) {
36
+
37
+
38
+ $path = [System.IO.Path ]::GetFullPath((Join-Path $_.DirectoryName $prjcRelativePath ))
39
+ if (Test-Path $path ) {
40
+ $dllFile = Get-Item - Path $path
41
+ $packagesFolder = $dllFile
42
+
43
+ while ($packagesFolder.Name -ne " packages" -and $packagesFolder.Parent -ne $packagesFolder ) {
44
+ $packagesFolder = Get-Item $packagesFolder.PSParentPath
45
+ }
46
+
47
+ if ($foldersToRemove.Length -eq 0 -or ($foldersToRemove.FullName.Contains ($packagesFolder.FullName ) -eq $false )) {
48
+ Write-Verbose " `t [PACKAGES]:`t $packagesFolder "
49
+ $foldersToRemove += $packagesFolder
50
+ }
51
+ }
52
+ }
53
+ }
54
+ }
55
+ $foldersToRemove
56
+ }
57
+
58
+ end {
59
+ Write-Verbose " Cmdlet Get-FoldersToRemove - End"
60
+ }
61
+ }
62
+
63
+ function Clear-VisualStudioSolutionFolder {
64
+ <#
65
+ . SYNOPSIS
66
+ Solution folder cleanup
67
+
68
+ . DESCRIPTION
69
+ Removes bin, obj folders for all projects in a given directory
70
+
71
+ . PARAMETER StartPath
72
+ Start directory
73
+
74
+ . EXAMPLE
75
+ Clear-VisualStudioSolutionFolder -StartPath "C:\repo\XA\src\"
76
+ Runs solution cleanup for all projects under "C:\repo\XA\src\"
77
+
78
+ #>
79
+ [CmdletBinding ()]
80
+ param (
81
+ [Parameter (Mandatory = $true , Position = 0 )]
82
+ [string ]$StartPath ,
83
+ [Parameter (Mandatory = $false , Position = 1 )]
84
+ [switch ]$Confirm
85
+ )
86
+
87
+ begin {
88
+ Write-Verbose " Cmdlet Clear-VisualStudioSolutionFolder - Begin"
89
+ }
90
+
91
+ process {
92
+ Write-Verbose " Cmdlet Clear-VisualStudioSolutionFolder - Process"
93
+ Get-FoldersToRemove $StartPath | % {
94
+ if ($Confirm ) {
95
+ Remove-Item - Path $_.FullName - Recurse - Confirm
96
+ }
97
+ else {
98
+ Remove-Item - Path $_.FullName - Recurse
99
+ }
100
+ }
101
+ }
102
+
103
+ end {
104
+ Write-Verbose " Cmdlet Clear-VisualStudioSolutionFolder - End"
105
+ }
106
+ }
0 commit comments