1#!/bin/bash
2
3set -e
4
5export ZED_BUNDLE=true
6export MACOSX_DEPLOYMENT_TARGET=10.15.7
7
8which cargo-bundle > /dev/null || cargo install cargo-bundle --version 0.5.0
9rustup target add wasm32-wasi
10
11# Deal with versions of macOS that don't include libstdc++ headers
12export CXXFLAGS="-stdlib=libc++"
13
14echo "Compiling zed binary for aarch64-apple-darwin"
15cargo build --release --package zed --target aarch64-apple-darwin
16echo "Compiling zed binary for x86_64-apple-darwin"
17cargo build --release --package zed --target x86_64-apple-darwin
18echo "Compiling cli binary for aarch64-apple-darwin"
19cargo build --release --package cli --target aarch64-apple-darwin
20echo "Compiling cli binary for x86_64-apple-darwin"
21cargo build --release --package cli --target x86_64-apple-darwin
22
23echo "Creating application bundle"
24pushd crates/zed
25channel=$(<RELEASE_CHANNEL)
26cp Cargo.toml Cargo.toml.backup
27sed \
28 -i .backup \
29 "s/package.metadata.bundle-${channel}/package.metadata.bundle/" \
30 Cargo.toml
31app_path=$(cargo bundle --release --target x86_64-apple-darwin | xargs)
32mv Cargo.toml.backup Cargo.toml
33popd
34echo "Bundled ${app_path}"
35
36echo "Creating fat binaries"
37lipo \
38 -create \
39 target/{x86_64-apple-darwin,aarch64-apple-darwin}/release/Zed \
40 -output \
41 "${app_path}/Contents/MacOS/zed"
42lipo \
43 -create \
44 target/{x86_64-apple-darwin,aarch64-apple-darwin}/release/cli \
45 -output \
46 "${app_path}/Contents/MacOS/cli"
47
48echo "Copying WebRTC.framework into the frameworks folder"
49mkdir "${app_path}/Contents/Frameworks"
50cp -R target/x86_64-apple-darwin/release/WebRTC.framework "${app_path}/Contents/Frameworks/"
51
52mv "${app_path}/Contents/Info.plist" "${app_path}/Contents/WithoutDocumentTypes.plist"
53awk \
54 "/<\/dict>/{while(getline line<\"./crates/zed/BundleDocumentTypes.plist\"){print line}}1" \
55 "${app_path}/Contents/WithoutDocumentTypes.plist" \
56 > "${app_path}/Contents/Info.plist"
57rm "${app_path}/Contents/WithoutDocumentTypes.plist"
58
59if [[ -n $MACOS_CERTIFICATE && -n $MACOS_CERTIFICATE_PASSWORD && -n $APPLE_NOTARIZATION_USERNAME && -n $APPLE_NOTARIZATION_PASSWORD ]]; then
60 echo "Signing bundle with Apple-issued certificate"
61 security create-keychain -p "$MACOS_CERTIFICATE_PASSWORD" zed.keychain || echo ""
62 security default-keychain -s zed.keychain
63 security unlock-keychain -p "$MACOS_CERTIFICATE_PASSWORD" zed.keychain
64 echo "$MACOS_CERTIFICATE" | base64 --decode > /tmp/zed-certificate.p12
65 security import /tmp/zed-certificate.p12 -k zed.keychain -P "$MACOS_CERTIFICATE_PASSWORD" -T /usr/bin/codesign
66 rm /tmp/zed-certificate.p12
67 security set-key-partition-list -S apple-tool:,apple:,codesign: -s -k "$MACOS_CERTIFICATE_PASSWORD" zed.keychain
68 /usr/bin/codesign --force --deep --timestamp --options runtime --sign "Zed Industries, Inc." "${app_path}" -v
69 security default-keychain -s login.keychain
70else
71 echo "One or more of the following variables are missing: MACOS_CERTIFICATE, MACOS_CERTIFICATE_PASSWORD, APPLE_NOTARIZATION_USERNAME, APPLE_NOTARIZATION_PASSWORD"
72 echo "Performing an ad-hoc signature, but this bundle should not be distributed"
73 codesign --force --deep --sign - "${app_path}" -v
74fi
75
76dmg_target_directory="target/release"
77dmg_source_directory="${dmg_target_directory}/dmg"
78dmg_file_path="${dmg_target_directory}/Zed.dmg"
79
80echo "Creating DMG"
81rm -rf ${dmg_source_directory}
82mkdir -p ${dmg_source_directory}
83mv "${app_path}" "${dmg_source_directory}"
84
85ln -s /Applications ${dmg_source_directory}
86hdiutil create -volname Zed -srcfolder "${dmg_source_directory}" -ov -format UDZO "${dmg_file_path}"
87# If someone runs this bundle script locally, a symlink will be placed in `dmg_source_directory`.
88# This symlink causes CPU issues with Zed if the Zed codebase is the project being worked on, so we simply remove it for now.
89rm ${dmg_source_directory}/Applications
90
91if [[ -n $MACOS_CERTIFICATE && -n $MACOS_CERTIFICATE_PASSWORD && -n $APPLE_NOTARIZATION_USERNAME && -n $APPLE_NOTARIZATION_PASSWORD ]]; then
92 echo "Notarizing DMG with Apple"
93 npm install -g notarize-cli
94 npx notarize-cli --file ${dmg_file_path} --bundle-id dev.zed.Zed --username "$APPLE_NOTARIZATION_USERNAME" --password "$APPLE_NOTARIZATION_PASSWORD"
95fi
96
97# If -o option is specified, open the $dmg_target_directory directory in Finder to reveal the DMG
98while getopts o flag
99do
100 case "${flag}" in
101 o) open $dmg_target_directory;;
102 esac
103done