-
Notifications
You must be signed in to change notification settings - Fork 0
/
remove_unused.ps1
64 lines (44 loc) · 2.17 KB
/
remove_unused.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
<#
.SYNOPSIS
Find and process files in a project folder that are not included in the project.
.DESCRIPTION
Find and process files in a project folder that are not included in the project.
Options to delete the files.
.PARAMETER Project
The path/name for the project file.
.PARAMETER DeleteFromDisk
Just delete the files from disk. No interaction with any source control.
#>
[CmdletBinding()]
param(
[Parameter(Position=0, Mandatory=$true)]
[string]$Project,
[switch]$DeleteFromDisk
)
$projectPath = Split-Path $project
$projectPath
$excludeRegex = "\\obj\\|\\bin\\|\\logs\\|\.user|\.csproj|App_Configuration\\"
$fullProjectPath = (Resolve-Path $projectPath).Path
$fullProjectPath
$compileFiles = Select-String -Path $project -Pattern '<compile' | % { $_.Line -split '\t' } | `
% {$_ -replace "(<Compile Include=|\s|/>|["">])", ""} | % { "{0}\{1}" -f $projectPath, $_ } | % {(Resolve-Path $_).Path}
$noneFiles = Select-String -Path $project -Pattern '<none' | % { $_.Line -split '\t' } | `
% {$_ -replace "(<None Include=|\s|/>|["">])", ""} | % { "{0}\{1}" -f $projectPath, $_ } | % {(Resolve-Path $_).Path}
$contentFiles = Select-String -Path $project -Pattern '<content' | % { $_.Line -split '\t' } | `
% {$_ -replace "(<Content Include=|\s|/>|["">])", ""} | % { "{0}\{1}" -f $projectPath, $_ } | % {(Resolve-Path $_).Path}
$embedded = Select-String -Path $project -Pattern '<embeddedresource' | % { $_.Line -split '\t' } | `
% {$_ -replace "(<EmbeddedResource Include=|\s|/>|["">])", ""} | % { "{0}\{1}" -f $projectPath, $_ } | % {(Resolve-Path $_).Path}
$projectFiles = ($compileFiles + $noneFiles + $contentFiles + $embedded) | ?{ $_ -like "$fullProjectPath*" }
Write-Host "Project:" $projectFiles.Count
#$projectFiles | %{ $_ }
$diskFiles = gci -Path $path -Recurse | ?{ if($_.Extension) { $_ } else {} } | %{ $_.FullName } | ?{ $_ -notmatch $excludeRegex }
Write-Host "Disk files:" $diskFiles.Count
#$diskFiles | %{ $_ }
$diff = (compare-object $diskFiles $projectFiles -PassThru)
Write-Host "Excluded Files:" $diff.Count
$diff | %{ $_ }
#just remove the files from disk
if($DeleteFileOnly)
{
$diff | % { Remove-Item -Path $_ -Force -Verbose}
}