forked from dodola/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check-gn-format
executable file
·60 lines (51 loc) · 1.22 KB
/
check-gn-format
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
#!/bin/bash
# Copyright 2018 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
readonly BUILDTOOLS_DIR="${SCRIPT_DIR}/../buildtools"
readonly GN="${BUILDTOOLS_DIR}/gn"
if [[ $# -gt 1 && "$1" = --fix ]]; then
readonly mode=fix
shift
else
readonly mode=check
fi
if [[ $# -eq 0 ]]; then
echo >&2 "Usage: $0 [--fix] project..."
exit 1
fi
check_file() {
local -r file="$1"
"$GN" format --dry-run "$file" && return
local -r status=$?
if [[ $status -eq 2 ]]; then
echo "*** File $file is not formatted correctly."
echo "*** To fix, run: gn format $file"
echo
diff -u "$file" <("$GN" format --stdin < "$file")
fi
return $status
}
fix_file() {
local -r file="$1"
"$GN" format "$file"
}
handle_files() {
local status=0
while read file; do
${mode}_file "$project/$file" || status=$?
done
return $status
}
set -o pipefail
status=0
for project in "$@"; do
git -C "$project" ls-files -- '*.gn*' | handle_files || status=$?
done
if [[ $status -ne 0 ]]; then
echo
echo "*** For a good time, try git file-format"
echo
fi
exit $status