-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathswift-lint.sh
executable file
·117 lines (91 loc) · 3.36 KB
/
swift-lint.sh
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
#!/bin/bash
#Path to swiftlint
SWIFT_LINT=/usr/local/bin/swiftlint
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
GIT_ROOT=$(git rev-parse --show-toplevel)
ARGUMENT1=$1
ARGUMENT2=$2
ARGUMENT3=$3
if ! [[ -e "${SWIFT_LINT}" ]]; then
#### If SwiftLint is not installed, do not allow commit
echo "warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint"
echo "If you have Homebrew, you can directly use 'brew install swiftlint' to install SwiftLint"
exit 1
fi
if [ -e "$HOME/.rvm/scripts/rvm" ]; then
source "$HOME/.rvm/scripts/rvm"
fi
echo -e "$(ruby -v)\n"
declare -a SOURCE_FILES="($(ruby "$CURRENT_DIR/sourceFiles.rb"))"
lintDirectory() {
cd $1
count=0
echo "🚀 Start linting: $(pwd)"
if [ -e "$1/.gitignore" ]; then
echo "✔︎ .gitignore exists."
FILE_PATHS=($(git ls-files -m --full-name --exclude-from=.gitignore | grep ".swift$"))
DELETED_FILE_PATHS=($(git ls-files -d --full-name --exclude-from=.gitignore | grep ".swift$"))
else
echo "✘ .gitignore does not exist."
FILE_PATHS=($(git ls-files -m --full-name | grep ".swift$"))
DELETED_FILE_PATHS=($(git ls-files -d --full-name | grep ".swift$"))
fi
# Remove deleted files
for i in "${DELETED_FILE_PATHS[@]}"; do
FILE_PATHS=(${FILE_PATHS[@]//*$i*})
done
##### Check for modified files in unstaged/Staged area #####
for file_path in $(git diff --name-only --cached --diff-filter=AM | grep ".swift$"); do
FILE_PATHS=("${FILE_PATHS[@]}" $file_path)
done
# Remove files not in Podspec
if [[ $(pwd) != $GIT_ROOT ]]; then
for i in "${FILE_PATHS[@]}"; do
if [[ ! " ${SOURCE_FILES[@]} " =~ " $1/$i " ]]; then
echo "Remove: $1/$i"
FILE_PATHS=(${FILE_PATHS[@]//*$i*})
fi
done
fi
for file_path in "${FILE_PATHS[@]}"; do
export SCRIPT_INPUT_FILE_$count="$1/$file_path"
count=$((count + 1))
echo "Found lintable file: $1/$file_path"
done
##### Make the count avilable as global variable #####
export SCRIPT_INPUT_FILE_COUNT=$count
##### Lint files or exit if no files found for lintint #####
if [ "$count" == 0 ]; then
echo -e "🎉 No files to lint!\n"
return 0
else
echo "Number of lintable files: ${SCRIPT_INPUT_FILE_COUNT}"
fi
CONFIG_PATH="$CURRENT_DIR/swiftlint.yml"
if [ "$ARGUMENT2" == "--config" ] && [ -e "$ARGUMENT3" ]; then
echo "📂 Found custom config file: $ARGUMENT3"
CONFIG_PATH="$ARGUMENT3"
fi
if [ "$ARGUMENT1" == "autocorrect" ]; then
echo "autocorrect enabled!"
$SWIFT_LINT autocorrect --use-script-input-files --config $CONFIG_PATH #autocorrects before commit.
fi
$SWIFT_LINT lint --use-script-input-files --config $CONFIG_PATH #lint before commit.
RESULT=$?
if [ $RESULT -eq 0 ]; then
echo "🔥 Violation found of the type WARNING! Must fix before commit!"
else
echo "🔥 Violation found of the type ERROR! Must fix before commit!"
fi
echo -e "RESULT: $RESULT\n"
}
lintDirectory $GIT_ROOT
declare -a POD_DIRS="($(ruby "$CURRENT_DIR/localPods.rb"))"
echo "Lintable pods: "
printf '%s\n' "${POD_DIRS[@]}"
for i in "${POD_DIRS[@]}"
do
if [[ $i != *"node_modules"* ]]; then
lintDirectory $i
fi
done