1#!/bin/bash 2# 3# Copyright (c) 2019, The OpenThread Authors. 4# All rights reserved. 5# 6# Redistribution and use in source and binary forms, with or without 7# modification, are permitted provided that the following conditions are met: 8# 1. Redistributions of source code must retain the above copyright 9# notice, this list of conditions and the following disclaimer. 10# 2. Redistributions in binary form must reproduce the above copyright 11# notice, this list of conditions and the following disclaimer in the 12# documentation and/or other materials provided with the distribution. 13# 3. Neither the name of the copyright holder nor the 14# names of its contributors may be used to endorse or promote products 15# derived from this software without specific prior written permission. 16# 17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 21# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27# POSSIBILITY OF SUCH DAMAGE. 28# 29 30# 31# The script to check or format source code of OpenThread. 32# 33# Format c/c++, markdown, python, and shell: 34# 35# script/make-pretty 36# 37# Format c/c++ only: 38# 39# script/make-pretty clang 40# 41# Format markdown only: 42# 43# script/make-pretty markdown 44# 45# Format python only: 46# 47# script/make-pretty python 48# 49# Format shell only: 50# 51# script/make-pretty shell 52# 53# Check only: 54# 55# script/make-pretty check clang 56# script/make-pretty check markdown 57# script/make-pretty check python 58# script/make-pretty check shell 59# 60 61set -euo pipefail 62 63OT_BUILD_JOBS=$(getconf _NPROCESSORS_ONLN) 64readonly OT_BUILD_JOBS 65 66OT_EXCLUDE_DIRS=(third_party doc/site) 67readonly OT_EXCLUDE_DIRS 68 69OT_CLANG_SOURCES=('*.c' '*.cc' '*.cpp' '*.h' '*.hpp') 70readonly OT_CLANG_SOURCES 71 72OT_MARKDOWN_SOURCES=('*.md') 73readonly OT_MARKDOWN_SOURCES 74 75OT_PYTHON_SOURCES=('*.py') 76readonly OT_PYTHON_SOURCES 77 78do_clang_format() 79{ 80 echo -e '=====================' 81 echo -e ' format c/c++' 82 echo -e '=====================' 83 84 git ls-files "${OT_CLANG_SOURCES[@]}" | grep -v -E "^($(echo "${OT_EXCLUDE_DIRS[@]}" | tr ' ' '|'))" \ 85 | xargs -n3 -P"$OT_BUILD_JOBS" script/clang-format -style=file -i -verbose 86} 87 88do_clang_check() 89{ 90 echo -e '=====================' 91 echo -e ' check c/c++' 92 echo -e '=====================' 93 94 git ls-files "${OT_CLANG_SOURCES[@]}" | grep -v -E "^($(echo "${OT_EXCLUDE_DIRS[@]}" | tr ' ' '|'))" \ 95 | xargs -n3 -P"$OT_BUILD_JOBS" script/clang-format-check 96} 97 98do_markdown_format() 99{ 100 echo -e '======================' 101 echo -e ' format markdown' 102 echo -e '======================' 103 104 git ls-files "${OT_MARKDOWN_SOURCES[@]}" | grep -v -E "^($(echo "${OT_EXCLUDE_DIRS[@]}" | tr ' ' '|'))" \ 105 | xargs -n10 -P"$OT_BUILD_JOBS" npx prettier@2.0.4 --write 106} 107 108do_markdown_check() 109{ 110 echo -e '======================' 111 echo -e ' check markdown' 112 echo -e '======================' 113 114 git ls-files "${OT_MARKDOWN_SOURCES[@]}" | grep -v -E "^($(echo "${OT_EXCLUDE_DIRS[@]}" | tr ' ' '|'))" \ 115 | xargs -n10 -P"$OT_BUILD_JOBS" npx prettier@2.0.4 --check 116} 117 118do_python_format() 119{ 120 echo -e '======================' 121 echo -e ' format python' 122 echo -e '======================' 123 124 git ls-files "${OT_PYTHON_SOURCES[@]}" | grep -v -E "^($(echo "${OT_EXCLUDE_DIRS[@]}" | tr ' ' '|'))" \ 125 | xargs -n10 -P"$OT_BUILD_JOBS" python3 -m yapf --verbose --style google -ipr 126} 127 128do_python_check() 129{ 130 echo -e '=====================' 131 echo -e ' check python' 132 echo -e '=====================' 133 134 git ls-files "${OT_PYTHON_SOURCES[@]}" | grep -v -E "^($(echo "${OT_EXCLUDE_DIRS[@]}" | tr ' ' '|'))" \ 135 | xargs -n10 -P"$OT_BUILD_JOBS" python3 -m yapf --verbose --style google -dpr 136} 137 138do_shell_format() 139{ 140 echo -e '=====================' 141 echo -e ' format shell' 142 echo -e '=====================' 143 144 shfmt -f . | grep -v -E "^($(echo "${OT_EXCLUDE_DIRS[@]}" | tr ' ' '|'))" \ 145 | xargs -n10 -P"$OT_BUILD_JOBS" shfmt -i 4 -bn -ci -fn -s -w 146} 147 148do_shell_check() 149{ 150 echo -e '=====================' 151 echo -e ' check shell' 152 echo -e '=====================' 153 154 shfmt -f . | grep -v -E "^($(echo "${OT_EXCLUDE_DIRS[@]}" | tr ' ' '|'))" \ 155 | xargs -n10 -P"$OT_BUILD_JOBS" shfmt -i 4 -bn -ci -fn -s -d 156 157 shfmt -f . | grep -v -E "^($(echo "${OT_EXCLUDE_DIRS[@]}" | tr ' ' '|'))" \ 158 | xargs -n10 -P"$OT_BUILD_JOBS" shellcheck -x 159} 160 161do_check() 162{ 163 if [ $# == 0 ]; then 164 do_clang_check 165 do_markdown_check 166 # python not currently used in this project 167 # do_python_check 168 do_shell_check 169 elif [ "$1" == 'clang' ]; then 170 do_clang_check 171 elif [ "$1" == 'markdown' ]; then 172 do_markdown_check 173 elif [ "$1" == 'python' ]; then 174 do_python_check 175 elif [ "$1" == 'shell' ]; then 176 do_shell_check 177 else 178 echo >&2 "Unsupported check: $1. Supported: clang, markdown, python, shell" 179 # 128 for Invalid arguments 180 exit 128 181 fi 182} 183 184main() 185{ 186 if [ $# == 0 ]; then 187 do_clang_format 188 do_markdown_format 189 # python not currently used in this project 190 # do_python_format 191 do_shell_format 192 elif [ "$1" == 'clang' ]; then 193 do_clang_format 194 elif [ "$1" == 'markdown' ]; then 195 do_markdown_format 196 elif [ "$1" == 'python' ]; then 197 do_python_format 198 elif [ "$1" == 'shell' ]; then 199 do_shell_format 200 elif [ "$1" == 'check' ]; then 201 shift 202 do_check "$@" 203 else 204 echo >&2 "Unsupported action: $1. Supported: clang, markdown, python, shell" 205 # 128 for Invalid arguments 206 exit 128 207 fi 208 209} 210 211main "$@" 212