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=""
 12
 13# This must match the team in the provsiioning profile.
 14APPLE_NOTORIZATION_TEAM="MQ55VZLNZQ"
 15
 16# Function for displaying help info
 17help_info() {
 18  echo "
 19Usage: ${0##*/} [options] [bundle_name]
 20Build the application bundle.
 21
 22Options:
 23  -d    Compile in debug mode
 24  -l    Compile for local architecture and copy bundle to /Applications, implies -d.
 25  -o    Open the resulting DMG or the app itself in local mode.
 26  -f    Overwrite the local app bundle if it exists.
 27  -h    Display this help and exit.
 28  "
 29}
 30
 31# If -o option is specified, the folder of the resulting dmg will be opened in finder
 32# If -d is specified, Zed will be compiled in debug mode and the application's path printed
 33# If -od or -do is specified Zed will be bundled in debug and the application will be run.
 34while getopts 'dlfoh' flag
 35do
 36    case "${flag}" in
 37        o) open_result=true;;
 38        d)
 39            export CARGO_INCREMENTAL=true
 40            export CARGO_BUNDLE_SKIP_BUILD=true
 41            build_flag="";
 42            local_arch=true
 43            target_dir="debug"
 44            ;;
 45        l)
 46            export CARGO_INCREMENTAL=true
 47            export CARGO_BUNDLE_SKIP_BUILD=true
 48            build_flag=""
 49            local_arch=true
 50            local_only=true
 51            target_dir="debug"
 52            ;;
 53        f) overwrite_local_app=true;;
 54        h)
 55           help_info
 56           exit 0
 57           ;;
 58    esac
 59done
 60
 61shift $((OPTIND-1))
 62
 63if [ "$1" ]; then
 64    bundle_name=$1
 65fi
 66
 67export ZED_BUNDLE=true
 68export MACOSX_DEPLOYMENT_TARGET=10.15.7
 69
 70cargo_bundle_version=$(cargo -q bundle --help 2>&1 | head -n 1 || echo "")
 71if [ "$cargo_bundle_version" != "cargo-bundle v0.6.0-zed" ]; then
 72    cargo install cargo-bundle --git https://github.com/zed-industries/cargo-bundle.git --branch zed-deploy
 73fi
 74
 75rustup target add wasm32-wasi
 76
 77# Deal with versions of macOS that don't include libstdc++ headers
 78export CXXFLAGS="-stdlib=libc++"
 79
 80version_info=$(rustc --version --verbose)
 81host_line=$(echo "$version_info" | grep host)
 82local_target_triple=${host_line#*: }
 83
 84if [ "$local_arch" = true ]; then
 85    echo "Building for local target only."
 86    cargo build ${build_flag} --package zed
 87    cargo build ${build_flag} --package cli
 88else
 89    echo "Compiling zed binaries"
 90    cargo build ${build_flag} --package zed --package cli --target aarch64-apple-darwin --target x86_64-apple-darwin
 91fi
 92
 93echo "Creating application bundle"
 94pushd crates/zed
 95channel=$(<RELEASE_CHANNEL)
 96cp Cargo.toml Cargo.toml.backup
 97sed \
 98    -i .backup \
 99    "s/package.metadata.bundle-${channel}/package.metadata.bundle/" \
100    Cargo.toml
101
102if [ "$local_arch" = true ]; then
103    app_path=$(cargo bundle ${build_flag} --select-workspace-root | xargs)
104else
105    app_path=$(cargo bundle ${build_flag} --target x86_64-apple-darwin --select-workspace-root | xargs)
106fi
107
108mv Cargo.toml.backup Cargo.toml
109popd
110echo "Bundled ${app_path}"
111
112if [ "$local_arch" = false ]; then
113    echo "Creating fat binaries"
114    lipo \
115        -create \
116        target/{x86_64-apple-darwin,aarch64-apple-darwin}/${target_dir}/Zed \
117        -output \
118        "${app_path}/Contents/MacOS/zed"
119    lipo \
120        -create \
121        target/{x86_64-apple-darwin,aarch64-apple-darwin}/${target_dir}/cli \
122        -output \
123        "${app_path}/Contents/MacOS/cli"
124fi
125
126echo "Copying WebRTC.framework into the frameworks folder"
127mkdir "${app_path}/Contents/Frameworks"
128if [ "$local_arch" = false ]; then
129    cp -R target/${local_target_triple}/${target_dir}/WebRTC.framework "${app_path}/Contents/Frameworks/"
130else
131    cp -R target/${target_dir}/WebRTC.framework "${app_path}/Contents/Frameworks/"
132fi
133
134cp crates/zed/contents/$channel/embedded.provisionprofile "${app_path}/Contents/"
135
136if [[ -n $MACOS_CERTIFICATE && -n $MACOS_CERTIFICATE_PASSWORD && -n $APPLE_NOTARIZATION_USERNAME && -n $APPLE_NOTARIZATION_PASSWORD ]]; then
137    echo "Signing bundle with Apple-issued certificate"
138    security create-keychain -p "$MACOS_CERTIFICATE_PASSWORD" zed.keychain || echo ""
139    security default-keychain -s zed.keychain
140    security unlock-keychain -p "$MACOS_CERTIFICATE_PASSWORD" zed.keychain
141    echo "$MACOS_CERTIFICATE" | base64 --decode > /tmp/zed-certificate.p12
142    security import /tmp/zed-certificate.p12 -k zed.keychain -P "$MACOS_CERTIFICATE_PASSWORD" -T /usr/bin/codesign
143    rm /tmp/zed-certificate.p12
144    security set-key-partition-list -S apple-tool:,apple:,codesign: -s -k "$MACOS_CERTIFICATE_PASSWORD" zed.keychain
145
146    # sequence of codesign commands modeled after this example: https://developer.apple.com/forums/thread/701514
147    /usr/bin/codesign --deep --force --timestamp --sign "Zed Industries, Inc." "${app_path}/Contents/Frameworks/WebRTC.framework" -v
148    /usr/bin/codesign --deep --force --timestamp --options runtime --sign "Zed Industries, Inc." "${app_path}/Contents/MacOS/cli" -v
149    /usr/bin/codesign --deep --force --timestamp --options runtime --entitlements crates/zed/resources/zed.entitlements --sign "Zed Industries, Inc." "${app_path}/Contents/MacOS/zed" -v
150    /usr/bin/codesign --force --timestamp --options runtime --entitlements crates/zed/resources/zed.entitlements --sign "Zed Industries, Inc." "${app_path}" -v
151
152    security default-keychain -s login.keychain
153else
154    echo "One or more of the following variables are missing: MACOS_CERTIFICATE, MACOS_CERTIFICATE_PASSWORD, APPLE_NOTARIZATION_USERNAME, APPLE_NOTARIZATION_PASSWORD"
155    if [[ "$local_only" = false ]]; then
156        echo "To create a self-signed local build use ./scripts/build.sh -ldf"
157        exit 1
158    fi
159
160    echo "====== WARNING ======"
161    echo "This bundle is being signed without all entitlements, some features (e.g. universal links) will not work"
162    echo "====== WARNING ======"
163
164    # NOTE: if you need to test universal links you have a few paths forward:
165    # - create a PR and tag it with the `run-build-dmg` label, and download the .dmg file from there.
166    # - get a signing key for the MQ55VZLNZQ team from Nathan.
167    # - create your own signing key, and update references to MQ55VZLNZQ to your own team ID
168    # then comment out this line.
169    cat crates/zed/resources/zed.entitlements | sed '/com.apple.developer.associated-domains/,+1d' > "${app_path}/Contents/Resources/zed.entitlements"
170
171    codesign --force --deep --entitlements "${app_path}/Contents/Resources/zed.entitlements" --sign ${MACOS_SIGNING_KEY:- -} "${app_path}" -v
172fi
173
174if [[ "$target_dir" = "debug" && "$local_only" = false ]]; then
175    if [ "$open_result" = true ]; then
176        open "$app_path"
177    else
178        echo "Created application bundle:"
179        echo "$app_path"
180    fi
181    exit 0
182fi
183
184if [ "$local_only" = true ]; then
185    # If bundle_name is not set or empty, use the basename of $app_path
186    if [ -z "$bundle_name" ]; then
187        bundle_name=$(basename "$app_path")
188    else
189        # If bundle_name doesn't end in .app, append it
190        if [[ "$bundle_name" != *.app ]]; then
191            bundle_name="$bundle_name.app"
192        fi
193    fi
194
195    if [ "$overwrite_local_app" = true ]; then
196        rm -rf "/Applications/$bundle_name"
197    fi
198    mv "$app_path" "/Applications/$bundle_name"
199
200    if [ "$open_result" = true ]; then
201        open "/Applications/$bundle_name"
202    else
203        echo "Installed application bundle:"
204        echo "/Applications/$bundle_name"
205    fi
206else
207    echo "Creating DMG"
208    dmg_target_directory="target/${target_dir}"
209    dmg_source_directory="${dmg_target_directory}/dmg"
210    dmg_file_path="${dmg_target_directory}/Zed.dmg"
211
212    rm -rf ${dmg_source_directory}
213    mkdir -p ${dmg_source_directory}
214    mv "${app_path}" "${dmg_source_directory}"
215
216    ln -s /Applications ${dmg_source_directory}
217    hdiutil create -volname Zed -srcfolder "${dmg_source_directory}" -ov -format UDZO "${dmg_file_path}"
218    # If someone runs this bundle script locally, a symlink will be placed in `dmg_source_directory`.
219    # This symlink causes CPU issues with Zed if the Zed codebase is the project being worked on, so we simply remove it for now.
220    rm ${dmg_source_directory}/Applications
221
222    echo "Adding license agreement to DMG"
223    npm install --global dmg-license minimist
224    dmg-license script/eula/eula.json "${dmg_file_path}"
225
226    if [[ -n $MACOS_CERTIFICATE && -n $MACOS_CERTIFICATE_PASSWORD && -n $APPLE_NOTARIZATION_USERNAME && -n $APPLE_NOTARIZATION_PASSWORD ]]; then
227        echo "Notarizing DMG with Apple"
228        "$(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}"
229    fi
230
231    if [ "$open_result" = true ]; then
232        open $dmg_target_directory
233    fi
234fi