beetlebug▾
/

Projects

ztcl init writes a new project: a folder with a project file, the code, the tests, and a README. A project keeps its logic apart from its start script, runs its tests with no capability, and lists the capabilities that it needs.

Make a project

ztcl init foobar
ztcl init --library github.com/me/foobar

The first line writes a command-line tool in the folder foobar. The second writes a library in the folder foobar, with the import path github.com/me/foobar.

Option Description
NAME or PATH The project. The folder and the name are the last element. A name has only a-z, 0-9, -, and _, and starts with a letter. A path is for a library.
--library A library in place of a tool.
--tcl 8.6 or --tcl 9.1 The Tcl release of the project. The default is 9.1 for a tool and 8.6 for a library, so that each user of a library can use it.
--force Writes into a folder that is not empty. init never writes over a file, also with --force.

init writes files only. It runs no script, starts no git init, and uses no network. It prints each file that it writes.

A tool

foobar/
  ztcl.project      the project file
  main.tcl          the start: it sources lib/ and has no logic
  lib/foobar.tcl    the logic, with no host access
  tests/foobar.test the tests of the logic
  README.md
  .gitignore

Run it from its folder with ztcl run. The words after run are argv:

cd foobar
ztcl run you
hello, you

How ztcl run works

ztcl run reads ztcl.project, puts main and each file below the paths of files and data into the payload that ztcl build writes, and runs it as a built program runs:

A library has no main: run a script of it with ztcl FILE. The keys requires, net, and limits are errors of ztcl run until the dependencies, the network policy, and the limits of a project come.

How ztcl build works in a project

ztcl build with no script and no -o, in the folder of a project, builds the project: main is the script, each path of files and data goes into the program as with --embed, and allow and tcl are the defaults of --allow and --tcl. The program is build/NAME, or build/NAME.exe for Windows:

ztcl build                  # build/hello for this computer
ztcl build --target x86_64-linux-musl
./build/hello               # prints what `ztcl run` prints

--target, --aot, --allow, and --tcl work as in the full form; --allow and --tcl replace the values of the project file. A library has no program to build, and requires, net, and limits are errors here too.

A library

foobar/
  ztcl.project        the project file, with the import path
  foobar.tcl          the namespace foobar and its exported commands
  tests/foobar.test   the tests
  examples/hello.tcl  an example that uses the library
  docs/foobar.md      the reference of the commands
  README.md
  LICENSE             BSD-2-Clause, with the year and the owner to fill in
  .gitignore

Pick the license of the library before you publish it.

Test a project

ztcl test runs each file tests/*.test of the project, in sorted order, or the files that you name:

ztcl test
ztcl test tests/foobar.test
tests/foobar.test:	Total	1	Passed	1	Skipped	0	Failed	0
source [file join [file dirname [info script]] .. lib foobar.tcl]

test greet-1 {greet gives a greeting} -body {
    foobar::greet you
} -result {hello, you}

test greet-2 {the output of a proc} -body {
    puts [foobar::greet me]
} -output "hello, me
"

The project file

ztcl.project is Tcl words: one key value pair on each line, with # comments. ztcl reads it with the parser of Tcl and runs no command, so a word with $, [, or a backslash out of braces is an error.

format   1
kind     tool
name     foobar
version  0.1.0
tcl      9.1
main     main.tcl
files    {lib}
allow    {}
tests    tests
requires {}
Key Value
format 1.
kind tool, library, or app.
name The name of a tool or an app.
path The import path of a library.
version MAJOR.MINOR.PATCH of a tool or an app.
tcl 8.6 or 9.1.
main The first script of a tool or an app.
files The files and folders of the program.
allow The capabilities of the program, with the names of --allow.
tests The folder of the tests.
requires The dependencies: an import path and its version.

A key that ztcl does not know, a key given two times, and a key of another kind of project are errors with their line.

The rules of a good project

  1. The logic is apart from the start script. lib/ has the model and the rules, and main.tcl connects the parts.
  2. The tests need no capability.
  3. The capabilities are in allow, and the template starts with none.
  4. The program gets its code from its own files, never from the network at run time.
  5. Paths come from [file dirname [info script]], so the same code runs from the project folder and from a program of ztcl build.
  6. The logic uses no clock and no random number without a seed from the caller, so a test gives the same result each time.
  7. A library has one namespace, exports its commands, and sets no global variable.

Next

The dependencies of requires come next.