|
| 1 | +#!/bin/zsh |
| 2 | + |
| 3 | +# pkgAndNotarize.sh |
| 4 | + |
| 5 | +# this script will |
| 6 | +# - build the swift package project executable |
| 7 | +# - sign the binary |
| 8 | +# - create a signed pkg installer file |
| 9 | +# - submit the pkg for notarization |
| 10 | +# - staple the pkg |
| 11 | + |
| 12 | +# more detail here: |
| 13 | +# https://scriptingosx.com/2023/08/build-a-notarized-package-with-a-swift-package-manager-executable/ |
| 14 | + |
| 15 | +# by Armin Briegel - Scripting OS X |
| 16 | + |
| 17 | +# Permission is granted to use this code in any way you want. |
| 18 | +# Credit would be nice, but not obligatory. |
| 19 | +# Provided "as is", without warranty of any kind, express or implied. |
| 20 | + |
| 21 | + |
| 22 | +# modify these variables for your project |
| 23 | + |
| 24 | +# Developer ID Installer cert name |
| 25 | +developer_name_and_id="Armin Briegel (JME5BW3F3R)" |
| 26 | +installer_sign_cert="Developer ID Installer: ${developer_name_and_id}" |
| 27 | +application_sign_cert="Developer ID Application: ${developer_name_and_id}" |
| 28 | + |
| 29 | +# profile name used with `notarytool --store-credentials` |
| 30 | +credential_profile="notary-scriptingosx" |
| 31 | + |
| 32 | +# build info |
| 33 | +product_name="swift-prefs-tools" |
| 34 | +binary_names=( "prefs" "plist2profile" ) |
| 35 | + |
| 36 | +# pkg info |
| 37 | +pkg_name="$product_name" |
| 38 | +identifier="com.scriptingosx.${product_name}" |
| 39 | +min_os_version="13.5" |
| 40 | +install_location="/usr/local/bin/" |
| 41 | + |
| 42 | + |
| 43 | +# don't modify below here |
| 44 | + |
| 45 | + |
| 46 | +# calculated variables |
| 47 | +SRCROOT=$(dirname ${0:A}) |
| 48 | +build_dir="$SRCROOT/.build" |
| 49 | + |
| 50 | +date +"%F %T" |
| 51 | + |
| 52 | +# build the binary |
| 53 | + |
| 54 | +#swift package clean |
| 55 | +echo |
| 56 | +echo "### building $product_name" |
| 57 | +if ! swift build --configuration release \ |
| 58 | + --arch arm64 --arch x86_64 |
| 59 | +then |
| 60 | + echo "error building binary" |
| 61 | + exit 2 |
| 62 | +fi |
| 63 | + |
| 64 | +if [[ ! -d $build_dir ]]; then |
| 65 | + echo "couldn't find .build directory" |
| 66 | + exit 3 |
| 67 | +fi |
| 68 | + |
| 69 | + |
| 70 | +binary_source_path="${build_dir}/apple/Products/Release/${binary_names[1]}" |
| 71 | + |
| 72 | +if [[ ! -e $binary_source_path ]]; then |
| 73 | + echo "cannot find binary at $binary_source_path" |
| 74 | + exit 4 |
| 75 | +fi |
| 76 | + |
| 77 | +# get version from binary |
| 78 | +version=$($binary_source_path --version) |
| 79 | + |
| 80 | +if [[ $version == "" ]]; then |
| 81 | + echo "could not get version" |
| 82 | + exit 5 |
| 83 | +fi |
| 84 | + |
| 85 | +component_path="${build_dir}/${pkg_name}.pkg" |
| 86 | +product_path="${build_dir}/${pkg_name}-${version}.pkg" |
| 87 | +pkgroot="${build_dir}/pkgroot" |
| 88 | + |
| 89 | +echo |
| 90 | +echo "### Signing, Packaging and Notarizing '$product_name'" |
| 91 | +echo "Version: $version" |
| 92 | +echo "Identifier: $identifier" |
| 93 | +echo "Min OS Version: $min_os_version" |
| 94 | +echo "Developer ID: $developer_name_and_id" |
| 95 | + |
| 96 | +pkgroot="$build_dir/pkgroot" |
| 97 | +if [[ ! -d $pkgroot ]]; then |
| 98 | + mkdir -p $pkgroot |
| 99 | +fi |
| 100 | + |
| 101 | +for binary in ${binary_names}; do |
| 102 | + binary_source_path="${build_dir}/apple/Products/Release/${binary}" |
| 103 | + |
| 104 | + if [[ ! -f $binary_source_path ]]; then |
| 105 | + echo "can't find binary at $binary_path" |
| 106 | + exit 6 |
| 107 | + fi |
| 108 | + |
| 109 | + cp $binary_source_path $pkgroot |
| 110 | + #scripts_dir="$SRCROOT/scripts" |
| 111 | + |
| 112 | + binary_path=$pkgroot/$binary |
| 113 | + |
| 114 | + # sign the binary |
| 115 | + echo |
| 116 | + echo "### signing '${binary}'" |
| 117 | + if ! codesign --sign $application_sign_cert \ |
| 118 | + --options runtime \ |
| 119 | + --runtime-version $min_os_version \ |
| 120 | + --timestamp \ |
| 121 | + $binary_path |
| 122 | + then |
| 123 | + echo "error signing binary '${binary}'" |
| 124 | + exit 7 |
| 125 | + fi |
| 126 | + |
| 127 | +done |
| 128 | + |
| 129 | +# create the component pkg |
| 130 | +echo |
| 131 | +echo "### building component pkg file" |
| 132 | + |
| 133 | +if ! pkgbuild --root $pkgroot \ |
| 134 | + --identifier $identifier \ |
| 135 | + --version $version-$build_number \ |
| 136 | + --install-location $install_location \ |
| 137 | + --min-os-version $min_os_version \ |
| 138 | + --compression latest \ |
| 139 | + $component_path |
| 140 | + |
| 141 | +# --scripts "$scripts_dir" \ |
| 142 | +then |
| 143 | + echo "error building component" |
| 144 | + exit 8 |
| 145 | +fi |
| 146 | + |
| 147 | +# create the distribution pkg |
| 148 | +echo |
| 149 | +echo "### building distribution pkg file" |
| 150 | + |
| 151 | +if ! productbuild --package "$component_path" \ |
| 152 | + --identifier "$identifier" \ |
| 153 | + --version "$version-$build_number" \ |
| 154 | + --sign "$installer_sign_cert" \ |
| 155 | + "$product_path" |
| 156 | +then |
| 157 | + echo "error building distribution archive" |
| 158 | + exit 9 |
| 159 | +fi |
| 160 | + |
| 161 | +# notarize |
| 162 | +echo |
| 163 | +echo "### submitting for notarization" |
| 164 | +if ! xcrun notarytool submit "$product_path" \ |
| 165 | + --keychain-profile "$credential_profile" \ |
| 166 | + --wait |
| 167 | +then |
| 168 | + echo "error notarizing pkg" |
| 169 | + echo "use 'xcrun notarylog <submission-id> --keychain-profile \"$credential_profile\"' for more detail" |
| 170 | + exit 10 |
| 171 | +fi |
| 172 | + |
| 173 | +# staple |
| 174 | +echo |
| 175 | +echo "### staple" |
| 176 | +if ! xcrun stapler staple "$product_path" |
| 177 | +then |
| 178 | + echo "error stapling pkg" |
| 179 | + exit 11 |
| 180 | +fi |
| 181 | + |
| 182 | +# clean up component pkg |
| 183 | +rm "$component_path" |
| 184 | + |
| 185 | +# clean up pkgroot |
| 186 | +rm -rf $pkgroot |
| 187 | + |
| 188 | +echo |
| 189 | +# show result path |
| 190 | +echo "### complete" |
| 191 | +echo "$product_path" |
| 192 | + |
| 193 | +exit 0 |
0 commit comments