summaryrefslogtreecommitdiff
path: root/src/host/root.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-16 23:37:54 -0300
committerGabriel Schneider <[email protected]>2026-11-17 12:15:01 -0300
commitca602943e3fc9faad24e51497eeafb0ae7856800 (patch)
treedc35633094f5355d535587f388d6ffd3896dace1 /src/host/root.zig
parentc3b9938e9cb381bc7378fdca9073520ff1711ad6 (diff)
downloadsketchbook-ca602943e3fc9faad24e51497eeafb0ae7856800.tar.gz
sketchbook-ca602943e3fc9faad24e51497eeafb0ae7856800.zip
Keep a sketch's files in .sketchbook/, a shipped game's in XDG
zig build run, probe and export pass --state .sketchbook/<name>.state. The host makes that directory with a .gitignore of * (a sessions database grows, and the jj snapshot build_info takes would commit it while under a megabyte), and moves the state and store an older build kept beside it in once, with renameat2(NOREPLACE): when both exist nothing moves and the host says so. Without --state an interactive run keeps its files in $XDG_DATA_HOME/<name>/ (~/.local/share without it), not the cwd; a script or an export still uses sketch.state. test-paths checks all three cases. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'src/host/root.zig')
-rw-r--r--src/host/root.zig38
1 files changed, 36 insertions, 2 deletions
diff --git a/src/host/root.zig b/src/host/root.zig
index 618512b..5889b42 100644
--- a/src/host/root.zig
+++ b/src/host/root.zig
@@ -2,7 +2,8 @@
//! that it hot-reloads whenever a build replaces it.
//!
//! sketchbook [options] [LIBRARY]
-//! --state FILE persistent state (default: <name>.state)
+//! --state FILE persistent state (default: $XDG_DATA_HOME/<name>/<name>.state;
+//! sketch.state for a script or an export)
//! --headless offscreen video, silent audio
//! --script FILE play a script instead of reading devices
//! --fresh start from default state (sessions, marks and clips are kept)
@@ -99,8 +100,10 @@ pub fn main(init: std.process.Init, linked: ?abi.Sketch) !void {
// removed instead: nothing reads them, and a player may have the given
// ones open.
var private_buf: [std.fs.max_path_bytes]u8 = undefined;
+ if (options.state.len == 0 and (script_path != null or options.export_to != null)) options.state = "sketch.state";
const given = options.state;
const private = (script_path != null or options.export_to != null) and options.fresh;
+ if (!private) sketchbookDir(init.io, given);
if (private) options.state = std.mem.print(&private_buf, "{s}.{d}", .{ given, std.os.linux.getpid() }) catch fatal("--state path too long");
defer if (private) for ([_][]const u8{ "", ".store" }) |suffix| {
var from: [std.fs.max_path_bytes + 16]u8 = undefined;
@@ -128,7 +131,8 @@ pub fn main(init: std.process.Init, linked: ?abi.Sketch) !void {
const usage =
\\usage: sketchbook [options] [LIBRARY]
\\ --9p SOCKET local 9P controls: ctl, status, log, screen.png
- \\ --state FILE persistent state (default: sketch.state)
+ \\ --state FILE persistent state; sessions are kept beside it (default:
+ \\ $XDG_DATA_HOME/<name>/<name>.state, sketch.state for --script, --export)
\\ --headless offscreen video, silent audio, no vsync
\\ --script FILE play a script instead of reading devices
\\ --fresh start from default state (sessions, marks and clips are kept)
@@ -144,6 +148,36 @@ const usage =
\\
;
+/// A state file in a directory named `.sketchbook` (`zig build run`'s):
+/// the directory is made, with a `.gitignore` of `*` (its sessions database
+/// grows, and a jj snapshot would commit it), and the state and store files
+/// an older build kept beside it are moved in, once. A file is never
+/// replaced: when both exist, nothing moves.
+fn sketchbookDir(io: std.Io, state: []const u8) void {
+ const dir = std.fs.path.dirname(state) orelse return;
+ if (!std.mem.eql(u8, std.fs.path.basename(dir), ".sketchbook")) return;
+ const cwd = std.Io.Dir.cwd();
+ cwd.createDirPath(io, dir) catch return;
+ var buf: [std.fs.max_path_bytes]u8 = undefined;
+ const ignore = std.fmt.bufPrint(&buf, "{s}/.gitignore", .{dir}) catch return;
+ cwd.writeFile(io, .{ .sub_path = ignore, .data = "*\n", .flags = .{ .exclusive = true } }) catch {};
+ for ([_][]const u8{ "", ".store" }) |suffix| {
+ var from_buf: [std.fs.max_path_bytes]u8 = undefined;
+ var to_buf: [std.fs.max_path_bytes]u8 = undefined;
+ const parent = std.fs.path.dirname(dir) orelse ".";
+ const from = std.fmt.bufPrintSentinel(&from_buf, "{s}/{s}{s}", .{ parent, std.fs.path.basename(state), suffix }, 0) catch return;
+ const to = std.fmt.bufPrintSentinel(&to_buf, "{s}{s}", .{ state, suffix }, 0) catch return;
+ switch (std.os.linux.errno(std.os.linux.renameat2(std.os.linux.AT.FDCWD, from.ptr, std.os.linux.AT.FDCWD, to.ptr, .{ .NOREPLACE = true }))) {
+ .SUCCESS => std.debug.print("sketchbook: moved {s} to {s}\n", .{ from, to }),
+ .EXIST => {
+ std.debug.print("sketchbook: {s} and {s} both exist: nothing moved, {s} is used\n", .{ from, to, to });
+ return;
+ },
+ else => {},
+ }
+ }
+}
+
fn fatal(message: []const u8) noreturn {
std.debug.print("sketchbook: {s}\n", .{message});
std.process.exit(2);