Script Writers
rb.scripts is sugar over rb.file() for executable scripts. Every helper does the same three things:
- Prepends
#!/usr/bin/env <interpreter>(or the literal interpreter path if it begins with/). - Writes the body via the standard deferred
WriteFileop. - Defers a
Chmodop that sets mode0755.
Both ops participate in rb plan and rb apply like any other Rootbeer side effect.
local rb = require("rootbeer")
rb.scripts.bash("~/.local/bin/hello", [[
echo "hello $1"
]])
rb.scripts.python("~/.local/bin/greet", [[
import sys
print(f"hello, {sys.argv[1] if len(sys.argv) > 1 else 'world'}")
]])What these writers do — and don't
Script writers do not validate the body, install the interpreter, or manage PATH. The interpreter is referenced by name and must already be resolvable on the target machine when the script runs — linting and toolchain isolation are out of scope. A trailing newline is added if the body doesn't already end in one, so the resulting file is always well-formed.
Named helpers
Each helper is rb.scripts.<lang>(path, body):
| Helper | Shebang |
|---|---|
rb.scripts.bash | #!/usr/bin/env bash |
rb.scripts.sh | #!/usr/bin/env sh |
rb.scripts.zsh | #!/usr/bin/env zsh |
rb.scripts.fish | #!/usr/bin/env fish |
rb.scripts.python | #!/usr/bin/env python3 |
rb.scripts.node | #!/usr/bin/env node |
rb.scripts.lua | #!/usr/bin/env lua |
rb.scripts.nu | #!/usr/bin/env nu |
rb.scripts.ruby | #!/usr/bin/env ruby |
rb.scripts.perl | #!/usr/bin/env perl |
Custom interpreters
Use rb.scripts.script(interpreter, path, body) for languages without a named helper, or to pin a specific interpreter path:
rb.scripts.script("awk", "~/.local/bin/sum", [[
{ total += $1 } END { print total }
]])
rb.scripts.script("/opt/homebrew/bin/python3.12", "~/.local/bin/pinned", [[
import sys; print(sys.version)
]])A bare command ("awk") becomes #!/usr/bin/env awk. An absolute path (/opt/homebrew/bin/python3.12) is used verbatim as the shebang.
API
rootbeer.scripts.bash(path, body)
Writes an executable Bash script (#!/usr/bin/env bash). Defers a WriteFile op for the body and a Chmod op (mode 0755).
Parameters
pathstringbodystringrootbeer.scripts.fish(path, body)
Writes an executable Fish script (#!/usr/bin/env fish).
Parameters
pathstringbodystringrootbeer.scripts.lua(path, body)
Writes an executable Lua script (#!/usr/bin/env lua).
Parameters
pathstringbodystringrootbeer.scripts.node(path, body)
Writes an executable Node.js script (#!/usr/bin/env node).
Parameters
pathstringbodystringrootbeer.scripts.nu(path, body)
Writes an executable Nushell script (#!/usr/bin/env nu).
Parameters
pathstringbodystringrootbeer.scripts.perl(path, body)
Writes an executable Perl script (#!/usr/bin/env perl).
Parameters
pathstringbodystringrootbeer.scripts.python(path, body)
Writes an executable Python 3 script (#!/usr/bin/env python3).
Parameters
pathstringbodystringrootbeer.scripts.ruby(path, body)
Writes an executable Ruby script (#!/usr/bin/env ruby).
Parameters
pathstringbodystringrootbeer.scripts.script(interpreter, path, body)
Generic script writer with a custom interpreter shebang. Use a bare command ("bash") which becomes #!/usr/bin/env bash, or an absolute path ("/bin/sh") which is used verbatim. Prefer the named helpers (bash, python, …) for common languages.
Parameters
interpreterstringpathstring~ is expanded).bodystringrootbeer.scripts.sh(path, body)
Writes an executable POSIX sh script (#!/usr/bin/env sh).
Parameters
pathstringbodystringrootbeer.scripts.zsh(path, body)
Writes an executable Zsh script (#!/usr/bin/env zsh).
Parameters
pathstringbodystring