-
Notifications
You must be signed in to change notification settings - Fork 33
/
make.sh
executable file
·685 lines (583 loc) · 23.3 KB
/
make.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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
#!/bin/bash
#
# Make the PiPer Safari extension
EXTENSION_NAME="PiPer"
SOURCE_FILES=("main.js" "fix.js" "background.js" "install.js" "localization-bridge.js" "legacy.js")
# Certifcate paths
LEAF_CERT_PATH="../certs/cert.pem"
INTERMEDIATE_CERT_PATH="../certs/apple-intermediate.pem"
ROOT_CERT_PATH="../certs/apple-root.pem"
PRIVATE_KEY_PATH="../certs/privatekey.pem"
# Display help then exit
show_help() {
cat << EOF
Usage: make.sh [options]
Options:
-h -? --help Show this screen
-t --target (all|safari|safari-legacy|chrome) Make extension for target browser [default: all]
-p --profile (release|debug|distribute) Set settings according to profile [default: debug]
-c --compress-css Compress CSS
-j --compress-js Compress JavaScript
-s --compress-svg Compress SVG
-l --logging-level <number> Set logging level (0=all 10=trace 20=info 30=warning 40=error)
-o --optimize-strings Remove unused localized strings by static program analysis
-i --development-team <id> Set development team ID
-a --archive-to-xcode Archive Safari extension to Xcode for Mac App Store distribution
-e --package-extension Package extension for distribution (safari-legacy requires private key)
-d --no-debug-js Remove JavaScript source maps to prevent debugging
-v --no-version-increment Disable automatic version incrementing
EOF
exit 0
}
arguments=("$@")
# First pass processing arguments
while :; do
case $1 in
-h|-\?|--help) show_help ;;
-p|--profile) [[ "$2" ]] && profile=$2 ;;
--profile=?*) profile=${1#*=} ;;
-l|-t|-i|--logging-level|--target|--development-team) shift ;;
-?*) ;;
*) break
esac
shift
done
# Set default settings as per profile
case $profile in
distribute)
compress_svg=1
compress_css=1
compress_js=1
debug_js=0
package_ext=1
logging_level=100
optimize_strings=1
;;
release)
compress_svg=1
compress_css=1
compress_js=1
debug_js=1
package_ext=0
logging_level=40
optimize_strings=1
;;
*)
compress_svg=0
compress_css=0
compress_js=0
debug_js=1
package_ext=0
logging_level=0
optimize_strings=0
profile="debug"
;;
esac
update_version=1
archive_xcode=0
development_team=""
targets="all"
set -- "${arguments[@]}"
# Second pass processing arguments
while :; do
case $1 in
-c|--compress-css) compress_css=1 ;;
-j|--compress-js) compress_js=1 ;;
-s|--compress-svg) compress_svg=1 ;;
-e|--package-extension) package_ext=1 ;;
-o|--optimize-localizations) optimize_strings=1 ;;
-d|--no-debug-js) debug_js=0 ;;
-v|--no-version-increment) update_version=0 ;;
-t|--target) [[ "$2" ]] && targets=$2 && shift ;;
--target=?*) targets=${1#*=} ;;
-l|--logging-level) [[ "$2" ]] && logging_level=$2 && shift ;;
--logging-level=?*) logging_level=${1#*=} ;;
-i|--development-team) [[ "$2" ]] && development_team=$2 && shift ;;
--development-team=?*) development_team=${1#*=} ;;
-a|--archive-to-xcode) archive_xcode=1 ;;
-p|--profile) shift ;;
-?*) ;;
*) break ;;
esac
shift
done
# Highlight selected build profile
echo "Setting '${profile}' profile"
# Validate targets
case $targets in
safari) targets=("safari") ;;
safari-legacy) targets=("safari-legacy") ;;
chrome) targets=("chrome") ;;
*) targets=("safari" "safari-legacy" "chrome")
esac
# Validate logging level
logging_level="${logging_level//[!0-9]/}"
[[ -z "$logging_level" ]] && logging_level=0
# Helper checks for build tool dependency and falls back to 'npx' if possible
function get_node_command() {
if type "$1" &>/dev/null; then
echo "$1"
elif type "npx" &>/dev/null; then
npx_package=$([[ -z "$2" ]] && echo "$1" || echo "$2")
echo "npx --quiet --package ${npx_package} $1"
echo "Info: '$1' command not found therefore falling back to 'npx'; performance may suffer (avoid this by installing package with 'npm install ${npx_package} -g')" >&2
else
echo "Error: '$1' command not found and neither fallback 'npx'" >&2
echo "Please install the latest version of Node.js (see https://nodejs.org/en/download/package-manager/)" >&2
return 1
fi
return 0
}
# Target specific build checks
for i in "${!targets[@]}"; do
if [[ "${targets[$i]}" = "safari" ]]; then
# Only build 'safari' extension target when running under macOS
if [[ "$(uname)" != "Darwin" ]]; then
echo "Warning: Building 'safari' extension skipped as requires macOS" >&2
unset "targets[$i]"
continue
fi
# Ensure with have Xcode command line tools installed
if [[ -z $(xcode-select --print-path) ]]; then
echo "Installing Xcode Command Line Tools (expect a GUI popup)"
xcode-select --install &>/dev/null
echo "Press any key after installation has completed"
read -rsn1
if [[ -z $(xcode-select --print-path) ]]; then
echo "Unable to find Xcode Command Line Tools"
exit 1
fi
fi
elif [[ "${targets[$i]}" = "safari-legacy" ]]; then
# Get 'safari-legacy' specific build tool path and exit if not found
[[ "${package_ext}" -eq 1 ]] && { XARJS_PATH=$(get_node_command "xarjs" "xar-js") || exit 1; }
fi
done
# Check for google closure compiler requirements and exit if not found
CCJS_PATH=$(get_node_command "google-closure-compiler") || exit 1;
if ${CCJS_PATH} --platform native --version &>/dev/null; then
CCJS_PATH="${CCJS_PATH} --platform native";
elif ${CCJS_PATH} --platform java --version &>/dev/null; then
CCJS_PATH="${CCJS_PATH} --platform java";
else
echo "Error: Java runtime required by 'google-closure-compiler' not found" >&2
echo "Please install the latest version of Java (see https://www.java.com/en/download/)" >&2
exit 1
fi
# Check for csso and exit if not found
[[ "${compress_css}" -eq 1 ]] && { CSSO_PATH=$(get_node_command "csso" "csso-cli") || exit 1; }
# Check for svgo and exit if not found
[[ "${compress_svg}" -eq 1 ]] && { SVGO_PATH=$(get_node_command "svgo") || exit 1; }
# Check for git and exit if not found
if [[ "${update_version}" -eq 1 ]] && { ! type "git" &>/dev/null; }; then
echo "Error: 'git' command not found" >&2
echo "Please install the latest version of git (see https://git-scm.com/book/en/v2/Getting-Started-Installing-Git)" >&2
exit 1
else
GIT_PATH=$(sh /etc/profile; which git)
fi
#
# Build script
#
# Set working directory to project root
cd "${BASH_SOURCE[0]%/*}"
# Remove output folder
rm -rf out
# Make common output folder
mkdir -p "out/${EXTENSION_NAME}"
# Copy common items into common output folder
cp -r "src/common"/* "out/${EXTENSION_NAME}/"
# Compress all supported images with SVGO
if [[ "${compress_svg}" -eq 1 ]]; then
${SVGO_PATH} -q -f "out/${EXTENSION_NAME}/images"
fi
# Get current version from git if automatic versioning enabled
if [[ "${update_version}" -eq 1 ]]; then
# Check we're inside a git work tree
inside_git_repo="$(git rev-parse --is-inside-work-tree 2>/dev/null)"
if [[ "${inside_git_repo}" ]]; then
# Get number of commits and release version from most recent tag
number_of_commits=$(($("${GIT_PATH}" rev-list HEAD --count) + 1))
git_release_version=$("${GIT_PATH}" describe --tags --always --abbrev=0)
git_release_version=${git_release_version%%-*};
git_release_version=${git_release_version#*v};
# Otherwise issue warning and set blank version
else
echo "Warning: Unable to set version automatically as cannot find 'git' repository (ensure repository has been cloned to fix this)" >&2
number_of_commits="0"
git_release_version="0.0.0"
fi
# Helper performs multiline sed regular expression
function multiline_sed_regex() {
mv "$1" "$1.bak"
echo -n "$(cat "$1.bak")" | tr "\n" "\f" | sed -E "$2" | tr "\f" "\n" > "$1"
rm -rf "$1.bak"
}
fi
# Make resources index file
import_list=""
resource_list=""
alias_list=""
resource_count=0
for path in "out/${EXTENSION_NAME}/scripts/resources"/*.js; do
path="${path##*/}"
[[ $path == "index.js" ]] && continue
resource_count=$((resource_count+1))
import_list="${import_list}"$'\n'"import * as r${resource_count} from \"./${path}\";"
resource=$(<"out/${EXTENSION_NAME}/scripts/resources/${path}")
regex_arr="(^|[ "$'\n'"])(const|let|var)[ "$'\n'"]+domain[ "$'\n'"]*=[ "$'\n'"]*\[([^]]+)\]"
regex_val="(^|[ "$'\n'"])(const|let|var)[ "$'\n'"]+domain[ "$'\n'"]*="
if [[ "$resource" =~ $regex_arr ]]; then
IFS=", " read -a arr <<< "${BASH_REMATCH[3]}"
resource_list="${resource_list}"$'\n'"resources[${arr[0]}] = r${resource_count}.resource;"
for ((i=1;i<${#arr[@]};i++)); do
alias_list="${alias_list}"$'\n'"resources[${arr[$i]}] = resources[${arr[0]}];"
done
elif [[ "$resource" =~ $regex_val ]]; then
resource_list="${resource_list}"$'\n'"resources[r${resource_count}.domain] = r${resource_count}.resource;"
else
echo "Warning: No domain's listed for resource '${path}'" >&2
fi
done
{
cat <<EOF
/** Auto-generated file **/
${import_list}
export const resources = {};
${resource_list}
${alias_list}
EOF
} >"out/${EXTENSION_NAME}/scripts/resources/index.js"
for target in "${targets[@]}"; do
echo "Building '${target}' extension"
# Set target specific flags
case $target in
safari)
browser=1
target_extension=""
common_file_path="/Extension/Resources"
;;
safari-legacy)
browser=1
target_extension=".safariextension"
common_file_path=""
;;
chrome)
browser=2
target_extension=""
common_file_path=""
;;
*) exit 1
esac
# Make target folder
mkdir -p "out/${EXTENSION_NAME}-${target}${target_extension}${common_file_path}"
# Copy items from common output folder to target folder
cp -r "out/${EXTENSION_NAME}"/* "out/${EXTENSION_NAME}-${target}${target_extension}${common_file_path}/"
# Copy target specific items to target output folder
cp -r "src/${target}"/* "out/${EXTENSION_NAME}-${target}${target_extension}/" 2>/dev/null
# Compress all inline CSS with CSSO
if [[ "${compress_css}" -eq 1 ]]; then
function minify_css() {
echo "$@" | sed -e 's/\\"/"/g' -e 's/\\\$/$/g' | ${CSSO_PATH} --declaration-list
}
export -f minify_css
export CSSO_PATH
for path in "out/${EXTENSION_NAME}-${target}${target_extension}${common_file_path}/scripts"/{*,**/*}.js; do
[[ ! -f "${path}" ]] && continue
source=$(cat "${path}")
echo "echo \"$(sed -e 's/\\/\\\\/g' -e 's/\$/\\$/g' -e 's/`/\\`/g' -e 's/\"/\\\"/g' -e 's/\\n/\\\\n/g' <<< "$source" \
| tr '\n' '\f' \
| sed -E 's/\/\*\*[[:space:]]+CSS[[:space:]]+\*\/[[:space:]]*\([[:space:]]*\\`([^`]*)\\`[[:space:]]*\)/\\`\$(minify_css '\''\1'\'')\\`/g' \
| tr '\f' '\n')\"" \
| sh > "${path}"
done
fi
# Use closure compiler to compress javascript
function remove_element() {
for i in "${!files[@]}"; do
if [[ ${files[$i]} = "$1" ]]; then
unset "files[$i]"
fi
done
}
function add_element() {
remove_element "$1"
files+=("$1")
}
function get_absolute_path() {
local dirname="${1%/*}"
local basename="${1##*/}"
echo "$(cd "$dirname" 2>/dev/null; pwd)/$basename"
}
# Convert absolute paths to platform native path on Windows
function fix_absolute_path() {
case "$(uname -s)" in
CYGWIN*|MINGW32*|MSYS*)
echo "$(cygpath -wa ${1})"
;;
*) echo "$1"
esac
}
function process_file() {
local dirname="${1%/*}"
local imports=()
if [[ ! -f "$1" ]]; then
remove_element "$1"
return
fi
local source=$(<"$1")
regex="(^| |"$'\n'")(import|export)["$'\n'" ]+(([*a-zA-Z0-9_,{}"$'\n'" $]+)from["$'\n'" ]+)?['\"]([^'\"]+)['\"][ "$'\n'";]"
while true; do
if [[ "$source" =~ $regex ]]; then
source="${source##*${BASH_REMATCH[0]}}"
imports+=("${BASH_REMATCH[5]}")
else
break
fi
done
for i in "${!imports[@]}"; do
imports[$i]=$(cd "$dirname"; get_absolute_path "${imports[$i]}")
add_element "${imports[$i]}"
done
for i in "${!imports[@]}"; do
process_file "${imports[$i]}"
done
}
scripts_path=$(get_absolute_path "out/${EXTENSION_NAME}-${target}${target_extension}${common_file_path}/scripts")
defines_path="${scripts_path}/defines.js"
extern_path=$(fix_absolute_path "${scripts_path}/externs.js")
defines_processed_path=$(echo "${defines_path%.*}" | sed -E 's|[/@\]|$|g' | sed -E 's/[-. ]/_/g' | sed -e 's/\[/%5B/g' -e 's/]/%5D/g' -e 's/>/%3E/g' -e 's/</%3C/g')
browser_flag="BROWSER$\$module${defines_processed_path}=${browser}"
logging_flag="LOGGING_LEVEL$\$module${defines_processed_path}=${logging_level}"
if [[ "$optimize_strings" -eq 1 ]]; then
localization_path="${scripts_path}/localization.js"
localization_source=$(<"$localization_path")
fi
for entry in "${SOURCE_FILES[@]}"; do
files=()
absolute_entry="${scripts_path}/${entry}"
[[ ! -f "$absolute_entry" ]] && continue
add_element "$absolute_entry"
process_file "$absolute_entry"
# Statically analyze javascript and remove unused localized strings
if [[ "$optimize_strings" -eq 1 ]]; then
locale_keys=()
regex="(=[ \t"$'\n'"]*localizedString(WithReplacements)?[ \t"$'\n'";,])|(localizedString(WithReplacements)?\([ \t"$'\n'"]*(\"([^\"]+)\"|'([^']+)'|([^'\",)]+))[ \t"$'\n'"]*[,)])"
dynamic_access=0
for path in "${files[@]}"; do
[[ "$path" == "$localization_path" ]] && continue
source=$(<"$path")
while true; do
if [[ "$source" =~ $regex ]]; then
source="${source##*${BASH_REMATCH[0]}}"
locale_key="${BASH_REMATCH[6]:-${BASH_REMATCH[7]}}"
if [[ ! -z "$locale_key" ]]; then
locale_keys+=("$locale_key")
else
dynamic_access=1
break
fi
else
break
fi
done
done
source="$localization_source"
if [[ "$dynamic_access" -eq 0 ]]; then
processing="$localization_source"
regex="localizations\[[ \t"$'\n'"]*('([^']+)'|\"([^\"]+)\")[ \t"$'\n'"]*\][^=]+=[ "$'\n'"]*{([^}'\"]*('([^'\\]|\\.)*'|\"([^\"\\]|\\.)*\")?)*}[ \t"$'\n'"]*;?"
while true; do
if [[ "$processing" =~ $regex ]]; then
processing=${processing##*"${BASH_REMATCH[0]}"}
locale_key="${BASH_REMATCH[2]:-${BASH_REMATCH[3]}}"
found=0
for key in "${locale_keys[@]}"; do
if [[ "$key" == "$locale_key" ]]; then
found=1
break
fi
done
if [[ "$found" -eq 0 ]]; then
source=${source/"${BASH_REMATCH[0]}"/}
fi
else
break
fi
done
fi
echo "$source" > "${localization_path}"
fi
absolute_entry=$(fix_absolute_path "$absolute_entry")
defines=()
js_code=()
for path in "${files[@]}"; do
path=$(fix_absolute_path "$path")
js_code=("--js" "$path" "${js_code[@]}")
if [[ "$path" = "$defines_path" ]]; then
defines=(
"--define" "$logging_flag"
"--define" "$browser_flag"
)
fi
done
if [[ "$debug_js" -eq 0 ]]; then
source_map_options=()
else
source_map_options=(
--create_source_map "${absolute_entry}.map"
--source_map_location_mapping "$scripts_path|."
--source_map_include_content
)
fi
if [[ "$compress_js" -eq 0 ]]; then
compression_options=(
--compilation_level WHITESPACE_ONLY \
--js_module_root "$scripts_path" \
--formatting PRETTY_PRINT \
--formatting PRINT_INPUT_DELIMITER \
)
else
compression_options=(
--compilation_level ADVANCED \
--use_types_for_optimization \
--assume_function_wrapper \
--jscomp_error strictCheckTypes \
--jscomp_error strictMissingProperties \
--jscomp_error checkTypes \
--jscomp_error checkVars \
--jscomp_error reportUnknownTypes \
--externs "$extern_path" \
"${defines[@]}" \
)
fi
${CCJS_PATH} \
"${compression_options[@]}" \
--warning_level VERBOSE \
--language_out ECMASCRIPT_2017 \
--output_wrapper "var a;a||(a=!0,(()=>{%output%})());" \
"${source_map_options[@]}" \
"${js_code[@]}" \
> "${absolute_entry%.*}.cjs"
done
# Remove uncompiled JavaScript
rm -f "${scripts_path}/"{*,**/*}.js
# Remove any empty folders
for path in "${scripts_path}/"{*,**/*}; do
if [[ -d "$path" ]] && [[ ! -f "$path"/* ]]; then
rm -rf "$path"
fi
done
# Restore '.js' extension for compiled JavaScript
for entry in "${SOURCE_FILES[@]}"; do
entry="${scripts_path}/"${entry%.*}
[ ! -f "${entry}.cjs" ] && continue
mv "${entry}.cjs" "${entry}.js"
done
# Embed source maps and remove map files
if [[ "$debug_js" -eq 1 ]]; then
for entry in "${SOURCE_FILES[@]}"; do
entry="${scripts_path}/${entry}"
[[ ! -f "${entry}" ]] && continue
source_map=$(base64 "${entry}.map" | tr -d \\n)
echo "//# sourceMappingURL=data:application/json;base64,${source_map}" >> "${entry}"
rm -f "${entry}.map"
done
fi
# Safari specific build steps
if [[ "${target}" == "safari" ]]; then
# Update version info from git
if [[ "${update_version}" -eq 1 ]]; then
multiline_sed_regex "out/${EXTENSION_NAME}-${target}/Extension/Info.plist" "s|(> *CFBundleShortVersionString *</key>[^>]+>)[^<]+|\1${git_release_version}|g"
multiline_sed_regex "out/${EXTENSION_NAME}-${target}/Extension/Info.plist" "s|(> *CFBundleVersion *</key>[^>]+>)[^<]+|\1${number_of_commits}|g"
multiline_sed_regex "out/${EXTENSION_NAME}-${target}/App/Info.plist" "s|(> *CFBundleShortVersionString *</key>[^>]+>)[^<]+|\1${git_release_version}|g"
multiline_sed_regex "out/${EXTENSION_NAME}-${target}/App/Info.plist" "s|(> *CFBundleVersion *</key>[^>]+>)[^<]+|\1${number_of_commits}|g"
fi
# Get development team id automatically if needed
if [[ -z "${development_team}" ]]; then
# Helper maintains unique list of ids
team_ids=()
function add_unique_team_ids() {
for i in "${!team_ids[@]}"; do
[[ ${team_ids[$i]} = "$1" ]] && return
done
team_ids+=("$1")
}
# Search mobileprovision files for team identifiers
regex="TeamIdentifier<\/key>[^\/]+>([A-Z0-9]{10})<\/"
for path in "${HOME}/Library/MobileDevice/Provisioning Profiles"/*.mobileprovision; do
source=$(cat "${path}" | iconv -f "ISO-8859-1" -t "UTF-8")
if [[ "${source}" =~ $regex ]]; then
add_unique_team_ids "${BASH_REMATCH[1]}"
fi
done
# If multiple or no identifiers found then prompt the user
development_team_hint="(avoid this message in future by specifying --development-team)"
if (( ${#team_ids[@]} == 0 )); then
echo "Unable to find development team automatically, please enter below: ${development_team_hint}"
read development_team </dev/tty
elif (( ${#team_ids[@]} == 1 )); then
development_team="${team_ids[0]}"
else
echo "Multiple development team's found"
for (( i=0; i < ${#team_ids[@]}; i++ )); do
echo " [${i}]: ${team_ids[$i]}"
done
echo "Please select development team to use: ${development_team_hint}"
read selection </dev/tty
development_team="${team_ids[$selection]}"
fi
fi
# Build the xcode project
config_profile=$([[ "${profile}" == "debug" ]] && echo "Debug" || echo "Release")
xcodebuild -allowProvisioningUpdates -allowProvisioningDeviceRegistration -quiet -project "./out/${EXTENSION_NAME}-${target}/PiPer.xcodeproj" -scheme "PiPer" archive -archivePath "./out/${EXTENSION_NAME}-${target}.xcarchive" -configuration "${config_profile}" CODE_SIGN_STYLE="Automatic" CODE_SIGN_IDENTITY="Mac Developer" DEVELOPMENT_TEAM="${development_team}"
xcodebuild -allowProvisioningUpdates -allowProvisioningDeviceRegistration -quiet -exportArchive -archivePath "./out/${EXTENSION_NAME}-${target}.xcarchive" -exportPath "./out/" -exportOptionsPlist "./out/${EXTENSION_NAME}-${target}/exportOptions.plist" CODE_SIGN_STYLE="Automatic" CODE_SIGN_IDENTITY="Mac Developer" DEVELOPMENT_TEAM="${development_team}" &>/dev/null
# Copy archive to Xcode if needed
if [[ "${archive_xcode}" -eq 1 ]]; then
archive_time=$(date '+%d-%m-%Y, %H.%M')
mv "./out/${EXTENSION_NAME}-${target}.xcarchive" "${HOME}/Library/Developer/Xcode/Archives/${EXTENSION_NAME} ${archive_time}.xcarchive"
fi
# Package extension
if [[ "${package_ext}" -eq 1 ]]; then
productbuild --quiet --component "./out/PiPer.app" "/Applications" "./out/${EXTENSION_NAME}-${target}.pkg"
rm -rf "./out/PiPer.app"
else
mv "out/PiPer.app" "out/${EXTENSION_NAME}-${target}.app"
fi
# Remove everything else
rm -rf "out/${EXTENSION_NAME}-${target}.xcarchive"
rm -rf "out/${EXTENSION_NAME}-${target}"
elif [[ "${target}" == "safari-legacy" ]]; then
# Remove irrelevant target file
rm -f "out/${EXTENSION_NAME}-${target}${target_extension}/update.plist"
# Update version info from git
if [[ "${update_version}" -eq 1 ]]; then
info_plist="out/${EXTENSION_NAME}-${target}${target_extension}/Info.plist"
update_plist="src/${target}/update.plist"
multiline_sed_regex "${info_plist}" "s|(> *CFBundleShortVersionString *</key>[^>]+>)[^<]+|\1${git_release_version}|g"
multiline_sed_regex "${info_plist}" "s|(> *CFBundleVersion *</key>[^>]+>)[^<]+|\1${number_of_commits}|g"
multiline_sed_regex "${update_plist}" "s|(> *CFBundleShortVersionString *</key>[^>]+>)[^<]+|\1${git_release_version}|g"
multiline_sed_regex "${update_plist}" "s|(> *CFBundleVersion *</key>[^>]+>)[^<]+|\1${number_of_commits}|g"
fi
# Package safari extension
if [[ "${package_ext}" -eq 1 ]] && [[ -f "out/${PRIVATE_KEY_PATH}" ]]; then
(cd out && ${XARJS_PATH} create "${EXTENSION_NAME}-${target}.safariextz" --cert "${LEAF_CERT_PATH}" --cert "${INTERMEDIATE_CERT_PATH}" --cert "${ROOT_CERT_PATH}" --private-key "${PRIVATE_KEY_PATH}" "${EXTENSION_NAME}-${target}${target_extension}")
rm -rf "out/${EXTENSION_NAME}-${target}${target_extension}"
fi
elif [[ "${target}" == "chrome" ]]; then
# Update manifest version information
if [[ "${update_version}" -eq 1 ]]; then
sed -i.bak -E "s|\"version\": *\"[^\"]+\"|\"version\": \"${git_release_version}.${number_of_commits}\"|g" "out/${EXTENSION_NAME}-${target}/manifest.json"
sed -i.bak -E "s|\"version_name\": *\"[^\"]+\"|\"version_name\": \"${git_release_version}\"|g" "out/${EXTENSION_NAME}-${target}/manifest.json"
rm -rf "out/${EXTENSION_NAME}-${target}/manifest.json.bak"
fi
# Package chrome extension
if [[ "${package_ext}" -eq 1 ]]; then
(cd "out/${EXTENSION_NAME}-${target}${target_extension}" && zip -rq "${EXTENSION_NAME}-${target}${target_extension}.zip" *)
mv "out/${EXTENSION_NAME}-${target}${target_extension}/${EXTENSION_NAME}-${target}${target_extension}.zip" "out/"
rm -rf "out/${EXTENSION_NAME}-${target}${target_extension}"
fi
fi
done
# Clean common output folder
rm -rf "out/${EXTENSION_NAME}"
echo "Done."