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:

  1. Complex filter rule: includeEndsWith, excludePathContains, excludePathEndsWith, excludePathStartsWith and style.
  2. Simple command line. npx clang-format-launcher -verify to verify the format, and npx clang-format-launcher to auto fix the format. It's easy to be used in the pipeline
  3. 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:

  1. It first runs git ls-tree to get the file list which is checked in.
  2. Apply includeEndsWith, excludePathContains, excludePathEndsWith, excludePathStartsWith to filter the files.
  3. 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
-verify
npx 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]