install.sh

  1#!/usr/bin/env sh
  2set -eu
  3
  4main() {
  5    platform="$(uname -s)"
  6    arch="$(uname -m)"
  7    channel="${ZED_CHANNEL:-stable}"
  8    temp="$(mktemp -d "/tmp/zed-XXXXX")"
  9
 10    if [ "$platform" = "Darwin" ]; then
 11        platform="macos"
 12    elif [ "$platform" = "Linux" ]; then
 13        platform="linux"
 14        channel="${ZED_CHANNEL:-preview}"
 15    else
 16        echo "Unsupported platform $platform"
 17        exit 1
 18    fi
 19
 20    case "$platform-$arch" in
 21        macos-arm64*)
 22            arch="aarch64"
 23            ;;
 24        macos-x86* | linux-x86* | linux-i686*)
 25            arch="x86_64"
 26            ;;
 27        *)
 28            echo "Unsupported platform or architecture"
 29            exit 1
 30            ;;
 31    esac
 32
 33    if which curl >/dev/null 2>&1; then
 34        curl () {
 35            command curl -fL "$@"
 36        }
 37    elif which wget >/dev/null 2>&1; then
 38        curl () {
 39    	    wget -O- "$@"
 40        }
 41    else
 42    	echo "Could not find 'curl' or 'wget' in your path"
 43    	exit 1
 44    fi
 45
 46    "$platform" "$@"
 47}
 48
 49linux() {
 50    if [[ -n "${ZED_BUNDLE_PATH:-}" ]]; then
 51        cp "$ZED_BUNDLE_PATH" "$temp/zed-linux-$arch.tar.gz"
 52    else
 53        echo "Downloading Zed"
 54        curl "https://zed.dev/api/releases/$channel/latest/zed-linux-$arch.tar.gz" > "$temp/zed-linux-$arch.tar.gz"
 55    fi
 56
 57    suffix=""
 58    if [ "$channel" != "stable" ]; then
 59        suffix="-$channel"
 60    fi
 61
 62    appid=""
 63    case "$channel" in
 64      stable)
 65        appid="dev.zed.Zed"
 66        ;;
 67      nightly)
 68        appid="dev.zed.Zed-Nightly"
 69        ;;
 70      preview)
 71        appid="dev.zed.Zed-Preview"
 72        ;;
 73      dev)
 74        appid="dev.zed.Zed-Dev"
 75        ;;
 76      *)
 77        echo "Unknown release channel: ${channel}. Using stable app ID."
 78        appid="dev.zed.Zed"
 79        ;;
 80    esac
 81
 82    # Unpack
 83    rm -rf "$HOME/.local/zed$suffix.app"
 84    mkdir -p "$HOME/.local/zed$suffix.app"
 85    tar -xzf "$temp/zed-linux-$arch.tar.gz" -C "$HOME/.local/"
 86
 87    # Setup ~/.local directories
 88    mkdir -p "$HOME/.local/bin" "$HOME/.local/share/applications"
 89
 90    # Link the binary
 91    if [ -f "$HOME/.local/zed$suffix.app/bin/zed" ]; then
 92        ln -sf "$HOME/.local/zed$suffix.app/bin/zed" "$HOME/.local/bin/zed"
 93    else
 94        # support for versions before 0.139.x.
 95        ln -sf "$HOME/.local/zed$suffix.app/bin/cli" "$HOME/.local/bin/zed"
 96    fi
 97
 98    # Copy .desktop file
 99    desktop_file_path="$HOME/.local/share/applications/${appid}.desktop"
100    cp "$HOME/.local/zed$suffix.app/share/applications/zed$suffix.desktop" "${desktop_file_path}"
101    sed -i "s|Icon=zed|Icon=$HOME/.local/zed$suffix.app/share/icons/hicolor/512x512/apps/zed.png|g" "${desktop_file_path}"
102    sed -i "s|Exec=zed|Exec=$HOME/.local/zed$suffix.app/libexec/zed-editor|g" "${desktop_file_path}"
103
104    if which "zed" >/dev/null 2>&1; then
105        echo "Zed has been installed. Run with 'zed'"
106    else
107        echo "To run Zed from your terminal, you must add ~/.local/bin to your PATH"
108        echo "Run:"
109        echo "   echo 'export PATH=\$HOME/.local/bin:\$PATH' >> ~/.bashrc"
110        echo "   source ~/.bashrc"
111        echo "To run Zed now, '~/.local/bin/zed'"
112    fi
113}
114
115macos() {
116    echo "Downloading Zed"
117    curl "https://zed.dev/api/releases/$channel/latest/Zed-$arch.dmg" > "$temp/Zed-$arch.dmg"
118    hdiutil attach -quiet "$temp/Zed-$arch.dmg" -mountpoint "$temp/mount"
119    app="$(cd "$temp/mount/"; echo *.app)"
120    echo "Installing $app"
121    if [ -d "/Applications/$app" ]; then
122        echo "Removing existing $app"
123        rm -rf "/Applications/$app"
124    fi
125    ditto "$temp/mount/$app" "/Applications/$app"
126    hdiutil detach -quiet "$temp/mount"
127
128    mkdir -p "$HOME/.local/bin"
129    # Link the binary
130    ln -sf "/Applications/$app/Contents/MacOS/cli" "$HOME/.local/bin/zed"
131
132    if which "zed" >/dev/null 2>&1; then
133        echo "Zed has been installed. Run with 'zed'"
134    else
135        echo "To run Zed from your terminal, you must add ~/.local/bin to your PATH"
136        echo "Run:"
137        echo "   echo 'export PATH=\$HOME/.local/bin:\$PATH' >> ~/.bashrc"
138        echo "   source ~/.bashrc"
139        echo "To run Zed now, '~/.local/bin/zed'"
140    fi
141}
142
143main "$@"