-
Notifications
You must be signed in to change notification settings - Fork 0
/
SucceedsPostAction.sh
executable file
·92 lines (70 loc) · 3 KB
/
SucceedsPostAction.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
#!/bin/bash
#---------------------------------------------------------------------
# Bash script to be used as a 'Succeeds' post action in Xcode.
# This script will trigger SwiftLint when you compile a Swift Package.
#---------------------------------------------------------------------
#---------------------------------------------------------------------
# Debugging scripts run from the Succeeds Behavior is complex
# Logs, warnings and errors are not visible in the Build results
# One solution is to write to a file or use the say command.
#---------------------------------------------------------------------
#say "Run!"
#---------------------------------------------------------------------
# Check if swiftlint is installed and is an executable
#---------------------------------------------------------------------
SWIFTLINT_PATH="/usr/local/bin/swiftlint"
if ! [ -x "${SWIFTLINT_PATH}" ]
then
SWIFTLINT_PATH="/opt/homebrew/bin/swiftlint"
if ! [ -x "${SWIFTLINT_PATH}" ]
then
exit 0
fi
fi
#---------------------------------------------------------------------
# Get useful paths
#---------------------------------------------------------------------
SCRIPT_FOLDER_PATH=`dirname "$0"`
cd "${SCRIPT_FOLDER_PATH}"
SCRIPT_FOLDER_PATH=`pwd -P`
#---------------------------------------------------------------------
# Check if the developer is compiling a Swift package
#---------------------------------------------------------------------
if [ "${XcodeWorkspace}" != "package.xcworkspace" ]
then
exit 0
fi
#---------------------------------------------------------------------
# Find the root folder of the Swift package
#---------------------------------------------------------------------
PACKAGE_WORKSPACE_SUFFIX="/.swiftpm/xcode/package.xcworkspace"
PACKAGE_ROOT_FOLDER="${XcodeWorkspacePath}"
PACKAGE_ROOT_FOLDER=${PACKAGE_ROOT_FOLDER%"${PACKAGE_WORKSPACE_SUFFIX}"}
#---------------------------------------------------------------------
# Check if the root folder of the package exists
#---------------------------------------------------------------------
if [ ! -d "${PACKAGE_ROOT_FOLDER}" ]
then
exit 0
fi
#---------------------------------------------------------------------
# Run swiftlint
#---------------------------------------------------------------------
OUTPUT="${SWIFTLINT_PATH} ${PACKAGE_ROOT_FOLDER}"
OUTPUT_FILE_PATH="/tmp/swiftlint.txt"
${OUTPUT} > ${OUTPUT_FILE_PATH}
#---------------------------------------------------------------------
# Remove the lines containing /.build/
#---------------------------------------------------------------------
sed -i '' '/\/\.build\//d' ${OUTPUT_FILE_PATH}
#---------------------------------------------------------------------
# Check if the file is empty
#---------------------------------------------------------------------
if [ ! -s "${OUTPUT_FILE_PATH}" ]
then
exit 0
fi
#---------------------------------------------------------------------
# Open result file in Xcode
#---------------------------------------------------------------------
xed ${OUTPUT_FILE_PATH}