#!/usr/bin/env bash # LLeMbas IDE from nothing: a complete Neovim editor with LLeMbas built in, for this user only. # # curl -fsSL https://github.com/LLeMbas/LLeMbas-IDE/releases/latest/download/install.sh | bash # curl -fsSL <…>/install.sh | bash -s -- --channel beta # options go after --; the betas # ide/install.sh --from-checkout # from a clone of the repository # # Run it with --help for what it installs and where, the options and the environment. In short: # everything goes under ~/.local/share/llembas-ide, ~/.config/llembas-ide and ~/.local/bin, for # this user only; nothing runs as root; a failure leaves the previous installation, or nothing. set -euo pipefail FORGE="${LLEMBAS_IDE_FORGE:-https://github.com}" FORGE="${FORGE%/}" SLUG="${LLEMBAS_IDE_SLUG:-LLeMbas/LLeMbas-IDE}" # The key LLeMbas releases are signed with (the LLeMbas CLI's get.sh checks the same one): a # release without a good SHA256SUMS.sig (ssh-keygen -Y sign, namespace lembas-release) is refused. # Set LLEMBAS_IDE_PUBKEY to another key for a build of your own, or to nothing for an unsigned # one, which is then checked against its own SHA256SUMS only. PUBKEY="${LLEMBAS_IDE_PUBKEY-ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIDSZO3Byow7R3ZpOH3MCvpPIga2pZBzhfX5wXfNP1cLa}" SIGN_NAMESPACE="lembas-release" CLI_SLUG="LLeMbas/LLeMbas-CLI" CLI_HOME="https://github.com/LLeMbas/LLeMbas-CLI" NVIM_RELEASES="https://github.com/neovim/neovim/releases/download" APP="llembas-ide" MIN_NVIM="0.10" VERSION_WANTED="" CHANNEL="stable" PREFIX="" FROM_CHECKOUT=0 FROM_DIR="" NVIM_BIN="" WITH_CLI=1 WITH_PARSERS=1 YES=0 DRY=0 TMP="" STAGE="" OLD="" MADE="" SWAPPED=() PLACED=() DONE=0 # Everything is inside functions called on the last line: a download cut short runs nothing. say() { printf '%s\n' "$*"; } note() { printf ' %s\n' "$*"; } warn() { printf 'install.sh: %s\n' "$*" >&2; } die() { printf 'install.sh: %s\n' "$*" >&2 exit 1 } # One line per action. With --dry-run the line is all that happens. step() { if [ "$DRY" = 1 ]; then printf ' would %s\n' "$*"; else printf ' %s\n' "$*"; fi } rollback() { local i unit # Files placed outside the prefix, newest first: the old one back, or gone if there was none. for ((i = ${#PLACED[@]} - 1; i >= 0; i--)); do unit="${PLACED[$i]}" rm -f "$unit" if [ -e "$OLD/files/$i" ]; then mv "$OLD/files/$i" "$unit"; fi done for ((i = ${#SWAPPED[@]} - 1; i >= 0; i--)); do unit="${SWAPPED[$i]}" rm -rf "${PREFIX:?}/$unit" if [ -e "$OLD/$unit" ]; then mv "$OLD/$unit" "$PREFIX/$unit"; fi done } cleanup() { local status=$? trap - EXIT if [ "$DONE" != 1 ] && [ -n "$OLD" ] && [ -d "$OLD" ]; then rollback || true warn "nothing was changed: the installation is as it was before." fi if [ "$status" != 0 ] && [ "$DONE" != 1 ] && { [ -z "$OLD" ] || [ ! -d "$OLD" ]; } && [ -n "$STAGE" ]; then warn "stopped before anything was put in place: nothing was installed or changed." fi [ -z "$TMP" ] || rm -rf "$TMP" [ -z "$STAGE" ] || rm -rf "$STAGE" [ -z "$OLD" ] || rm -rf "$OLD" # A prefix this run made and left empty goes again. if [ -n "$PREFIX" ] && [ -d "$PREFIX" ] && [ "$DONE" != 1 ]; then rmdir "$PREFIX/site/pack" "$PREFIX/site" "$PREFIX" 2>/dev/null || true if [ ! -d "$PREFIX" ] && [ -n "${MADE:-}" ]; then while IFS= read -r made; do [ -z "$made" ] || rmdir "$made" 2>/dev/null || break done <<<"$MADE" fi fi exit "$status" } # The help is text of its own, not the comment at the top read back from the file: piped into # bash (curl … | bash -s -- --help) there is no file to read it from. usage() { cat <<'HELP' LLeMbas IDE from nothing: a complete Neovim editor with LLeMbas built in, for this user only. curl -fsSL /install.sh | bash -s -- --channel beta # options go after -- ide/install.sh --from-checkout # from a clone of the repository What it installs, and where (nothing else is written, nothing runs as root): /nvim/ a pinned Neovim release, its SHA-256 checked (ide/neovim.sha256) /site/pack/llembas/ the IDE's configuration and its plugins, each plugin fetched with git at the one commit ide/plugins.lock names /lib/ the LLeMbas plugin, this installer, the lock files ~/.config/llembas-ide/ init.lua, and lua/user/init.lua: yours, never overwritten ~/.local/bin/llembas-ide the launcher (NVIM_APPNAME=llembas-ide: your own Neovim configuration in ~/.config/nvim is never read or written) ~/.local/share/applications/llembas-ide.desktop and an icon (Linux) is ${XDG_DATA_HOME:-~/.local/share}/llembas-ide unless --prefix says otherwise. Everything is built in a staging directory and tried there before it replaces what is installed; a failure leaves the previous installation, or nothing, exactly as it was. Running it again updates in place. Options: --version X install release X (1.2.3 or v1.2.3) instead of the newest --channel NAME stable (default) or beta: which releases "newest" looks at --prefix DIR where the IDE itself goes --from-checkout install from the repository this script sits in, not from a release --nvim-bin PATH use this Neovim (0.10 or newer) instead of downloading the pinned one --no-cli do not offer to install the LLeMbas CLI --no-parsers do not compile the Treesitter grammars --yes ask nothing: yes to the CLI offer --dry-run print every action, do nothing Environment: LLEMBAS_IDE_FORGE where releases come from (default https://github.com) LLEMBAS_IDE_SLUG owner/name there (default LLeMbas/LLeMbas-IDE) LLEMBAS_IDE_RELEASE_BASE the releases' download directory, when it is not the forge's own: an https:// address, or file:// for a release on this machine LLEMBAS_IDE_PUBKEY the ssh-ed25519 key a release's SHA256SUMS.sig must be signed with LLEMBAS_IDE_GIT_MIRROR a directory of git repositories named like the plugins' own, to fetch from instead of the network; the commit is checked all the same LLEMBAS_IDE_CLI_VERSION the LLeMbas CLI release to offer (2.0.0 or v2.0.0) instead of the newest of the channel LLEMBAS_IDE_CLI_RELEASE_BASE the CLI releases' download directory, when it is not the forge's own Needs bash, git, curl, tar, sha256sum (or shasum) and ssh-keygen (openssh-client: it checks the release's signature). A C compiler is used when there is one. Linux x86-64 and arm64. macOS is written for and has not been tried. HELP } need() { command -v "$1" >/dev/null 2>&1 || die "needs $1, which is not installed${2:+ ($2)}" } sha256_of() { if command -v sha256sum >/dev/null 2>&1; then sha256sum "$1" | cut -d' ' -f1 elif command -v shasum >/dev/null 2>&1; then shasum -a 256 "$1" | cut -d' ' -f1 else die "needs sha256sum or shasum to check what it downloads" fi } # Only https, or a file on this machine: nothing is fetched over a connection that cannot be trusted. fetch() { local url="$1" out="$2" case "$url" in https://* | file:///*) ;; *) die "refusing to download $url: only https:// (or a local file://) is used" ;; esac curl -fsSL --retry 2 --proto '=https,file' --proto-redir '=https' -o "$out" "$url" } # Questions go to the terminal: piped into bash, this script's stdin is the script itself. has_tty() { [ -r /dev/tty ] && { : /dev/null; } ask_yes() { local answer="" [ "$YES" = 1 ] && return 0 has_tty || return 1 printf '%s [y/N] ' "$1" >/dev/tty read -r answer /dev/null && return 0 [ -x "$HOME/.local/bin/lembas" ] && printf '%s\n' "$HOME/.local/bin/lembas" && return 0 return 1 } # What was changed in a plugin's directory since its commit was checked out: files edited or # deleted, and files added (ignored ones too: Neovim loads whatever lies there). The help tags # this installer writes itself are not a change. changed_in() { git_quiet -C "$1" status --porcelain --untracked-files=all --ignored 2>/dev/null | grep -v -E '^(\?\?|!!) doc/tags$' || true } # --- which release ----------------------------------------------------------------------------- api_base() { if [ "$FORGE" = https://github.com ]; then echo https://api.github.com; else echo "$FORGE/api/v1"; fi } # The newest release tag of the channel, by version: vX.Y.Z, and for beta also vX.Y.Z-beta.N # (a beta sorts below its own release). newest_tag() { # [owner/name] [channel]: this product's own and the chosen channel unless said local slug="${1:-$SLUG}" channel="${2:-$CHANNEL}" json tags json="$(curl -fsSL --proto '=https' --proto-redir '=https' "$(api_base)/repos/$slug/releases?per_page=50&limit=50")" || return 1 tags="$(printf '%s' "$json" | tr ',' '\n' | sed -n 's/.*"tag_name": *"\([^"]*\)".*/\1/p')" if [ "$channel" = beta ]; then tags="$(printf '%s\n' "$tags" | grep -E '^v[0-9]+\.[0-9]+\.[0-9]+(-beta\.[0-9]+)?$' || true)" else tags="$(printf '%s\n' "$tags" | grep -E '^v[0-9]+\.[0-9]+\.[0-9]+$' || true)" fi printf '%s\n' "$tags" | sed '/^$/d' | sed -E 's/^v//; s/-beta\.([0-9]+)$/.beta.\1/; /beta/!s/$/.zz/' | sort -V | tail -n1 | sed -E 's/\.zz$//; s/\.beta\.([0-9]+)$/-beta.\1/; s/^/v/' } check_signature() { local dir="$1" if [ -z "$PUBKEY" ]; then note "checked against the release's SHA256SUMS only: LLEMBAS_IDE_PUBKEY is empty, so no signature is asked for" return 0 fi need ssh-keygen "openssh-client; it checks the release's signature" [ -s "$dir/SHA256SUMS.sig" ] || die "the release is not signed (no SHA256SUMS.sig) — nothing installed" printf '%s %s\n' "$SIGN_NAMESPACE" "$PUBKEY" >"$dir/allowed" ssh-keygen -Y verify -f "$dir/allowed" -I "$SIGN_NAMESPACE" -n "$SIGN_NAMESPACE" -s "$dir/SHA256SUMS.sig" <"$dir/SHA256SUMS" >/dev/null 2>&1 || die "SHA256SUMS is not signed by the release key — nothing installed" note "signature good (release key)" } # No release in hand: fetch one, check it, and hand over to the installer inside it. from_release() { local tag version base tarball top listed need curl need tar if [ -n "$VERSION_WANTED" ]; then tag="v${VERSION_WANTED#v}" elif [ "$DRY" = 1 ]; then say "LLeMbas IDE (dry run: nothing is downloaded or written)" step "ask $(api_base)/repos/$SLUG/releases for the newest $CHANNEL release" step "download its llembas-ide-.tar.gz and SHA256SUMS, and check the one against the other" step "unpack it in a temporary directory and run the installer inside it with the same options" note "what that installer does is listed by: ide/install.sh --from-checkout --dry-run" return 0 else tag="$(newest_tag)" || die "could not list the releases at $FORGE/$SLUG (network, or a rate limit) — nothing installed" if [ -z "$tag" ]; then [ "$CHANNEL" = stable ] && die "there is no stable release of LLeMbas IDE yet — nothing installed. The betas: add --channel beta (piped: … | bash -s -- --channel beta)" die "there is no release at $FORGE/$SLUG — nothing installed" fi fi version="${tag#v}" [[ "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+(-beta\.[0-9]+)?$ ]] || die "not a version: $version" base="${LLEMBAS_IDE_RELEASE_BASE:-$FORGE/$SLUG/releases/download}" base="${base%/}/$tag" # Said here, in words: the first download below keeps curl quiet, and would keep this quiet too. case "$base" in https://* | file:///*) ;; *) die "refusing to download from $base: only https:// (or a local file://) is used — nothing installed" ;; esac tarball="llembas-ide-$version.tar.gz" if [ "$DRY" = 1 ]; then say "LLeMbas IDE $version (dry run: nothing is downloaded or written)" step "download $base/$tarball and $base/SHA256SUMS, and check the one against the other" step "unpack it in a temporary directory and run the installer inside it with the same options" note "what that installer does is listed by: ide/install.sh --from-checkout --dry-run" return 0 fi TMP="$(mktemp -d)" say "LLeMbas IDE $version" note "downloading $tarball" fetch "$base/$tarball" "$TMP/$tarball" 2>/dev/null || die "could not download $tarball: release $tag has no LLeMbas IDE in it, or the network failed — nothing installed" fetch "$base/SHA256SUMS" "$TMP/SHA256SUMS" || die "could not download SHA256SUMS from release $tag — nothing installed" if [ -n "$PUBKEY" ]; then fetch "$base/SHA256SUMS.sig" "$TMP/SHA256SUMS.sig" 2>/dev/null || rm -f "$TMP/SHA256SUMS.sig"; fi check_signature "$TMP" listed="$(awk -v f="$tarball" '$2 == f || $2 == "*" f { print $1 }' "$TMP/SHA256SUMS")" [ "$(printf '%s\n' "$listed" | sed '/^$/d' | wc -l | tr -d ' ')" = 1 ] || die "SHA256SUMS does not list $tarball exactly once — nothing installed" [ "$listed" = "$(sha256_of "$TMP/$tarball")" ] || die "$tarball does not match SHA256SUMS — nothing installed" note "checksum good" # Nothing in the archive may land outside the directory it is unpacked into. top="llembas-ide-$version" if tar -tzf "$TMP/$tarball" | grep -v -E "^$top(/|\$)" | grep -q .; then die "$tarball holds files outside $top/ — nothing installed"; fi if tar -tzf "$TMP/$tarball" | grep -q -E '(^|/)\.\.(/|$)'; then die "$tarball holds a path with .. — nothing installed"; fi # Files and directories only: a link could point anywhere, and the IDE has none. if tar -tvzf "$TMP/$tarball" | grep -q -E '^[lh]'; then die "$tarball holds a link — nothing installed"; fi mkdir "$TMP/payload" tar -xzf "$TMP/$tarball" -C "$TMP/payload" [ "$(tr -d '[:space:]' <"$TMP/payload/$top/VERSION")" = "$version" ] || die "release $tag carries the files of another version — nothing installed" [ -f "$TMP/payload/$top/ide/install.sh" ] || die "release $tag has no installer in it — nothing installed" local args=(--from-dir "$TMP/payload/$top") [ -z "$PREFIX" ] || args+=(--prefix "$PREFIX") [ -z "$NVIM_BIN" ] || args+=(--nvim-bin "$NVIM_BIN") [ "$WITH_CLI" = 1 ] || args+=(--no-cli) [ "$WITH_PARSERS" = 1 ] || args+=(--no-parsers) [ "$YES" = 0 ] || args+=(--yes) bash "$TMP/payload/$top/ide/install.sh" "${args[@]}" } # --- the parts --------------------------------------------------------------------------------- nvim_asset() { case "$(uname -s)-$(uname -m)" in Linux-x86_64) echo nvim-linux-x86_64 ;; Linux-aarch64 | Linux-arm64) echo nvim-linux-arm64 ;; Darwin-arm64) echo nvim-macos-arm64 ;; Darwin-x86_64) echo nvim-macos-x86_64 ;; *) return 1 ;; esac } # "NVIM v0.10.4" is at least MIN_NVIM. nvim_new_enough() { local v major minor v="$(nvim_quiet "$1" --version 2>/dev/null | sed -n '1s/^NVIM v\([0-9][0-9.]*\).*/\1/p')" [ -n "$v" ] || return 1 major="${v%%.*}" minor="${v#*.}" minor="${minor%%.*}" [ "$major" -gt "${MIN_NVIM%%.*}" ] || { [ "$major" -eq "${MIN_NVIM%%.*}" ] && [ "$minor" -ge "${MIN_NVIM#*.}" ]; } } # A git repository at exactly one commit: fetched by its hash, and the hash is the check. fetch_commit() { local url="$1" commit="$2" dest="$3" from="$1" mirror="${LLEMBAS_IDE_GIT_MIRROR:-}" if [ -n "$mirror" ] && [ -d "$mirror/$(basename "$url")" ]; then from="$mirror/$(basename "$url")"; fi mkdir -p "$dest" git_quiet -C "$dest" init -q git_quiet -C "$dest" remote add origin "$url" git_quiet -C "$dest" fetch -q --depth 1 "$from" "$commit" 2>/dev/null || die "could not fetch $url at $commit (network?) — nothing installed" git_quiet -C "$dest" -c advice.detachedHead=false checkout -q --detach FETCH_HEAD [ "$(git_quiet -C "$dest" rev-parse HEAD)" = "$commit" ] || die "$url did not give commit $commit — nothing installed" } # An installed plugin is at its pin, and no file in it was changed or added. at_pin() { local dir="$1" commit="$2" [ -d "$dir/.git" ] || return 1 [ "$(git_quiet -C "$dir" rev-parse HEAD 2>/dev/null)" = "$commit" ] || return 1 [ -z "$(changed_in "$dir")" ] } stage_neovim() { local src="$1" version asset want got dir="$STAGE/nvim" if [ -n "$NVIM_BIN" ]; then step "use the Neovim at $NVIM_BIN instead of the pinned release" [ "$DRY" = 1 ] && return 0 printf '%s\n' "$NVIM_BIN" >"$STAGE/lib/nvim-path" printf 'own %s\n' "$NVIM_BIN" >"$STAGE/lib/nvim-source" return 0 fi version="$(tr -d '[:space:]' <"$src/ide/neovim.version")" asset="$(nvim_asset)" || die "there is no Neovim release build for $(uname -s) $(uname -m); install Neovim $MIN_NVIM or newer yourself and pass --nvim-bin " want="$(awk -v f="$asset.tar.gz" '$2 == f { print $1 }' "$src/ide/neovim.sha256")" [ -n "$want" ] || die "ide/neovim.sha256 has no checksum for $asset.tar.gz" if [ -f "$PREFIX/lib/nvim-source" ] && [ "$(cat "$PREFIX/lib/nvim-source")" = "release $version $want" ] && [ -x "$PREFIX/nvim/bin/nvim" ]; then step "keep Neovim $version (already installed, checksum $want)" [ "$DRY" = 1 ] && return 0 cp -a "$PREFIX/nvim" "$dir" else step "download $NVIM_RELEASES/v$version/$asset.tar.gz" step "check its SHA-256 is $want, and unpack it to $PREFIX/nvim" [ "$DRY" = 1 ] && return 0 fetch "$NVIM_RELEASES/v$version/$asset.tar.gz" "$TMP/$asset.tar.gz" || die "could not download Neovim $version — nothing installed" got="$(sha256_of "$TMP/$asset.tar.gz")" [ "$got" = "$want" ] || die "Neovim $version's download has SHA-256 $got, not the pinned $want — nothing installed" if tar -tzf "$TMP/$asset.tar.gz" | grep -v -E "^$asset(/|\$)" | grep -q .; then die "the Neovim archive holds files outside $asset/ — nothing installed"; fi mkdir "$TMP/nvim" tar -xzf "$TMP/$asset.tar.gz" -C "$TMP/nvim" mv "$TMP/nvim/$asset" "$dir" fi printf '%s\n' "$PREFIX/nvim/bin/nvim" >"$STAGE/lib/nvim-path" printf 'release %s %s\n' "$version" "$want" >"$STAGE/lib/nvim-source" } stage_plugins() { local src="$1" name where url commit licence rest dest have while read -r name where url commit licence rest; do [[ "$commit" =~ ^[0-9a-f]{40}$ ]] || die "ide/plugins.lock: $name is not pinned to a full commit" case "$url" in https://*) ;; *) die "ide/plugins.lock: $name does not come over https" ;; esac case "$where" in start | opt) ;; *) die "ide/plugins.lock: $name is neither start nor opt" ;; esac dest="$STAGE/site/pack/llembas/$where/$name" have="$PREFIX/site/pack/llembas/$where/$name" if at_pin "$have" "$commit"; then step "keep $name at $commit (already installed, unchanged)" [ "$DRY" = 1 ] || { mkdir -p "$(dirname "$dest")" && cp -a "$have" "$dest"; } else step "fetch $name from $url at commit $commit ($licence)" [ "$DRY" = 1 ] || fetch_commit "$url" "$commit" "$dest" fi done < <(lock_lines "$src/ide/plugins.lock") } stage_own() { local src="$1" dest="$STAGE/site/pack/llembas/start/llembas-ide" step "place the IDE's configuration (ide/config) as the package llembas-ide" step "place the LLeMbas plugin (nvim/) $(tr -d '[:space:]' <"$src/VERSION")" [ "$DRY" = 1 ] && return 0 mkdir -p "$dest" "$STAGE/lib/llembas" cp -R "$src/ide/config/lua" "$src/ide/config/colors" "$dest/" [ -f "$src/nvim/plugin/llembas.lua" ] || die "the LLeMbas plugin (nvim/) is not in $src — nothing installed" cp -R "$src/nvim" "$STAGE/lib/llembas/nvim" rm -rf "$STAGE/lib/llembas/nvim/tests" # The plugin reads its version from the VERSION file beside its directory. cp "$src/VERSION" "$STAGE/lib/llembas/VERSION" cp "$src/VERSION" "$STAGE/lib/VERSION" mkdir -p "$STAGE/lib/ide" cp "$src/ide/install.sh" "$src/ide/plugins.lock" "$src/ide/parsers.lock" "$src/ide/neovim.version" \ "$src/ide/neovim.sha256" "$src/ide/user-init.lua" "$src/ide/THIRD_PARTY.md" "$STAGE/lib/ide/" cp -R "$src/ide/bin" "$src/ide/assets" "$src/ide/config" "$STAGE/lib/ide/" [ ! -f "$src/LICENSE" ] || cp "$src/LICENSE" "$STAGE/lib/LICENSE" } c_compiler() { local c for c in cc gcc clang; do if command -v "$c" >/dev/null 2>&1; then echo "$c" return 0 fi done return 1 } # The Treesitter grammars, all or none: a half set would colour some files by one rule and some # by another. Without them the IDE uses the grammars Neovim itself carries. stage_parsers() { local src="$1" cc lang url commit dir licence rest sources out have="$PREFIX/site/pack/llembas/start/llembas-parsers" local dest="$STAGE/site/pack/llembas/start/llembas-parsers" shared="-shared" count count="$(lock_lines "$src/ide/parsers.lock" | wc -l | tr -d ' ')" if [ "$WITH_PARSERS" != 1 ]; then step "leave the Treesitter grammars out (--no-parsers): Neovim's bundled ones are used" return 0 fi if ! cc="$(c_compiler)"; then step "leave the Treesitter grammars out: no C compiler here (cc, gcc or clang). Neovim's bundled ones are used; install a compiler and run this again for all $count" return 0 fi if [ -f "$PREFIX/lib/ide/parsers.lock" ] && cmp -s "$PREFIX/lib/ide/parsers.lock" "$src/ide/parsers.lock" && [ "$(find "$have/parser" -name '*.so' 2>/dev/null | wc -l | tr -d ' ')" = "$count" ]; then step "keep the $count compiled Treesitter grammars (already installed, same pins)" [ "$DRY" = 1 ] || { mkdir -p "$(dirname "$dest")" && cp -a "$have" "$dest"; } return 0 fi [ "$(uname -s)" != Darwin ] || shared="-bundle" if [ "$DRY" = 1 ]; then while read -r lang url commit dir licence rest; do step "fetch the $lang grammar from $url at commit $commit ($licence) and compile it with $cc" done < <(lock_lines "$src/ide/parsers.lock") return 0 fi note "compiling $count Treesitter grammars with $cc (a minute or two)" mkdir -p "$dest/parser" "$TMP/grammars" while read -r lang url commit dir licence rest; do [[ "$commit" =~ ^[0-9a-f]{40}$ ]] || die "ide/parsers.lock: $lang is not pinned to a full commit" case "$url" in https://*) ;; *) die "ide/parsers.lock: $lang does not come over https" ;; esac [ -d "$TMP/grammars/$commit" ] || fetch_commit "$url" "$commit" "$TMP/grammars/$commit" out="$TMP/grammars/$commit/$dir/src" sources=("$out/parser.c") [ ! -f "$out/scanner.c" ] || sources+=("$out/scanner.c") if [ ! -f "$out/parser.c" ] || [ -f "$out/scanner.cc" ] || ! "$cc" -o "$dest/parser/$lang.so" -I "$out" "${sources[@]}" -Os -fPIC "$shared" 2>"$TMP/cc.log"; then warn "the $lang grammar did not compile with $cc ($(head -n1 "$TMP/cc.log" 2>/dev/null)); the IDE is installed without compiled grammars and uses Neovim's bundled ones." rm -rf "$dest" return 0 fi done < <(lock_lines "$src/ide/parsers.lock") step "compiled $count Treesitter grammars" } # Help tags for every plugin that has documentation and no tags yet, so :help finds them. stage_helptags() { local nvim="$1" d [ "$DRY" = 1 ] && return 0 for d in "$STAGE"/site/pack/llembas/*/*/doc "$STAGE/lib/llembas/nvim/doc"; do if [ ! -d "$d" ] || [ -f "$d/tags" ]; then continue; fi nvim_quiet "$nvim" --headless -u NONE -i NONE -c "silent! helptags $d" -c qa >/dev/null 2>&1 || true done } # The whole thing started once where it was built, away from the person's own files: a # configuration that does not start is never swapped in. try_staged() { local src="$1" nvim="$2" out step "start the staged IDE once, headless, to see that every part loads" [ "$DRY" = 1 ] && return 0 mkdir -p "$TMP/try/config/$APP" "$TMP/try/state" "$TMP/try/cache" "$TMP/try/data" cp "$src/ide/config/init.lua" "$TMP/try/config/$APP/init.lua" out="$(XDG_CONFIG_HOME="$TMP/try/config" XDG_STATE_HOME="$TMP/try/state" XDG_CACHE_HOME="$TMP/try/cache" XDG_DATA_HOME="$TMP/try/data" \ NVIM_LOG_FILE="$TMP/nvim.log" NVIM_APPNAME="$APP" LLEMBAS_IDE_PREFIX="$STAGE" "$nvim" --headless \ --cmd 'lua vim.g.llembas_ide_no_session = true; vim.g.llembas_ide_no_dashboard = true' \ -c 'lua local ok, ide = pcall(require, "llembas_ide"); io.stdout:write(ok and ("problems=" .. #ide.problems .. " " .. table.concat(ide.problems, " | ")) or "problems=all the IDE did not load")' \ -c 'qa!' 2>&1)" || die "the staged IDE did not start ($out) — nothing installed" case "$out" in *"problems=0"*) ;; *) die "the staged IDE did not start cleanly (${out#*problems=}) — nothing installed" ;; esac } # Put one of the three managed directories in place, keeping the old one until the end. swap_unit() { local unit="$1" [ -e "$STAGE/$unit" ] || { # Nothing staged (no Neovim of our own with --nvim-bin): the old one still goes. if [ -e "$PREFIX/$unit" ]; then mkdir -p "$(dirname "$OLD/$unit")" mv "$PREFIX/$unit" "$OLD/$unit" SWAPPED+=("$unit") fi return 0 } mkdir -p "$(dirname "$OLD/$unit")" "$(dirname "$PREFIX/$unit")" SWAPPED+=("$unit") if [ -e "$PREFIX/$unit" ]; then mv "$PREFIX/$unit" "$OLD/$unit"; fi mv "$STAGE/$unit" "$PREFIX/$unit" } # Put one file in place outside the prefix, keeping what was there until the end. place_file() { local from="$1" to="$2" mode="$3" n="${#PLACED[@]}" mkdir -p "$(dirname "$to")" "$OLD/files" PLACED+=("$to") if [ -e "$to" ] || [ -L "$to" ]; then mv "$to" "$OLD/files/$n"; fi cp "$from" "$to.tmp.$$" chmod "$mode" "$to.tmp.$$" mv "$to.tmp.$$" "$to" } # The CLI's own installer (get.sh) from one release of the CLI, checked before it is run: the # release's SHA256SUMS must be signed with the release key, and get.sh must be the file it lists. # Run in a subshell by install_cli: a failure here ends the check, not the installation. checked_cli_installer() { # checked_cli_installer local base="$1" dir="$2" listed case "$base" in https://* | file:///*) ;; *) die "refusing to download from $base: only https:// (or a local file://) is used" ;; esac mkdir -p "$dir" fetch "$base/SHA256SUMS" "$dir/SHA256SUMS" 2>/dev/null || die "could not download the SHA256SUMS of the LLeMbas CLI's release" if [ -n "$PUBKEY" ]; then fetch "$base/SHA256SUMS.sig" "$dir/SHA256SUMS.sig" 2>/dev/null || rm -f "$dir/SHA256SUMS.sig"; fi check_signature "$dir" fetch "$base/get.sh" "$dir/get.sh" 2>/dev/null || die "could not download the LLeMbas CLI's installer (get.sh)" listed="$(awk -v f=get.sh '$2 == f || $2 == "*" f { print $1 }' "$dir/SHA256SUMS")" [ "$(printf '%s\n' "$listed" | sed '/^$/d' | wc -l | tr -d ' ')" = 1 ] || die "the LLeMbas CLI release's SHA256SUMS does not list get.sh exactly once" [ "$listed" = "$(sha256_of "$dir/get.sh")" ] || die "the LLeMbas CLI's installer does not match its release's SHA256SUMS" note "the LLeMbas CLI's installer matches its release" } install_cli() { local channel=stable version="$1" at tag base later [ "$WITH_CLI" = 1 ] || return 0 if at="$(find_cli)"; then step "leave the LLeMbas CLI alone: it is installed ($at)" return 0 fi later="Later: $APP update offers it again, or see $CLI_HOME" # A beta of the IDE goes with a beta of the CLI. case "$version" in *-beta.*) channel=beta ;; esac if [ "$(uname -s)" != Linux ]; then step "leave the LLeMbas CLI out: its installer is for Linux. See $CLI_HOME" return 0 fi if [ "$DRY" = 1 ]; then step "offer to install the LLeMbas CLI ($channel): its own installer (get.sh) from its newest $channel release at $CLI_HOME, run only once it matches that release's signed SHA256SUMS" return 0 fi say "" say "The LLeMbas CLI (the lembas command) is the agent the IDE talks to, and it is not installed." if ! ask_yes "Install it now with its own installer?"; then note "not installed. $later" return 0 fi # Which release of the CLI: the one named, else the newest of the channel. if [ -n "${LLEMBAS_IDE_CLI_VERSION:-}" ]; then tag="v${LLEMBAS_IDE_CLI_VERSION#v}" else tag="$(newest_tag "$CLI_SLUG" "$channel" 2>/dev/null)" || tag="" fi if ! [[ "$tag" =~ ^v[0-9]+\.[0-9]+\.[0-9]+(-beta\.[0-9]+)?$ ]]; then warn "could not find a $channel release of the LLeMbas CLI (network, or a rate limit); the IDE is installed and works as an editor. $later" return 0 fi base="${LLEMBAS_IDE_CLI_RELEASE_BASE:-$FORGE/$CLI_SLUG/releases/download}" base="${base%/}/$tag" note "LLeMbas CLI ${tag#v}" # Its own installer, from that release, and only after checking it. It then checks the # program it downloads itself. if ! (checked_cli_installer "$base" "$TMP/cli"); then warn "the LLeMbas CLI's installer could not be checked, so it was not run; the IDE is installed and works as an editor. $later" return 0 fi if [ "$YES" = 1 ] || ! has_tty; then LEMBAS_REF="$tag" LEMBAS_CHANNEL="$channel" bash "$TMP/cli/get.sh" --yes || warn "the CLI's installer failed; the IDE itself is installed." else LEMBAS_REF="$tag" LEMBAS_CHANNEL="$channel" bash "$TMP/cli/get.sh" /dev/null || die "there is no Neovim release build for $(uname -s) $(uname -m); install Neovim $MIN_NVIM or newer yourself and pass --nvim-bin " fi [ ! -f "$PREFIX/lib/installed" ] || old_init_sha="$(sed -n 's/^init_sha=//p' "$PREFIX/lib/installed")" if [ "$DRY" = 1 ]; then say "LLeMbas IDE $version (dry run: nothing is downloaded or written)" else say "LLeMbas IDE $version" # The directories above the prefix that are not there yet: made now, and taken away again, # deepest first, if this run ends with nothing installed. MADE="" up="$(dirname "$PREFIX")" while [ ! -d "$up" ] && [ "$up" != / ] && [ "$up" != . ]; do MADE="$MADE$up"$'\n' up="$(dirname "$up")" done mkdir -p "$PREFIX" TMP="$(mktemp -d)" STAGE="$(mktemp -d "$PREFIX/.staging.XXXXXX")" mkdir -p "$STAGE/lib" "$STAGE/site/pack/llembas" fi step "build everything in a staging directory inside $PREFIX" stage_neovim "$src" stage_plugins "$src" stage_own "$src" stage_parsers "$src" if [ "$DRY" != 1 ]; then if [ -n "$NVIM_BIN" ]; then nvim="$NVIM_BIN"; else nvim="$STAGE/nvim/bin/nvim"; fi nvim_quiet "$nvim" --version >/dev/null 2>&1 || die "the Neovim at $nvim does not run on this machine — nothing installed" stage_helptags "$nvim" fi try_staged "$src" "${nvim:-}" step "swap the staged nvim/, site/pack/llembas/ and lib/ into $PREFIX" if [ -f "$config/init.lua" ] && [ "$(sha256_of "$config/init.lua")" != "$(sha256_of "$src/ide/config/init.lua")" ] && [ "$(sha256_of "$config/init.lua")" != "$old_init_sha" ]; then init_state=edited step "keep your edited $config/init.lua; the IDE's own goes beside it as init.lua.new" else init_state=ours step "write $config/init.lua" fi if [ -f "$config/lua/user/init.lua" ]; then step "keep your $config/lua/user/init.lua as it is" else step "create $config/lua/user/init.lua, yours: comments only" fi step "write the launcher $launcher" if [ "$(uname -s)" = Linux ]; then step "write the menu entry $desktop and its icon $icon" fi if [ "$DRY" = 1 ]; then install_cli "$version" say "Dry run: nothing was changed." return 0 fi { printf 'version=%s\n' "$version" printf 'config=%s\n' "$config" printf 'launcher=%s\n' "$launcher" printf 'init_sha=%s\n' "$(sha256_of "$src/ide/config/init.lua")" printf 'user_sha=%s\n' "$(sha256_of "$src/ide/user-init.lua")" [ "$WITH_PARSERS" = 1 ] || printf 'parsers=off\n' if [ "$(uname -s)" = Linux ]; then printf 'desktop=%s\n' "$desktop" printf 'icon=%s\n' "$icon" fi } >"$STAGE/lib/installed" OLD="$(mktemp -d "$PREFIX/.old.XXXXXX")" swap_unit nvim swap_unit site/pack/llembas swap_unit lib if [ "$init_state" = edited ]; then place_file "$src/ide/config/init.lua" "$config/init.lua.new" 644 else place_file "$src/ide/config/init.lua" "$config/init.lua" 644 fi if [ ! -f "$config/lua/user/init.lua" ]; then place_file "$src/ide/user-init.lua" "$config/lua/user/init.lua" 644 fi sed "s|^PREFIX=.*# set by install.sh\$|PREFIX='$(printf '%s' "$PREFIX" | sed "s/'/'\\\\\\\\''/g; s/[|&]/\\\\&/g")' # set by install.sh|" \ "$src/ide/bin/llembas-ide" >"$TMP/launcher" grep -q "^PREFIX='" "$TMP/launcher" || die "the launcher could not be written — nothing installed" place_file "$TMP/launcher" "$launcher" 755 if [ "$(uname -s)" = Linux ]; then { printf '[Desktop Entry]\nType=Application\nName=LLeMbas IDE\nGenericName=Text Editor\n' printf 'Comment=A complete Neovim editor with LLeMbas built in\n' printf 'Exec="%s" %%F\nTryExec=%s\nIcon=%s\nTerminal=true\n' "$launcher" "$launcher" "$APP" printf 'Categories=Development;TextEditor;IDE;\nKeywords=editor;code;neovim;llembas;\n' printf 'MimeType=text/plain;\nStartupNotify=false\n' } >"$TMP/desktop" place_file "$TMP/desktop" "$desktop" 644 place_file "$src/ide/assets/llembas-ide.png" "$icon" 644 fi "$launcher" --version >/dev/null 2>&1 || die "the launcher does not run — nothing installed" DONE=1 say "" say "LLeMbas IDE $version is installed. Start it with: $APP" case ":$PATH:" in *":$bin:"*) ;; *) note "$bin is not on your PATH: add it (export PATH=\"\$HOME/.local/bin:\$PATH\"), or run $launcher" ;; esac [ "$init_state" != edited ] || note "your edited init.lua was kept; compare it with init.lua.new. Your own settings are safer in lua/user/init.lua" note "check it any time: $APP doctor update: $APP update remove: $APP uninstall" install_cli "$version" } main() { trap cleanup EXIT while [ $# -gt 0 ]; do case "$1" in --version) VERSION_WANTED="${2:?--version needs a version}" && shift ;; --channel) CHANNEL="${2:?--channel needs stable or beta}" && shift ;; --prefix) PREFIX="${2:?--prefix needs a directory}" && shift ;; --from-checkout) FROM_CHECKOUT=1 ;; --from-dir) FROM_DIR="${2:?--from-dir needs a directory}" && shift ;; --nvim-bin) NVIM_BIN="${2:?--nvim-bin needs a path}" && shift ;; --no-cli) WITH_CLI=0 ;; --no-parsers) WITH_PARSERS=0 ;; --yes | -y) YES=1 ;; --dry-run) DRY=1 ;; -h | --help) usage return 0 ;; *) die "unknown option $1 (--help lists them)" ;; esac shift done case "$CHANNEL" in stable | beta) ;; *) die "--channel is stable or beta" ;; esac if [ -z "${HOME:-}" ] || [ ! -d "$HOME" ]; then die "HOME is not set to a directory"; fi [ "$(id -u)" != 0 ] || warn "running as root: LLeMbas IDE is installed for one user and needs no root." if [ -n "$FROM_DIR" ]; then install_from "$(cd "$FROM_DIR" && pwd)" elif [ "$FROM_CHECKOUT" = 1 ]; then [ -f "${BASH_SOURCE[0]:-}" ] || die "--from-checkout needs this script run as a file inside a clone of the repository" install_from "$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" else from_release fi } main "$@"