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