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