-
Notifications
You must be signed in to change notification settings - Fork 313
Expand file tree
/
Copy pathformat-code.ps1
More file actions
51 lines (43 loc) · 2.26 KB
/
format-code.ps1
File metadata and controls
51 lines (43 loc) · 2.26 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
# Format all C++ and C# code in the WindowsML directory
param([switch]$WhatIf, [switch]$Verbose)
$WindowsMLDir = $PSScriptRoot
Write-Host "Formatting code in WindowsML directory..." -ForegroundColor Green
# Test tools availability
function Test-Tool($name, $cmd) {
try { Invoke-Expression $cmd | Out-Null; return $true }
catch { Write-Host "$name not available" -ForegroundColor Red; return $false }
}
if (-not ((Test-Tool "clang-format" "clang-format --version") -and (Test-Tool "dotnet format" "dotnet format --version"))) {
exit 1
}
# Format C++ files
Write-Host "Formatting C++ files..." -ForegroundColor Yellow
$cppFiles = Get-ChildItem -Path $WindowsMLDir -Recurse -Include "*.cpp", "*.h", "*.hpp", "*.c" |
Where-Object { $_.FullName -notlike "*\packages\*" -and $_.FullName -notlike "*\.vs\*" -and $_.FullName -notlike "*\x64\*" -and $_.FullName -notlike "*\Debug\*" -and $_.FullName -notlike "*\Release\*" -and $_.FullName -notlike "*Generated Files*" }
Write-Host "Found $($cppFiles.Count) C++ files" -ForegroundColor Cyan
foreach ($file in $cppFiles) {
$rel = $file.FullName.Substring($WindowsMLDir.Length + 1)
if ($WhatIf) {
Write-Host " Would format: $rel" -ForegroundColor Gray
} else {
if ($Verbose) { Write-Host " Formatting: $rel" -ForegroundColor Gray }
clang-format -i "$($file.FullName)"
}
}
# Format C# files
Write-Host "Formatting C# files..." -ForegroundColor Yellow
$csProjects = Get-ChildItem -Path $WindowsMLDir -Recurse -Include "*.csproj", "*.sln" |
Where-Object { $_.FullName -notlike "*\packages\*" -and $_.FullName -notlike "*\.vs\*" -and $_.FullName -notlike "*\x64\*" -and $_.FullName -notlike "*\Debug\*" -and $_.FullName -notlike "*\Release\*" }
Write-Host "Found $($csProjects.Count) C# projects" -ForegroundColor Cyan
foreach ($project in $csProjects) {
$rel = $project.FullName.Substring($WindowsMLDir.Length + 1)
if ($WhatIf) {
Write-Host " Would format: $rel" -ForegroundColor Gray
} else {
if ($Verbose) { Write-Host " Formatting: $rel" -ForegroundColor Gray }
Push-Location (Split-Path $project.FullName)
dotnet format "$($project.Name)" --verbosity quiet
Pop-Location
}
}
Write-Host "Formatting complete!" -ForegroundColor Green