1 | #!/bin/bash
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | SCRIPT_PATH=$(dirname $0)
|
22 | CONFIG="$SCRIPT_PATH/uncrustify.cfg"
|
23 | EXE="$SCRIPT_PATH/../node_modules/.bin/uncrustify"
|
24 |
|
25 | function Usage() {
|
26 | echo "Formats code using uncrustify."
|
27 | echo "Usage: bin/uncrustify.sh --changed # Runs on changed (staged or not) files"
|
28 | echo " bin/uncrustify.sh --staged # Runs on staged files"
|
29 | echo " bin/uncrustify.sh --all # Runs on all source files under the current directory"
|
30 | echo " bin/uncrustify.sh --check-file file # Returns 1 if the given file requires changes, 0 otherwise."
|
31 | echo " bin/uncrustify.sh files # Runs on the given files"
|
32 | exit 1
|
33 | }
|
34 |
|
35 | function VerifyEnv() {
|
36 | if ! which "$EXE" > /dev/null; then
|
37 | echo "uncrustify binary not found in the cordova-ios repo."
|
38 | echo "In the repo root, install via npm: npm install"
|
39 | exit 1
|
40 | fi
|
41 | }
|
42 |
|
43 | function FilterFileList() {
|
44 |
|
45 |
|
46 | for f in "$@"; do
|
47 |
|
48 | [[ ! -e "$f" ]] && continue
|
49 |
|
50 | [[ "$f" != *.[hm] ]] && continue
|
51 |
|
52 | [[ "$f" == *JSONKit* ]] && continue
|
53 | echo $f
|
54 | done
|
55 | }
|
56 |
|
57 | function FilterAndRun() {
|
58 | files=$(FilterFileList "$@")
|
59 |
|
60 | if [[ -z "$files" ]]; then
|
61 | echo No files to uncrustify.
|
62 | exit 2
|
63 | else
|
64 | echo "$files" | xargs uncrustify -l OC --no-backup -c "$CONFIG"
|
65 | fi
|
66 | }
|
67 |
|
68 | if [[ "$1" = "--changed" ]]; then
|
69 | VerifyEnv
|
70 | files=$(git status --porcelain | sed s:...::)
|
71 | FilterAndRun $files
|
72 | elif [[ "$1" = "--staged" ]]; then
|
73 | VerifyEnv
|
74 | files=$(git diff --cached --name-only)
|
75 | FilterAndRun $files
|
76 | elif [[ "$1" = "--all" ]]; then
|
77 | VerifyEnv
|
78 | files=$(find .)
|
79 | FilterAndRun $files
|
80 | elif [[ "$1" = "--check-file" ]]; then
|
81 | "$EXE" -q -l OC -c "$CONFIG" -f "$2" | cmp --quiet - "$2"
|
82 | elif [[ "$1" = "--filter" ]]; then
|
83 | FilterFileList "$@"
|
84 | elif [[ "$1" = -* ]]; then
|
85 | Usage
|
86 | else
|
87 | VerifyEnv
|
88 | FilterAndRun "$@"
|
89 | fi
|