forked from tensorflow/tfjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclang_format_ts.sh
More file actions
executable file
·78 lines (69 loc) · 1.98 KB
/
Copy pathclang_format_ts.sh
File metadata and controls
executable file
·78 lines (69 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/bin/bash
# Copyright 2018 Google LLC
#
# Use of this source code is governed by an MIT-style
# license that can be found in the LICENSE file or at
# https://opensource.org/licenses/MIT.
# ==============================================================================
# This script applies google-style clang format on all TypeScript (.ts) files
# within a certain scope.
#
# Usage examples:
# 1. Format all .ts files touched by this change (unstaged or staged in git).
# clang_format_ts.sh
#
# 2. Format all .ts files under the source tree.
# clang_format_ts.sh -a
#
# 3. Format specific files.
# clang_format_ts.sh src/types.ts
set -e
FILE_SCOPE=""
if [[ "$#" -gt 0 ]]; then
while true; do
if [[ -z "$1" ]]; then
break
fi
if [[ "$1" == "-a" ]]; then
if [[ -z "${FILE_SCOPE}" ]]; then
FILE_SCOPE="__all__"
else
echo "ERROR: -a flag should not be used with file names"
exit 1
fi
else
if [[ "${FILE_SCOPE}" != "__all__" ]]; then
FILE_SCOPE="${FILE_SCOPE} $1"
else
echo "ERROR: -a flag should not be used with file names"
exit 1
fi
fi
shift
done
else
FILE_SCOPE="__touched__"
fi
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
CLANG_FORMAT_PREFIX="${SCRIPT_DIR}/../node_modules/.bin/clang-format -i --verbose --style=google"
if [[ "${FILE_SCOPE}" == "__touched__" ]]; then
TOUCHED_TS_FILES="$(git status --porcelain | grep '.*\.ts$' | sed s/^...//)"
if [[ -z "${TOUCHED_TS_FILES}" ]]; then
exit 0
else
pushd "${SCRIPT_DIR}/.." > /dev/null
for TS_FILE in ${TOUCHED_TS_FILES}; do
if [[ -f ${TS_FILE} ]]; then
${CLANG_FORMAT_PREFIX} "${TS_FILE}"
fi
done
popd > /dev/null
fi
elif [[ "${FILE_SCOPE}" == "__all__" ]]; then
ALL_TS_FILES="$(find "${SCRIPT_DIR}/../src" "${SCRIPT_DIR}/../demos" -name '*.ts')"
for TS_FILE in ${ALL_TS_FILES}; do
${CLANG_FORMAT_PREFIX} "${TS_FILE}"
done
else
${CLANG_FORMAT_PREFIX} ${FILE_SCOPE}
fi