bundle

  1#!/bin/bash
  2
  3set -e
  4
  5build_flag="--release"
  6target_dir="release"
  7open_result=false
  8
  9# If -o option is specified, the folder of the resulting dmg will be opened in finder
 10# If -d is specified, Zed will be compiled in debug mode and the application's path printed
 11# If -od or -do is specified Zed will be bundled in debug and the application will be run.
 12while getopts 'od' flag
 13do
 14    case "${flag}" in
 15        o) open_result=true;;
 16        d)
 17            build_flag="";
 18            target_dir="debug"
 19            ;;
 20    esac
 21done
 22
 23export ZED_BUNDLE=true
 24export MACOSX_DEPLOYMENT_TARGET=10.15.7
 25
 26cargo_bundle_version=$(cargo -q bundle --help 2>&1 | head -n 1 || echo "")
 27if [ "$cargo_bundle_version" != "cargo-bundle v0.6.0-zed" ]; then
 28    cargo install cargo-bundle --git https://github.com/zed-industries/cargo-bundle.git --branch zed-deploy
 29fi
 30
 31rustup target add wasm32-wasi
 32
 33# Deal with versions of macOS that don't include libstdc++ headers
 34export CXXFLAGS="-stdlib=libc++"
 35
 36echo "Compiling zed binary for aarch64-apple-darwin"
 37cargo build ${build_flag} --package zed --target aarch64-apple-darwin
 38echo "Compiling zed binary for x86_64-apple-darwin"
 39cargo build ${build_flag} --package zed --target x86_64-apple-darwin
 40echo "Compiling cli binary for aarch64-apple-darwin"
 41cargo build ${build_flag} --package cli --target aarch64-apple-darwin
 42echo "Compiling cli binary for x86_64-apple-darwin"
 43cargo build ${build_flag} --package cli --target x86_64-apple-darwin
 44
 45echo "Creating application bundle"
 46pushd crates/zed
 47channel=$(<RELEASE_CHANNEL)
 48cp Cargo.toml Cargo.toml.backup
 49sed \
 50    -i .backup \
 51    "s/package.metadata.bundle-${channel}/package.metadata.bundle/" \
 52    Cargo.toml
 53app_path=$(cargo bundle ${build_flag} --target x86_64-apple-darwin --select-workspace-root | xargs)
 54
 55mv Cargo.toml.backup Cargo.toml
 56popd
 57echo "Bundled ${app_path}"
 58
 59echo "Creating fat binaries"
 60lipo \
 61    -create \
 62    target/{x86_64-apple-darwin,aarch64-apple-darwin}/${target_dir}/Zed \
 63    -output \
 64    "${app_path}/Contents/MacOS/zed"
 65lipo \
 66    -create \
 67    target/{x86_64-apple-darwin,aarch64-apple-darwin}/${target_dir}/cli \
 68    -output \
 69    "${app_path}/Contents/MacOS/cli"
 70
 71echo "Copying WebRTC.framework into the frameworks folder"
 72mkdir "${app_path}/Contents/Frameworks"
 73cp -R target/x86_64-apple-darwin/${target_dir}/WebRTC.framework "${app_path}/Contents/Frameworks/"
 74
 75if [[ -n $MACOS_CERTIFICATE && -n $MACOS_CERTIFICATE_PASSWORD && -n $APPLE_NOTARIZATION_USERNAME && -n $APPLE_NOTARIZATION_PASSWORD ]]; then
 76    echo "Signing bundle with Apple-issued certificate"
 77    security create-keychain -p "$MACOS_CERTIFICATE_PASSWORD" zed.keychain || echo ""
 78    security default-keychain -s zed.keychain
 79    security unlock-keychain -p "$MACOS_CERTIFICATE_PASSWORD" zed.keychain
 80    echo "$MACOS_CERTIFICATE" | base64 --decode > /tmp/zed-certificate.p12
 81    security import /tmp/zed-certificate.p12 -k zed.keychain -P "$MACOS_CERTIFICATE_PASSWORD" -T /usr/bin/codesign
 82    rm /tmp/zed-certificate.p12
 83    security set-key-partition-list -S apple-tool:,apple:,codesign: -s -k "$MACOS_CERTIFICATE_PASSWORD" zed.keychain
 84    /usr/bin/codesign --force --deep --timestamp --options runtime --entitlements crates/zed/resources/zed.entitlements --sign "Zed Industries, Inc." "${app_path}" -v
 85    security default-keychain -s login.keychain
 86else
 87    echo "One or more of the following variables are missing: MACOS_CERTIFICATE, MACOS_CERTIFICATE_PASSWORD, APPLE_NOTARIZATION_USERNAME, APPLE_NOTARIZATION_PASSWORD"
 88    echo "Performing an ad-hoc signature, but this bundle should not be distributed"
 89    codesign --force --deep --entitlements crates/zed/resources/zed.entitlements --sign - "${app_path}" -v
 90fi
 91
 92if [ "$target_dir" = "debug" ]; then
 93    if [ "$open_result" = true ]; then
 94        open "$app_path"
 95    else
 96        echo "Created application bundle:"
 97        echo "$app_path"
 98    fi
 99    exit 0
100fi
101
102dmg_target_directory="target/${target_dir}"
103dmg_source_directory="${dmg_target_directory}/dmg"
104dmg_file_path="${dmg_target_directory}/Zed.dmg"
105
106echo "Creating DMG"
107rm -rf ${dmg_source_directory}
108mkdir -p ${dmg_source_directory}
109mv "${app_path}" "${dmg_source_directory}"
110
111ln -s /Applications ${dmg_source_directory}
112hdiutil create -volname Zed -srcfolder "${dmg_source_directory}" -ov -format UDZO "${dmg_file_path}"
113# If someone runs this bundle script locally, a symlink will be placed in `dmg_source_directory`.
114# This symlink causes CPU issues with Zed if the Zed codebase is the project being worked on, so we simply remove it for now.
115rm ${dmg_source_directory}/Applications
116
117echo "Adding license agreement to DMG"
118npm install --global dmg-license minimist
119dmg-license script/eula/eula.json "${dmg_file_path}"
120
121if [[ -n $MACOS_CERTIFICATE && -n $MACOS_CERTIFICATE_PASSWORD && -n $APPLE_NOTARIZATION_USERNAME && -n $APPLE_NOTARIZATION_PASSWORD ]]; then
122    echo "Notarizing DMG with Apple"
123    npm install -g notarize-cli
124    npx notarize-cli --file "${dmg_file_path}" --bundle-id dev.zed.Zed --username "$APPLE_NOTARIZATION_USERNAME" --password "$APPLE_NOTARIZATION_PASSWORD"
125fi
126
127if [ "$open_result" = true ]; then
128    open $dmg_target_directory
129fi