forked from x360ce/x360ce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCleanup_Solution.ps1
234 lines (233 loc) · 8.33 KB
/
Cleanup_Solution.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
<#
.SYNOPSIS
Removes temporary bin and obj folders.
Kill and clear IIS Express configuration.
Removes temporary and user specific solution files.
.NOTES
Author: Evaldas Jocys <[email protected]>
Modified: 2021-10-20
.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"))
{
$commandPath = "& '" + $MyInvocation.MyCommand.Path + "'";
Start-Process PowerShell -Verb runAs -ArgumentList $commandPath;
break;
}
# ----------------------------------------------------------------------------
# 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.
[Console]::WriteLine("Script Path: {0}", $scriptPath);
[Environment]::CurrentDirectory = $scriptPath;
Set-Location $scriptPath;
# ----------------------------------------------------------------------------
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)
{
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";
}
# ----------------------------------------------------------------------------
function ClearCache
{
Write-Host "Clear IIS Express configuration and remove temp files";
KillProcess "iisexpress.exe";
KillProcess "iisexpresstray.exe";
RemoveSubFoldersAndFiles "$($env:USERPROFILE)\Documents\My Web Sites" $true;
Write-Host "Remove temp directories";
RemoveDirectories ".vs";
Write-Host "Remove temp files";
RemoveFiles "*.dbmdl";
RemoveFiles "*.user";
RemoveFiles "*.suo";
}
# ----------------------------------------------------------------------------
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";
RemoveSubFoldersAndFiles "$($env:USERPROFILE)\AppData\Local\Microsoft\VisualStudio\12.*\ProjectAssemblies";
RemoveSubFoldersAndFiles "$($env:USERPROFILE)\AppData\Local\Microsoft\VisualStudio\13.*\ProjectAssemblies";
RemoveSubFoldersAndFiles "$($env:USERPROFILE)\AppData\Local\Microsoft\VisualStudio\14.*\ProjectAssemblies";
RemoveSubFoldersAndFiles "$($env:USERPROFILE)\AppData\Local\Microsoft\VisualStudio\15.*\ProjectAssemblies";
RemoveSubFoldersAndFiles "$($env:USERPROFILE)\AppData\Local\Microsoft\VisualStudio\16.*\ProjectAssemblies";
Write-Host "Clear IIS Express Cache";
RemoveSubFoldersAndFiles "$($env:LOCALAPPDATA)\Temp\iisexpress";
RemoveSubFoldersAndFiles "$($env:LOCALAPPDATA)\Temp\Temporary ASP.NET Files";
Write-Host "Clear Xamarin Cache";
RemoveSubFoldersAndFiles "$($env:LOCALAPPDATA)\Temp\Xamarin";
RemoveSubFoldersAndFiles "$($env:LOCALAPPDATA)\Xamarin\iOS\Provisioning";
Write-Host "Clear .NET Framework Cache";
RemoveSubFoldersAndFiles "$($env:SystemRoot)\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files";
RemoveSubFoldersAndFiles "$($env:SystemRoot)\Microsoft.NET\Framework64\v4.0.30319\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 {
# Clear screen.
Clear-Host;
Write-Host;
Write-Host " 1 - Clear 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;
$m = Read-Host -Prompt "Type option and press ENTER to continue";
Write-Host;
# Options:
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 option was choosen.
IF ("$m" -ne "") {
pause;
}
} until ("$m" -eq "");
return $m;
}
# ----------------------------------------------------------------------------
# Execute.
# ----------------------------------------------------------------------------
ShowMainMenu;