-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlint.sh
More file actions
executable file
·141 lines (118 loc) · 4.03 KB
/
Copy pathlint.sh
File metadata and controls
executable file
·141 lines (118 loc) · 4.03 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#! /bin/bash
set -euo pipefail
# Function to print usage
function print_usage {
echo "Usage: $0 --mode [check|format]"
exit 1
}
function grep_xxx {
# Don't show up in the search
X="X"
BAD_STRING="$X$X$X"
# Collect matches with line numbers
MATCHES=$(git grep -In ${BAD_STRING} || true)
if [ -n "$MATCHES" ]; then
echo "${BAD_STRING} found in source"
while IFS= read -r line; do
echo "$line"
file=$(echo "$line" | cut -d: -f1)
linenum=$(echo "$line" | cut -d: -f2)
echo "::error file=${file},line=${linenum}::${BAD_STRING} comment found"
done <<< "$MATCHES"
exit 1
fi
}
# Parse arguments using getopt
OPTS=$(getopt -o '' -l 'mode:' -- "$@")
if [ $? != 0 ]; then
print_usage
fi
eval set -- "$OPTS"
# Initialize the flag
mode=
while true; do
case "$1" in
--mode)
if [ "$2" = "check" ]; then
mode=$2
elif [ "$2" = "format" ]; then
mode=$2
else
echo "Invalid value for --mode. Use 'check' or 'format'."
exit 1
fi
shift 2
;;
--)
shift
break
;;
*)
print_usage
;;
esac
done
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd -P)"
# Find all git-tracked Bazel-ish files - these templates come from Buildifier's
# default search list. Literal (non-glob) names need both the bare and "**/"
# forms since git pathspec only matches wildcard patterns at any depth.
BAZEL_FILES=$(git -C "${REPO_ROOT}" ls-files -- \
'*.bzl' \
'*.sky' \
'BUILD.bazel' '**/BUILD.bazel' \
'BUILD' '**/BUILD' \
'*.BUILD' \
'BUILD.*.bazel' \
'BUILD.*.oss' \
'MODULE.bazel' '**/MODULE.bazel' \
'WORKSPACE' '**/WORKSPACE' \
'WORKSPACE.bazel' '**/WORKSPACE.bazel' \
'WORKSPACE.oss' '**/WORKSPACE.oss' \
'WORKSPACE.*.bazel' \
'WORKSPACE.*.oss' \
| sed "s|^|${REPO_ROOT}/|")
# Find all git-tracked Markdown-ish files
MARKDOWN_FILES=$(git -C "${REPO_ROOT}" ls-files -- '*.md' | sed "s|^|${REPO_ROOT}/|")
# Check if the flag was set
if [ -z "$mode" ]; then
print_usage
elif [ "$mode" = "check" ]; then
BUILDIFIER_ARGS=("-lint=off" "-mode=check" "-v=false")
PRETTIER_ARGS=("--check" "--config" "${REPO_ROOT}/.prettierrc" "--ignore-path" "${REPO_ROOT}/.prettierignore" "--ignore-path" "${REPO_ROOT}/.gitignore")
BAZEL_TOOL="//tools:check"
GAZELLE_ARGS=("-mode" "diff")
RULES_LINT_CMD="//tools/format:format.check"
elif [ "$mode" = "format" ]; then
BUILDIFIER_ARGS=("-lint=fix" "-mode=fix" "-v=false")
PRETTIER_ARGS=("--write" "--config" "${REPO_ROOT}/.prettierrc" "--ignore-path" "${REPO_ROOT}/.prettierignore" "--ignore-path" "${REPO_ROOT}/.gitignore")
BAZEL_TOOL="//tools:format"
GAZELLE_ARGS=("-mode" "fix")
RULES_LINT_CMD="//tools/format:format"
fi
PRETTIER_INVOCATION=""
function print_error {
read line file <<<$(caller)
printf "\n⛔️ An error occurred during the following lint step ⛔️\n" >&2
sed "${line}q;d" "$file" >&2
}
trap print_error ERR
CONFIG="--config quiet"
# Can uncomment to get more verbose
# CONFIG=""
# Run query generator
bazel run ${CONFIG} --output_groups=-mypy //packaging:query_generator -- --mode $mode
# Regenerate/verify gazelle_cc index files
bazel run ${CONFIG} --output_groups=-mypy //tools:generate_ccindex -- --mode $mode
echo $BAZEL_FILES | xargs bazel run ${CONFIG} -- //tools/buildifier ${BUILDIFIER_ARGS[@]}
bazel run ${CONFIG} -- ${BAZEL_TOOL}
echo $MARKDOWN_FILES | xargs bazel run ${CONFIG} -- //tools/prettier ${PRETTIER_ARGS[@]}
bazel run ${CONFIG} -- //:gazelle ${GAZELLE_ARGS[@]}
bazel run $RULES_LINT_CMD
grep_xxx
# TODO(#139)
# Add this back in (conflicts with emboss at the moment)
# See https://github.com/aspect-build/rules_lint/blob/main/example/lint.sh
# --aspects=//tools/lint:linters.bzl%clang_tidy
# Verify MODULE.bazel.lock is up to date (fixed by rules_rust upgrade, #57, #392)
bazel mod deps --lockfile_mode=error
printf "\n✨ Linting completed successfully! ✨\n"