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:
- The files are at
//zipfs:/app/, read-only.[info script]is//zipfs:/app/main.tcl, so[file dirname [info script]]andsourcefind the files of the project with no capability. - The program has the capabilities of
allowand no other. Withallow {}, it cannot open a file of the host, run a program, or open a socket. - The first run of a project with capabilities puts a question on the terminal before any code of the project runs, as the capability lines of a script do: the path of the project file, each capability of
allow, and a warning for each capability that gives the full power of your user account. The answers arey(this run),a(always), andn(no run). "Always" is stored in~/.ztcl_allowfor the real path of the folder, the capabilities, and the SHA-256 of the code of the project:mainand each file offilesanddata. A copy in another folder gets the question again. A new capability ofallowgets the question for that capability alone, and the stored capabilities stay given.execincludesenv: a given or storedexeccovers an addedenvwith no question, and the question shows no line forenvnext to the line ofexec. A capability that leavesallowleaves the stored answer too, so it gets the question again when it comes back. Changed code gets the question for each capability again, with a line about the change of the code. A change of the project file outsideallow, such as itsname, gets no question. ztcl run --allow LISTgives the capabilities ofLISTin advance, as the answer to the question. The words after it areargv. With no terminal, a run with capabilities that the store and--allowdo not give stops and names the flag:ztcl run --allow clock,exec.ztcl buildputs no question: you build a project of your own, and the program has the capabilities ofallowby design.- A file that the project file does not list is not in the payload, so a run fails as the built program does.
ztcl script.tclstays as before and reads no project file.
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
- Each file runs in a new interpreter with no capability: no file of the host, no channel, no process, no socket, and no clock. The files of the project are at
//zipfs:/app/, read-only, as inztcl run, so a test file sources the code of the project with[file dirname [info script]]. - The interpreter has the commands of tcltest that a test file uses:
test,testConstraint,customMatch, andcleanupTests.testhas both forms:test NAME DESC BODY RESULT, and the options-constraints,-setup,-body,-cleanup,-result,-returnCodes,-match(exact,glob, orregexp),-output, and-errorOutput. - A failed test prints the report of tcltest. An error of a test file counts as a failure of the file. The status is 0 when each test passes, and 1 otherwise.
- With more than one file, a last line
all:gives the sums.
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
- The logic is apart from the start script.
lib/has the model and the rules, andmain.tclconnects the parts. - The tests need no capability.
- The capabilities are in
allow, and the template starts with none. - The program gets its code from its own files, never from the network at run time.
- Paths come from
[file dirname [info script]], so the same code runs from the project folder and from a program ofztcl build. - The logic uses no clock and no random number without a seed from the caller, so a test gives the same result each time.
- A library has one namespace, exports its commands, and sets no global variable.
Next
The dependencies of requires come next.