-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclean.ps1
135 lines (119 loc) · 3.51 KB
/
clean.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
[cmdletBinding()]
param(
[switch]$quiet,
[switch]$force
)
function Remove-EmptyFolders {
<#
.SYNOPSIS
Remove empty folders recursively from a root directory.
The root directory itself is not removed.
Author: Joakim Borger Svendsen, Svendsen Tech, Copyright 2022.
MIT License.
.EXAMPLE
. .\Remove-EmptyFolders.ps1
Remove-EmptyFolders -Path E:\FileShareFolder
.EXAMPLE
Remove-EmptyFolders -Path \\server\share\data
NB. You might have to change $ChildDirectory.FullName to
$ChildDirectory.ProviderPath in the code for this to work.
Untested with UNC paths as of 2022-01-28.
#>
[CmdletBinding()]
Param(
[String] $Path
)
Begin {
[Int32] $Script:Counter = 0
if (++$Counter -eq 1) {
$RootPath = $Path
Write-Verbose -Message "Saved root path as '$RootPath'."
}
# Avoid overflow. Overly cautious? ~2.15 million directories...
if ($Counter -eq [Int32]::MaxValue) {
$Counter = 1
}
}
Process {
# List directories.
foreach ($ChildDirectory in Get-ChildItem -LiteralPath $Path -Force |
Where-Object {$_.PSIsContainer}) {
# Use .ProviderPath on Windows instead of .FullName,
# in order to support UNC paths (untested).
# Process each child directory recursively.
Remove-EmptyFolders -Path $ChildDirectory.FullName
}
$CurrentChildren = Get-ChildItem -LiteralPath $Path -Force
# If it's empty, the condition below evaluates to true. Get-ChildItem
# returns $null for empty folders.
if ($null -eq $CurrentChildren) {
# Do not delete the root folder itself.
if ($Path -ne $RootPath) {
Write-Verbose -Message "Removing empty folder '$Path'."
Remove-Item -LiteralPath $Path -Force
}
}
}
}
Function remove {
param([string]$item)
If (Test-Path $item){
if (-not $quiet.IsPresent) {
Write-Output "Removing $item"
}
Remove-Item $item -Force -Recurse
}
}
Function Invoke-Cleanup {
if (-not $quiet.IsPresent) {
Write-Output "---------------------"
Write-Output "Invoke-Cleanup"
Write-Output "---------------------"
}
# clean package, bin and obj folders
Get-ChildItem .\ -include packages,bin,obj,node_modules,TestResults -Recurse | Where-Object {$_.FullName -NotMatch "BuildScripts"} | %{
Write-Host "Removing $($_.fullname)";
remove-item $_.fullname -Force -Recurse
}
#Find nunit files
Get-ChildItem -include *.nunit -Recurse |
ForEach-Object{
if (-not $quiet.IsPresent) {
Write-Host $_
}
$results = "$($_.BaseName).xml"
If (Test-Path $results){
if (-not $quiet.IsPresent) {
Write-Host "Removing $results"
}
Remove-Item $results
}
}
#delete dist folder in directories that have package.json
gci -Recurse package.json | %{
$dir = "$($_.DirectoryName)\dist";
If (Test-Path $dir ){
Write-Output "Removing $dir";
rm $dir -force -recurse
}
}
remove "TestResults"
remove "OpenCover"
remove "Publish"
remove "TestBin"
remove "output"
remove "coveragereport"
remove ".sonarqube"
}
if ($force.IsPresent) {
# stop extraneous processes
dotnet build-server shutdown
# cleanup all nuget resources
#dotnet nuget locals --clear all
# remove the .vs folder
remove "src/.vs"
}
# remove all bin/obj folders
Invoke-Cleanup
# remove emtpy directories
Remove-EmptyFolders -Path . -Verbose