tatara-lisp-script 0.2.1

tatara-script — standalone interpreter that makes tatara-lisp a full scripting language for nix-run apps. Wraps tatara-lisp-eval with a scripting-oriented stdlib (http, json, yaml, sops, file I/O, env, sha256, string ops) so a .tlisp file can replace a bash script one-for-one.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
//! Filesystem primitives — directories, globs, metadata, temp files.
//!
//!   (glob PATTERN)              → list of paths matching (e.g. "src/**/*.rs")
//!   (walk-dir PATH)             → flat list of every file under PATH
//!   (ls PATH)                   → list of entries (files + dirs) directly inside
//!   (mkdir PATH)                → nil (fails silently if exists)
//!   (mkdir-p PATH)              → nil; creates all intermediate dirs
//!   (rm PATH)                   → nil; deletes file
//!   (rm-rf PATH)                → nil; deletes recursively
//!   (cwd)                       → current working directory
//!   (chdir PATH)                → nil; changes cwd
//!   (path-join A B …)           → joined path string
//!   (path-basename PATH)        → last path component
//!   (path-dirname PATH)         → parent dir
//!   (path-extension PATH)       → extension without leading dot
//!   (path-absolute PATH)        → absolute canonical path
//!   (file-size PATH)            → bytes
//!   (is-dir? PATH)              → bool
//!   (is-file? PATH)             → bool
//!   (tmp-dir)                   → fresh temp dir (auto-cleaned by OS)
//!   (tmp-file)                  → fresh temp file path

use std::path::{Path, PathBuf};
use std::sync::Arc;

use tatara_lisp_eval::{Arity, EvalError, Interpreter, Value};

use crate::script_ctx::ScriptCtx;
use crate::stdlib::env::str_arg;

pub fn install(interp: &mut Interpreter<ScriptCtx>) {
    // ── glob / walk ──────────────────────────────────────────────
    interp.register_fn(
        "glob",
        Arity::Exact(1),
        |args: &[Value], _ctx: &mut ScriptCtx, sp| {
            let pattern = str_arg(&args[0], "glob", sp)?;
            let entries = simple_glob(&pattern).map_err(|e| EvalError::native_fn("glob", e, sp))?;
            Ok(Value::list(
                entries
                    .into_iter()
                    .map(|p| Value::Str(Arc::from(p.to_string_lossy().into_owned())))
                    .collect::<Vec<_>>(),
            ))
        },
    );

    interp.register_fn(
        "walk-dir",
        Arity::Exact(1),
        |args: &[Value], _ctx: &mut ScriptCtx, sp| {
            let root = str_arg(&args[0], "walk-dir", sp)?;
            let mut out = Vec::new();
            walk_collect(Path::new(&*root), &mut out)
                .map_err(|e| EvalError::native_fn("walk-dir", e.to_string(), sp))?;
            Ok(Value::list(
                out.into_iter()
                    .map(|p| Value::Str(Arc::from(p.to_string_lossy().into_owned())))
                    .collect::<Vec<_>>(),
            ))
        },
    );

    interp.register_fn(
        "ls",
        Arity::Exact(1),
        |args: &[Value], _ctx: &mut ScriptCtx, sp| {
            let dir = str_arg(&args[0], "ls", sp)?;
            let mut entries: Vec<PathBuf> = std::fs::read_dir(&*dir)
                .map_err(|e| EvalError::native_fn("ls", format!("{dir}: {e}"), sp))?
                .filter_map(|r| r.ok().map(|e| e.path()))
                .collect();
            entries.sort();
            Ok(Value::list(
                entries
                    .into_iter()
                    .map(|p| Value::Str(Arc::from(p.to_string_lossy().into_owned())))
                    .collect::<Vec<_>>(),
            ))
        },
    );

    // ── create / delete ──────────────────────────────────────────
    interp.register_fn(
        "mkdir",
        Arity::Exact(1),
        |args: &[Value], _ctx: &mut ScriptCtx, sp| {
            let path = str_arg(&args[0], "mkdir", sp)?;
            match std::fs::create_dir(&*path) {
                Ok(()) => Ok(Value::Nil),
                Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => Ok(Value::Nil),
                Err(e) => Err(EvalError::native_fn("mkdir", format!("{path}: {e}"), sp)),
            }
        },
    );

    interp.register_fn(
        "mkdir-p",
        Arity::Exact(1),
        |args: &[Value], _ctx: &mut ScriptCtx, sp| {
            let path = str_arg(&args[0], "mkdir-p", sp)?;
            std::fs::create_dir_all(&*path)
                .map_err(|e| EvalError::native_fn("mkdir-p", format!("{path}: {e}"), sp))?;
            Ok(Value::Nil)
        },
    );

    interp.register_fn(
        "rm",
        Arity::Exact(1),
        |args: &[Value], _ctx: &mut ScriptCtx, sp| {
            let path = str_arg(&args[0], "rm", sp)?;
            std::fs::remove_file(&*path)
                .map_err(|e| EvalError::native_fn("rm", format!("{path}: {e}"), sp))?;
            Ok(Value::Nil)
        },
    );

    interp.register_fn(
        "rm-rf",
        Arity::Exact(1),
        |args: &[Value], _ctx: &mut ScriptCtx, sp| {
            let path = str_arg(&args[0], "rm-rf", sp)?;
            if Path::new(&*path).is_dir() {
                std::fs::remove_dir_all(&*path)
                    .map_err(|e| EvalError::native_fn("rm-rf", format!("{path}: {e}"), sp))?;
            } else if Path::new(&*path).exists() {
                std::fs::remove_file(&*path)
                    .map_err(|e| EvalError::native_fn("rm-rf", format!("{path}: {e}"), sp))?;
            }
            Ok(Value::Nil)
        },
    );

    // ── cwd / chdir ──────────────────────────────────────────────
    interp.register_fn(
        "cwd",
        Arity::Exact(0),
        |_args: &[Value], _ctx: &mut ScriptCtx, sp| {
            let d = std::env::current_dir()
                .map_err(|e| EvalError::native_fn("cwd", e.to_string(), sp))?;
            Ok(Value::Str(Arc::from(d.to_string_lossy().into_owned())))
        },
    );

    interp.register_fn(
        "chdir",
        Arity::Exact(1),
        |args: &[Value], _ctx: &mut ScriptCtx, sp| {
            let path = str_arg(&args[0], "chdir", sp)?;
            std::env::set_current_dir(&*path)
                .map_err(|e| EvalError::native_fn("chdir", format!("{path}: {e}"), sp))?;
            Ok(Value::Nil)
        },
    );

    // ── path ops ─────────────────────────────────────────────────
    interp.register_fn(
        "path-join",
        Arity::AtLeast(1),
        |args: &[Value], _ctx: &mut ScriptCtx, sp| {
            let mut buf = PathBuf::new();
            for v in args {
                let s = str_arg(v, "path-join", sp)?;
                buf.push(&*s);
            }
            Ok(Value::Str(Arc::from(buf.to_string_lossy().into_owned())))
        },
    );

    interp.register_fn(
        "path-basename",
        Arity::Exact(1),
        |args: &[Value], _ctx: &mut ScriptCtx, sp| {
            let p = str_arg(&args[0], "path-basename", sp)?;
            let base = Path::new(&*p)
                .file_name()
                .map(|s| s.to_string_lossy().into_owned())
                .unwrap_or_default();
            Ok(Value::Str(Arc::from(base)))
        },
    );

    interp.register_fn(
        "path-dirname",
        Arity::Exact(1),
        |args: &[Value], _ctx: &mut ScriptCtx, sp| {
            let p = str_arg(&args[0], "path-dirname", sp)?;
            let dir = Path::new(&*p)
                .parent()
                .map(|s| s.to_string_lossy().into_owned())
                .unwrap_or_default();
            Ok(Value::Str(Arc::from(dir)))
        },
    );

    interp.register_fn(
        "path-extension",
        Arity::Exact(1),
        |args: &[Value], _ctx: &mut ScriptCtx, sp| {
            let p = str_arg(&args[0], "path-extension", sp)?;
            let ext = Path::new(&*p)
                .extension()
                .map(|s| s.to_string_lossy().into_owned())
                .unwrap_or_default();
            Ok(Value::Str(Arc::from(ext)))
        },
    );

    interp.register_fn(
        "path-absolute",
        Arity::Exact(1),
        |args: &[Value], _ctx: &mut ScriptCtx, sp| {
            let p = str_arg(&args[0], "path-absolute", sp)?;
            let abs = std::fs::canonicalize(&*p)
                .map_err(|e| EvalError::native_fn("path-absolute", format!("{p}: {e}"), sp))?;
            Ok(Value::Str(Arc::from(abs.to_string_lossy().into_owned())))
        },
    );

    // ── metadata ─────────────────────────────────────────────────
    interp.register_fn(
        "file-size",
        Arity::Exact(1),
        |args: &[Value], _ctx: &mut ScriptCtx, sp| {
            let p = str_arg(&args[0], "file-size", sp)?;
            let meta = std::fs::metadata(&*p)
                .map_err(|e| EvalError::native_fn("file-size", format!("{p}: {e}"), sp))?;
            Ok(Value::Int(meta.len() as i64))
        },
    );

    interp.register_fn(
        "is-dir?",
        Arity::Exact(1),
        |args: &[Value], _ctx: &mut ScriptCtx, sp| {
            let p = str_arg(&args[0], "is-dir?", sp)?;
            Ok(Value::Bool(Path::new(&*p).is_dir()))
        },
    );

    interp.register_fn(
        "is-file?",
        Arity::Exact(1),
        |args: &[Value], _ctx: &mut ScriptCtx, sp| {
            let p = str_arg(&args[0], "is-file?", sp)?;
            Ok(Value::Bool(Path::new(&*p).is_file()))
        },
    );

    // ── temp ─────────────────────────────────────────────────────
    interp.register_fn(
        "tmp-dir",
        Arity::Exact(0),
        |_args: &[Value], _ctx: &mut ScriptCtx, sp| {
            let base = std::env::temp_dir();
            let now = std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .map(|d| d.as_nanos())
                .unwrap_or(0);
            let dir = base.join(format!("tatara-script-{now:x}"));
            std::fs::create_dir_all(&dir)
                .map_err(|e| EvalError::native_fn("tmp-dir", e.to_string(), sp))?;
            Ok(Value::Str(Arc::from(dir.to_string_lossy().into_owned())))
        },
    );

    interp.register_fn(
        "tmp-file",
        Arity::Exact(0),
        |_args: &[Value], _ctx: &mut ScriptCtx, sp| {
            let base = std::env::temp_dir();
            let now = std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .map(|d| d.as_nanos())
                .unwrap_or(0);
            let path = base.join(format!("tatara-script-{now:x}.tmp"));
            std::fs::write(&path, b"")
                .map_err(|e| EvalError::native_fn("tmp-file", e.to_string(), sp))?;
            Ok(Value::Str(Arc::from(path.to_string_lossy().into_owned())))
        },
    );
}

/// Walk a directory tree, collecting every file (not directories).
fn walk_collect(root: &Path, out: &mut Vec<PathBuf>) -> std::io::Result<()> {
    let mut stack = vec![root.to_path_buf()];
    while let Some(cur) = stack.pop() {
        if cur.is_file() {
            out.push(cur);
            continue;
        }
        if !cur.is_dir() {
            continue;
        }
        for entry in std::fs::read_dir(&cur)? {
            let entry = entry?;
            stack.push(entry.path());
        }
    }
    Ok(())
}

/// Minimal glob engine supporting `*` (non-slash) and `**` (recursive).
/// Patterns are absolute or relative; relative patterns resolve against
/// the current directory.
fn simple_glob(pattern: &str) -> Result<Vec<PathBuf>, String> {
    let (prefix, remainder) = split_glob_prefix(pattern);
    let base = if prefix.is_empty() {
        PathBuf::from(".")
    } else {
        PathBuf::from(&prefix)
    };
    let parts: Vec<&str> = remainder.split('/').filter(|s| !s.is_empty()).collect();
    if parts.is_empty() {
        return Ok(vec![base]);
    }
    let mut out = Vec::new();
    walk_glob(&base, &parts, 0, &mut out);
    Ok(out)
}

fn split_glob_prefix(pattern: &str) -> (String, String) {
    // Return (literal_prefix, glob_tail). Literal prefix is the path up
    // to the first component containing `*`.
    let mut literal = String::new();
    let mut rest = String::new();
    let mut found_glob = false;
    for (i, component) in pattern.split('/').enumerate() {
        if !found_glob && !component.contains('*') {
            if i > 0 && !literal.is_empty() {
                literal.push('/');
            }
            literal.push_str(component);
        } else {
            found_glob = true;
            if !rest.is_empty() {
                rest.push('/');
            }
            rest.push_str(component);
        }
    }
    if literal.is_empty() && !found_glob {
        literal = pattern.to_string();
    }
    (literal, rest)
}

fn walk_glob(dir: &Path, parts: &[&str], idx: usize, out: &mut Vec<PathBuf>) {
    if idx >= parts.len() {
        out.push(dir.to_path_buf());
        return;
    }
    let pat = parts[idx];
    let entries = match std::fs::read_dir(dir) {
        Ok(e) => e,
        Err(_) => return,
    };
    if pat == "**" {
        // Match zero or more directory levels.
        walk_glob(dir, parts, idx + 1, out);
        for entry in entries.flatten() {
            let p = entry.path();
            if p.is_dir() {
                walk_glob(&p, parts, idx, out);
            }
        }
        return;
    }
    for entry in entries.flatten() {
        let name = entry.file_name();
        let name_s = name.to_string_lossy();
        if glob_match(pat, &name_s) {
            let p = entry.path();
            if idx + 1 == parts.len() {
                out.push(p);
            } else if p.is_dir() {
                walk_glob(&p, parts, idx + 1, out);
            }
        }
    }
}

fn glob_match(pattern: &str, name: &str) -> bool {
    // Simple `*` semantics: matches any sequence of characters except /.
    let mut pi = 0;
    let mut ni = 0;
    let pbytes = pattern.as_bytes();
    let nbytes = name.as_bytes();
    let mut star: Option<(usize, usize)> = None;
    while ni < nbytes.len() {
        if pi < pbytes.len() && (pbytes[pi] == b'?' || pbytes[pi] == nbytes[ni]) {
            pi += 1;
            ni += 1;
        } else if pi < pbytes.len() && pbytes[pi] == b'*' {
            star = Some((pi, ni));
            pi += 1;
        } else if let Some((sp, sn)) = star {
            pi = sp + 1;
            ni = sn + 1;
            star = Some((sp, ni));
        } else {
            return false;
        }
    }
    while pi < pbytes.len() && pbytes[pi] == b'*' {
        pi += 1;
    }
    pi == pbytes.len()
}