-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
build.zsh
executable file
·265 lines (202 loc) · 9.3 KB
/
build.zsh
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#!/bin/zsh
# build.zsh
# SupportCompanion
#
# Created by Tobias Almén on 2024-11-26.
#
check_exit_code() {
if [ "$1" != "0" ]; then
echo "$2: $1" 1>&2
exit 1
fi
}
create_pkg() {
local identifier="$1"
local version="$2"
local input_path="$3"
local output_path="$4"
local install_location="$5"
pkgbuild --root "${input_path}/payload" \
--scripts "${input_path}/scripts" \
--install-location "${install_location}" \
--identifier "${identifier}" \
--version "${version}" \
--sign "${SIGNING_IDENTITY}" \
"${output_path}" >/dev/null 2>&1
check_exit_code "$?" "Error creating pkg"
}
generate_dist_file() {
local bundle_identifier="$1"
local build_version="$2"
local output_file="$3"
local pkg_type="$4"
local pkg_ref_path="SupportCompanion${pkg_type}-${build_version}.pkg"
cat <<EOF > "$output_file"
<?xml version="1.0" encoding="utf-8"?>
<installer-gui-script minSpecVersion="2">
<title>${APP_NAME}</title>
<options customize="never" require-scripts="false" hostArchitectures="x86_64,arm64"/>
<volume-check>
<allowed-os-versions>
<os-version min="14"/>
</allowed-os-versions>
</volume-check>
<choices-outline>
<line choice="${bundle_identifier}"/>
</choices-outline>
<choice id="${bundle_identifier}" title="${APP_NAME}">
<pkg-ref id="${bundle_identifier}"/>
</choice>
<pkg-ref id="${bundle_identifier}" version="${build_version}" onConclusion="none">
${pkg_ref_path}
</pkg-ref>
</installer-gui-script>
EOF
}
run_product_build_sign() {
local package_path="$1"
local package="$2"
productbuild --distribution "$DIST_FILE" \
--package-path "${package_path}" \
"${package}_dist.pkg"
check_exit_code "$?" "Error running productbuild"
# Sign package
productsign --sign "${SIGNING_IDENTITY}" \
"${package}_dist.pkg" \
"${package}.pkg"
check_exit_code "$?" "Error running productsign"
}
notarize_and_staple() {
local pkg_path="$1"
$XCODE_NOTARY_PATH submit "$pkg_path" --keychain-profile "${KEYCHAIN_PROFILE}" --wait
check_exit_code "$?" "Error notarizing pkg"
$XCODE_STAPLER_PATH staple "$pkg_path"
check_exit_code "$?" "Error stapling pkg"
}
# Exit on error
set -e
CONFIGURATION="$1"
APP_NAME="SupportCompanion"
BUNDLE_IDENTIFIER="com.github.macadmins.SupportCompanion"
LA_BUNDLE_IDENTIFIER="com.github.macadmins.SupportCompanion.LaunchAgent"
LA_NAME="com.github.macadmins.SupportCompanion.agent.plist"
SUITE_BUNDLE_IDENTIFIER="com.github.macadmins.SupportCompanion.suite"
if [ "$CONFIGURATION" = "Debug" ]; then
SIGNING_IDENTITY="Developer ID Installer: ANDERS TOBIAS ALMÉN (H92SB6Z7S4)"
SIGNING_IDENTITY_APP="Developer ID Application: ANDERS TOBIAS ALMÉN (H92SB6Z7S4)"
KEYCHAIN_PROFILE="AC-creds"
XCODE_PATH="/Applications/Xcode.app"
TEAM_ID="H92SB6Z7S4"
elif [ "$CONFIGURATION" = "Release" ]; then
SIGNING_IDENTITY_APP="Developer ID Application: Mac Admins Open Source (T4SK8ZXCXG)"
SIGNING_IDENTITY="Developer ID Installer: Mac Admins Open Source (T4SK8ZXCXG)"
KEYCHAIN_PROFILE="supportcompanion"
XCODE_PATH="/Applications/Xcode_16.app"
TEAM_ID="T4SK8ZXCXG"
else
echo "No configuration set, exiting..."
exit 1
fi
XCODE_NOTARY_PATH="$XCODE_PATH/Contents/Developer/usr/bin/notarytool"
XCODE_STAPLER_PATH="$XCODE_PATH/Contents/Developer/usr/bin/stapler"
XCODE_BUILD_PATH="$XCODE_PATH/Contents/Developer/usr/bin/xcodebuild"
TOOLSDIR=$(dirname $0)
BUILDSDIR="$TOOLSDIR/build"
PKGBUILDDIR="$BUILDSDIR/pkgbuild"
OUTPUTSDIR="$TOOLSDIR/outputs"
RELEASEDIR="$TOOLSDIR/release"
PKG_PATH="$TOOLSDIR/SupportCompanion/pkgbuild"
LA_PATH="$TOOLSDIR/SupportCompanion/LaunchAgent"
SCRIPTS="$PKG_PATH/scripts"
LA_SCRIPTS="$LA_PATH/scripts"
DIST_FILE="$BUILDSDIR/Distribution.xml"
CURRENT_SC_MAIN_BUILD_VERSION=$(/usr/libexec/PlistBuddy -c Print:CFBundleVersion $TOOLSDIR/SupportCompanion/Info.plist)
NEWSUBBUILD=$((80620 + $(git rev-parse HEAD~0 | xargs -I{} git rev-list --count {})))
# automate the build version bump
AUTOMATED_SC_BUILD="$CURRENT_SC_MAIN_BUILD_VERSION.$NEWSUBBUILD"
# Ensure Xcode is set to run-time
sudo xcode-select -s "$XCODE_PATH"
# Resolve package dependencies
$XCODE_BUILD_PATH -resolvePackageDependencies
if [ "$CONFIGURATION" = "Release" ]; then
# Setup notary item
$XCODE_NOTARY_PATH store-credentials --apple-id "[email protected]" --team-id "T4SK8ZXCXG" --password "$2" supportcompanion
fi
# Create release folder
if [ -e $RELEASEDIR ]; then
/bin/rm -rf $RELEASEDIR
fi
/bin/mkdir -p "$RELEASEDIR"
# Create build folder
if [ -e $BUILDSDIR ]; then
/bin/rm -rf $BUILDSDIR
fi
/bin/mkdir -p "$BUILDSDIR"
# build Support Companion
echo "=========== Building SupportCompanion $CONFIGURATION ==========="
echo "$AUTOMATED_SC_BUILD" > "$BUILDSDIR/build_info.txt"
echo "$CURRENT_SC_MAIN_BUILD_VERSION" > "$BUILDSDIR/build_info_main.txt"
$XCODE_BUILD_PATH clean archive -scheme SupportCompanion -project "$TOOLSDIR/SupportCompanion.xcodeproj" \
-configuration $CONFIGURATION \
CODE_SIGN_IDENTITY="$SIGNING_IDENTITY_APP" \
OTHER_CODE_SIGN_FLAGS="--timestamp --options runtime --deep" \
DEVELOPMENT_TEAM="$TEAM_ID" \
ARCHS="arm64 x86_64" \
ONLY_ACTIVE_ARCH=NO \
-archivePath "$BUILDSDIR/SupportCompanion" >/dev/null 2>&1
check_exit_code "$?" "Error running xcodebuild"
cp -r $PKG_PATH "$BUILDSDIR/pkgbuild"
# move the app to the payload folder
echo "Moving SupportCompanion.app to payload folder"
if [ -d "$PKGBUILDDIR/payload/Applications/SupportCompanion.app" ]; then
rm -r "$PKGBUILDDIR/payload/Applications/SupportCompanion.app"
fi
mkdir "$PKGBUILDDIR/payload/Applications"
cp -R "${BUILDSDIR}/SupportCompanion.xcarchive/Products/Applications/SupportCompanion.app" "$PKGBUILDDIR/payload/Applications/SupportCompanion.app"
# set the plist version to AUTOMATED_SC_BUILD
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $AUTOMATED_SC_BUILD" "$PKGBUILDDIR/payload/Applications/SupportCompanion.app/Contents/Info.plist"
/usr/libexec/PlistBuddy -c "Set :CFBundleShortVersionString $AUTOMATED_SC_BUILD" "$PKGBUILDDIR/payload/Applications/SupportCompanion.app/Contents/Info.plist"
codesign --force --options runtime --deep --timestamp --sign $SIGNING_IDENTITY_APP "$PKGBUILDDIR/payload/Applications/SupportCompanion.app"
# Build and export pkg
create_pkg $BUNDLE_IDENTIFIER $AUTOMATED_SC_BUILD $PKGBUILDDIR "${BUILDSDIR}/SupportCompanion-${AUTOMATED_SC_BUILD}.pkg" "/"
generate_dist_file $BUNDLE_IDENTIFIER $AUTOMATED_SC_BUILD $DIST_FILE
run_product_build_sign $BUILDSDIR "${BUILDSDIR}/${APP_NAME}-${AUTOMATED_SC_BUILD}"
notarize_and_staple "${BUILDSDIR}/${APP_NAME}-${AUTOMATED_SC_BUILD}.pkg"
cp "${BUILDSDIR}/${APP_NAME}-${AUTOMATED_SC_BUILD}.pkg" "${RELEASEDIR}/${APP_NAME}-${AUTOMATED_SC_BUILD}.pkg"
echo "Build complete: ${RELEASEDIR}/${APP_NAME}-${AUTOMATED_SC_BUILD}.pkg"
rm -f $DIST_FILE
rm -r $PKGBUILDDIR
echo "=========== Building LaunchAgent ==========="
cp -r $LA_PATH "$BUILDSDIR/pkgbuild"
LA_VERSION="1.0.0"
create_pkg $LA_BUNDLE_IDENTIFIER $LA_VERSION $PKGBUILDDIR "${BUILDSDIR}/${APP_NAME}_LaunchAgent-${LA_VERSION}.pkg" "/Library/LaunchAgents"
generate_dist_file $LA_BUNDLE_IDENTIFIER $LA_VERSION $DIST_FILE "_LaunchAgent"
run_product_build_sign $BUILDSDIR "${BUILDSDIR}/${APP_NAME}_LaunchAgent-${LA_VERSION}"
notarize_and_staple "${BUILDSDIR}/${APP_NAME}_LaunchAgent-${LA_VERSION}.pkg"
cp "${BUILDSDIR}/${APP_NAME}_LaunchAgent-${LA_VERSION}.pkg" "${RELEASEDIR}/${APP_NAME}_LaunchAgent-${LA_VERSION}.pkg"
echo "Build complete: ${RELEASEDIR}/${APP_NAME}_LaunchAgent-${LA_VERSION}.pkg"
rm -f $DIST_FILE
rm -r $PKGBUILDDIR
echo "=========== Building suite ==========="
cp -r $PKG_PATH "$BUILDSDIR/pkgbuild"
echo "Moving SupportCompanion.app to payload folder"
if [ -d "$PKGBUILDDIR/payload/Applications/SupportCompanion.app" ]; then
rm -r "$PKGBUILDDIR/payload/Applications/SupportCompanion.app"
fi
mkdir "$PKGBUILDDIR/payload/Applications"
cp -R "${BUILDSDIR}/SupportCompanion.xcarchive/Products/Applications/SupportCompanion.app" "$PKGBUILDDIR/payload/Applications/SupportCompanion.app"
# set the plist version to AUTOMATED_SC_BUILD
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $AUTOMATED_SC_BUILD" "$PKGBUILDDIR/payload/Applications/SupportCompanion.app/Contents/Info.plist"
/usr/libexec/PlistBuddy -c "Set :CFBundleShortVersionString $AUTOMATED_SC_BUILD" "$PKGBUILDDIR/payload/Applications/SupportCompanion.app/Contents/Info.plist"
codesign --force --options runtime --deep --timestamp --sign $SIGNING_IDENTITY_APP "$PKGBUILDDIR/payload/Applications/SupportCompanion.app"
cp -r $PKG_PATH "$BUILDSDIR/pkgbuild"
mkdir "$PKGBUILDDIR/payload/Library/LaunchAgents"
cp "$LA_PATH/payload/$LA_NAME" "$PKGBUILDDIR/payload/Library/LaunchAgents/$LA_NAME"
check_exit_code "$?" "Error copying launch agent"
create_pkg $BUNDLE_IDENTIFIER $AUTOMATED_SC_BUILD $PKGBUILDDIR "${BUILDSDIR}/SupportCompanion_Suite-${AUTOMATED_SC_BUILD}.pkg" "/"
generate_dist_file $BUNDLE_IDENTIFIER $AUTOMATED_SC_BUILD $DIST_FILE "_Suite"
run_product_build_sign $BUILDSDIR "${BUILDSDIR}/${APP_NAME}_Suite-${AUTOMATED_SC_BUILD}"
notarize_and_staple "${BUILDSDIR}/${APP_NAME}_Suite-${AUTOMATED_SC_BUILD}.pkg"
cp "${BUILDSDIR}/${APP_NAME}_Suite-${AUTOMATED_SC_BUILD}.pkg" "${RELEASEDIR}/${APP_NAME}_Suite-${AUTOMATED_SC_BUILD}.pkg"
echo "Build complete: ${RELEASEDIR}/${APP_NAME}_Suite-${AUTOMATED_SC_BUILD}.pkg"
rm -f $DIST_FILE