KScript docs
EN

The .kscript format

Converting

You may convert a script, a folder or a whole project from one format to the other. kscript convert writes the other format next to the original, which is left unchanged:

CommandWhat it does
kscript convert tour.ks.xmlWrites tour.kscript.
kscript convert tour.kscriptWrites tour.ks.xml.
kscript convert scripts/ --syntax kscriptEvery XML script in a folder, tests too.
kscript convert tour.ks.xml --stdoutPrint instead of writing.

Options: --syntax kscript|xml, --out file, --stdout, --force (replace an existing file). Comments, doc comments and blank lines are kept, and expressions are copied as written. Where one meaning has two spellings, the converter picks one (<call fn="greet" args="'x'" /> becomes @greet('x'); $x = 3 becomes <let> or <set>; const $x = 1 becomes <const>), so the first conversion may tidy a file, and converting back and forth after that changes nothing. It warns when something can't be carried over exactly, such as a <let> inside a block that hides an outer variable of the same name.

Converting keeps a project working in the new format:

  • An import of a script that is converted in the same run, or already has a converted version, points at the new file (<import path="lib/tasks.ks.xml" /> becomes import "lib/tasks.kscript"). An import of a script that stays in the old format is kept, with a note; the two formats can import each other.
  • File names in comments and strings that name a converted script are renamed too (@run_script('hello.ks.xml')), and one-line XML examples in comments are written the .kscript way.
  • Converting a folder also converts its tests (name.test.ks.xml becomes name.test.kscript), renames converted files in its .html and .md pages, and updates the project's kscript.json: "main" and other file names, and each *.ks.xml include pattern gains a *.kscript twin.
  • Scripts in the folder that stay as they are (a .kscript component beside XML scripts) have their imports of converted scripts renamed too.
  • The new file is laid out as kscript fmt would lay it out, with the project's format settings, so it passes kscript fmt --check.
CommandWhat it does
kscript convert . --syntax kscriptThe whole project.

Tests

Test files may be written in this format too: name.test.kscript files get KScript.Test without an import and run with kscript test like XML ones.

<import path="../lib/strings.kscript" />

<describe name="slugify">
  <test name="joins words with dashes">
    <expect value="@slugify('Hello World')" equals="'hello-world'" />
  </test>
</describe>
import "../lib/strings.kscript"

describe(name: 'slugify') {
  test(name: 'joins words with dashes') {
    expect(@slugify('Hello World'), equals: 'hello-world')
  }
}