UNPKG

2.87 kBapplication/x-shView Raw
1#!/bin/bash
2
3# Licensed to the Apache Software Foundation (ASF) under one
4# or more contributor license agreements. See the NOTICE file
5# distributed with this work for additional information
6# regarding copyright ownership. The ASF licenses this file
7# to you under the Apache License, Version 2.0 (the
8# "License"); you may not use this file except in compliance
9# with the License. You may obtain a copy of the License at
10#
11# http://www.apache.org/licenses/LICENSE-2.0
12#
13# Unless required by applicable law or agreed to in writing,
14# software distributed under the License is distributed on an
15# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16# KIND, either express or implied. See the License for the
17# specific language governing permissions and limitations
18# under the License.
19#
20
21SCRIPT_PATH=$(dirname $0)
22CONFIG="$SCRIPT_PATH/uncrustify.cfg"
23EXE="$SCRIPT_PATH/../node_modules/.bin/uncrustify"
24
25function 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
35function 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
43function FilterFileList() {
44#-name "*.[hm]" | grep -v "JSONKit"
45#| grep "\.h\|\.m"
46 for f in "$@"; do
47 # Filter out deleted files.
48 [[ ! -e "$f" ]] && continue
49 # Filter out non .h & .m files.
50 [[ "$f" != *.[hm] ]] && continue
51 # Filter out Third-party sources.
52 [[ "$f" == *JSONKit* ]] && continue
53 echo $f
54 done
55}
56
57function 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
68if [[ "$1" = "--changed" ]]; then
69 VerifyEnv
70 files=$(git status --porcelain | sed s:...::)
71 FilterAndRun $files
72elif [[ "$1" = "--staged" ]]; then
73 VerifyEnv
74 files=$(git diff --cached --name-only)
75 FilterAndRun $files
76elif [[ "$1" = "--all" ]]; then
77 VerifyEnv
78 files=$(find .)
79 FilterAndRun $files
80elif [[ "$1" = "--check-file" ]]; then
81 "$EXE" -q -l OC -c "$CONFIG" -f "$2" | cmp --quiet - "$2"
82elif [[ "$1" = "--filter" ]]; then
83 FilterFileList "$@"
84elif [[ "$1" = -* ]]; then
85 Usage
86else
87 VerifyEnv
88 FilterAndRun "$@"
89fi