aboutsummaryrefslogtreecommitdiff
path: root/src/template.zig
blob: 3e7f530565adfee8384ab9901e4cb1c195341ca0 (plain) (blame)
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
const std = @import("std");
const builtin = @import("builtin");
const assert = std.debug.assert;

const cli = @import("cli.zig");
const fs = @import("fs.zig");
const conf = @import("conf.zig");
const shell = @import("shell.zig");
const Env = @import("env.zig");
const fatal = cli.fatal;

pub const Config = struct {
    const max_archives = 16;

    pub const Archive = struct {
        /// The id of the distribution file.
        /// Default: empty (no distname suffix)
        id: []const u8,
        /// The path that should be the root of the archive (relative to the repository's root).
        root: ?[]const u8 = null,
        /// A list of paths that should be included in the archive.
        /// All paths are relative to the root's root.
        /// Default: empty (everything is included)
        paths: [64][]const u8 = undefined,
        paths_len: usize = 0,
    };

    raw: conf.Config,

    prepare_target: ?[]const u8,
    archives: [max_archives]Archive,
    archives_len: usize,

    fn from_file(env: *const Env, tmpl: Template) !Config {
        const config_file = tmpl.config_file();
        const conf_res = try conf.Config.from_file(env.allocator, &config_file);
        switch (conf_res) {
            .conf => |c| return .init(env, c),
            .err => |e| {
                env.err("{s} (line {d})", .{ e.msg, e.line });
                tmpl_corrupted(tmpl.name, "invalid configuration");
            },
        }
    }

    fn init(env: *const Env, raw: conf.Config) Config {
        var dist_prepare_target: ?[]const u8 = null;
        var archives: [max_archives]Archive = undefined;
        var n_archives: usize = 0;

        var iter = raw.iterate();
        while (iter.next()) |v| {
            if (std.mem.eql(u8, v.key, "dist.prepare-target")) {
                dist_prepare_target = if (v.val.len > 0) v.val else null;
            } else if (std.mem.startsWith(u8, v.key, "dist.archive")) {
                var archive_key = v.key[12..];
                if (std.mem.findScalar(u8, archive_key, '.')) |dot| {
                    var archive_id: []const u8 = undefined;
                    if (dot == 0) {
                        archive_id = "";
                    } else if (archive_key[0] != '[' or archive_key[dot - 1] != ']') {
                        env.warn("unknown template configuration `{s}`", .{v.key});
                        continue;
                    } else {
                        archive_id = archive_key[1 .. dot - 1];
                    }
                    archive_key = archive_key[dot + 1 ..];

                    var archive: ?*Archive = null;
                    for (0..n_archives) |i| {
                        if (std.mem.eql(u8, archives[i].id, archive_id)) {
                            archive = &archives[i];
                            break;
                        }
                    }
                    if (archive == null) {
                        if (n_archives == max_archives) {
                            fatal("maximum number of distribution files exceeded", .{});
                        }
                        archive = &archives[n_archives];
                        archives[n_archives] = .{ .id = archive_id };
                        n_archives += 1;
                    }
                    if (std.mem.eql(u8, archive_key, "root")) {
                        archive.?.root = if (v.val.len > 0) v.val else null;
                    } else if (std.mem.eql(u8, archive_key, "paths")) {
                        archive.?.paths_len = split_paths(&archive.?.paths, v.val);
                    } else {
                        env.warn("unknown template configuration `{s}`", .{v.key});
                    }
                } else {
                    env.warn("unknown template configuration `{s}`", .{v.key});
                }
            } else {
                env.warn("unknown template configuration `{s}`", .{v.key});
            }
        }

        if (n_archives == 0) {
            archives[0] = .{ .id = "" };
            n_archives = 1;
        }

        return .{
            .raw = raw,
            .prepare_target = dist_prepare_target,
            .archives = archives,
            .archives_len = n_archives,
        };
    }

    pub fn deinit(self: Config) void {
        self.raw.deinit();
    }
};

pub const Template = struct {
    const config_filename = ".config";

    name: []const u8,
    path: fs.Path,

    pub fn init(env: *const Env, template_name: []const u8) Template {
        var template_path = etc_dir(env);
        template_path.append(.{template_name});
        return .{
            .name = template_name,
            .path = template_path,
        };
    }

    pub fn render(self: *const Template, env: *const Env, tmpl_confs: []const conf.Config, target: *const fs.Path) !void {
        try render_files_recursively(env, target, &self.path, "", tmpl_confs);
    }

    fn render_files_recursively(env: *const Env, dest_root: *const fs.Path, src_root: *const fs.Path, src_subpath: []const u8, tmpl_confs: []const conf.Config) !void {
        const path: fs.Path = .join(.{ src_root.name(), src_subpath });
        var iter = try fs.iterate(&path);
        defer iter.deinit();
        while (try iter.next()) |entry| {
            const entry_subpath: fs.Path = .join(.{ src_subpath, entry.name });
            const src_path: fs.Path = .join(.{ src_root.name(), entry_subpath.name() });
            const dest_path = try replace_vars_in_path(dest_root, entry_subpath.name(), tmpl_confs);
            switch (entry.type) {
                .dir => {
                    try fs.mkdir(&dest_path);
                    try render_files_recursively(env, dest_root, src_root, entry_subpath.name(), tmpl_confs);
                },
                .file => {
                    const filename = dest_path.name()[dest_root.len + 1 ..];
                    if (std.mem.eql(u8, filename, config_filename)) return;

                    env.info("writing template file {s}", .{filename});
                    const input = try fs.read_file(env.allocator, &src_path);

                    var output_buf: [512]u8 = undefined;
                    var output_writer = try fs.file_writer(&dest_path, &output_buf, .{ .create = true });
                    defer output_writer.deinit();

                    const output = &output_writer.interface;
                    try replace_vars(output, input, tmpl_confs);
                    try output.flush();
                },
                else => {}, // skip
            }
        }
    }

    /// Does not flush the writer.
    fn replace_vars(w: *std.Io.Writer, text: []const u8, tmpl_confs: []const conf.Config) !void {
        var rest = text;
        while (rest.len > 0) {
            const pos = std.mem.findScalar(u8, rest, '{') orelse {
                try w.writeAll(rest);
                return;
            };

            if (pos > 0) {
                try w.writeAll(rest[0..pos]);
                rest = rest[pos..];
            }

            assert(rest.len > 0 and rest[0] == '{');
            if (rest.len == 1) {
                try w.writeByte(rest[0]);
                return;
            }
            if (rest[1] != '{') {
                try w.writeAll(rest[0..2]);
                rest = rest[2..];
                continue;
            }

            // Consume all opening braces to take the innermost pair.
            var begin: usize = 2;
            while (begin < rest.len and rest[begin] == '{') {
                try w.writeByte('{');
                begin += 1;
            }

            const end = std.mem.findScalarPos(u8, rest, begin, '}') orelse return error.missing_variable_closing;
            if (end == rest.len - 1 or rest[end + 1] != '}') return error.missing_variable_closing;

            const var_name = rest[begin..end];
            if (var_name.len == 0) return error.missing_variable_name;
            var found_var = false;
            for (tmpl_confs) |config| {
                if (config.find_var(var_name)) |v| {
                    if (v.val.len > 0) {
                        try w.writeAll(v.val);
                    }
                    found_var = true;
                    break;
                }
            }
            if (!found_var) {
                fatal("variable `{s}` not defined", .{var_name});
            }

            rest = rest[end + 2 ..];
        }
    }

    fn replace_vars_in_path(root: *const fs.Path, subpath: []const u8, tmpl_confs: []const conf.Config) !fs.Path {
        assert(root.len > 0);
        assert(subpath.len > 0);

        const slash = std.fs.path.sep;
        var res: fs.Path = root.*;
        res.buf[res.len] = slash;
        res.len += 1;

        var res_writer = std.Io.Writer.fixed(res.buf[res.len..]);
        var name = subpath;
        while (name.len > 0) {
            const idx = std.mem.findScalar(u8, name, slash) orelse name.len - 1;
            const part = name[0 .. idx + 1];
            const pos = res_writer.end;
            replace_vars(&res_writer, part, tmpl_confs) catch |err| switch (err) {
                error.missing_variable_closing, error.missing_variable_name => {
                    res_writer.undo(res_writer.end - pos);
                    res_writer.writeAll(part) catch unreachable;
                },
                error.WriteFailed => return error.PathTooLong,
                else => unreachable,
            };
            name = name[idx + 1 ..];
        }
        res_writer.flush() catch unreachable;

        res.len += res_writer.end;
        res.buf[res.len] = 0;
        return res;
    }

    fn config_file(self: Template) fs.Path {
        return .join(.{ self.path.name(), config_filename });
    }
};

pub const Iterator = struct {
    path: fs.Path,
    iter: ?fs.Iterator,

    pub fn deinit(self: *Iterator) void {
        if (self.iter) |*iter| iter.deinit();
    }

    pub fn next(self: *Iterator) !?Template {
        if (self.iter) |*iter| {
            while (try iter.next()) |entry| {
                if (entry.type == .dir) {
                    return .{
                        .name = entry.name,
                        .path = .join(.{ self.path.name(), entry.name }),
                    };
                }
            }
        }
        return null;
    }
};

pub inline fn iterate(env: *const Env) !Iterator {
    var tmpl_iter: Iterator = .{ .path = etc_dir(env), .iter = undefined };
    tmpl_iter.iter = fs.iterate(&tmpl_iter.path) catch |err| it: {
        if (err == error.FileNotFound) break :it null;
        return err;
    };
    return tmpl_iter;
}

pub fn exists(env: *const Env, template_name: []const u8) !bool {
    return try get(env, template_name) != null;
}

pub fn get(env: *const Env, template_name: []const u8) !?Template {
    const tmpl: Template = .init(env, template_name);
    const info = try fs.dir_info(&tmpl.path);
    return if (info.exists) tmpl else null;
}

pub fn must_get(env: *const Env, template_name: []const u8) !Template {
    return try get(env, template_name) orelse {
        fatal("template not found: {s}", .{template_name});
    };
}

pub fn read_config(env: *const Env, tmpl: Template) !Config {
    return Config.from_file(env, tmpl);
}

pub fn edit_config(env: *const Env, template_name: []const u8) !void {
    const tmpl = try must_get(env, template_name);
    const config_file = tmpl.config_file();
    try shell.edit_config_file(env, &config_file, verify_config);
}

pub fn import_config(env: *const Env, template_name: []const u8, new_config_file: *const fs.Path) !void {
    const tmpl = try must_get(env, template_name);

    const parsed = try conf.Config.from_file(env.allocator, new_config_file);
    const config = switch (parsed) {
        .conf => |c| c,
        .err => |e| {
            env.err("{s} (line {d})", .{ e.msg, e.line });
            fatal("failed to import {s}", .{new_config_file.name()});
        },
    };
    defer config.deinit();

    if (!verify_config(env, config)) {
        std.process.exit(1);
    }

    const config_file = tmpl.config_file();
    try fs.mv_file(new_config_file, &config_file);
}

pub fn etc_dir(env: *const Env) fs.Path {
    return env.etc_path(.{"tmpl"});
}

fn verify_config(env: *const Env, raw: conf.Config) bool {
    const tmpl_conf: Config = .init(env, raw);
    var valid = true;
    if (tmpl_conf.prepare_target) |prepare_target| {
        if (std.mem.findAny(u8, prepare_target, " \n\t\r")) |_| {
            cli.stderr.write_line("Error: invalid value `dist.prepare-target`", .{});
            valid = false;
        }
    }
    for (tmpl_conf.archives[0..tmpl_conf.archives_len]) |archive| {
        if (archive.root) |root| {
            if (!fs.is_relative(root)) {
                if (archive.id.len > 0) {
                    cli.stderr.write_line("Error: value `dist.archive[{s}].root` is not a relative path", .{archive.id});
                } else {
                    cli.stderr.write_line("Error: value `dist.archive.root` is not a relative path", .{});
                }
                valid = false;
            }
        }
        for (archive.paths[0..archive.paths_len]) |path| {
            if (!fs.is_relative(path)) {
                if (archive.id.len > 0) {
                    cli.stderr.write_line("Error: value `dist.archive[{s}].paths` contains a non-relative path", .{archive.id});
                } else {
                    cli.stderr.write_line("Error: value `dist.archive.paths` contains a non-relative path", .{});
                }
                valid = false;
            }
        }
    }
    return valid;
}

fn split_paths(buf: [][]const u8, str: []const u8) usize {
    var path_count: usize = 0;
    var pos: usize = 0;
    while (pos < str.len) {
        pos = std.mem.findNonePos(u8, str, pos, " \t\r\n") orelse break;
        var space = pos;
        while (space < str.len) : (space += 1) {
            switch (str[space]) {
                ' ' => if (str[space - 1] != '\\') break,
                '\t', '\r', '\n' => break,
                else => {},
            }
        }

        buf[path_count] = str[pos..space];
        path_count += 1;
        pos = space + 1;
    }
    return path_count;
}

fn tmpl_corrupted(tmpl_name: []const u8, comptime reason: []const u8) noreturn {
    fatal("template {s} corrupted (" ++ reason ++ ")", .{tmpl_name});
}

test "parse config" {
    var raw: conf.Config = .init(std.testing.allocator);
    try raw.add_var("dist.prepare-target", "prepare-everything");
    try raw.add_var("dist.archive.root", "foo");
    try raw.add_var("dist.archive.paths", "foo");
    try raw.add_var("dist.archive[bar].root", "foo");
    try raw.add_var("dist.archive[bar].paths", "bar\nbaz");

    const env: Env = undefined;
    const config: Config = .init(&env, raw);
    defer config.deinit();

    try std.testing.expectEqualStrings("prepare-everything", config.prepare_target.?);
    try std.testing.expectEqual(2, config.archives_len);

    try std.testing.expectEqualStrings("", config.archives[0].id);
    try std.testing.expectEqualStrings("foo", config.archives[0].root.?);
    try std.testing.expectEqual(1, config.archives[0].paths_len);
    try std.testing.expectEqualStrings("foo", config.archives[0].paths[0]);

    try std.testing.expectEqualStrings("bar", config.archives[1].id);
    try std.testing.expectEqualStrings("foo", config.archives[1].root.?);
    try std.testing.expectEqual(2, config.archives[1].paths_len);
    try std.testing.expectEqualStrings("bar", config.archives[1].paths[0]);
    try std.testing.expectEqualStrings("baz", config.archives[1].paths[1]);
}

test "render simple template - variable at end" {
    const tmpl = "This is a simple {{test}} with multiple {{vars}} in different {{places}}";

    var config: conf.Config = .init(std.testing.allocator);
    defer config.deinit();

    try config.add_var("test", "template");
    try config.add_var("not", "used");
    try config.add_var("vars", "variables");
    try config.add_var("places", "positions");

    var rendered: std.Io.Writer.Allocating = try .initCapacity(std.testing.allocator, 2 * tmpl.len);
    defer rendered.deinit();

    try Template.replace_vars(&rendered.writer, tmpl, &.{config});
    try std.testing.expectEqualStrings("This is a simple template with multiple variables in different positions", rendered.written());
}

test "render simple template - variable at beginning" {
    const tmpl = "{{this}} is a simple {{test}} with multiple {{vars}} in different places";

    var config: conf.Config = .init(std.testing.allocator);
    defer config.deinit();

    try config.add_var("this", "This");
    try config.add_var("test", "template");
    try config.add_var("not", "used");
    try config.add_var("vars", "variables");
    try config.add_var("places", "positions");

    var rendered: std.Io.Writer.Allocating = try .initCapacity(std.testing.allocator, 2 * tmpl.len);
    defer rendered.deinit();

    try Template.replace_vars(&rendered.writer, tmpl, &.{config});
    try std.testing.expectEqualStrings("This is a simple template with multiple variables in different places", rendered.written());
}

test "render empty template" {
    const tmpl = "";

    const config: conf.Config = .init(std.testing.allocator);
    defer config.deinit();

    var rendered: std.Io.Writer.Allocating = .init(std.testing.allocator);
    defer rendered.deinit();

    try Template.replace_vars(&rendered.writer, tmpl, &.{config});
    try std.testing.expectEqualStrings("", rendered.written());
}

test "render template without braces" {
    const tmpl = "No braces at all";

    var config: conf.Config = .init(std.testing.allocator);
    defer config.deinit();

    try config.add_var("a", "foo");
    try config.add_var("b", "bar");

    var rendered: std.Io.Writer.Allocating = try .initCapacity(std.testing.allocator, tmpl.len);
    defer rendered.deinit();

    try Template.replace_vars(&rendered.writer, tmpl, &.{config});
    try std.testing.expectEqualStrings("No braces at all", rendered.written());
}

test "render template without variable and brace" {
    const tmpl = "{No variable seen here {";

    var config: conf.Config = .init(std.testing.allocator);
    defer config.deinit();

    try config.add_var("a", "foo");
    try config.add_var("b", "bar");

    var rendered: std.Io.Writer.Allocating = try .initCapacity(std.testing.allocator, tmpl.len);
    defer rendered.deinit();

    try Template.replace_vars(&rendered.writer, tmpl, &.{config});
    try std.testing.expectEqualStrings("{No variable seen here {", rendered.written());
}

test "render template with multiple opening braces" {
    const tmpl = "Use ${{{foo}}} in your env.";

    var config: conf.Config = .init(std.testing.allocator);
    defer config.deinit();

    try config.add_var("foo", "FOOBAR");

    var rendered: std.Io.Writer.Allocating = try .initCapacity(std.testing.allocator, tmpl.len);
    defer rendered.deinit();

    try Template.replace_vars(&rendered.writer, tmpl, &.{config});
    try std.testing.expectEqualStrings("Use ${FOOBAR} in your env.", rendered.written());
}

test "render template with missing variable closing" {
    const tmpl = "Invalid template {{foo";

    var config: conf.Config = .init(std.testing.allocator);
    defer config.deinit();

    try config.add_var("a", "foo");
    try config.add_var("b", "bar");

    var rendered: std.Io.Writer.Allocating = try .initCapacity(std.testing.allocator, tmpl.len);
    defer rendered.deinit();

    const res = Template.replace_vars(&rendered.writer, tmpl, &.{config});
    try std.testing.expectError(error.missing_variable_closing, res);
}

test "render template with missing variable closing at the end" {
    const tmpl = "Invalid template {{";

    var config: conf.Config = .init(std.testing.allocator);
    defer config.deinit();

    try config.add_var("a", "foo");
    try config.add_var("b", "bar");

    var rendered: std.Io.Writer.Allocating = try .initCapacity(std.testing.allocator, tmpl.len);
    defer rendered.deinit();

    const res = Template.replace_vars(&rendered.writer, tmpl, &.{config});
    try std.testing.expectError(error.missing_variable_closing, res);
}

test "render template with incomplete variable closing" {
    const tmpl = "Invalid {{foo} template";

    var config: conf.Config = .init(std.testing.allocator);
    defer config.deinit();

    try config.add_var("a", "foo");
    try config.add_var("b", "bar");

    var rendered: std.Io.Writer.Allocating = try .initCapacity(std.testing.allocator, tmpl.len);
    defer rendered.deinit();

    const res = Template.replace_vars(&rendered.writer, tmpl, &.{config});
    try std.testing.expectError(error.missing_variable_closing, res);
}

test "replace variables in path" {
    var config: conf.Config = .init(std.testing.allocator);
    defer config.deinit();

    try config.add_var("a", "foo");
    try config.add_var("b", "bar");

    var replaced: fs.Path = undefined;

    inline for ([_]struct {
        root: []const u8,
        subpath: []const u8,
        expected: []const u8,
    }{
        .{ .root = "root", .subpath = ".config/foo", .expected = "root/.config/foo" },
        .{ .root = "/usr/local", .subpath = "etc", .expected = "/usr/local/etc" },
        .{ .root = "/usr/local", .subpath = "etc/{{a}}.{{b}}", .expected = "/usr/local/etc/foo.bar" },
        .{ .root = "/usr/local", .subpath = "etc/{{/foo", .expected = "/usr/local/etc/{{/foo" },
        .{ .root = "/usr/local", .subpath = "etc/{{}}/foo", .expected = "/usr/local/etc/{{}}/foo" },
    }) |t| {
        const root: fs.Path = .init(t.root);
        replaced = try Template.replace_vars_in_path(&root, t.subpath, &.{config});
        try std.testing.expectEqualStrings(t.expected, replaced.name());
    }
}

test "split paths" {
    var paths: [8][]const u8 = undefined;
    var n: usize = undefined;

    n = split_paths(&paths, "");
    try std.testing.expectEqual(0, n);

    n = split_paths(&paths, "dir");
    try std.testing.expectEqual(1, n);
    try std.testing.expectEqualStrings("dir", paths[0]);

    n = split_paths(&paths, "dir/subdir");
    try std.testing.expectEqual(1, n);
    try std.testing.expectEqualStrings("dir/subdir", paths[0]);

    n = split_paths(&paths, "dir/sub\\ dir");
    try std.testing.expectEqual(1, n);
    try std.testing.expectEqualStrings("dir/sub\\ dir", paths[0]);

    n = split_paths(&paths, "dir_one/subdir dir_two");
    try std.testing.expectEqual(2, n);
    try std.testing.expectEqualStrings("dir_one/subdir", paths[0]);
    try std.testing.expectEqualStrings("dir_two", paths[1]);

    n = split_paths(&paths, "dir_one/subdir dir_two\ndir_three/foo\\ bar");
    try std.testing.expectEqual(3, n);
    try std.testing.expectEqualStrings("dir_one/subdir", paths[0]);
    try std.testing.expectEqualStrings("dir_two", paths[1]);
    try std.testing.expectEqualStrings("dir_three/foo\\ bar", paths[2]);
}