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