forked from OpenSalamander/salamander
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnormalize.ps1
161 lines (146 loc) · 5.97 KB
/
normalize.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
#requires -version 7.2
$stopWatch = [Diagnostics.Stopwatch]::StartNew()
$ErrorActionPreference = 'Stop'
Set-Location -Path $PSScriptRoot
[Console]::ResetColor()
# set to $false to search for all files regardless of Git status (SLOW)
$gitModifiedOrStagedOnly = $false
$throttleLimit = 12
$clangFormat = 'C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\Llvm\bin\clang-format.exe'
Write-Host -ForegroundColor White 'Normalize Salamander Source Code'
$clangIncludes = @('*.cpp', '*.h', '*.c')
# from $clangExcludes we moved to .clang-format with DisableFormat: true
$clangExcludes = @()
$utfIncludes = @('*.c', '*.cc', '*.cxx', '*.cpp', '*.c++', '*.cs', '*.hpp', '*.h', '*.h++', '*.hh', '*.tlh',
'*.tli', '*.inl', '*.pas', '*.dpr', '*.dproj', '*.rh', '*.rc', '*.rc2', '*.htm', '*.sln', '*.csproj',
'*.vbproj', '*.vcxproj', '*.vcproj', '*.dbproj', '*.fsproj', '*.lsproj', '*.wixproj', '*.modelproj',
'*.sqlproj', '*.wmaproj', '*.xproj', '*.props', '*.filters', '*.vcxitems', '*.user', '*.config', '*.ps1')
$utfExcludes = @(
'.\src\common\dep\*',
'.\src\sfx7zip\7zip\*',
'.\src\sfx7zip\branch\*',
'.\src\sfx7zip\lzma\*',
'.\src\plugins\7zip\7za\c\*',
'.\src\plugins\7zip\7za\cpp\*',
'.\src\plugins\automation\generated\*',
'.\src\plugins\checksum\tomcrypt\*',
'.\src\plugins\ftp\openssl\*',
'.\src\plugins\ieviewer\cmark-gfm\*',
'.\src\plugins\mmviewer\ogg\vorbis\*',
'.\src\plugins\mmviewer\wma\wmsdk\*',
'.\src\plugins\pictview\exif\libexif\*',
'.\src\plugins\pictview\exif\libjpeg\*',
'.\src\plugins\pictview\twain\*',
'.\src\plugins\portables\wtl\*',
'.\src\plugins\unchm\chmlib\*',
'.\src\plugins\winscp\core\*',
'.\src\plugins\winscp\forms\*',
'.\src\plugins\winscp\packages\*',
'.\src\plugins\winscp\putty\*',
'.\src\plugins\winscp\resource\*',
'.\src\plugins\winscp\windows\*')
function ArrayContainsItem {
param ([string[]]$array, [string]$item)
foreach ($i in $array) {
if ($item -ilike $i) {
return $true
}
}
return $false
}
function EnumFiles
{
param ([string[]]$includes, [string[]]$excludes)
if ( $gitModifiedOrStagedOnly ) {
# store git output (could be zero or single item) to arrays @()
$gitModified = @(git diff --name-only --diff-filter=ACMRT)
$gitStaged = @(git diff --name-only --cached)
$gitItems = $gitModified + $gitStaged
$items = $gitItems | %{
$file = -join('.\\', $_ -replace '/', '\\');
if (-Not (ArrayContainsItem -array $excludes -item $file)) {
if (ArrayContainsItem -array $includes -item $file) {
$file
}
}
}
$items
} else {
$items = Get-ChildItem -Recurse -Include $includes | Resolve-Path -Relative | %{
$file = $_
if (-Not (ArrayContainsItem -array $excludes -item $file)) {
$file
}
}
$items
}
}
$errorsQueue = [System.Collections.Concurrent.ConcurrentQueue[string]]::new()
if ($gitModifiedOrStagedOnly) {
Write-Host -ForegroundColor Gray 'Only Git modified or staged files are processed (FAST PATH)'
} else {
Write-Host -ForegroundColor Gray 'All files are processed regadless of Git status (SLOW PATH)'
}
Write-Host -ForegroundColor Yellow 'Formating with clang-format...'
$todo = EnumFiles -includes $clangIncludes -excludes $clangExcludes
Write-Host -ForegroundColor Gray 'Files count:' $todo.Count
$todo | ForEach-Object -ThrottleLimit $throttleLimit -Parallel {
$errors = $using:errorsQueue
$file = $_
$pinfo = New-Object System.Diagnostics.ProcessStartInfo
$pinfo.WorkingDirectory = $PWD
$pinfo.FileName = $using:clangFormat
$pinfo.RedirectStandardError = $true
$pinfo.RedirectStandardOutput = $true
$pinfo.UseShellExecute = $false
$pinfo.Arguments = "--verbose -i $file"
$p = New-Object System.Diagnostics.Process
$p.StartInfo = $pinfo
$p.Start() | Out-Null
$stdout = $p.StandardOutput.ReadToEnd()
$stderr = $p.StandardError.ReadToEnd()
if ($p.ExitCode -eq 0) {
#$mutex = New-Object System.Threading.Mutex($false, 'ConsoleMutex')
#[void]$mutex.WaitOne()
#Write-Host -NoNewline -ForegroundColor White "$stderr"
#[void]$mutex.ReleaseMutex()
} else {
$mutex = New-Object System.Threading.Mutex($false, 'ConsoleMutex')
[void]$mutex.WaitOne()
Write-Host -NoNewline -ForegroundColor Red "$stderr"
[void]$mutex.ReleaseMutex()
[void]$errors.TryAdd($file+'`r`n'+$stderr)
}
}
Write-Host -ForegroundColor Yellow 'Checking utf-8-bom files...'
$todo = EnumFiles -includes $utfIncludes -excludes $utfExcludes
Write-Host -ForegroundColor Gray 'Files count:' $todo.Count
$todo | ForEach-Object -ThrottleLimit $throttleLimit -Parallel {
$errors = $using:errorsQueue
$file = $_
$fullname = $(Join-Path -Path $using:PSScriptRoot -ChildPath $file)
$contents = new-object byte[] 3
$stream = [System.IO.File]::OpenRead($fullname)
$stream.Read($contents, 0, 3) | Out-Null
$streamLength = $stream.Length
$stream.Close()
if ($streamLength -lt 3) {
Write-Host -ForegroundColor Red "Small file: $file"
[void]$errors.TryAdd($file)
}
if ($streamLength -ge 3 -and ($contents[0] -ne 0xEF -or $contents[1] -ne 0xBB -or $contents[2] -ne 0xBF)) {
(Get-Content -path "$fullname" -Encoding utf8) | Set-Content -Encoding utf8BOM -Path "$fullname"
$mutex = New-Object System.Threading.Mutex($false, 'ConsoleMutex')
[void]$mutex.WaitOne()
Write-Host -ForegroundColor White "Converted to utf-8-bom: $file"
[void]$mutex.ReleaseMutex()
#[void]$errors.TryAdd($file)
}
}
if ($errorsQueue.Count -gt 0) {
Write-Host -ForegroundColor Red 'ERRORS FOUND ^^^^^^^^^^^^^^^^^^^^'
} else {
Write-Host -ForegroundColor Green '✓ SUCCESS'
}
$stopWatch.Stop()
Write-Host -ForegroundColor White "Execution time: $($stopWatch.Elapsed)"