forked from zigbyexample/zigbyexample.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.zig
More file actions
72 lines (60 loc) · 2.29 KB
/
Copy pathbuild.zig
File metadata and controls
72 lines (60 loc) · 2.29 KB
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
const std = @import("std");
const builtin = @import("builtin");
pub fn build(b: *std.build.Builder) !void {
const cwd = std.fs.cwd();
const optimize = b.standardOptimizeOption(.{});
const test_step = b.step("test", "Test code exmaples");
for (examples) |example| {
if (builtin.os.tag == .windows and example.unix_only) continue;
const contact_code_example = try std.mem.concat(b.allocator, u8, &.{ example.name, ".zig" });
const code_path = try std.fs.path.join(b.allocator, &.{ "src/", contact_code_example });
const example_test = b.addTest(.{
.name = example.name,
.root_source_file = .{ .path = code_path },
.optimize = optimize,
});
if (example.build_only) {
test_step.dependOn(&example_test.run().step);
} else {
test_step.dependOn(&example_test.step);
}
}
inline for (examples) |example| {
const output_path = example.name ++ ".md";
const code_path = "src/" ++ example.name ++ ".zig";
const template_path = "template/" ++ example.name ++ ".md";
try cwd.copyFile(template_path, cwd, output_path, .{});
var output_file = try cwd.openFile(output_path, .{ .mode = .write_only });
var code_file = try cwd.openFile(code_path, .{ .mode = .read_only });
defer output_file.close();
defer code_file.close();
const code_content = try code_file.readToEndAlloc(b.allocator, 1024 * 1024);
try output_file.seekFromEnd(0);
try output_file.writer().print(
\\
\\```zig
\\{s}
\\```
\\
, .{code_content});
}
}
const examples = &[_]Example{
.{ .name = "atomic" },
.{ .name = "compressing-data" },
.{ .name = "command-line-arguments" },
.{ .name = "directory-listing" },
.{ .name = "hashing" },
.{ .name = "http-client", .build_only = true, .unix_only = true }, // TODO
.{ .name = "json" },
.{ .name = "mutex" },
.{ .name = "read-input", .build_only = true },
.{ .name = "read-write-file" },
.{ .name = "spawn-subprocess", .unix_only = true },
.{ .name = "tcp-connection" },
};
const Example = struct {
name: []const u8,
build_only: bool = false,
unix_only: bool = false,
};