-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.zig
90 lines (69 loc) · 2.86 KB
/
build.zig
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
const std = @import("std");
const zdf = @import("./src/zdf/src/main.zig");
const templatesPath = "./gitignore/templates";
const filenameWithoutExtension = zdf.utils.filenameWithoutExtension;
fn countFiles(path: []const u8) !usize {
var dir = try std.fs.cwd().openDir(path, .{ .iterate = true });
defer dir.close();
var walker = try dir.walk(std.heap.page_allocator);
defer walker.deinit();
var n: usize = 0;
while (try walker.next()) |entry| {
if (std.mem.eql(u8, std.fs.path.extension(entry.path), ".gitignore")) {
n += 1;
}
}
return n;
}
pub fn build(b: *std.build.Builder) !void {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();
const allocator = &arena.allocator;
// Standard target options allows the person running `zig build` to choose
// what target to build for. Here we do not override the defaults, which
// means any target is allowed, and the default is native. Other options
// for restricting supported target set are available.
const target = b.standardTargetOptions(.{});
// Standard release options allow the person running `zig build` to select
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
const mode = b.standardReleaseOptions();
const exe = b.addExecutable("gi", "src/main.zig");
// b.addUserInputOption
exe.addPackagePath("zdf", "./src/zdf/src/main.zig");
exe.setTarget(target);
exe.setBuildMode(mode);
exe.install();
const run_cmd = exe.run();
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
var numFiles = try countFiles(templatesPath);
var dir = try std.fs.cwd().openDir(templatesPath, .{ .iterate = true });
var walker = try dir.walk(allocator);
defer walker.deinit();
// var opts = b.addOptions();
// opts.addOption()
// _ = numFiles;
var files: [][]const u8 = try allocator.alloc([]const u8, numFiles);
var contents: [][]const u8 = try allocator.alloc([]const u8, numFiles);
defer allocator.free(files);
defer allocator.free(contents);
var i: usize = 0;
while (try walker.next()) |entry| {
if (std.mem.eql(u8, std.fs.path.extension(entry.path), ".gitignore")) {
files[i] = try std.mem.dupe(allocator, u8, filenameWithoutExtension(entry.path));
var f = try dir.openFile(entry.basename, .{});
defer f.close();
var content = try f.readToEndAlloc(allocator, 1024 * 1024 * 1024);
contents[i] = content;
i += 1;
}
}
var opts = b.addOptions();
opts.addOption([]const []const u8, "filenames", files);
opts.addOption([]const []const u8, "contents", contents);
exe.addOptions("data", opts);
}