KScript docs
EN

Installing KScript

Introduction

KScript is one program, kscript, that runs scripts, serves sites, checks code and builds apps. It is a native program, so it doesn't need .NET or anything else installed first. An install script picks the right build for your computer and puts it on your PATH.

Installing

On Linux and macOS, run:

Shell
curl -fsSL https://raw.githubusercontent.com/kylepantall/kscript/master/install.sh | sh

On Windows, run this in PowerShell:

PowerShell
irm https://raw.githubusercontent.com/kylepantall/kscript/master/install.ps1 | iex

The scripts pick the build for your operating system and CPU and install it to ~/.kscript/bin (on Windows, %LOCALAPPDATA%\Programs\kscript). They then add that folder to your PATH. On macOS they also clear the quarantine flag, since the binary isn't notarized.

Then check it worked:

CommandWhat it does
kscript --versionThe version you have.

Install options

The install scripts read a few environment variables:

  • KSCRIPT_VERSION=v0.34.0 installs a specific release instead of the latest.
  • KSCRIPT_INSTALL chooses the folder.
  • KSCRIPT_NO_MODIFY_PATH=1 leaves your PATH alone.
Shell
curl -fsSL https://raw.githubusercontent.com/kylepantall/kscript/master/install.sh | KSCRIPT_VERSION=v0.34.0 sh

Updating

kscript update installs the latest release when it is newer than yours:

CommandWhat it does
kscript updateInstall the latest release if it's newer.
kscript update --checkOnly report whether there is one.
kscript update --no-ideLeave the editor extension alone.

To run an older release, or a project's own version, use kscript version instead: it keeps each version beside the one you have, so you can switch back.

It replaces the binary in place, and clears a default you chose with kscript version use, so the computer runs the new release. A project's own version, in its kscript.json, stays as it is. On Windows, the running kscript.exe is renamed to kscript.exe.old, and deleted the next time kscript starts. Set GITHUB_TOKEN to avoid GitHub's rate limit, such as on a shared CI runner.

kscript update also brings the editor extension up to the same release in every editor that already has it (VS Code, Insiders, Cursor, VSCodium and Windsurf), and tells you which ones it updated. Editors without the extension are left alone. This happens even when the binary is already current, so an extension that fell behind is caught up. --check reports stale extensions too, and --force reinstalls them. Reload the editor window afterwards.

Switching versions

Introduction

Sometimes one computer needs more than one version of KScript. An older project may be written for the release it started on, a library may need testing against the last few releases, or you may want to try a new release and go back if something breaks. kscript version keeps each version you ask for side by side, and the kscript on your PATH runs the right one for each command. Nothing is replaced, so switching back is instant and works offline.

To see which version runs where you are, and what chose it:

Shell
kscript version
Text
KScript 0.55.0
  Chosen by ........................................ the installed kscript
  Runs from ......................... /home/joan/.kscript/bin/kscript
  Installed kscript ......... 0.55.0 (/home/joan/.kscript/bin/kscript)

kscript --version still prints just the number (0.55.0), for scripts that read it.

Switching this computer

kscript version use picks the version every folder on this computer runs, unless a project asks for another. A version that isn't on the computer yet is downloaded first, and checked against its release's SHA256SUMS, the same way kscript update checks one:

CommandWhat it does
kscript version use 0.50.0Every command now runs KScript 0.50.0.
kscript --version0.50.0.
kscript version use latestBack to the newest release.
kscript --version0.55.0.

The second use asks GitHub nothing when the version is already here, so switching between versions you have works without a network.

Pinning a project

A project can say which version it is written for, so everyone who works on it, and its CI, runs the same one. kscript version pin writes it into the project's kscript.json and gets that version ready:

Shell
cd shop
kscript version pin 0.50   # the newest 0.50.x: 0.50.2 today
JSON
{
  "name": "shop",
  "kscript": "0.50"   // kscript version pin wrote this line
}

From then on, any kscript command in the project or a folder inside it runs KScript 0.50.2, and a kscript that doesn't have it yet downloads it the first time. The setting is described with the rest of kscript.json.

Pin a series, such as 0.50, to take bug-fix releases as they come out, and an exact release, such as 0.50.2, to never move until you say so. kscript version pin latest writes the newest release as an exact version ("0.55.0"), so the project doesn't move by itself when the next one comes out.

To stop pinning the project, so it runs the computer's version again:

CommandWhat it does
kscript version unpinTakes "kscript" out of kscript.json, and leaves its other settings and comments alone.

One command with another version

KSCRIPT_USE runs a command with a version without changing anything else. It wins over the project and the computer, which makes it the way to test a project against several releases in CI:

Shell
KSCRIPT_USE=0.49.0 kscript test   # this run only: kscript.json and the default stay as they are
YAML
# .github/workflows/test.yml: run the tests on the last three releases
strategy:
  matrix:
    kscript: ["0.53", "0.54", "0.55"]
steps:
  - run: curl -fsSL https://raw.githubusercontent.com/kylepantall/kscript/master/install.sh | sh
  - run: kscript test
    env:
      KSCRIPT_USE: ${{ matrix.kscript }}

Which version runs

The kscript on your PATH picks the version for each command, from the first of these that names one:

Asked for by Example Set with
KSCRIPT_USE KSCRIPT_USE=0.49.0 your shell or CI
the nearest kscript.json with a "kscript" setting "kscript": "0.50" kscript version pin
this computer's default 0.51.0 kscript version use
nothing: the installed kscript runs 0.55.0 the install script or kscript update

Each of them can name an exact release (0.50.2), a series (0.50, its newest release) or latest. For a series, a version already on the computer is used before asking GitHub: if you have 0.50.1 and the project says 0.50, it runs 0.50.1 until you run kscript version install 0.50 to get a newer one.

kscript version and kscript update always run with the installed kscript, whatever a project asks for, because they manage the versions themselves.

Listing and removing versions

kscript version list shows the versions on this computer, and which one runs where you are:

Shell
kscript version list
Text
  0.55.0 ............................................ the installed kscript
  0.51.0 ............................. runs here, this computer's default
  0.50.2 ........................................................

--available lists every release there is, newest first, and marks the ones you have:

Shell
kscript version list --available

kscript version install downloads a version without switching to it, such as before going offline, and kscript version remove deletes one you no longer need:

CommandWhat it does
kscript version install 0.50Keep the newest 0.50.x, ready to use.
kscript version remove 0.50.0Delete 0.50.0: name the exact version.

The computer's default can't be removed until you switch to another version, so the next command always has a version to run.

Where versions are kept

Each version lives in a folder of its own beside the installed kscript, with the libraries desktop windows need:

System Installed kscript Kept versions
Linux and macOS ~/.kscript/bin/kscript ~/.kscript/versions/0.50.2/kscript
Windows %LOCALAPPDATA%\Programs\kscript\kscript.exe %LOCALAPPDATA%\Programs\kscript\versions\0.50.2\kscript.exe

The default from kscript version use is the file versions/default. Deleting the versions folder removes every kept version and the default, and leaves the installed kscript as it was.

Checking downloads

Each release has a SHA256SUMS file that lists the SHA-256 of every file in it. The install scripts, kscript update, kscript version and kscript compile (when it fetches kscript for another platform) check each download against that list before unpacking it, and install nothing when it doesn't match. Releases before 0.26 have no list, so installing one of those says the download wasn't checked.

To check a download you made by hand, put SHA256SUMS beside it and run:

Shell
sha256sum --check --ignore-missing SHA256SUMS            # Linux
shasum -a 256 --check --ignore-missing SHA256SUMS        # macOS

Installing by hand

You may also download a build from the releases page. There are builds for linux-x64, linux-arm64, osx-arm64 (Apple Silicon), osx-x64 and win-x64.

Each kscript-<platform> archive holds kscript and the two libraries desktop windows draw with (libSkiaSharp and libHarfBuzzSharp). Keep the three files in the same folder. Everything except windows works without the libraries:

Shell
mkdir kscript && tar -xzf kscript-osx-arm64.tar.gz -C kscript   # unpack the archive
xattr -dr com.apple.quarantine kscript                          # macOS only: the files aren't notarized
./kscript/kscript --version                                     # check it runs

The kscript-runtime-<platform> archives are what kscript compile builds programs from. You don't need them to run kscript.

Building from source

To build kscript yourself, you need the .NET 8 SDK and a C++ toolchain for Native AOT: clang and zlib1g-dev on Linux, Xcode on macOS, or Visual Studio's C++ tools on Windows. Then publish for your platform:

Shell
dotnet publish KScriptApp -c Release -r osx-arm64 -p:PublishAot=true -o out

Running scripts

Run a script with kscript and its file name. Anything after the name is in $args, a list:

CommandWhat it does
kscript hello.kscriptRun a script.
kscript greet.kscript Joan$args is ['Joan'].

The exit code is 0, the code given to exit, or 3 when the script stops with an error, printed as file:line:column: Type: message. 2 means the command line itself was wrong (see exit codes). Ctrl+C stops a script, and its finally blocks still run.

Reading the docs

kscript docs opens these docs in your browser. They are built into kscript, so they work offline and match the version you have:

CommandWhat it does
kscript docsOpen the docs.
kscript docs replaceOpen them with a search for "replace".

Docs without kscript running

To read the docs somewhere kscript docs can't run, such as a computer without KScript, a USB stick or your own web server, export them. --export-to writes the whole site into a folder as plain files:

CommandWhat it does
kscript docs --export-to kscript-docsWrite the docs into kscript-docs.
kscript docs --export-to kscript-docs --syntax xmlWith the examples shown as XML first.

Open kscript-docs/index.html in a browser. Every page is its own .html file and every link is relative, so the folder works straight from the disk, with no server and no network, and keeps working when you copy or upload it. Search works too: its index comes in assets/search.js. Exporting into the same folder again replaces the pages and leaves any other files there alone.

To put several versions on one website, export each into a folder named after its version, side by side (docs/0.55.0/, docs/0.54.0/). --versions url turns the version in the header into a picker that reads a list such as {"latest": "0.55.0", "versions": ["0.55.0", "0.54.0"]} and opens the same page in the version picked, and --stylesheet url loads a stylesheet of your own after the docs' own, so they match the rest of the site. The KScript website publishes every release's docs this way at /docs/<version>/.

Docs for your website

A copy you put on a website is read as published documentation, not as a local tool. --published makes it one: the playground is left out, with every link to it, pages end without an "Edit this page on GitHub" link, and the home page doesn't talk about how the copy was made. kscript docs on your computer keeps all of them.

--hide leaves out pages and reference entries, with every link to them and their search entries, as a list separated by commas:

Shell
kscript docs --export-to public/docs --published --hide /guide/roadmap,/reference/methods/*,/reference/ai#cmd-chat
In --hide Leaves out
/guide/roadmap (or guide/roadmap) that page, and its topic pages
/reference/methods/* every page under the path
/reference/ai#cmd-chat one reference entry: the anchor in its address
playground the playground
edit-links the "Edit this page on GitHub" links

To add your own content, such as illustrations or a note about your support, or to replace a page, put the settings in a JSON file and pass it with --config:

JSON
{
  "published": true,
  "hide": ["/guide/roadmap"],
  "pages": { "/guide/install": "install.md" },
  "before": { "/guide/language": "language-intro.md" },
  "after": { "/": "support.md" },
  "files": "docs-files",
  "versions": "/docs/versions.json",
  "stylesheet": "/assets/docs-theme.css",
  "syntax": "kscript"
}
  • pages replaces a page with your Markdown (a guide's topic pages go with it).
  • before adds Markdown under a page's title, and after at its end. Their headings join the page's "On this page".
  • files is a folder copied into the top of the copy, for the pictures and other files your Markdown uses: docs-files/illustrations/flow.svg is ![How a script runs](/illustrations/flow.svg).
  • Paths in the file are found from the file's own folder. The command line adds to the file: --hide adds to hide, and --published, --versions, --stylesheet and --syntax win over the file.

A path that matches no page or entry is an error, and nothing is written, so a typo never leaves a page in by accident.

The examples can still be read, copied and switched between .kscript and XML, but they can't run, because running them needs kscript docs. So the Run boxes and the playground's editor are left out, and each runnable example leaves a hidden placeholder where its box was:

HTML page
<div class="kscript-example" data-example="guide-getting-started-1">
  <!-- the example's code, to read -->
  <template class="kscript-try" data-example="guide-getting-started-1" data-format="kscript"
            data-kscript="echo 'Hello'" data-xml="&lt;echo&gt;Hello&lt;/echo&gt;"></template>
</div>

data-example names the example: the page's file and its number on that page, so it stays the same from one export to the next. data-format is the format the example was written in, data-kscript and data-xml hold its code in each format, and data-input and data-title are there when the example has them. A <template> is never shown, so the placeholders change nothing until a script of your own uses them. To run the examples with your own runner, add a script to the pages that swaps each one for it:

js
document.querySelectorAll('template.kscript-try').forEach(placeholder => {
  const example = placeholder.closest('.kscript-example');
  // The example's code in the reader's format, falling back to the one it was written in.
  const format = document.documentElement.dataset.format;
  const code = placeholder.dataset[format] ?? placeholder.dataset[placeholder.dataset.format];
  example.replaceWith(myRunner(code, format));   // myRunner is yours: an editor and a Run button
});

Editor support

The KScript extension adds errors as you type, completion, hover, go to definition, rename and highlighting for scripts and kscript serve pages. Install it into VS Code, Cursor, VSCodium or Windsurf:

CommandWhat it does
kscript ide installEvery editor it finds.
kscript ide install cursorJust one.
kscript ideList the editors it found.
kscript ide uninstallRemove the extension again.

The extension works out types as it goes. $person. and $name-> offer the fields and methods of that value, including what your own functions return. Inlay hints show the types you didn't write, and thin lines divide a script into blocks like a notebook.

Press F5 in a script to debug it: it stops at your breakpoints, steps through a line at a time and shows every variable. See Debugging.

It runs kscript lsp from your installed kscript, so keep that current with kscript update. To use a different kscript, set kscript.server.path in the editor's settings. Every command and setting of the extension is listed in VS Code commands and settings.

Other editors

Other editors with LSP support work too. kscript ide config prints the settings to add:

CommandWhat it does
kscript ide config neovimAlso helix, sublime or emacs.

kscript check <folder> reports the same problems on the command line, for CI. See the language server for how it works.