diff --git a/ide/README.md b/ide/README.md index a05d0ef..b1b879c 100644 --- a/ide/README.md +++ b/ide/README.md @@ -21,7 +21,9 @@ the IDE. curl -fsSL https://github.com/LLeMbas/LLeMbas-IDE/releases/latest/download/install.sh | bash ``` -While LLeMbas IDE is in beta there is no stable release for that to find; ask for the betas: +While LLeMbas IDE is in beta there is no stable release for that to find; take a beta's own +installer and ask for the betas (`` from the +[releases page](https://github.com/LLeMbas/LLeMbas-IDE/releases)): ```sh curl -fsSL https://github.com/LLeMbas/LLeMbas-IDE/releases/download/v/install.sh | bash -s -- --channel beta @@ -32,7 +34,8 @@ From a clone of this repository: `ide/install.sh --from-checkout`. Then start it with `llembas-ide` (any arguments go to Neovim: `llembas-ide src/main.c`). Linux on x86-64 and arm64. macOS is written for and has not been tried. It needs `bash`, `git`, -`curl`, `tar` and `sha256sum`; nothing runs as root. Good to have: a C compiler (`cc`) for the +`curl`, `tar`, `sha256sum` and `ssh-keygen` (the `openssh-client` package: it checks the +release's signature); nothing runs as root. `curl … | bash -s -- --help` prints every option. Good to have: a C compiler (`cc`) for the Treesitter grammars, `rg` (ripgrep) for searching in files, a Nerd Font is not needed. | Option | What it does | @@ -273,29 +276,29 @@ first one). In an empty prompt `` switches ask, rewrite, add; no menu opens | Keys | In | What it does | |---|---|---| -| ` or la` | normal, visual | To the chat prompt; the selection, or else the cursor's line, comes along | +| ` or la` | normal, visual; the chord also in insert | To the chat prompt; the selection, or else the cursor's line, comes along | | `le` | normal, visual | To the prompt, to have the selection or the line rewritten (shown as changes) | | `lg` | normal, visual | To the prompt, to have new code written at the cursor (shown as changes) | | `lx` | normal, visual | Explain the line or the selection | | `lf` | normal | Fix this file's problems | | `` | insert, with a suggestion | Accept the suggestion (the grey text); without one, Neovim's own Tab | | `` | insert, with a suggestion | Accept one word of the suggestion | -| ` or lt` | normal | Suggestions as you type, on and off | -| ` or lC` | normal | Choose the model that writes suggestions | -| ` or lp` | normal | The LLeMbas menu: every command and every setting | -| ` or lm` | normal | Switch the mode | -| ` or lM` | normal | Choose the model | +| ` or lt` | normal; the chord also in visual, insert | Suggestions as you type, on and off | +| ` or lC` | normal; the chord also in visual, insert | Choose the model that writes suggestions | +| ` or lp` | normal; the chord also in visual, insert | The LLeMbas menu: every command and every setting | +| ` or lm` | normal; the chord also in visual, insert | Switch the mode | +| ` or lM` | normal; the chord also in visual, insert | Choose the model | | `lE` | normal | Choose the effort | -| ` or lI` | normal | LLeMbas on and off (off: a plain editor, nothing is sent) | -| ` or l.` | normal | Stop the running turn | +| ` or lI` | normal; the chord also in visual, insert | LLeMbas on and off (off: a plain editor, nothing is sent) | +| ` or l.` | normal; the chord also in visual, insert | Stop the running turn | | ` or ga` | a proposed change | Accept the proposed change under the cursor | | ` or gr` | a proposed change | Reject the proposed change under the cursor | | `]h` | a proposed change | The next proposed change | | `[h` | a proposed change | The previous proposed change | | ` or gA` | a proposed change | Accept every proposed change in the file | | `gR` | a proposed change | Reject every proposed change in the file | -| ` or lb` | normal | The file tree, show and hide | -| ` or ll` | normal | The chat (the right bar), show and hide | +| ` or lb` | normal; the chord also in visual, insert | The file tree, show and hide | +| ` or ll` | normal; the chord also in visual, insert | The chat (the right bar), show and hide | | `` | normal, insert | To the bar on the left: chat, code, tree | | `` | normal, insert | To the bar on the right: tree, code, chat | | `ls` | normal | Earlier conversations | @@ -432,8 +435,19 @@ settings unless you say otherwise, and touches nothing else. - **LLeMbas features** talk to the LLeMbas CLI on your machine, which talks to the LLeMbas server you signed in to, and to nothing else. That begins when you use one: open the chat, ask, rewrite, or type with suggestions on. Suggestions as you type are on once the CLI is - installed and signed in, and send the text around the cursor to your server; `lt` - turns them off, and `complete = { auto = false }` in your settings keeps them off. + installed; `lt` turns them off, and `complete = { auto = false }` in your settings + keeps them off. +- **What a suggestion sends** to your server: the text around the cursor, and pieces of up to + six other files you have open. **What a message sends** besides what you typed: the name of + the file you are in, the cursor's line, the lines that came along, and the names of the other + open files. Other files go along — by name or by text — only when they are **inside the + working directory**, and never when they **look like they hold secrets** (`.env`, keys and + certificate stores, `credentials*`, `secrets.*`, anything under `.ssh/`, `.gnupg/`, `.aws/`, + `.kube/`; the whole list is in `:help llembas-privacy`) or when the project's **`.gitignore`** + leaves them out. In a file that looks like it holds secrets no suggestion is asked for at + all. The agent itself reads files as a turn needs them, under the CLI's permissions. +- **The LLeMbas CLI**, when you let the installer install it, comes from its own release: its + installer is run only once it matches that release's signed checksums. - **A language server** you installed runs locally; what it does on the network is its own. - **LLeMbas off** (`Ctrl+Shift+I`, `lI`) makes it a plain editor: the agent is stopped, no suggestion is asked for, the chat is put away, and it stays so in that directory until you diff --git a/ide/bin/llembas-ide b/ide/bin/llembas-ide index dff67c1..fada39d 100755 --- a/ide/bin/llembas-ide +++ b/ide/bin/llembas-ide @@ -39,6 +39,22 @@ has_tty() { [ -r /dev/tty ] && { : /dev/null; } git_quiet() { GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_NOSYSTEM=1 git "$@"; } lock_lines() { grep -v -E '^[[:space:]]*(#|$)' "$1"; } +# Where the LLeMbas CLI is, or nothing: on PATH, or where its installer puts it. The installer has +# this function word for word, and the plugin the same rule in Lua (require("llembas").cli()); +# scripts/test-ide.sh holds the three together. +find_cli() { + command -v lembas 2>/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 +# the 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 +} + # Remove a directory only when it is the IDE's own: its last part is the application's name. remove_app_dir() { case "$1" in @@ -96,15 +112,17 @@ doctor() { say "" say "Plugins, each at its pinned commit" - if command -v git >/dev/null 2>&1 && [ -f "$LIB/ide/plugins.lock" ]; then + if [ ! -f "$LIB/ide/plugins.lock" ]; then + bad "the list of pinned plugins ($LIB/ide/plugins.lock) is missing; run the installer again" + elif command -v git >/dev/null 2>&1; then while read -r name where _url commit rest; do dir="$PREFIX/site/pack/llembas/$where/$name" if [ ! -d "$dir/.git" ]; then bad "$name is missing" elif [ "$(git_quiet -C "$dir" rev-parse HEAD 2>/dev/null)" != "$commit" ]; then bad "$name is at $(git_quiet -C "$dir" rev-parse --short HEAD 2>/dev/null), not its pin ${commit:0:12}" - elif [ -n "$(git_quiet -C "$dir" status --porcelain --untracked-files=no 2>/dev/null)" ]; then - bad "$name is at its pin but its files were changed" + elif [ -n "$(changed_in "$dir")" ]; then + bad "$name is at its pin but its files were changed or added to" else good "$name ${commit:0:12}" fi @@ -122,7 +140,8 @@ doctor() { if [ "$have" = "$count" ] && [ "$count" != 0 ]; then good "$have Treesitter grammars compiled" else - off "Treesitter grammars are not compiled: Neovim's bundled ones colour C, Lua, Markdown, Vimscript and help; other languages use plain syntax files. With a C compiler installed, run: $APP update" + off "Treesitter grammars are not compiled: Neovim's bundled ones colour C, Lua, Markdown, Vimscript and help;" + printf ' %s\n' "other languages use plain syntax files. With a C compiler installed, run: $APP update" fi say "" @@ -132,11 +151,11 @@ doctor() { --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 || true)" - if [ "$(printf '%s' "$out" | tr -d '[:space:]')" = "problems=0" ]; then - good "every part of the IDE loads" - else - bad "the IDE does not start cleanly: ${out#problems=}" - fi + # As the installer judges the same start: a line of Neovim's own beside it is not a failure. + case "$out" in + *"problems=0"*) good "every part of the IDE loads" ;; + *) bad "the IDE does not start cleanly: ${out#*problems=}" ;; + esac fi say "" @@ -158,6 +177,11 @@ doctor() { off "no C compiler: Treesitter grammars cannot be compiled at install" fi if command -v curl >/dev/null 2>&1; then good "curl"; else off "curl is missing: $APP update cannot download"; fi + if command -v ssh-keygen >/dev/null 2>&1; then + good "ssh-keygen" + else + off "ssh-keygen (openssh-client) is missing: $APP update cannot check a release's signature, and stops" + fi if [ -n "${WAYLAND_DISPLAY:-}" ] && ! command -v wl-copy >/dev/null 2>&1; then off "wl-clipboard is missing: the system clipboard is off" fi @@ -168,11 +192,12 @@ doctor() { say "" say "LLeMbas" - lembas="$(command -v lembas 2>/dev/null || true)" + lembas="$(find_cli || true)" if [ -z "$lembas" ]; then off "the LLeMbas CLI (lembas) is not installed: chat, asking, rewriting and suggestions are off; the editor works. https://github.com/LLeMbas/LLeMbas-CLI" else good "LLeMbas CLI $("$lembas" --version 2>/dev/null || echo '(no version)') ($lembas)" + command -v lembas >/dev/null 2>&1 || off "$(dirname "$lembas") is not on your PATH: the IDE finds the CLI there all the same; a terminal does not" # Where the CLI keeps its sign-ins; it has no command that only says whether there is one. paths="$("$lembas" __paths 2>/dev/null || true)" index="$(printf '%s\n' "$paths" | sed -n 's/^config=//p')/lembas.json" @@ -187,11 +212,11 @@ doctor() { say "" if [ "$BAD" -gt 0 ]; then - say "$BAD thing(s) are broken. Running the installer again repairs the installation: $APP update" + if [ "$BAD" = 1 ]; then say "1 check failed. $APP update installs it again."; else say "$BAD checks failed. $APP update installs it again."; fi return 1 fi if [ "$WARNED" -gt 0 ]; then - say "The installation is sound; $WARNED note(s) above say what is off." + if [ "$WARNED" = 1 ]; then say "The installation is sound; the note above says what is off."; else say "The installation is sound; the $WARNED notes above say what is off."; fi else say "The installation is sound." fi diff --git a/ide/config/lua/llembas_ide/dashboard.lua b/ide/config/lua/llembas_ide/dashboard.lua index 4114993..b985c27 100644 --- a/ide/config/lua/llembas_ide/dashboard.lua +++ b/ide/config/lua/llembas_ide/dashboard.lua @@ -68,8 +68,17 @@ function M.content(welcome) note("The LLeMbas CLI is not installed.") note("The editor works without it. Press i.") else - note("LLeMbas needs a server and a sign-in: press i.") - note("The editor works without them.") + -- Whether there is a sign-in is looked up on this machine once; until it is known, and when + -- it cannot be told, nothing is claimed either way. + local signed = llembas.signed_in(M.redraw) + if signed == true then + note("LLeMbas is signed in: press l to chat.") + elseif signed == false then + note("LLeMbas needs a server and a sign-in: press i.") + note("The editor works without them.") + else + note("LLeMbas: press l to chat, i to sign in.") + end end if welcome then row("") @@ -80,7 +89,7 @@ function M.content(welcome) end if #ide.problems > 0 then row("") - note(#ide.problems .. " part(s) did not start: :LLeMbasIdeProblems", "LLeMbasIdeWarn") + note((#ide.problems == 1 and "1 part" or (#ide.problems .. " parts")) .. " did not start: :LLeMbasIdeProblems", "LLeMbasIdeWarn") end return rows end @@ -108,8 +117,17 @@ local function draw(buf, win, welcome) end end +--- Draw the start screen again where it is shown (its notes changed). +function M.redraw() + for _, w in ipairs(vim.api.nvim_list_wins()) do + local b = vim.api.nvim_win_get_buf(w) + if vim.bo[b].filetype == "llembas-ide-dashboard" then draw(b, w, vim.b[b].llembas_ide_welcome == true) end + end +end + function M.open(welcome) local buf = vim.api.nvim_create_buf(false, true) + vim.b[buf].llembas_ide_welcome = welcome == true vim.bo[buf].bufhidden = "wipe" vim.bo[buf].buftype = "nofile" vim.bo[buf].swapfile = false diff --git a/ide/config/lua/llembas_ide/init.lua b/ide/config/lua/llembas_ide/init.lua index 0c9b99e..e135ab3 100644 --- a/ide/config/lua/llembas_ide/init.lua +++ b/ide/config/lua/llembas_ide/init.lua @@ -63,7 +63,7 @@ function M.start(opts) if #M.problems > 0 then vim.schedule(function() - vim.notify("LLeMbas IDE: " .. #M.problems .. " part(s) did not start. :LLeMbasIdeProblems lists them.", vim.log.levels.WARN) + vim.notify("LLeMbas IDE: " .. (#M.problems == 1 and "1 part" or (#M.problems .. " parts")) .. " did not start. :LLeMbasIdeProblems lists them.", vim.log.levels.WARN) end) end end diff --git a/ide/config/lua/llembas_ide/keys.lua b/ide/config/lua/llembas_ide/keys.lua index 7218022..fa3fadf 100644 --- a/ide/config/lua/llembas_ide/keys.lua +++ b/ide/config/lua/llembas_ide/keys.lua @@ -143,6 +143,14 @@ function M.llembas_rows() local names = {} for _, m in ipairs(r.modes) do names[#names + 1] = ({ n = "normal", x = "visual", i = "insert" })[m] or m end where = table.concat(names, ", ") + -- The chord form is mapped in normal, visual and insert mode, whatever the other form is. + if r.chord then + local more = {} + for _, m in ipairs({ "n", "x", "i" }) do + if not vim.tbl_contains(r.modes, m) then more[#more + 1] = ({ n = "normal", x = "visual", i = "insert" })[m] end + end + if #more > 0 then where = where .. "; the chord also in " .. table.concat(more, ", ") end + end end if #both > 0 then out[#out + 1] = { table.concat(both, " or "), where, r.desc } end end diff --git a/ide/config/lua/llembas_ide/llembas.lua b/ide/config/lua/llembas_ide/llembas.lua index 01df98d..84fe605 100644 --- a/ide/config/lua/llembas_ide/llembas.lua +++ b/ide/config/lua/llembas_ide/llembas.lua @@ -5,12 +5,43 @@ local M = {} M.loaded = false ---- Whether the agent's program can be started: `lembas` on PATH or where its installer puts it, ---- or whatever `cmd` was set to. -function M.has_cli() +--- The agent's program, or nil: `lembas` on PATH or where its installer puts it, or whatever +--- `cmd` was set to. The plugin decides (`require("llembas").cli()`): there is one rule for +--- "is the LLeMbas CLI installed", and the launcher's `doctor` keeps the same one in shell. +function M.cli() local ok, llembas = pcall(require, "llembas") - if ok and type(llembas._command) == "function" then return llembas._command() ~= nil end - return vim.fn.executable("lembas") == 1 + if not ok or type(llembas.cli) ~= "function" then return nil end + local cmd = llembas.cli() + return cmd and cmd[1] or nil +end + +function M.has_cli() return M.cli() ~= nil end + +local signed = nil -- true | false once it was looked up; nil before, and when it cannot be told + +--- Whether the CLI is signed in to a server: true, false, or nil when that is not known (yet). +--- Looked up once, on this machine only — where the CLI keeps its sign-ins (`lembas __paths`), +--- as `llembas-ide doctor` does; no server is asked. `cb()` is called when the answer is in. +function M.signed_in(cb) + if signed ~= nil or M.looking then return signed end + local exe = M.cli() + if not exe then return nil end + M.looking = true + local ok = pcall(vim.system, { exe, "__paths" }, { text = true, timeout = 5000 }, function(res) + local dir = res.code == 0 and (res.stdout or ""):match("config=([^\r\n]+)") or nil + vim.schedule(function() + M.looking = false + if dir then + local f = io.open(dir .. "/lembas.json", "r") + local text = f and f:read("*a") or "" + if f then f:close() end + signed = text:find('"base_url"', 1, true) ~= nil + end + if cb then cb() end + end) + end) + if not ok then M.looking = false end + return nil end --- The layout LLeMbas IDE starts with: all four bars. `lua/user/init.lua` changes it with diff --git a/ide/config/lua/llembas_ide/lsp.lua b/ide/config/lua/llembas_ide/lsp.lua index 099f9e1..6421764 100644 --- a/ide/config/lua/llembas_ide/lsp.lua +++ b/ide/config/lua/llembas_ide/lsp.lua @@ -3,7 +3,7 @@ -- each is installed. local M = {} --- name (nvim-lspconfig's), the program looked for, what it is for, how its own project says to install it. +-- name (nvim-lspconfig's), the program looked for, what it is for, one way to install it. M.servers = { { "lua_ls", "lua-language-server", "Lua", "your package manager, or github.com/LuaLS/lua-language-server releases" }, { "pyright", "pyright-langserver", "Python", "npm install -g pyright (or: pipx install pyright)" }, diff --git a/ide/install.sh b/ide/install.sh index 1a688ec..37bac5b 100755 --- a/ide/install.sh +++ b/ide/install.sh @@ -2,59 +2,25 @@ # 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 -- +# curl -fsSL <…>/install.sh | bash -s -- --channel beta # options go after --; the betas # 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 -# -# Needs bash, git, curl, tar and sha256sum (or shasum). A C compiler is used when there is one. -# Linux x86-64 and arm64. macOS is written for and has not been tried. +# 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 releases are signed with. Empty: releases of LLeMbas IDE are not signed yet, and a -# release is checked against its own SHA256SUMS only. Set, a release without a good -# SHA256SUMS.sig (ssh-keygen -Y sign, namespace lembas-release) is refused. -# The key LLeMbas releases are signed with (the LLeMbas CLI's get.sh checks the same one). Set -# LLEMBAS_IDE_PUBKEY to another key for a build of your own, or to nothing for an unsigned one. +# 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_GET="https://github.com/LLeMbas/LLeMbas-CLI/releases/latest/download/get.sh" +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" @@ -132,8 +98,57 @@ cleanup() { 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() { - sed -n '2,/^set -euo/p' "${BASH_SOURCE[0]}" 2>/dev/null | sed -e '$d' -e 's/^# \{0,1\}//' + 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() { @@ -190,6 +205,22 @@ git_quiet() { # The lines of a lock file that say something. lock_lines() { grep -v -E '^[[:space:]]*(#|$)' "$1"; } +# Where the LLeMbas CLI is, or nothing: on PATH, or where its installer puts it. The launcher has +# this function word for word, and the plugin the same rule in Lua (require("llembas").cli()); +# scripts/test-ide.sh holds the three together. +find_cli() { + command -v lembas 2>/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() { @@ -198,11 +229,11 @@ api_base() { # 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() { - local json tags - json="$(curl -fsSL --proto '=https' "$(api_base)/repos/$SLUG/releases?per_page=50&limit=50")" || return 1 +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 + 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)" @@ -242,7 +273,7 @@ from_release() { 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" + [ "$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 @@ -279,6 +310,8 @@ from_release() { 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" @@ -328,12 +361,12 @@ fetch_commit() { [ "$(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 nobody changed its files. +# 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 "$(git_quiet -C "$dir" status --porcelain --untracked-files=no 2>/dev/null)" ] + [ -z "$(changed_in "$dir")" ] } stage_neovim() { @@ -522,38 +555,73 @@ place_file() { 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" + local channel=stable version="$1" at tag base later [ "$WITH_CLI" = 1 ] || return 0 - if command -v lembas >/dev/null 2>&1 || [ -x "$HOME/.local/bin/lembas" ]; then - step "leave the LLeMbas CLI alone: it is installed ($(command -v lembas 2>/dev/null || echo "$HOME/.local/bin/lembas"))" + 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 https://github.com/LLeMbas/LLeMbas-CLI" + 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) with its own installer: $CLI_GET (it checks the release's signature itself)" + 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: curl -fsSL $CLI_GET | LEMBAS_CHANNEL=$channel bash" + note "not installed. $later" return 0 fi - # Its own installer, whole, then run: it checks the CLI's release signature itself. - if ! fetch "$CLI_GET" "$TMP/get.sh"; then - warn "could not download the CLI's installer; the IDE is installed and works as an editor. Later: curl -fsSL $CLI_GET | LEMBAS_CHANNEL=$channel bash" + # 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_CHANNEL="$channel" bash "$TMP/get.sh" --yes || warn "the CLI's installer failed; the IDE itself is installed." + 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_CHANNEL="$channel" bash "$TMP/get.sh" &1 || true)" -expect "with the CLI missing, offers the CLI's own installer" "$out" "offer to install the LLeMbas CLI (beta) with its own installer" +expect "with the CLI missing, offers the CLI's own installer" "$out" "offer to install the LLeMbas CLI (beta): its own installer (get.sh)" out="$(as "$A" bash "$REPO/ide/install.sh" --dry-run --version 9.9.9 2>&1 || true)" expect "from a release: names the archive and the checksum check" "$out" "releases/download/v9.9.9/llembas-ide-9.9.9.tar.gz" out="$(installer "$A" --from-checkout --prefix relative/dir 2>&1 || true)" @@ -203,6 +203,13 @@ expect "a --nvim-bin that is not Neovim 0.10 or newer is refused in words" "$out out="$(as "$A" bash -c 'cat "$1" | bash -s -- --from-checkout' _ "$REPO/ide/install.sh" 2>&1 || true)" expect "piped into bash, --from-checkout is refused in words" "$out" "--from-checkout needs this script run as a file" if [ -z "$(find "$A" -mindepth 1 | head -n1)" ]; then ok "refusals write nothing"; else fail "refusals write nothing" "$(find "$A" -mindepth 1 | head)"; fi +# The help, also when the script comes down a pipe and there is no file to read it from. +attempt as "$A" bash -c 'cat "$1" | bash -s -- --help' _ "$REPO/ide/install.sh" +passes "piped into bash, --help ends well" +expect "…and prints the options" "$OUT" "--channel NAME stable (default) or beta" +expect "…and what the script needs, ssh-keygen with it" "$OUT" "ssh-keygen (openssh-client" +if [ "$(as "$A" bash "$REPO/ide/install.sh" --help 2>&1)" = "$OUT" ]; then ok "the same help as from the file"; else fail "the same help as from the file"; fi +if [ -z "$(find "$A" -mindepth 1 | head -n1)" ]; then ok "the help writes nothing"; else fail "the help writes nothing" "$(find "$A" -mindepth 1 | head)"; fi # --- 3. a real install, with the Neovim of this machine and its bundled grammars --------------- echo "A real install (the Neovim on this machine: $("$SYSTEM_NVIM" --version | head -n1), bundled grammars)" @@ -271,6 +278,15 @@ expect "doctor: a plugin whose files were changed is a failure" "$out" "FAIL co out="$(installer "$A" --from-checkout --nvim-bin "$SYSTEM_NVIM" --no-parsers || true)" expect "the installer fetches that plugin again" "$out" "fetch conform.nvim from" check "and doctor is content" as "$A" "$A/.local/bin/llembas-ide" doctor +# A file somebody added is loaded by Neovim like any other: it is a change too. +echo 'vim.g.llembas_ide_test_added = true' >"$P/site/pack/llembas/start/conform.nvim/plugin/added.lua" +out="$(as "$A" "$A/.local/bin/llembas-ide" doctor 2>&1 || true)" +expect "doctor: a plugin with a file added to it is a failure" "$out" "FAIL conform.nvim is at its pin but its files were changed or added to" +expect "doctor: and says how many checks failed, in words" "$out" "1 check failed. llembas-ide update installs it again." +out="$(installer "$A" --from-checkout --nvim-bin "$SYSTEM_NVIM" --no-parsers || true)" +expect "the installer fetches that plugin again too" "$out" "fetch conform.nvim from" +check "and the added file is gone" test ! -e "$P/site/pack/llembas/start/conform.nvim/plugin/added.lua" +check "and doctor is content again" as "$A" "$A/.local/bin/llembas-ide" doctor # --- 5. failing without leaving anything half-done ---------------------------------------------- echo "Failures" @@ -320,6 +336,102 @@ expect "the installer leaves an installed CLI alone" "$out" "leave the LLeMbas C out="$(EXTRA_PATH="$WORK/cli/bin" as "$A" "$A/.local/bin/llembas-ide" --headless \ -c 'lua local t, live = require("llembas_ide.llembas").status(); io.stdout:write(t .. " live=" .. tostring(live))' -c 'qa!' 2>&1 || true)" expect "the statusline shows LLeMbas and that suggestions are on" "$out" "◈ LLeMbas · suggest on live=true" +dash='lua local l, d = require("llembas_ide.llembas"), require("llembas_ide.dashboard"); local function notes() local t = {}; for _, r in ipairs(d.content(false)) do t[#t + 1] = r[1] end; return table.concat(t, "|") end; local first = notes(); vim.wait(5000, function() return l.signed_in() ~= nil end, 20); io.stdout:write(first .. " THEN " .. notes())' +out="$(EXTRA_PATH="$WORK/cli/bin" as "$A" "$A/.local/bin/llembas-ide" --headless -c "$dash" -c 'qa!' 2>&1 || true)" +expect "the start screen claims nothing before it has looked" "${out%% THEN *}" "LLeMbas: press l to chat, i to sign in." +expect "the start screen of somebody signed in does not ask for a sign-in" "${out##* THEN }" "LLeMbas is signed in: press l to chat." +if has "${out##* THEN }" "needs a server and a sign-in"; then fail "and no longer says one is needed" "$out"; else ok "and no longer says one is needed"; fi +rm -f "$WORK/cli/config/lembas.json" +out="$(EXTRA_PATH="$WORK/cli/bin" as "$A" "$A/.local/bin/llembas-ide" --headless -c "$dash" -c 'qa!' 2>&1 || true)" +expect "the start screen of somebody not signed in says what is needed" "${out##* THEN }" "LLeMbas needs a server and a sign-in: press i." +printf '{ "instances": { "home": { "base_url": "https://llembas.example.org" } } }\n' >"$WORK/cli/config/lembas.json" + +# One rule for "is the LLeMbas CLI installed": the installer's and the launcher's shell function, +# word for word the same, and the plugin's require("llembas").cli(), in the three places a CLI +# can be: on PATH, in ~/.local/bin without that being on PATH (the state right after a first +# install), and nowhere. +rule_installer="$(sed -n '/^find_cli() {$/,/^}$/p' ide/install.sh)" +rule_launcher="$(sed -n '/^find_cli() {$/,/^}$/p' ide/bin/llembas-ide)" +if [ -n "$rule_installer" ] && [ "$rule_installer" = "$rule_launcher" ]; then + ok "the installer and the launcher find the CLI by the same function, word for word" +else + fail "the installer and the launcher find the CLI by the same function, word for word" "$rule_installer +--- +$rule_launcher" +fi +for where in path home nowhere; do + H="$WORK/find-$where" + mkdir -p "$H/.local/bin" "$H/xdg" + p="$CLEAN_PATH" + want=none + case "$where" in + path) + p="$WORK/cli/bin:$CLEAN_PATH" + want="$WORK/cli/bin/lembas" + ;; + home) + cp "$WORK/cli/bin/lembas" "$H/.local/bin/lembas" + want="$H/.local/bin/lembas" + ;; + esac + by_shell="$(env -i PATH="$p" HOME="$H" bash -c "$rule_installer"' +find_cli || echo none' 2>&1)" + by_plugin="$(env -i PATH="$p" HOME="$H" XDG_STATE_HOME="$H/xdg" XDG_DATA_HOME="$H/xdg" XDG_CACHE_HOME="$H/xdg" XDG_CONFIG_HOME="$H/xdg" TERM=dumb \ + "$SYSTEM_NVIM" --headless -u NONE -i NONE --cmd "set rtp+=$REPO/nvim" \ + -c 'lua local c = require("llembas").cli(); io.stdout:write(c and vim.fn.exepath(c[1]) or "none")' -c 'qa!' 2>&1)" + if [ "$by_shell" = "$want" ] && [ "$by_plugin" = "$want" ]; then + ok "a CLI $where: the scripts and the plugin agree ($want)" + else + fail "a CLI $where: the scripts and the plugin agree ($want)" "shell: $by_shell +plugin: $by_plugin" + fi +done +# The state right after a first install: the CLI in ~/.local/bin, which is not on PATH. +cp "$WORK/cli/bin/lembas" "$A/.local/bin/lembas" +out="$(env -i PATH="$CLEAN_PATH" HOME="$A" TERM=dumb LANG=C.UTF-8 "$A/.local/bin/llembas-ide" doctor 2>&1 || true)" +expect "doctor finds a CLI that is in ~/.local/bin and not on PATH" "$out" "LLeMbas CLI 2.0.0-beta.99 ($A/.local/bin/lembas)" +expect "doctor: and says that a terminal will not find it" "$out" "$A/.local/bin is not on your PATH" +out="$(env -i PATH="$CLEAN_PATH" HOME="$A" TERM=dumb LANG=C.UTF-8 "$A/.local/bin/llembas-ide" --headless \ + -c 'lua local t, live = require("llembas_ide.llembas").status(); io.stdout:write(t .. " live=" .. tostring(live))' -c 'qa!' 2>&1 || true)" +expect "and so does the IDE: LLeMbas is on, not \"no CLI\"" "$out" "◈ LLeMbas · suggest on live=true" +rm -f "$A/.local/bin/lembas" + +# The CLI's own installer is run only once it matches its release's signed SHA256SUMS. The +# stand-in installer leaves a mark saying it ran, and with what. +CLIREL="$WORK/cli-releases" +mkdir -p "$CLIREL/v2.0.0-beta.99" +printf '%s\n' '#!/usr/bin/env bash' "echo \"ran ref=\${LEMBAS_REF:-} channel=\${LEMBAS_CHANNEL:-} args=\$*\" >\"$WORK/cli-installer-ran\"" >"$CLIREL/v2.0.0-beta.99/get.sh" +(cd "$CLIREL/v2.0.0-beta.99" && sha256sum get.sh >SHA256SUMS) +ssh-keygen -q -t ed25519 -N '' -C cli -f "$WORK/clikey" >/dev/null +ssh-keygen -q -t ed25519 -N '' -C stranger -f "$WORK/strangerkey" >/dev/null +cli_install() { # the installer over the installation in A, saying yes to the CLI, against a CLI release + as "$A" env LLEMBAS_IDE_CLI_RELEASE_BASE="file://$CLIREL" LLEMBAS_IDE_CLI_VERSION=2.0.0-beta.99 LLEMBAS_IDE_PUBKEY="$(cut -d' ' -f1,2 "$WORK/clikey.pub")" \ + bash "$REPO/ide/install.sh" --from-checkout --nvim-bin "$SYSTEM_NVIM" --no-parsers --yes 2>&1 || true +} +out="$(cli_install)" +expect "a CLI release that is not signed: its installer is not run" "$out" "the LLeMbas CLI's installer could not be checked, so it was not run" +check "…really not" test ! -e "$WORK/cli-installer-ran" +expect "…and the IDE is installed all the same" "$out" "is installed. Start it with: llembas-ide" +ssh-keygen -Y sign -q -f "$WORK/strangerkey" -n lembas-release "$CLIREL/v2.0.0-beta.99/SHA256SUMS" 2>/dev/null +out="$(cli_install)" +expect "a CLI release signed with another key: its installer is not run" "$out" "SHA256SUMS is not signed by the release key" +check "…really not" test ! -e "$WORK/cli-installer-ran" +rm -f "$CLIREL/v2.0.0-beta.99/SHA256SUMS.sig" +ssh-keygen -Y sign -q -f "$WORK/clikey" -n lembas-release "$CLIREL/v2.0.0-beta.99/SHA256SUMS" 2>/dev/null +echo '# one line more' >>"$CLIREL/v2.0.0-beta.99/get.sh" +out="$(cli_install)" +expect "an installer that is not the one the signed list names is not run" "$out" "the LLeMbas CLI's installer does not match its release's SHA256SUMS" +check "…really not" test ! -e "$WORK/cli-installer-ran" +sed -i '$d' "$CLIREL/v2.0.0-beta.99/get.sh" +out="$(cli_install)" +expect "signed by the release key and matching: the CLI's installer is checked" "$out" "the LLeMbas CLI's installer matches its release" +if [ "$(cat "$WORK/cli-installer-ran" 2>/dev/null)" = "ran ref=v2.0.0-beta.99 channel=beta args=--yes" ]; then + ok "…and run, for that release and its channel" +else + fail "…and run, for that release and its channel" "$(cat "$WORK/cli-installer-ran" 2>&1) +$out" +fi +check "and doctor is still content" as "$A" "$A/.local/bin/llembas-ide" doctor # --- 7. a release: packaged, checked, installed with the pinned Neovim and compiled grammars ---- echo "A release"