bundle-linux

  1#!/usr/bin/env bash
  2
  3set -euxo pipefail
  4
  5build_flag="--release"
  6target_dir="release"
  7bundle_name=""
  8zed_crate="zed"
  9
 10
 11help_info() {
 12  echo "
 13Usage: ${0##*/} [options] [bundle_name]
 14Build the application bundle for Linux.
 15
 16Options:
 17  -d    Compile in debug mode
 18  -h    Display this help and exit
 19  "
 20}
 21
 22while getopts 'dh' flag
 23do
 24    case "${flag}" in
 25        d)
 26            export CARGO_INCREMENTAL=true
 27            export CARGO_BUNDLE_SKIP_BUILD=true
 28            build_flag="";
 29            target_dir="debug"
 30            ;;
 31        h)
 32           help_info
 33           exit 0
 34           ;;
 35    esac
 36done
 37
 38shift $((OPTIND-1))
 39
 40if [[ $# -gt 0 ]]; then
 41    if [ "$1" ]; then
 42        bundle_name=$1
 43    fi
 44fi
 45
 46export ZED_BUNDLE=true
 47
 48cargo_bundle_version=$(cargo -q bundle --help 2>&1 | head -n 1 || echo "")
 49if [ "$cargo_bundle_version" != "cargo-bundle v0.6.0-zed" ]; then
 50    cargo install cargo-bundle --git https://github.com/zed-industries/cargo-bundle.git --branch zed-deploy
 51fi
 52
 53echo "Compiling zed binaries"
 54cargo build ${build_flag} --package ${zed_crate} --package cli
 55
 56echo "Creating application bundle"
 57pushd crates/${zed_crate}
 58    channel=$(<RELEASE_CHANNEL)
 59    cp Cargo.toml Cargo.toml.backup
 60    sed \
 61        -i.backup -e \
 62        "s/package.metadata.bundle-${channel}/package.metadata.bundle/" \
 63        Cargo.toml
 64
 65    # TODO linux `zed_cli` does not get into this bundle despite being built
 66    bundle_path=$(cargo bundle ${build_flag} --select-workspace-root | xargs)
 67
 68    mv Cargo.toml.backup Cargo.toml
 69popd
 70
 71# For nightly, cut off the version out of the bundle name that `cargo bundle` always adds.
 72if [ "$channel" == "nightly" ]; then
 73    version="$(cargo metadata --no-deps --manifest-path crates/zed/Cargo.toml --offline --format-version=1 | jq -r '.packages | map(select(.name == "zed"))[0].version')"
 74    version_less_bundle_path=$(echo "$bundle_path" | sed "s/_$version//")
 75    mv "$bundle_path" "$version_less_bundle_path"
 76    bundle_path="$version_less_bundle_path"
 77fi
 78
 79# TODO linux
 80# Other Linux systems will need a different set of manipulations + a way to know which ones to do.
 81# If bundle_name is not set or empty, use the basename of $bundle_path
 82if [ -z "${bundle_name}" ]; then
 83    bundle_name=$(basename "${bundle_path}")
 84fi
 85# If bundle_name doesn't end in .deb, append it
 86if [[ "$bundle_name" != *.deb ]]; then
 87    bundle_name="$bundle_name.deb"
 88fi
 89
 90pushd target/
 91    rm -rf bundle/ 2>/dev/null || true
 92    dpkg-deb -x "${bundle_path}" bundle/
 93    dpkg-deb --control "${bundle_path}" bundle/DEBIAN
 94    mkdir -p bundle/usr/local/bin/
 95    mv bundle/usr/bin/Zed "bundle/usr/local/bin/zed-$channel"
 96    cp "${target_dir}/cli" "bundle/usr/local/bin/cli-$channel"
 97    ln -s "/usr/local/bin/cli-$channel" "bundle/usr/local/bin/zed"
 98    rm -rf bundle/usr/bin/
 99    dpkg-deb -b bundle/ "${target_dir}/${bundle_name}"
100    bundle_path="${PWD}/${target_dir}/${bundle_name}"
101popd
102echo "Bundled ${bundle_path}"