|
| 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="quickpkg" |
| 34 | +binary_names=( "quickpkg" ) |
| 35 | + |
| 36 | +# pkg info |
| 37 | +pkg_name="$product_name" |
| 38 | +identifier="com.scriptingosx.${product_name}" |
| 39 | +min_os_version="15.0" |
| 40 | +install_location="/" |
| 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 | +binary_source_path="${build_dir}/apple/Products/Release/${binary_names[1]}" |
| 70 | + |
| 71 | +if [[ ! -e $binary_source_path ]]; then |
| 72 | + echo "cannot find binary at $binary_source_path" |
| 73 | + exit 4 |
| 74 | +fi |
| 75 | + |
| 76 | +# get version from binary |
| 77 | +version=$($binary_source_path --version) |
| 78 | + |
| 79 | +if [[ $version == "" ]]; then |
| 80 | + echo "could not get version" |
| 81 | + exit 5 |
| 82 | +fi |
| 83 | + |
| 84 | +# generate man page |
| 85 | +if ! swift package plugin generate-manual; then |
| 86 | + echo "error generating man page" |
| 87 | + exit 11 |
| 88 | +fi |
| 89 | + |
| 90 | +manpage_source_path="${build_dir}/plugins/GenerateManual/outputs/${binary_names[1]}/${binary_names[1]}.1" |
| 91 | + |
| 92 | +if [[ ! -e $manpage_source_path ]]; then |
| 93 | + echo "cannot find manpage at $manpage_source_path" |
| 94 | + exit 11 |
| 95 | +fi |
| 96 | + |
| 97 | +component_path="${build_dir}/${pkg_name}.pkg" |
| 98 | +product_path="${build_dir}/${pkg_name}-${version}.pkg" |
| 99 | +pkgroot="${build_dir}/pkgroot" |
| 100 | + |
| 101 | +binary_location="${pkgroot}/usr/local/bin/" |
| 102 | +manpage_location="${pkgroot}/usr/local/share/man/man1/" |
| 103 | + |
| 104 | +echo |
| 105 | +echo "### Signing, Packaging and Notarizing '$product_name'" |
| 106 | +echo "Version: $version" |
| 107 | +echo "Identifier: $identifier" |
| 108 | +echo "Min OS Version: $min_os_version" |
| 109 | +echo "Developer ID: $developer_name_and_id" |
| 110 | + |
| 111 | +pkgroot="$build_dir/pkgroot" |
| 112 | +if [[ ! -d $pkgroot ]]; then |
| 113 | + mkdir -p $pkgroot |
| 114 | +fi |
| 115 | + |
| 116 | +mkdir -p $binary_location |
| 117 | +mkdir -p $manpage_location |
| 118 | + |
| 119 | +# copy and sign the binaries |
| 120 | + |
| 121 | +for binary in ${binary_names}; do |
| 122 | + binary_source_path="${build_dir}/apple/Products/Release/${binary}" |
| 123 | + |
| 124 | + if [[ ! -f $binary_source_path ]]; then |
| 125 | + echo "can't find binary at $binary_source_path" |
| 126 | + exit 6 |
| 127 | + fi |
| 128 | + |
| 129 | + cp $binary_source_path $binary_location |
| 130 | + |
| 131 | + binary_path=${binary_location}/${binary} |
| 132 | + |
| 133 | + # sign the binary |
| 134 | + echo |
| 135 | + echo "### signing '${binary}'" |
| 136 | + if ! codesign --sign $application_sign_cert \ |
| 137 | + --options runtime \ |
| 138 | + --runtime-version $min_os_version \ |
| 139 | + --timestamp \ |
| 140 | + $binary_path |
| 141 | + then |
| 142 | + echo "error signing binary '${binary}'" |
| 143 | + exit 7 |
| 144 | + fi |
| 145 | + |
| 146 | +done |
| 147 | + |
| 148 | +# copy the manpage |
| 149 | +echo |
| 150 | +echo "copying man page" |
| 151 | +cp $manpage_source_path $manpage_location |
| 152 | + |
| 153 | +# create the component pkg |
| 154 | +echo |
| 155 | +echo "### building component pkg file" |
| 156 | + |
| 157 | +if ! pkgbuild --root $pkgroot \ |
| 158 | + --identifier $identifier \ |
| 159 | + --version $version-$build_number \ |
| 160 | + --install-location $install_location \ |
| 161 | + --min-os-version $min_os_version \ |
| 162 | + --compression latest \ |
| 163 | + $component_path |
| 164 | + |
| 165 | +# --scripts "$scripts_dir" \ |
| 166 | +then |
| 167 | + echo "error building component" |
| 168 | + exit 8 |
| 169 | +fi |
| 170 | + |
| 171 | +# create the distribution pkg |
| 172 | +echo |
| 173 | +echo "### building distribution pkg file" |
| 174 | + |
| 175 | +if ! productbuild --package "$component_path" \ |
| 176 | + --identifier "$identifier" \ |
| 177 | + --version "$version-$build_number" \ |
| 178 | + --sign "$installer_sign_cert" \ |
| 179 | + "$product_path" |
| 180 | +then |
| 181 | + echo "error building distribution archive" |
| 182 | + exit 9 |
| 183 | +fi |
| 184 | + |
| 185 | +# notarize |
| 186 | +echo |
| 187 | +echo "### submitting for notarization" |
| 188 | +if ! xcrun notarytool submit "$product_path" \ |
| 189 | + --keychain-profile "$credential_profile" \ |
| 190 | + --wait |
| 191 | +then |
| 192 | + echo "error notarizing pkg" |
| 193 | + echo "use 'xcrun notarylog <submission-id> --keychain-profile \"$credential_profile\"' for more detail" |
| 194 | + exit 10 |
| 195 | +fi |
| 196 | + |
| 197 | +# staple |
| 198 | +echo |
| 199 | +echo "### staple" |
| 200 | +if ! xcrun stapler staple "$product_path" |
| 201 | +then |
| 202 | + echo "error stapling pkg" |
| 203 | + exit 11 |
| 204 | +fi |
| 205 | + |
| 206 | +# clean up component pkg |
| 207 | +rm "$component_path" |
| 208 | + |
| 209 | +# clean up pkgroot |
| 210 | +rm -rf $pkgroot |
| 211 | + |
| 212 | +echo |
| 213 | +# show result path |
| 214 | +echo "### complete" |
| 215 | +echo "$product_path" |
| 216 | + |
| 217 | +exit 0 |
0 commit comments