• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
63readonly OT_BUILD_JOBS=$(getconf _NPROCESSORS_ONLN)
64readonly OT_EXCLUDE_DIRS=(third_party doc/site)
65
66readonly OT_CLANG_SOURCES=('*.c' '*.cc' '*.cpp' '*.h' '*.hpp')
67readonly OT_MARKDOWN_SOURCES=('*.md')
68readonly OT_PYTHON_SOURCES=('*.py')
69
70do_clang_format()
71{
72    echo -e '====================='
73    echo -e '     format c/c++'
74    echo -e '====================='
75
76    git ls-files "${OT_CLANG_SOURCES[@]}" | grep -v -E "^($(echo "${OT_EXCLUDE_DIRS[@]}" | tr ' ' '|'))" \
77        | xargs -n3 -P"$OT_BUILD_JOBS" script/clang-format -style=file -i -verbose
78}
79
80do_clang_check()
81{
82    echo -e '====================='
83    echo -e '     check c/c++'
84    echo -e '====================='
85
86    git ls-files "${OT_CLANG_SOURCES[@]}" | grep -v -E "^($(echo "${OT_EXCLUDE_DIRS[@]}" | tr ' ' '|'))" \
87        | xargs -n3 -P"$OT_BUILD_JOBS" script/clang-format-check
88}
89
90do_markdown_format()
91{
92    echo -e '======================'
93    echo -e '     format markdown'
94    echo -e '======================'
95
96    git ls-files "${OT_MARKDOWN_SOURCES[@]}" | grep -v -E "^($(echo "${OT_EXCLUDE_DIRS[@]}" | tr ' ' '|'))" \
97        | xargs -n10 -P"$OT_BUILD_JOBS" npx prettier@2.0.4 --write
98}
99
100do_markdown_check()
101{
102    echo -e '======================'
103    echo -e '     check markdown'
104    echo -e '======================'
105
106    git ls-files "${OT_MARKDOWN_SOURCES[@]}" | grep -v -E "^($(echo "${OT_EXCLUDE_DIRS[@]}" | tr ' ' '|'))" \
107        | xargs -n10 -P"$OT_BUILD_JOBS" npx prettier@2.0.4 --check
108}
109
110do_python_format()
111{
112    echo -e '======================'
113    echo -e '     format python'
114    echo -e '======================'
115
116    git ls-files "${OT_PYTHON_SOURCES[@]}" | grep -v -E "^($(echo "${OT_EXCLUDE_DIRS[@]}" | tr ' ' '|'))" \
117        | xargs -n10 -P"$OT_BUILD_JOBS" python3 -m yapf --verbose --style google -ipr
118}
119
120do_python_check()
121{
122    echo -e '====================='
123    echo -e '     check python'
124    echo -e '====================='
125
126    git ls-files "${OT_PYTHON_SOURCES[@]}" | grep -v -E "^($(echo "${OT_EXCLUDE_DIRS[@]}" | tr ' ' '|'))" \
127        | xargs -n10 -P"$OT_BUILD_JOBS" python3 -m yapf --verbose --style google -dpr
128}
129
130do_shell_format()
131{
132    echo -e '====================='
133    echo -e '     format shell'
134    echo -e '====================='
135
136    shfmt -f . | grep -v -E "^($(echo "${OT_EXCLUDE_DIRS[@]}" | tr ' ' '|'))" \
137        | xargs -n10 -P"$OT_BUILD_JOBS" shfmt -i 4 -bn -ci -fn -s -w
138}
139
140do_shell_check()
141{
142    echo -e '====================='
143    echo -e '     check shell'
144    echo -e '====================='
145
146    shfmt -f . | grep -v -E "^($(echo "${OT_EXCLUDE_DIRS[@]}" | tr ' ' '|'))" \
147        | xargs -n10 -P"$OT_BUILD_JOBS" shfmt -i 4 -bn -ci -fn -s -d
148
149    shfmt -f . | grep -v -E "^($(echo "${OT_EXCLUDE_DIRS[@]}" | tr ' ' '|'))" \
150        | xargs -n10 -P"$OT_BUILD_JOBS" shellcheck -x
151}
152
153do_check()
154{
155    if [ $# == 0 ]; then
156        do_clang_check
157        do_markdown_check
158        # python not currently used in this project
159        # do_python_check
160        do_shell_check
161    elif [ "$1" == 'clang' ]; then
162        do_clang_check
163    elif [ "$1" == 'markdown' ]; then
164        do_markdown_check
165    elif [ "$1" == 'python' ]; then
166        do_python_check
167    elif [ "$1" == 'shell' ]; then
168        do_shell_check
169    else
170        echo >&2 "Unsupported check: $1. Supported: clang, markdown, python, shell"
171        # 128 for Invalid arguments
172        exit 128
173    fi
174}
175
176main()
177{
178    if [ $# == 0 ]; then
179        do_clang_format
180        do_markdown_format
181        # python not currently used in this project
182        # do_python_format
183        do_shell_format
184    elif [ "$1" == 'clang' ]; then
185        do_clang_format
186    elif [ "$1" == 'markdown' ]; then
187        do_markdown_format
188    elif [ "$1" == 'python' ]; then
189        do_python_format
190    elif [ "$1" == 'shell' ]; then
191        do_shell_format
192    elif [ "$1" == 'check' ]; then
193        shift
194        do_check "$@"
195    else
196        echo >&2 "Unsupported action: $1. Supported: clang, markdown, python, shell"
197        # 128 for Invalid arguments
198        exit 128
199    fi
200
201}
202
203main "$@"
204