-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClean-Build-Files-Enh.ps1
247 lines (199 loc) · 8.94 KB
/
Clean-Build-Files-Enh.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#
# NOTE: Might need to open an adminstrative ps prompt to clean files if VS solution was opened as ADMIN.
#
# VERSION: 20170221
#
# DEPENDENCY: When exclude filter is used, this version of the script uses Get-Child-Enhanced (might improve performance, but will it differ from my match filter in orig script?) which can be found at: https://www.simple-talk.com/dotnet/net-framework/practical-powershell-pruning-file-trees-and-extending-cmdlets/
#
# TODO:
# + IMPORTANT: Address issue as mentioned at this article! https://www.simple-talk.com/dotnet/net-framework/practical-powershell-pruning-file-trees-and-extending-cmdlets/
# + When no args passed, prompt user for options. We still get quick launch when launching with args.
# + Allow to customize folders via command arg as well as file.
# + Still some strange errors for some folders about "length" cannot be found when using Measure-object, even after I test
# for it. Not sure how the error is possible. Doesn't seem to affect results, so will look later.
. .\GetChildItemExtension.ps1
# PowerShell script that recursively deletes all 'bin' and 'obj' (or any other specified) folders inside current folder
function Clean-Build-Files($Values = $args)
{
<#
.SYNOPSIS
This function cleans build files recusively starting from target path specified by user. To run without any
arguments, just place this script and the exclude list in the target directory you want to clean and run it.
.OUTPUTS
N/A
.NOTES
This should be made into a cmdlet...?
.EXAMPLE
powershell.exe D:\BTSync\!Projects\Common\Powershell\CleanBuildFiles\Clean-Build-Files.ps1 '-excludelistpath=D:\BTSync\!Projects\Common\Powershell\CleanBuildFiles\exclude-list.txt' '-targetfolder=D:\OssDevRoot\CleanBuildFiles_Testing'
powershell.exe D:\BTSync\!Projects\Common\Powershell\CleanBuildFiles\Clean-Build-Files.ps1 '-excludelistpath=D:\BTSync\!Projects\Common\Powershell\CleanBuildFiles\exclude-list.txt' '-targetfolder=D:\OssDevRoot\hybridview\HomeGenie' -viewonly
#>
# Defaults in case user passes no args.
$excludeListFilePath = "exclude-list.txt"
$includeListFilePath = "include-list.txt"
$targetFolder = (Get-Location -PSProvider FileSystem).ProviderPath
$viewOnly=1
#$includeFolderNameList = "bin,obj,node_modules"
foreach($value in $Values){
Write-Host $value
$arrTmp = $value.Split("=")
switch ($arrTmp[0].ToLower()) {
-excludelistpath {
$excludeListFilePath = $arrTmp[1]
}
-targetfolder {
$targetFolder = $arrTmp[1]
}
-viewonly {
$viewOnly=1
}
-includelistpath {
$includeListFilePath = $arrTmp[1]
}
-help {
Clean-Build-Files-Show-Help
exit
}
}
}
if ($viewOnly -eq 1) {
Write-Host ' ! Running in VIEW ONLY mode. No files will be deleted!' -foregroundcolor yellow
}
if (Test-Path $includeListFilePath)
{
$IncludeList = Get-Content -Path $includeListFilePath | Select-Object
} else {
$IncludeList = @("bin","obj")
Write-Host "No include list file located. Using defaults."
}
if (Test-Path $excludeListFilePath)
{
$ExcludeList = Get-Content -Path $excludeListFilePath | Select-Object
} else {
$ExcludeList = @("browserify")
Write-Host "No exclude list file located. Using defaults."
}
#
# Display configuration options.
#
$CurrentPath = $targetFolder
Write-Host
Write-Host 'Inclusion List ('$IncludeList.Count' found):' -foregroundcolor gray
foreach($include in $IncludeList){
Write-Host ' ' $include -foregroundcolor green
}
Write-Host 'Exclusion List ('$ExcludeList.Count' found):' -foregroundcolor gray
foreach($exclude in $ExcludeList){
Write-Host ' ' $exclude -foregroundcolor gray
}
Write-Host
Write-Host
Write-Host 'Removing files...' -foregroundcolor white
#$_ -notmatch '_tools' -and $_ -notmatch '_build'
# recursively get all folders matching given includes, except ignored folders
$ObjFoldersToRemove = Get-EnhancedChildItem $CurrentPath -include $IncludeList -Recurse -ExcludeTree $ExcludeList
if ($ExcludeList.Count -gt 0)
{
$ObjFoldersToRemove = Get-EnhancedChildItem $CurrentPath -include $IncludeList -Recurse -ExcludeTree $ExcludeList
} else {
$ObjFoldersToRemove = Get-ChildItem $CurrentPath -include "$includeFolderNameList" -Recurse
}
#$ObjFoldersToRemove = Get-ChildItem $CurrentPath -include "$includeFolderNameList" -Recurse | where {$_ -notmatch (
# '(' + [string]::Join(')|(', $ExcludeList) + ')') }
$FoldersToRemove = $ObjFoldersToRemove | foreach {$_.fullname}
# Some script code below based on: https://github.com/doblak/ps-clean/blob/master/DeleteObjBinFolders.ps1
# recursively get all folders matching given includes
$AllFoldersObj = Get-EnhancedChildItem $CurrentPath -include $IncludeList -Recurse
$AllFolders = $AllFoldersObj | foreach {$_.fullname}
# subtract arrays to calculate ignored ones
$IgnoredFolders = $AllFolders | where {$FoldersToRemove -notcontains $_}
$ItemsRemovedCount = 0
$TotalSpaceKbCleared = 0
#$TotalSpaceMbCleared = ($AllFoldersObj | Measure-Object -Sum length)
if($ObjFoldersToRemove -ne $null)
{
Write-Host
foreach ($objitem in $ObjFoldersToRemove)
{
Try
{
$item = $objitem.fullname
$temp = Get-ChildItem $item -Recurse -Force
$sizeBytes = 0
#$hasProp = [bool]($temp.PSobject.Properties.name -match "length")
$hasProp = [bool]($temp.PSobject.Properties.Name -contains "length")
if ($hasProp) #$temp -ne $null)
{
if ($temp.length -ne $null) {
$sizeObj = $temp | Measure-Object -property length -sum
$sizeBytes = $sizeObj.Sum
}
}
$sizeKBytes = [math]::Round($sizeBytes / 1024)
Write-Host "Removing: " $item.replace($CurrentPath, "") -nonewline;
if ($viewOnly -eq 0) {
remove-item $item -Force -Recurse -ea stop; # Instructs PS to generate terminating error if error occurs here. That way, we can use the try catch.
}
Write-Host " [Removed $sizeKBytes KB]" -foregroundcolor green;
#Write-Host $sizeKBytes -foregroundcolor green -nonewline;
#Write-Host " KB]" -foregroundcolor green;
$ItemsRemovedCount++
$TotalSpaceKbCleared += $sizeKBytes
#$directory | Get-ChildItem |
# Measure-Object -Sum Length | Select-Object `
# @{Name=”Path”; Expression={$directory.FullName}},
# @{Name=”Files”; Expression={$_.Count}},
# @{Name=”Size”; Expression={$_.Sum}}
}
Catch [system.exception]
{
write-host 'ERROR:' $_.Exception.Message -foregroundcolor red
}
Finally
{
#"end of action"
}
#Write-Host $item.replace($CurrentPath, "");
}
}
# print ignored folders to output
if($IgnoredFolders -ne $null)
{
Write-Host
foreach ($item in $IgnoredFolders)
{
Write-Host "Ignored: " -foregroundcolor yellow -nonewline;
Write-Host $item.replace($CurrentPath, "");
}
Write-Host
Write-Host $IgnoredFolders.count "folders ignored" -foregroundcolor yellow
}
# print summary of the operation
Write-Host
if($FoldersToRemove -ne $null)
{
Write-Host $ItemsRemovedCount "folders removed ($TotalSpaceKbCleared KB)" -foregroundcolor green
#Write-Host $FoldersToRemove.count-$ItemsRemovedCount "folders removed" -foregroundcolor green
}
else { Write-Host "No folders to remove" -foregroundcolor green }
Write-Host
# prevent closing the window immediately
$dummy = Read-Host "Completed, press enter to continue."
}
function Clean-Build-Files-Show-Help()
{
Write-Host
Write-Host "Clean-Build-Files HELP" -foregroundcolor white
Write-Host "Cleans build files recusively starting from target path specified by user. To run without any arguments, just place this script and the exclude list in the target directory you want to clean and run it." -foregroundcolor white
Write-Host
Write-Host "NOTE: Exclusion list file entries should be seperated by line break (1 on each line)." -foregroundcolor white
Write-Host
Write-Host "EXAMPLE" -foregroundcolor white
Write-Host " powershell.exe Clean-Build-Files.ps1 '-excludelistpath=<PathToExcludeListFile>' '-targetfolder=<FolderToClean>'" -foregroundcolor yellow
Write-Host
}
# Run the function.
Clean-Build-Files $args
# Original ABM code below for old examples.
# This will delete all BIN folders.
# Get-ChildItem .\ -include bin,obj -Recurse | foreach ($_) { remove-item $_.fullname -Force -Recurse }
# get-childitem -rec | where {($_.PSIsContainer -eq $true) -and ($_.name -like "bin*")} | foreach ($_) {remove-item $_.fullname -recurse -force }