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=""
 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_crate}"
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
153    # todo!(restore cli to zed2)
154    if [[ "$zed_crate" == "zed" ]]; then
155        /usr/bin/codesign --deep --force --timestamp --options runtime --sign "Zed Industries, Inc." "${app_path}/Contents/MacOS/cli" -v
156    fi
157
158    /usr/bin/codesign --deep --force --timestamp --options runtime --entitlements crates/${zed_crate}/resources/zed.entitlements --sign "Zed Industries, Inc." "${app_path}/Contents/MacOS/${zed_crate}" -v
159    /usr/bin/codesign --force --timestamp --options runtime --entitlements crates/${zed_crate}/resources/zed.entitlements --sign "Zed Industries, Inc." "${app_path}" -v
160
161    security default-keychain -s login.keychain
162else
163    echo "One or more of the following variables are missing: MACOS_CERTIFICATE, MACOS_CERTIFICATE_PASSWORD, APPLE_NOTARIZATION_USERNAME, APPLE_NOTARIZATION_PASSWORD"
164    if [[ "$local_only" = false ]]; then
165        echo "To create a self-signed local build use ./scripts/build.sh -ldf"
166        exit 1
167    fi
168
169    echo "====== WARNING ======"
170    echo "This bundle is being signed without all entitlements, some features (e.g. universal links) will not work"
171    echo "====== WARNING ======"
172
173    # NOTE: if you need to test universal links you have a few paths forward:
174    # - create a PR and tag it with the `run-build-dmg` label, and download the .dmg file from there.
175    # - get a signing key for the MQ55VZLNZQ team from Nathan.
176    # - create your own signing key, and update references to MQ55VZLNZQ to your own team ID
177    # then comment out this line.
178    cat crates/${zed_crate}/resources/zed.entitlements | sed '/com.apple.developer.associated-domains/,+1d' > "${app_path}/Contents/Resources/zed.entitlements"
179
180    codesign --force --deep --entitlements "${app_path}/Contents/Resources/zed.entitlements" --sign ${MACOS_SIGNING_KEY:- -} "${app_path}" -v
181fi
182
183if [[ "$target_dir" = "debug" && "$local_only" = false ]]; then
184    if [ "$open_result" = true ]; then
185        open "$app_path"
186    else
187        echo "Created application bundle:"
188        echo "$app_path"
189    fi
190    exit 0
191fi
192
193if [ "$local_only" = true ]; then
194    # If bundle_name is not set or empty, use the basename of $app_path
195    if [ -z "$bundle_name" ]; then
196        bundle_name=$(basename "$app_path")
197    else
198        # If bundle_name doesn't end in .app, append it
199        if [[ "$bundle_name" != *.app ]]; then
200            bundle_name="$bundle_name.app"
201        fi
202    fi
203
204    if [ "$overwrite_local_app" = true ]; then
205        rm -rf "/Applications/$bundle_name"
206    fi
207    mv "$app_path" "/Applications/$bundle_name"
208
209    if [ "$open_result" = true ]; then
210        open "/Applications/$bundle_name"
211    else
212        echo "Installed application bundle:"
213        echo "/Applications/$bundle_name"
214    fi
215else
216    echo "Creating DMG"
217    dmg_target_directory="target/${target_dir}"
218    dmg_source_directory="${dmg_target_directory}/dmg"
219    dmg_file_path="${dmg_target_directory}/Zed.dmg"
220
221    rm -rf ${dmg_source_directory}
222    mkdir -p ${dmg_source_directory}
223    mv "${app_path}" "${dmg_source_directory}"
224
225    ln -s /Applications ${dmg_source_directory}
226    hdiutil create -volname Zed -srcfolder "${dmg_source_directory}" -ov -format UDZO "${dmg_file_path}"
227    # If someone runs this bundle script locally, a symlink will be placed in `dmg_source_directory`.
228    # This symlink causes CPU issues with Zed if the Zed codebase is the project being worked on, so we simply remove it for now.
229    rm ${dmg_source_directory}/Applications
230
231    echo "Adding license agreement to DMG"
232    npm install --global dmg-license minimist
233    dmg-license script/eula/eula.json "${dmg_file_path}"
234
235    if [[ -n $MACOS_CERTIFICATE && -n $MACOS_CERTIFICATE_PASSWORD && -n $APPLE_NOTARIZATION_USERNAME && -n $APPLE_NOTARIZATION_PASSWORD ]]; then
236        echo "Notarizing DMG with Apple"
237        "$(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}"
238    fi
239
240    if [ "$open_result" = true ]; then
241        open $dmg_target_directory
242    fi
243fi