234 lines · 8.6 KB · Text
RawDownload
#!/bin/bash set -e build_flag="--release"target_dir="release"open_result=falselocal_arch=falselocal_only=falseoverwrite_local_app=falsebundle_name="" # This must match the team in the provsiioning profile.APPLE_NOTORIZATION_TEAM="MQ55VZLNZQ" # Function for displaying help infohelp_info() {  echo "Usage: ${0##*/} [options] [bundle_name]Build the application bundle. Options:  -d    Compile in debug mode  -l    Compile for local architecture and copy bundle to /Applications, implies -d.  -o    Open the resulting DMG or the app itself in local mode.  -f    Overwrite the local app bundle if it exists.  -h    Display this help and exit.  "} # If -o option is specified, the folder of the resulting dmg will be opened in finder# If -d is specified, Zed will be compiled in debug mode and the application's path printed# If -od or -do is specified Zed will be bundled in debug and the application will be run.while getopts 'dlfoh' flagdo    case "${flag}" in        o) open_result=true;;        d)            export CARGO_INCREMENTAL=true            export CARGO_BUNDLE_SKIP_BUILD=true            build_flag="";            local_arch=true            target_dir="debug"            ;;        l)            export CARGO_INCREMENTAL=true            export CARGO_BUNDLE_SKIP_BUILD=true            build_flag=""            local_arch=true            local_only=true            target_dir="debug"            ;;        f) overwrite_local_app=true;;        h)           help_info           exit 0           ;;    esacdone shift $((OPTIND-1)) if [ "$1" ]; then    bundle_name=$1fi export ZED_BUNDLE=trueexport MACOSX_DEPLOYMENT_TARGET=10.15.7 cargo_bundle_version=$(cargo -q bundle --help 2>&1 | head -n 1 || echo "")if [ "$cargo_bundle_version" != "cargo-bundle v0.6.0-zed" ]; then    cargo install cargo-bundle --git https://github.com/zed-industries/cargo-bundle.git --branch zed-deployfi rustup target add wasm32-wasi # Deal with versions of macOS that don't include libstdc++ headersexport CXXFLAGS="-stdlib=libc++" version_info=$(rustc --version --verbose)host_line=$(echo "$version_info" | grep host)local_target_triple=${host_line#*: } if [ "$local_arch" = true ]; then    echo "Building for local target only."    cargo build ${build_flag} --package zed    cargo build ${build_flag} --package clielse    echo "Compiling zed binaries"    cargo build ${build_flag} --package zed --package cli --target aarch64-apple-darwin --target x86_64-apple-darwinfi echo "Creating application bundle"pushd crates/zedchannel=$(<RELEASE_CHANNEL)cp Cargo.toml Cargo.toml.backupsed \    -i .backup \    "s/package.metadata.bundle-${channel}/package.metadata.bundle/" \    Cargo.toml if [ "$local_arch" = true ]; then    app_path=$(cargo bundle ${build_flag} --select-workspace-root | xargs)else    app_path=$(cargo bundle ${build_flag} --target x86_64-apple-darwin --select-workspace-root | xargs)fi mv Cargo.toml.backup Cargo.tomlpopdecho "Bundled ${app_path}" if [ "$local_arch" = false ]; then    echo "Creating fat binaries"    lipo \        -create \        target/{x86_64-apple-darwin,aarch64-apple-darwin}/${target_dir}/Zed \        -output \        "${app_path}/Contents/MacOS/zed"    lipo \        -create \        target/{x86_64-apple-darwin,aarch64-apple-darwin}/${target_dir}/cli \        -output \        "${app_path}/Contents/MacOS/cli"fi echo "Copying WebRTC.framework into the frameworks folder"mkdir "${app_path}/Contents/Frameworks"if [ "$local_arch" = false ]; then    cp -R target/${local_target_triple}/${target_dir}/WebRTC.framework "${app_path}/Contents/Frameworks/"else    cp -R target/${target_dir}/WebRTC.framework "${app_path}/Contents/Frameworks/"fi cp crates/zed/contents/$channel/embedded.provisionprofile "${app_path}/Contents/" if [[ -n $MACOS_CERTIFICATE && -n $MACOS_CERTIFICATE_PASSWORD && -n $APPLE_NOTARIZATION_USERNAME && -n $APPLE_NOTARIZATION_PASSWORD ]]; then    echo "Signing bundle with Apple-issued certificate"    security create-keychain -p "$MACOS_CERTIFICATE_PASSWORD" zed.keychain || echo ""    security default-keychain -s zed.keychain    security unlock-keychain -p "$MACOS_CERTIFICATE_PASSWORD" zed.keychain    echo "$MACOS_CERTIFICATE" | base64 --decode > /tmp/zed-certificate.p12    security import /tmp/zed-certificate.p12 -k zed.keychain -P "$MACOS_CERTIFICATE_PASSWORD" -T /usr/bin/codesign    rm /tmp/zed-certificate.p12    security set-key-partition-list -S apple-tool:,apple:,codesign: -s -k "$MACOS_CERTIFICATE_PASSWORD" zed.keychain     # sequence of codesign commands modeled after this example: https://developer.apple.com/forums/thread/701514    /usr/bin/codesign --deep --force --timestamp --sign "Zed Industries, Inc." "${app_path}/Contents/Frameworks/WebRTC.framework" -v    /usr/bin/codesign --deep --force --timestamp --options runtime --sign "Zed Industries, Inc." "${app_path}/Contents/MacOS/cli" -v    /usr/bin/codesign --deep --force --timestamp --options runtime --entitlements crates/zed/resources/zed.entitlements --sign "Zed Industries, Inc." "${app_path}/Contents/MacOS/zed" -v    /usr/bin/codesign --force --timestamp --options runtime --entitlements crates/zed/resources/zed.entitlements --sign "Zed Industries, Inc." "${app_path}" -v     security default-keychain -s login.keychainelse    echo "One or more of the following variables are missing: MACOS_CERTIFICATE, MACOS_CERTIFICATE_PASSWORD, APPLE_NOTARIZATION_USERNAME, APPLE_NOTARIZATION_PASSWORD"    if [[ "$local_only" = false ]]; then        echo "To create a self-signed local build use ./scripts/build.sh -ldf"        exit 1    fi     echo "====== WARNING ======"    echo "This bundle is being signed without all entitlements, some features (e.g. universal links) will not work"    echo "====== WARNING ======"     # NOTE: if you need to test universal links you have a few paths forward:    # - create a PR and tag it with the `run-build-dmg` label, and download the .dmg file from there.    # - get a signing key for the MQ55VZLNZQ team from Nathan.    # - create your own signing key, and update references to MQ55VZLNZQ to your own team ID    # then comment out this line.    cat crates/zed/resources/zed.entitlements | sed '/com.apple.developer.associated-domains/,+1d' > "${app_path}/Contents/Resources/zed.entitlements"     codesign --force --deep --entitlements "${app_path}/Contents/Resources/zed.entitlements" --sign ${MACOS_SIGNING_KEY:- -} "${app_path}" -vfi if [[ "$target_dir" = "debug" && "$local_only" = false ]]; then    if [ "$open_result" = true ]; then        open "$app_path"    else        echo "Created application bundle:"        echo "$app_path"    fi    exit 0fi if [ "$local_only" = true ]; then    # If bundle_name is not set or empty, use the basename of $app_path    if [ -z "$bundle_name" ]; then        bundle_name=$(basename "$app_path")    else        # If bundle_name doesn't end in .app, append it        if [[ "$bundle_name" != *.app ]]; then            bundle_name="$bundle_name.app"        fi    fi     if [ "$overwrite_local_app" = true ]; then        rm -rf "/Applications/$bundle_name"    fi    mv "$app_path" "/Applications/$bundle_name"     if [ "$open_result" = true ]; then        open "/Applications/$bundle_name"    else        echo "Installed application bundle:"        echo "/Applications/$bundle_name"    fielse    echo "Creating DMG"    dmg_target_directory="target/${target_dir}"    dmg_source_directory="${dmg_target_directory}/dmg"    dmg_file_path="${dmg_target_directory}/Zed.dmg"     rm -rf ${dmg_source_directory}    mkdir -p ${dmg_source_directory}    mv "${app_path}" "${dmg_source_directory}"     ln -s /Applications ${dmg_source_directory}    hdiutil create -volname Zed -srcfolder "${dmg_source_directory}" -ov -format UDZO "${dmg_file_path}"    # If someone runs this bundle script locally, a symlink will be placed in `dmg_source_directory`.    # This symlink causes CPU issues with Zed if the Zed codebase is the project being worked on, so we simply remove it for now.    rm ${dmg_source_directory}/Applications     echo "Adding license agreement to DMG"    npm install --global dmg-license minimist    dmg-license script/eula/eula.json "${dmg_file_path}"     if [[ -n $MACOS_CERTIFICATE && -n $MACOS_CERTIFICATE_PASSWORD && -n $APPLE_NOTARIZATION_USERNAME && -n $APPLE_NOTARIZATION_PASSWORD ]]; then        echo "Notarizing DMG with Apple"        "$(xcode-select -p)/usr/bin/notarytool" submit --wait --apple-id "$APPLE_NOTARIZATION_USERNAME" --password "$APPLE_NOTARIZATION_PASSWORD" --team-id "$APPLE_NOTORIZATION_TEAM" "${dmg_file_path}"    fi     if [ "$open_result" = true ]; then        open $dmg_target_directory    fifi