bundle

  1#!/bin/bash
  2
  3set -e
  4
  5build_flag="--release"
  6target_dir="release"
  7open_result=false
  8local_arch=false
  9local_only=false
 10overwrite_local_app=false
 11bundle_name=""
 12zed_crate="zed"
 13binary_name="Zed"
 14
 15# This must match the team in the provisioning profile.
 16APPLE_NOTORIZATION_TEAM="MQ55VZLNZQ"
 17
 18# Function for displaying help info
 19help_info() {
 20  echo "
 21Usage: ${0##*/} [options] [bundle_name]
 22Build the application bundle.
 23
 24Options:
 25  -d    Compile in debug mode
 26  -l    Compile for local architecture and copy bundle to /Applications, implies -d.
 27  -o    Open the resulting DMG or the app itself in local mode.
 28  -f    Overwrite the local app bundle if it exists.
 29  -h    Display this help and exit.
 30  "
 31}
 32
 33function uploadDsym
 34{
 35  SPACE="zed-debug-symbols"
 36  REGION="nyc3"
 37  file_to_upload="$1"
 38  file_name="$2"
 39  date=$(date +"%a, %d %b %Y %T %z")
 40  acl="x-amz-acl:public-read"
 41  content_type="application/octet-stream"
 42  storage_type="x-amz-storage-class:STANDARD"
 43  string="PUT\n\n${content_type}\n${date}\n${acl}\n${storage_type}\n/${SPACE}/${file_name}"
 44  signature=$(echo -en "${string}" | openssl sha1 -hmac "${DIGITALOCEAN_SPACES_SECRET_KEY}" -binary | base64)
 45
 46  curl --fail -vv -s -X PUT -T "$file_to_upload" \
 47    -H "Host: ${SPACE}.${REGION}.digitaloceanspaces.com" \
 48    -H "Date: $date" \
 49    -H "Content-Type: $content_type" \
 50    -H "$storage_type" \
 51    -H "$acl" \
 52    -H "Authorization: AWS ${DIGITALOCEAN_SPACES_ACCESS_KEY}:$signature" \
 53    "https://${SPACE}.${REGION}.digitaloceanspaces.com/${file_name}"
 54}
 55
 56while getopts 'dlfoh' flag
 57do
 58    case "${flag}" in
 59        o) open_result=true;;
 60        d)
 61            export CARGO_INCREMENTAL=true
 62            export CARGO_BUNDLE_SKIP_BUILD=true
 63            build_flag="";
 64            target_dir="debug"
 65            ;;
 66        l)
 67            export CARGO_INCREMENTAL=true
 68            export CARGO_BUNDLE_SKIP_BUILD=true
 69            build_flag=""
 70            local_arch=true
 71            local_only=true
 72            target_dir="debug"
 73            ;;
 74        f) overwrite_local_app=true;;
 75        h)
 76           help_info
 77           exit 0
 78           ;;
 79    esac
 80done
 81
 82shift $((OPTIND-1))
 83
 84if [ "$1" ]; then
 85    bundle_name=$1
 86fi
 87
 88export ZED_BUNDLE=true
 89export MACOSX_DEPLOYMENT_TARGET=10.15.7
 90
 91cargo_bundle_version=$(cargo -q bundle --help 2>&1 | head -n 1 || echo "")
 92if [ "$cargo_bundle_version" != "cargo-bundle v0.6.0-zed" ]; then
 93    cargo install cargo-bundle --git https://github.com/zed-industries/cargo-bundle.git --branch zed-deploy
 94fi
 95
 96rustup target add wasm32-wasi
 97
 98# Deal with versions of macOS that don't include libstdc++ headers
 99export CXXFLAGS="-stdlib=libc++"
100
101version_info=$(rustc --version --verbose)
102host_line=$(echo "$version_info" | grep host)
103local_target_triple=${host_line#*: }
104
105if [ "$local_arch" = true ]; then
106    echo "Building for local target only."
107    cargo build ${build_flag} --package ${zed_crate}
108    cargo build ${build_flag} --package cli
109else
110    echo "Compiling zed binaries"
111    cargo build ${build_flag} --package ${zed_crate} --package cli --target aarch64-apple-darwin --target x86_64-apple-darwin
112fi
113
114echo "Creating application bundle"
115pushd crates/zed
116channel=$(<RELEASE_CHANNEL)
117popd
118
119pushd crates/${zed_crate}
120cp Cargo.toml Cargo.toml.backup
121sed \
122    -i .backup \
123    "s/package.metadata.bundle-${channel}/package.metadata.bundle/" \
124    Cargo.toml
125
126if [ "$local_arch" = true ]; then
127    app_path=$(cargo bundle ${build_flag} --select-workspace-root | xargs)
128else
129    app_path=$(cargo bundle ${build_flag} --target x86_64-apple-darwin --select-workspace-root | xargs)
130fi
131
132mv Cargo.toml.backup Cargo.toml
133popd
134echo "Bundled ${app_path}"
135
136if [ "$local_arch" = false ]; then
137    echo "Uploading dSYMs"
138    dsymutil --flat target/aarch64-apple-darwin/${target_dir}/Zed
139    dsymutil --flat target/x86_64-apple-darwin/${target_dir}/Zed
140    version="$(cargo metadata --no-deps --manifest-path crates/zed/Cargo.toml --offline --format-version=1 | jq -r '.packages | map(select(.name == "zed"))[0].version')"
141    if [ "$channel" == "nightly" ]; then
142        version="$version-$(git rev-parse --short HEAD)"
143    fi
144
145    echo "Removing existing gzipped dSYMs"
146    rm -f target/aarch64-apple-darwin/${target_dir}/Zed.dwarf.gz
147    rm -f target/x86_64-apple-darwin/${target_dir}/Zed.dwarf.gz
148
149    echo "Gzipping dSYMs"
150    gzip target/aarch64-apple-darwin/${target_dir}/Zed.dwarf
151    gzip target/x86_64-apple-darwin/${target_dir}/Zed.dwarf
152
153    echo "Uploading dSYMs"
154    uploadDsym target/aarch64-apple-darwin/${target_dir}/Zed.dwarf.gz "$channel/Zed-$version-aarch64-apple-darwin.dwarf.gz"
155    uploadDsym target/x86_64-apple-darwin/${target_dir}/Zed.dwarf.gz "$channel/Zed-$version-x86_64-apple-darwin.dwarf.gz"
156
157    echo "Creating fat binaries"
158    lipo \
159        -create \
160        target/{x86_64-apple-darwin,aarch64-apple-darwin}/${target_dir}/${binary_name} \
161        -output \
162        "${app_path}/Contents/MacOS/${zed_crate}"
163    lipo \
164        -create \
165        target/{x86_64-apple-darwin,aarch64-apple-darwin}/${target_dir}/cli \
166        -output \
167        "${app_path}/Contents/MacOS/cli"
168fi
169
170echo "Copying WebRTC.framework into the frameworks folder"
171mkdir "${app_path}/Contents/Frameworks"
172if [ "$local_arch" = false ]; then
173    cp -R target/${local_target_triple}/${target_dir}/WebRTC.framework "${app_path}/Contents/Frameworks/"
174else
175    cp -R target/${target_dir}/WebRTC.framework "${app_path}/Contents/Frameworks/"
176    cp -R target/${target_dir}/cli "${app_path}/Contents/MacOS/"
177fi
178
179# Note: The app identifier for our development builds is the same as the app identifier for nightly.
180cp crates/${zed_crate}/contents/$channel/embedded.provisionprofile "${app_path}/Contents/"
181
182if [[ -n $MACOS_CERTIFICATE && -n $MACOS_CERTIFICATE_PASSWORD && -n $APPLE_NOTARIZATION_USERNAME && -n $APPLE_NOTARIZATION_PASSWORD ]]; then
183    echo "Signing bundle with Apple-issued certificate"
184    security create-keychain -p "$MACOS_CERTIFICATE_PASSWORD" zed.keychain || echo ""
185    security default-keychain -s zed.keychain
186    security unlock-keychain -p "$MACOS_CERTIFICATE_PASSWORD" zed.keychain
187    echo "$MACOS_CERTIFICATE" | base64 --decode > /tmp/zed-certificate.p12
188    security import /tmp/zed-certificate.p12 -k zed.keychain -P "$MACOS_CERTIFICATE_PASSWORD" -T /usr/bin/codesign
189    rm /tmp/zed-certificate.p12
190    security set-key-partition-list -S apple-tool:,apple:,codesign: -s -k "$MACOS_CERTIFICATE_PASSWORD" zed.keychain
191
192    # sequence of codesign commands modeled after this example: https://developer.apple.com/forums/thread/701514
193    /usr/bin/codesign --deep --force --timestamp --sign "Zed Industries, Inc." "${app_path}/Contents/Frameworks/WebRTC.framework" -v
194    /usr/bin/codesign --deep --force --timestamp --options runtime --sign "Zed Industries, Inc." "${app_path}/Contents/MacOS/cli" -v
195    /usr/bin/codesign --deep --force --timestamp --options runtime --entitlements crates/${zed_crate}/resources/zed.entitlements --sign "Zed Industries, Inc." "${app_path}/Contents/MacOS/${zed_crate}" -v
196    /usr/bin/codesign --force --timestamp --options runtime --entitlements crates/${zed_crate}/resources/zed.entitlements --sign "Zed Industries, Inc." "${app_path}" -v
197
198    security default-keychain -s login.keychain
199else
200    echo "One or more of the following variables are missing: MACOS_CERTIFICATE, MACOS_CERTIFICATE_PASSWORD, APPLE_NOTARIZATION_USERNAME, APPLE_NOTARIZATION_PASSWORD"
201    if [[ "$local_only" = false ]]; then
202        echo "To create a self-signed local build use ./scripts/build.sh -ldf"
203        exit 1
204    fi
205
206    echo "====== WARNING ======"
207    echo "This bundle is being signed without all entitlements, some features (e.g. universal links) will not work"
208    echo "====== WARNING ======"
209
210    # NOTE: if you need to test universal links you have a few paths forward:
211    # - create a PR and tag it with the `run-build-dmg` label, and download the .dmg file from there.
212    # - get a signing key for the MQ55VZLNZQ team from Nathan.
213    # - create your own signing key, and update references to MQ55VZLNZQ to your own team ID
214    # then comment out this line.
215    cat crates/${zed_crate}/resources/zed.entitlements | sed '/com.apple.developer.associated-domains/,+1d' > "${app_path}/Contents/Resources/zed.entitlements"
216
217    codesign --force --deep --entitlements "${app_path}/Contents/Resources/zed.entitlements" --sign ${MACOS_SIGNING_KEY:- -} "${app_path}" -v
218fi
219
220if [[ "$target_dir" = "debug" && "$local_only" = false ]]; then
221    if [ "$open_result" = true ]; then
222        open "$app_path"
223    else
224        echo "Created application bundle:"
225        echo "$app_path"
226    fi
227    exit 0
228fi
229
230# If bundle_name is not set or empty, use the basename of $app_path
231if [ -z "$bundle_name" ]; then
232    bundle_name=$(basename "$app_path")
233else
234    # If bundle_name doesn't end in .app, append it
235    if [[ "$bundle_name" != *.app ]]; then
236        bundle_name="$bundle_name.app"
237    fi
238fi
239
240if [ "$local_only" = true ]; then
241    if [ "$overwrite_local_app" = true ]; then
242        rm -rf "/Applications/$bundle_name"
243    fi
244    mv "$app_path" "/Applications/$bundle_name"
245
246    if [ "$open_result" = true ]; then
247        open "/Applications/$bundle_name"
248    else
249        echo "Installed application bundle:"
250        echo "/Applications/$bundle_name"
251    fi
252else
253    dmg_target_directory="target/${target_dir}"
254    dmg_source_directory="${dmg_target_directory}/dmg"
255    dmg_file_path="${dmg_target_directory}/Zed.dmg"
256    xcode_bin_dir_path="$(xcode-select -p)/usr/bin"
257
258    rm -rf ${dmg_source_directory}
259    mkdir -p ${dmg_source_directory}
260    mv "${app_path}" "${dmg_source_directory}"
261
262    if [[ -n $MACOS_CERTIFICATE && -n $MACOS_CERTIFICATE_PASSWORD && -n $APPLE_NOTARIZATION_USERNAME && -n $APPLE_NOTARIZATION_PASSWORD ]]; then
263        echo "Creating temporary DMG at ${dmg_file_path} using ${dmg_source_directory} to notarize app bundle"
264        hdiutil create -volname Zed -srcfolder "${dmg_source_directory}" -ov -format UDZO "${dmg_file_path}"
265
266        echo "Notarizing DMG with Apple"
267        "${xcode_bin_dir_path}/notarytool" submit --wait --apple-id "$APPLE_NOTARIZATION_USERNAME" --password "$APPLE_NOTARIZATION_PASSWORD" --team-id "$APPLE_NOTORIZATION_TEAM" "${dmg_file_path}"
268
269        echo "Removing temporary DMG (used only for notarization)"
270        rm "${dmg_file_path}"
271
272        echo "Stapling notarization ticket to ${dmg_source_directory}/${bundle_name}"
273        "${xcode_bin_dir_path}/stapler" staple "${dmg_source_directory}/${bundle_name}"
274    fi
275
276    echo "Adding symlink to /Applications to ${dmg_source_directory}"
277    ln -s /Applications ${dmg_source_directory}
278
279    echo "Creating final DMG at ${dmg_file_path} using ${dmg_source_directory}"
280    hdiutil create -volname Zed -srcfolder "${dmg_source_directory}" -ov -format UDZO "${dmg_file_path}"
281
282    # If someone runs this bundle script locally, a symlink will be placed in `dmg_source_directory`.
283    # This symlink causes CPU issues with Zed if the Zed codebase is the project being worked on, so we simply remove it for now.
284    echo "Removing symlink to /Applications from ${dmg_source_directory}"
285    rm ${dmg_source_directory}/Applications
286
287    echo "Adding license agreement to DMG"
288    npm install --global dmg-license minimist
289    dmg-license script/eula/eula.json "${dmg_file_path}"
290
291    if [[ -n $MACOS_CERTIFICATE && -n $MACOS_CERTIFICATE_PASSWORD && -n $APPLE_NOTARIZATION_USERNAME && -n $APPLE_NOTARIZATION_PASSWORD ]]; then
292        echo "Notarizing DMG with Apple"
293        "${xcode_bin_dir_path}/notarytool" submit --wait --apple-id "$APPLE_NOTARIZATION_USERNAME" --password "$APPLE_NOTARIZATION_PASSWORD" --team-id "$APPLE_NOTORIZATION_TEAM" "${dmg_file_path}"
294        "${xcode_bin_dir_path}/stapler" staple "${dmg_file_path}"
295    fi
296
297    if [ "$open_result" = true ]; then
298        open $dmg_target_directory
299    fi
300fi