From 3cd3cf98303dae8ad43004027a6d5651bb5c9a1a Mon Sep 17 00:00:00 2001 From: yuyoyuppe Date: Tue, 19 Nov 2019 18:33:33 +0300 Subject: [PATCH] Implement powershell script which enables devs to clang-format modified source files --- format_sources.ps1 | 50 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 format_sources.ps1 diff --git a/format_sources.ps1 b/format_sources.ps1 new file mode 100644 index 0000000000..f0ffa30eb8 --- /dev/null +++ b/format_sources.ps1 @@ -0,0 +1,50 @@ +param ( + [switch]$all = $false +) + +if(!(Get-Command "git" -ErrorAction SilentlyContinue)) { + throw "You need to have a git in path to be able to format only the dirty files!" +} + +$clangFormat = "clang-format.exe" +if(!(Get-Command $clangFormat -ErrorAction SilentlyContinue)) { + Write-Information "Can't find clang-format.exe in %PATH%, trying to use %VCINSTALLDIR%..." + $clangFormat="$env:VCINSTALLDIR\Tools\Llvm\bin\clang-format.exe" + if(!(Test-Path -Path $clangFormat -PathType leaf)) { + throw "Can't find clang-format.exe executable. Make sure you either have it in %PATH% or run this script from vcvars.bat!" + } +} + +$sourceExtensions = New-Object System.Collections.Generic.HashSet[string] +$sourceExtensions.Add(".cpp") | Out-Null +$sourceExtensions.Add(".h") | Out-Null + +function Get-Dirty-Files-From-Git() { + $staged = & git diff --name-only --diff-filter=d --cached + $unstaged = & git ls-files -m + $untracked = & git ls-files --others --exclude-standard + $result = New-Object System.Collections.Generic.List[string] + $staged, $unstaged, $untracked | % { + $_.Split(" ") | + where {$sourceExtensions.Contains((Get-Item $_).Extension)} | + foreach {$result.Add($_)} + } + return $result +} + +if($all) { + $filesToFormat = + Get-ChildItem -Recurse -File src | + Resolve-Path -Relative | + where {$sourceExtensions.Contains((Get-Item $_).Extension)} +} +else { + $filesToFormat = Get-Dirty-Files-From-Git +} + +$filesToFormat | % { + Write-Host "Formatting $_" + & $clangFormat -i -style=file -fallback-style=none $_ 2>&1 +} + +Write-Host "Done!" \ No newline at end of file