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:
| Command | What it does |
|---|---|
kscript convert tour.ks.xml | Writes tour.kscript. |
kscript convert tour.kscript | Writes tour.ks.xml. |
kscript convert scripts/ --syntax kscript | Every XML script in a folder, tests too. |
kscript convert tour.ks.xml --stdout | Print 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" />becomesimport "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.xmlbecomesname.test.kscript), renames converted files in its.htmland.mdpages, and updates the project'skscript.json:"main"and other file names, and each*.ks.xmlinclude pattern gains a*.kscripttwin. - Scripts in the folder that stay as they are (a
.kscriptcomponent beside XML scripts) have their imports of converted scripts renamed too. - The new file is laid out as
kscript fmtwould lay it out, with the project'sformatsettings, so it passeskscript fmt --check.
| Command | What it does |
|---|---|
kscript convert . --syntax kscript | The 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')
}
}