install.sh

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