clang-format-launcher
Date: Dec 4, 2021
Keywords: JavaScript, clang-format, clang-format-launcher, C++, Object-C, Java
GitHub: https://github.com/licanhua/clang-format-launcher
Goal
Apply clang-format to Microsoft Adaptivecards project. Adaptivecards is a project covers iOS, Windows and android. Expect a generic tool to work on both Mac and Windows. The tool will format c++, java and object-c.
Overview
clang-format-launcher is a clang-format wrapper which is used to launch the clang-format with predefined rules.
This tool is designed for complex project, which glob
pattern is not enough.
It provides features:
- Complex filter rule:
includeEndsWith
,excludePathContains
,excludePathEndsWith
,excludePathStartsWith
andstyle
. - Simple command line.
npx clang-format-launcher -verify
to verify the format, andnpx clang-format-launcher
to auto fix the format. It's easy to be used in the pipeline - Only source code is formatted. It automatically skipped buid output and node_modules which are not checked in to the repo.
Here is the idea:
- It first runs
git ls-tree
to get the file list which is checked in. - Apply
includeEndsWith
,excludePathContains
,excludePathEndsWith
,excludePathStartsWith
to filter the files. - Do clang-format check or format based on the existence of
-verify
flag.
How to user it
Step 1
npm i --save-dev clang-format clang-format-launcher
Step 2 prepare clang.format.json
Assume clang-format-launcher is in ${project}/node_modules/clang-format-launcher, then you should be clang.format.json to ${project}/
Here is an example of clang.format.json:
{"includeEndsWith": [".h",".cpp"],"excludePathContains": ["/ios/", "/nodejs/", "/android/"],"excludePathEndsWith": [".g.h",".g.cpp"],"excludePathStartsWith": [],"folder": "./","style": "--style=file"}
Usage
Run scripts in package.json
npm run format
and npm run verify
"scripts": {
...
"format": "clang-format-launcher",
"verify": "clang-format-launcher -verify"
},
Run with npx
npx clang-format-launcher
npx clang-format-launcher -verify
Command details
npx clang-format-launcher [Options] [other options]Options:-raw-verifynpx clang-format-launcher [other options]equal to 'npx clang-format --style=file -Werror -i [other options] [Files after filter]'npx clang-format-launcher -verify [other options]equal to 'npx clang-format --style=file -Werror --dry-run [other options] [Files after filter]'npx clang-format-launcher -raw [other options]equal to 'npx clang-format [other options]