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