From c48f84d9ea5f7df1d3c7c2b023c0372377593364 Mon Sep 17 00:00:00 2001 From: Jonathan Hui Date: Thu, 23 Apr 2020 16:29:27 -0700 Subject: [PATCH] [pretty] add markdown support (#4881) --- .prettierrc | 7 +++++ script/make-pretty | 65 ++++++++++++++++++++++++++++++++++------------ 2 files changed, 56 insertions(+), 16 deletions(-) create mode 100644 .prettierrc diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 00000000000..89cc06e367e --- /dev/null +++ b/.prettierrc @@ -0,0 +1,7 @@ +{ + "$schema": "http://json.schemastore.org/prettierrc", + "printWidth": 80, + "tabWidth": 2, + "useTabs": false, + "proseWrap": "never" +} diff --git a/script/make-pretty b/script/make-pretty index 6709eadb77b..c9ffae45734 100755 --- a/script/make-pretty +++ b/script/make-pretty @@ -30,30 +30,37 @@ # # The script to check or format source code of OpenThread. # -# Format python and c/c++: +# Format c/c++, markdown, and python: # # script/make-pretty # -# Format python only: -# -# script/make-pretty python -# # Format c/c++ only: # # script/make-pretty clang # +# Format markdown only: +# +# script/make-pretty markdown +# +# Format python only: +# +# script/make-pretty python +# # Check only: # # script/make-pretty check clang +# script/make-pretty check markdown # script/make-pretty check python # set -euo pipefail -readonly OT_CLANG_DIRS=(examples include src tests tools) -readonly OT_PYTHON_DIRS=(tests tools) readonly OT_BUILD_JOBS=$(getconf _NPROCESSORS_ONLN) +readonly OT_EXCLUDE_DIRS=(third_party) + readonly OT_CLANG_SOURCES=('*.c' '*.cc' '*.cpp' '*.h' '*.hpp') +readonly OT_MARKDOWN_SOURCES=('*.md') +readonly OT_PYTHON_SOURCES=('*.py') do_clang_format() { @@ -61,7 +68,7 @@ do_clang_format() echo -e ' format c/c++' echo -e '=====================' - git ls-files "${OT_CLANG_SOURCES[@]}" | grep -E "^($(echo "${OT_CLANG_DIRS[@]}" | tr ' ' '|'))" \ + git ls-files "${OT_CLANG_SOURCES[@]}" | grep -v -E "^($(echo "${OT_EXCLUDE_DIRS[@]}" | tr ' ' '|'))" \ | xargs -n3 -P"${OT_BUILD_JOBS}" script/clang-format -style=file -i -verbose } @@ -71,17 +78,37 @@ do_clang_check() echo -e ' check c/c++' echo -e '=====================' - git ls-files "${OT_CLANG_SOURCES[@]}" | grep -E "^($(echo "${OT_CLANG_DIRS[@]}" | tr ' ' '|'))" \ + git ls-files "${OT_CLANG_SOURCES[@]}" | grep -v -E "^($(echo "${OT_EXCLUDE_DIRS[@]}" | tr ' ' '|'))" \ | xargs -n3 -P"${OT_BUILD_JOBS}" script/clang-format-check } +do_markdown_format() +{ + echo -e '======================' + echo -e ' format markdown' + echo -e '======================' + + git ls-files "${OT_MARKDOWN_SOURCES[@]}" | grep -v -E "^($(echo "${OT_EXCLUDE_DIRS[@]}" | tr ' ' '|'))" \ + | xargs -n10 -P"${OT_BUILD_JOBS}" npx prettier@2.0.4 --write +} + +do_markdown_check() +{ + echo -e '======================' + echo -e ' check markdown' + echo -e '======================' + + git ls-files "${OT_MARKDOWN_SOURCES[@]}" | grep -v -E "^($(echo "${OT_EXCLUDE_DIRS[@]}" | tr ' ' '|'))" \ + | xargs -n10 -P"${OT_BUILD_JOBS}" npx prettier@2.0.4 --check +} + do_python_format() { echo -e '======================' echo -e ' format python' echo -e '======================' - git ls-files '*.py' | grep -E "^($(echo "${OT_PYTHON_DIRS[@]}" | tr ' ' '|'))" \ + git ls-files "${OT_PYTHON_SOURCES[@]}" | grep -v -E "^($(echo "${OT_EXCLUDE_DIRS[@]}" | tr ' ' '|'))" \ | xargs -n10 -P"${OT_BUILD_JOBS}" python3 -m yapf --verbose --style google -ipr } @@ -91,21 +118,24 @@ do_python_check() echo -e ' check python' echo -e '=====================' - git ls-files '*.py' | grep -E "^($(echo "${OT_PYTHON_DIRS[@]}" | tr ' ' '|'))" \ + git ls-files "${OT_PYTHON_SOURCES[@]}" | grep -v -E "^($(echo "${OT_EXCLUDE_DIRS[@]}" | tr ' ' '|'))" \ | xargs -n10 -P"${OT_BUILD_JOBS}" python3 -m yapf --verbose --style google -dpr } do_check() { if [ $# == 0 ]; then - do_python_check do_clang_check + do_markdown_check + do_python_check elif [ "$1" == 'clang' ]; then do_clang_check + elif [ "$1" == 'markdown' ]; then + do_markdown_check elif [ "$1" == 'python' ]; then do_python_check else - >&2 echo "Unsupported check: $1. Supported: clang, python" + >&2 echo "Unsupported check: $1. Supported: clang, markdown, python" # 128 for Invalid arguments exit 128 fi @@ -115,16 +145,19 @@ main() { if [ $# == 0 ]; then do_clang_format - do_python_format - elif [ "$1" == 'python' ]; then + do_markdown_format do_python_format elif [ "$1" == 'clang' ]; then do_clang_format + elif [ "$1" == 'markdown' ]; then + do_markdown_format + elif [ "$1" == 'python' ]; then + do_python_format elif [ "$1" == 'check' ]; then shift do_check "$@" else - >&2 echo "Unsupported action: $1. Supported: clang, python" + >&2 echo "Unsupported action: $1. Supported: clang, markdown, python" # 128 for Invalid arguments exit 128 fi