| 26 | Check, |
| 27 | }; |
| 28 | static Result formatSourceFiles(FormatSources action, StringView clangFormatExecutable, StringView libraryDirectory) |
| 29 | { |
| 30 | // This would be the equivalent more or less of: |
| 31 | // |
| 32 | // cd "${libraryDirectory}" && \ |
| 33 | // find . \( -iname \*.h -o -iname \*.cpp -o -iname \*.inl \) -not \( -path "*/_Build/*" \) |
| 34 | // | xargs "${clangFormatExecutable}" -i |
| 35 | // or |
| 36 | // | xargs "${clangFormatExecutable}" --dry-run -Werror |
| 37 | |
| 38 | AsyncProcessExit processExits[32]; // Never launch more than 32 processes |
| 39 | ProcessLimiter processLimiter; |
| 40 | SC_TRY(processLimiter.create(Process::getNumberOfProcessors(), {processExits})); |
| 41 | |
| 42 | struct LambdaVariables |
| 43 | { |
| 44 | ProcessLimiter& processLimiter; |
| 45 | FormatSources action; |
| 46 | StringView clangFormatExecutable; |
| 47 | } lambda = {processLimiter, action, clangFormatExecutable}; |
| 48 | |
| 49 | auto formatSourceFile = [&lambda](const StringView path) |
| 50 | { |
| 51 | globalConsole->print("{}\n", path); |
| 52 | switch (lambda.action) |
| 53 | { |
| 54 | case FormatSources::Execute: // Executes actual formatting |
| 55 | return lambda.processLimiter.launch({lambda.clangFormatExecutable, path, "-i"}); |
| 56 | case FormatSources::Check: // Just checks if files are formatted |
| 57 | return lambda.processLimiter.launch({lambda.clangFormatExecutable, path, "-dry-run", "-Werror"}); |
| 58 | } |
| 59 | Assert::unreachable(); |
| 60 | }; |
| 61 | SC_TRY(FileSystemFinder::forEachFile(libraryDirectory, {".h", ".cpp", ".inl"}, {"_Build"}, formatSourceFile)); |
| 62 | return processLimiter.close(); |
| 63 | } |
| 64 | Result runFormatTool(Tool::Arguments& arguments) |
| 65 | { |
| 66 | SmallString<256> clangFormat; |
no test coverage detected