ela
Contents

ManualPackages

Paths and directories

In the terminalela --spec paths-and-directories

  • Modelled on Go's filepath package, with path.program as Go's exec.LookPath:
Built-inDoes
path.join(parts...)The parts joined with /, cleaned
path.base(p), path.dir(p), path.ext(p)The last element, everything before it, the last element's extension
path.abs(p)The path made absolute against the current folder
path.glob(pattern)The paths that match, as a list, empty when none do
path.program(name)The full path of the program name on the script's PATH, or "" when there is none
path.real(p)The path made absolute with every link in it followed, as far as the path is there; the rest as written, as realpath -m gives it
path.link(p)The target a symbolic link holds, as it is written in the link, as readlink gives it; anything else at p, or nothing, is an error
path.script()The absolute path of the file the call is written in, with its links followed, as readlink -f gives it; its folder is path.dir(path.script())
  • path.program(name) splits the script's own $env.PATH at :, so a with env.PATH block counts, and takes folder/name in each folder in turn, classified without opening it: the first regular file this user may run is the answer. A name with a / in it is checked as written. A script asks whether a program is there without running it or printing anything; the shell built-in which also names aliases, procs and packages.
if path.program("docker") != ""    docker compose up -dend if
  • path.real reads the links: a .. after a link goes up from where the link leads, and more than 40 links on the way, or a loop of them, is an error. Test that the path is there with file.exists.
root = path.real($args[0])                 # a checkout named through a link, as its own folderif file.is_link("$root/current")    echo "current is ${path.link("$root/current")}"end if
  • path.script() is the file the call is written in, worked out as the file is read, so a with dir or a cd later changes nothing. In an imported file it is that file, so a library finds what ships beside it. A script started through a link gives the file the link leads to. At the prompt, in ela -c and in a script read from stdin there is no file, and the call is an error: path.script: the code came from -c, so there is no file; give a path, or run it as a file.
#!/usr/bin/env ela# ~/Code/play/house-rules/scripts/roots, a link to ../ela/roots.elahere = path.dir(path.script())listed = file.read("$here/../roots")echo $here                                 # /home/ada/Code/play/house-rules/ela
  • path.abs works on the text alone, joining the path to the current folder and cleaning it: the path need not exist, and a link in it is not followed. path.base("/srv/app/") is app and path.dir("app") is ., as in Go.
  • Directories: dir.list(path, fold = false) returns the names in it, sorted by character code (B before a, ä after z), hidden ones included, and with fold = true as people read them, case and accents folded, a beside A and ä beside a, the same on every machine whatever LANG says (LANGUAGE.md, Sorting a list); dir.create(path) makes it and any missing parents, and a directory already there is fine; dir.delete(path) removes an empty directory only, since deleting a tree is one typo away from deleting the wrong one. dir.temp(), dir.size(path) and dir.walk(path) are mktemp -d, du -sb and find, under File programs in #files.
  • Globbing works as in Bash, for f in *.ela and for f in $dir/*.log, with one difference: a glob that matches nothing is an error, never passed on as text, so rm *.bak can't hand rm a file named *.bak.
  • A glob has * for any run of characters and ? for one, neither crossing a /, [abc] and [a-z] for one of a set and [^abc] for one outside it; \ makes the next character itself, in a set too ([\]\-]). Bash's [!abc] is an error that names [^abc], since Odin's match, which ela's globs use, reads it as ! or one of abc. Text from a variable or a quoted string is never a glob character, so [$x] is a set of $x's characters whatever they are. A name starting with a dot matches only a pattern that starts with one, . or \., so rm * leaves .git alone. The same rules hold in a command, in path.glob and in str.match.
  • Where no match is fine, path.glob(pattern) returns the matches as a list, empty when there are none, as Go's filepath.Glob does. Every *, ? and [ in its argument counts, since the argument is the pattern:
backups = path.glob("*.bak")if len($backups) > 0    rm $backupsend if