forked from JocysCom/FocusLogger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution_Cleanup.ps1
More file actions
295 lines (290 loc) · 11.3 KB
/
Solution_Cleanup.ps1
File metadata and controls
295 lines (290 loc) · 11.3 KB
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<#
.SYNOPSIS
Removes temporary bin and obj folders.
Kill and clear IIS Express configuration.
Removes temporary and user specific solution files.
.NOTES
Author: Evaldas Jocys <evaldas@jocys.com>
Modified: 2023-06-06
.LINK
http://www.jocys.com
#>
using namespace System;
using namespace System.IO;
# ----------------------------------------------------------------------------
# Run as administrator.
If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
# Pass arguments: script path, original user profile path and local app data path.
$argumentList = "& '" + $MyInvocation.MyCommand.Path + "' '$($env:USERNAME)' '$($env:USERPROFILE)' '$($env:LOCALAPPDATA)'";
Start-Process PowerShell.exe -Verb Runas -ArgumentList $argumentList
return;
}
# Add original user profile path and optionally admin user profile path to process.
$userNames = @( $args[0]);
if ($args[0] -ne $env:USERNAME) { $userNames += $env:USERNAME };
# Add original user profile path and optionally admin user profile path to process.
$userProfilePaths = @( $args[1]);
if ($args[1] -ne $env:USERPROFILE) { $userProfilePaths += $env:USERPROFILE };
# Add original user app data path and optionally admin user app data path to process.
$localAppDataPaths = @( $args[2]);
if ($args[2] -ne $env:LOCALAPPDATA) { $localAppDataPaths += $env:LOCALAPPDATA };
# ----------------------------------------------------------------------------
# Get current command path.
[string]$current = $MyInvocation.MyCommand.Path;
# Get calling command path.
[string]$calling = @(Get-PSCallStack)[1].InvocationInfo.MyCommand.Path;
# If executed directly then...
if ($calling -ne "") {
$current = $calling;
}
# ----------------------------------------------------------------------------
[FileInfo]$file = New-Object FileInfo($current);
# Set public parameters.
$global:scriptName = $file.Basename;
$global:scriptPath = $file.Directory.FullName;
# Change current directory.
Write-Host "Script Path: $scriptPath";
[Environment]::CurrentDirectory = $scriptPath;
Set-Location $scriptPath;
# ----------------------------------------------------------------------------
# Shot which profiles will be affected.
foreach ($p in $userProfilePaths) {
Write-host "User Profile: $p";
}
foreach ($d in $localAppDataPaths) {
Write-host "Local App Data: $d";
}
# ----------------------------------------------------------------------------
Function KillProcess {
param($pattern);
# -------------------------
# Function.
$procs = Get-Process;
foreach ($proc in $procs) {
if ($proc.Path) {
$item = Get-Item $proc.Path;
if ($item.Name -eq $pattern) {
Write-Output " Stopping process: $($item.Name)";
Stop-Process $proc;
}
}
}
}
# ----------------------------------------------------------------------------
Function RemoveDirectories {
param ($pattern, $mustBeInProject)
# -------------------------
# Function.
$items = Get-ChildItem $wdir -Filter $pattern -Recurse -Force | Where-Object { $_ -is [DirectoryInfo] };
foreach ($item in $items) {
if ($mustBeInProject) {
# Get parent folder.
[DirectoryInfo] $parent = $item.Parent;
$projects = $parent.GetFiles("*.*proj", [SearchOption]::TopDirectoryOnly);
# If parent folder do not contain *.*proj file then...
if ($projects.length -eq 0) {
# Ignore node_modules.
if ($item.FullName -like "*\node_modules\*") {
continue;
}
Write-Output " Skip: $($item.FullName)";
$global:skipCount += 1;
continue;
}
Write-Output " Remove: $($item.FullName)";
$global:removeCount += 1;
Remove-Item -LiteralPath $item.FullName -Force -Recurse
}
}
}
# ----------------------------------------------------------------------------
function RemoveSubFoldersAndFiles {
param($path, $onlyDirs);
# -------------------------
# Function.
$dirs = Get-Item $path -ErrorAction SilentlyContinue;
foreach ($dir in $dirs) {
Write-Output " $($dir.FullName)";
$items = Get-ChildItem -LiteralPath $dir.FullName -Force;
if ($onlyDirs -eq $true) {
$items = $items | Where-Object { $_ -is [DirectoryInfo] };
}
foreach ($item in $items) {
Write-Output " - $($item.Name)";
Remove-Item -LiteralPath $item.FullName -Force -Recurse;
}
}
}
# ----------------------------------------------------------------------------
Function RemoveFiles {
param($pattern);
# -------------------------
# Function.
$items = Get-ChildItem $wdir -Filter $pattern -Recurse -Force | Where-Object { $_ -is [FileInfo] };
foreach ($item in $items) {
Write-Output $item.FullName;
Remove-Item -LiteralPath $item.FullName -Force;
}
}
# ----------------------------------------------------------------------------
function ClearBuilds {
$global:removeCount = 0;
$global:skipCount = 0;
Write-Host "Clear Build Folders";
# Remove 'obj' folders first, because it can contain 'bin' inside.
RemoveDirectories "obj" $true;
RemoveDirectories "bin" $true;
#Write-Output "Skipped: $global:skipCount, Removed: $global:removeCount";
}
# ----------------------------------------------------------------------------
# Kill tasks which could lock files in the project folders.
function KillDeveloperTasks {
# TaskKill
# /IM <name> Name of the process to be terminated.
# /T Terminates the specified process and any child processes.
# /F Specifies to forcefully terminate the process(es).
#
# Kill Microsoft Build Engine.
& TaskKill.exe @("/F", "/T", "/IM", "MsBuild.exe");
# Kill Microsoft Visual Studio Team Foundation Server End Task.
& TaskKill.exe @("/F", "/T", "/IM", "EndTask.exe");
# Kill IIS Worker.
& TaskKill.exe @("/F", "/T", "/IM", "w3wp.exe");
# Kill Node.js JavaScript runtime environment.
& TaskKill.exe @("/F", "/T", "/IM", "node.exe");
# Kill Web View Host.
& TaskKill.exe @("/F", "/T", "/IM", "WebViewHost.exe");
# Kill ChromeDriver. Ued for UI testing.
& TaskKill.exe @("/F", "/T", "/IM", "ChromeDriver.exe");
# Kill IIS Express.
& TaskKill.exe @("/F", "/T", "/IM", "iisexpress.exe");
# Kill IIS Express Tray Icon.
& TaskKill.exe @("/F", "/T", "/IM", "iisexpresstray.exe");
}
# ----------------------------------------------------------------------------
function ClearCache {
Write-Host "Clear IIS Express configuration and remove temp files";
Start-Sleep -Seconds 2.0;
foreach ($p in $userProfilePaths) {
RemoveSubFoldersAndFiles "$p\Documents\My Web Sites" $true;
}
Write-Host "Remove temp directories";
RemoveDirectories ".vs"; # Visual Studio
RemoveDirectories ".vscode"; # Visual Studio Code
Write-Host "Remove temp files";
RemoveFiles "*.dbmdl";
RemoveFiles "*.user";
RemoveFiles "*.suo";
RemoveFiles "tsconfig.tsbuildinfo";
}
# ----------------------------------------------------------------------------
function ResetPermissions {
param([string]$path);
# -------------------------
# Give read write permissions to local users.
$di = new-Object System.IO.DirectoryInfo($path);
Write-Host "Reset Permissions on $($di.FullName)";
if ($di.Exists -eq $false) {
Write-Host "Folder not found!";
return;
}
# Take ownership.
& takeown.exe @("/F", $path);
# Return ownership to TrustedInstaller.
& icacls.exe @($path, "/setowner", "`"NT Service\TrustedInstaller`"", "/Q");
# Replace ACL with default inherited acls for all matching files.
& icacls.exe @($path, "/reset", "/T", "/C", "/Q");
# Add modify (M) & write (W) permission.
# Inherit: This folder and files (OI), This folder and subfolders (CI).
#& icacls.exe @($path, "/grant", "`"Users`":(OI)(CI)MW");
}
# ----------------------------------------------------------------------------
function ClearCacheVS {
# Fix Visual Studio "Windows Form Designer: Could not load file or assembly" designer error by
# clearing temporary compiled assemblies inside dynamically created folders by Visual Studio.
# Visual studio must be closed for this batch script to succeed.
#
Write-Host "Clear Visual Studio Cache";
foreach ($p in $userProfilePaths) {
for ($i = 12; $i -le 20; $i++) {
$vsPaths = @(
"$p\AppData\Local\Microsoft\VisualStudio\$($i).*\ProjectAssemblies",
"$p\AppData\Local\Microsoft\VisualStudio\$($i).*\ItemTemplatesCache_{00000000-0000-0000-0000-000000000000}",
"$p\AppData\Local\Microsoft\VisualStudio\$($i).*\ProjectTemplatesCache_{00000000-0000-0000-0000-000000000000}"
);
foreach ($vsPath in $vsPaths) {
$paExpanded = Get-Item $vsPath -ErrorAction SilentlyContinue;
if ($paExpanded.Length -ne 0) {
RemoveSubFoldersAndFiles $vsPath;
}
}
}
}
return;
Write-Host "Clear IIS Express Cache";
foreach ($p in $localAppDataPaths) {
RemoveSubFoldersAndFiles "$p\Temp\iisexpress";
RemoveSubFoldersAndFiles "$p\Temp\Temporary ASP.NET Files";
}
Write-Host "Clear Xamarin Cache";
foreach ($p in $localAppDataPaths) {
RemoveSubFoldersAndFiles "$p\Temp\Xamarin";
RemoveSubFoldersAndFiles "$p\Xamarin\iOS\Provisioning";
}
Write-Host "Clear .NET Framework Cache";
$netVersions = @("v2.0.50727", "v4.0.30319");
foreach ($v in $netVersions) {
RemoveSubFoldersAndFiles "$($env:SystemRoot)\Microsoft.NET\Framework\$v\Temporary ASP.NET Files";
RemoveSubFoldersAndFiles "$($env:SystemRoot)\Microsoft.NET\Framework64\$v\Temporary ASP.NET Files";
}
#
# Solution Explorer, right-click Solution
# Properties -> Common Properties -> Debug Source Files -> clean "Do not look for these source files" box.
#
# Tools -> Options -> Projects and Solutions -> Build and Run
# Set "On Run, when build or deployment errors occur:" Prompt to Launch
#
# .EditorConfig file.
# "charset=utf-8" option can trigger "The source file is different from when the module was built." warning when debugging.
}
# ----------------------------------------------------------------------------
function ShowMainMenu {
$m = "";
do {
$namesAffected = [String]::Join(", ", $userNames);
# Clear screen.
Clear-Host;
Write-Host;
Write-Host "User profiles will be affected: $namesAffected";
Write-Host "Please close Visual Studio before starting cleanup.";
Write-Host "The 'Clear' option will kill all developer tasks.";
Write-Host;
Write-Host " 1 - Clear Project builds";
Write-Host " 2 - Clear IIS and temp files";
Write-Host " 3 - Clear Visual Studio cache";
Write-Host;
Write-Host " 0 - Clear all";
Write-Host;
Write-Host " R - Reset Permissions";
Write-Host " K - Kill Developer Tasks";
Write-Host;
$m = Read-Host -Prompt "Type option and press ENTER to continue";
Write-Host;
# Options:
IF ("$m" -eq "0" -or "$m" -eq "1" -or "$m" -eq "2" -or "$m" -eq "3" ) { KillDeveloperTasks; };
IF ("$m" -eq "0" -or "$m" -eq "1") { ClearBuilds; };
IF ("$m" -eq "0" -or "$m" -eq "2") { ClearCache; };
IF ("$m" -eq "0" -or "$m" -eq "3") { ClearCacheVS; };
IF ("$m" -eq "R") { ResetPermissions "$scriptPath"; };
IF ("$m" -eq "K") { KillDeveloperTasks; Start-Sleep -Seconds 2.0; };
# If option was choosen.
IF ("$m" -ne "") {
pause;
}
} until ("$m" -eq "");
return $m;
}
# ----------------------------------------------------------------------------
# Execute.
# ----------------------------------------------------------------------------
ShowMainMenu;