From 0a2d89924bf54b29921f04ec5357166bbc74be9a Mon Sep 17 00:00:00 2001 From: briaguya <70942617+briaguya-ai@users.noreply.github.com> Date: Wed, 22 May 2024 09:46:12 -0400 Subject: [PATCH] clang format (#447) * add linux clang format script * first shot at powershell * whoops * webrequest is slow, try webclient * just get the one file * simplify and maybe make it work? * whoops again * try a different suggestion * why is it always windows * again * try again * bit of path stuff * chmod +x * add action to check * fix workflow name --- .github/workflows/clang-format.yml | 20 +++++++++++++ run-clang-format.ps1 | 47 ++++++++++++++++++++++++++++++ run-clang-format.sh | 29 ++++++++++++++++++ 3 files changed, 96 insertions(+) create mode 100644 .github/workflows/clang-format.yml create mode 100644 run-clang-format.ps1 create mode 100755 run-clang-format.sh diff --git a/.github/workflows/clang-format.yml b/.github/workflows/clang-format.yml new file mode 100644 index 000000000..443d6669d --- /dev/null +++ b/.github/workflows/clang-format.yml @@ -0,0 +1,20 @@ +name: clang-format +on: [pull_request] +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true +jobs: + clang-format: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 2 + - name: Install clang-format + run: | + sudo apt-get update + sudo apt-get install -y clang-format-14 + - name: Run clang-format + run: | + ./run-clang-format.sh + git diff --exit-code diff --git a/run-clang-format.ps1 b/run-clang-format.ps1 new file mode 100644 index 000000000..38d4d1422 --- /dev/null +++ b/run-clang-format.ps1 @@ -0,0 +1,47 @@ +$url = "https://github.com/llvm/llvm-project/releases/download/llvmorg-14.0.6/LLVM-14.0.6-win64.exe" +$llvmInstallerPath = ".\LLVM-14.0.6-win64.exe" +$clangFormatFilePath = ".\clang-format.exe" +$requiredVersion = "clang-format version 14.0.6" +$currentVersion = "" + +function Test-7ZipInstalled { + $sevenZipPath = "C:\Program Files\7-Zip\7z.exe" + return Test-Path $sevenZipPath -PathType Leaf +} + +if (Test-Path $clangFormatFilePath) { + $currentVersion = & $clangFormatFilePath --version + if (-not ($currentVersion -eq $requiredVersion)) { + # Delete the existing file if the version is incorrect + Remove-Item $clangFormatFilePath -Force + } +} + +if (-not (Test-Path $clangFormatFilePath) -or ($currentVersion -ne $requiredVersion)) { + if (-not (Test-7ZipInstalled)) { + Write-Host "7-Zip is not installed. Please install 7-Zip and run the script again." + exit + } + + $wc = New-Object net.webclient + $wc.Downloadfile($url, $llvmInstallerPath) + + $sevenZipPath = "C:\Program Files\7-Zip\7z.exe" + $specificFileInArchive = "bin\clang-format.exe" + & "$sevenZipPath" e $llvmInstallerPath $specificFileInArchive + + Remove-Item $llvmInstallerPath -Force +} + +$basePath = (Resolve-Path .).Path +$files = Get-ChildItem -Path $basePath\mm -Recurse -File ` + | Where-Object { ($_.Extension -eq '.c' -or $_.Extension -eq '.cpp' -or ` + ($_.Extension -eq '.h' -and ` + (-not ($_.FullName -like "*\mm\src\*" -or $_.FullName -like "*\mm\include\*")))) -and ` + (-not ($_.FullName -like "*\mm\assets\*")) } + +foreach ($file in $files) { + $relativePath = $file.FullName.Substring($basePath.Length + 1) + Write-Host "Formatting $relativePath" + .\clang-format.exe -i $file.FullName +} diff --git a/run-clang-format.sh b/run-clang-format.sh new file mode 100755 index 000000000..7f37ec3f4 --- /dev/null +++ b/run-clang-format.sh @@ -0,0 +1,29 @@ +# this line does quite a bit, so let's break it down +# +# find mm +# use "find" to look in the "mm" directory +# this ensures we don't try to format stuff in the submodules +# +# -type f +# only look for files +# +# -name "*.c" -o -name "*.cpp" +# find all .c and .cpp files +# +# -name "*.h" ! -path "mm/src/**.h" ! -path "mm/include/**.h" +# find all .h files that aren't in mm/src or mm/include +# this is because zret decomp only runs clang-format on c files +# https://github.com/zeldaret/mm/blob/b7e5468ca16315a7e322055eff3d97fe980bbc25/format.py#L182 +# +# ! -path "mm/assets/*" +# asset headers are autogenerated, don't fight them +# +# | sed 's| |\\ |g' +# pipe the result of find into sed to +# ensure all the paths returned by find have spaces escaped +# +# | xargs clang-format-14 -i +# use xargs to take each path we've found +# and pass it as an argument to clang-format + +find mm -type f \( -name "*.c" -o -name "*.cpp" -o \( -name "*.h" ! -path "mm/src/**.h" ! -path "mm/include/**.h" \) \) ! -path "mm/assets/*" | sed 's| |\\ |g' | xargs clang-format-14 -i