authorgravatar for meghan@unicycle.coMeghan Denny <meghan@unicycle.co> 2021-07-19 06:45:48 -07:00
committergravatar for meghan@unicycle.coMeghan Denny <meghan@unicycle.co> 2021-07-19 06:45:48 -07:00
logc8f50e0f0659d91a4bb8d6b22f5f8768490e6d65
tree073055f5204056d0461ed16da3a883d8bee99918
parentd95d1b7a27cc74c95fc4933cbe307c107d104c18

add `generate.zig`


5 files changed, 106 insertions(+), 1 deletions(-)

build.zig+18
......@@ -1,4 +1,5 @@
11const std = @import("std");
2const deps = @import("./deps.zig");
23
34pub fn build(b: *std.build.Builder) void {
45 // Standard target options allows the person running `zig build` to choose
......@@ -24,4 +25,21 @@ pub fn build(b: *std.build.Builder) void {
2425
2526 const run_step = b.step("run", "Run the app");
2627 run_step.dependOn(&run_cmd.step);
28
29 //
30
31 const exe2 = b.addExecutable("generate", "generate.zig");
32 exe2.setTarget(target);
33 exe2.setBuildMode(mode);
34 deps.addAllTo(exe2);
35 exe2.install();
36
37 const run_cmd2 = exe2.run();
38 run_cmd2.step.dependOn(b.getInstallStep());
39 if (b.args) |args| {
40 run_cmd2.addArgs(args);
41 }
42
43 const run_step2 = b.step("gen", "generate the list");
44 run_step2.dependOn(&run_cmd2.step);
2745}
generate.zig created+71
......@@ -0,0 +1,71 @@
1//! Run this with `zig build gen`
2
3const std = @import("std");
4const zfetch = @import("zfetch");
5const json = @import("json");
6
7pub fn main() !void {
8 var gpa = std.heap.GeneralPurposeAllocator(.{}){};
9 const alloc = &gpa.allocator;
10
11 const f = try std.fs.cwd().createFile("src/lib.zig", .{});
12 const w = f.writer();
13
14 {
15 std.log.info("spdx", .{});
16 const val = try simple_fetch(alloc, "https://raw.githubusercontent.com/spdx/license-list-data/master/json/licenses.json");
17
18 try w.writeAll("// SPDX License Data generated from https://github.com/spdx/license-list-data\n");
19 try w.writeAll("//\n");
20 try w.print("// Last generated from version {s}\n", .{val.get("licenseListVersion").?.String});
21 try w.writeAll("//\n");
22
23 try w.writeAll("\n");
24 try w.writeAll(
25 \\pub const License = struct {
26 \\ isOsiApproved: bool,
27 \\ url: []const u8,
28 \\};
29 \\
30 );
31
32 var licenses = val.get("licenses").?.Array;
33 std.sort.sort(json.Value, licenses, {}, spdxlicenseLessThan);
34
35 try w.writeAll("\n");
36 try w.writeAll("pub const spdx = struct {\n");
37 for (licenses) |lic| {
38 try w.print(" pub const @\"{s}\" = License{{.isOsiApproved = {}, .url = \"{s}\"}};\n", .{
39 lic.get("licenseId").?.String,
40 lic.get("isOsiApproved").?.Bool,
41 lic.get("seeAlso").?.Array[0].String,
42 });
43 }
44 try w.writeAll("};\n");
45
46 try w.writeAll("\n");
47 try w.writeAll("pub const osi = &[_][]const u8{\n");
48 for (licenses) |lic| {
49 if (!lic.get("isOsiApproved").?.Bool) continue;
50 try w.print(" \"{s}\",\n", .{lic.get("licenseId").?.String});
51 }
52 try w.writeAll("};\n");
53 }
54}
55
56pub fn simple_fetch(alloc: *std.mem.Allocator, url: []const u8) !json.Value {
57 const req = try zfetch.Request.init(alloc, url, null);
58 defer req.deinit();
59 try req.do(.GET, null, null);
60 const r = req.reader();
61 const body_content = try r.readAllAlloc(alloc, std.math.maxInt(usize));
62 const val = try json.parse(alloc, body_content);
63 return val;
64}
65
66fn spdxlicenseLessThan(context: void, lhs: json.Value, rhs: json.Value) bool {
67 _ = context;
68 const l = lhs.get("licenseId").?.String;
69 const r = rhs.get("licenseId").?.String;
70 return std.mem.lessThan(u8, l, r);
71}
zig.mod+4-1
......@@ -2,4 +2,7 @@ id: 0npcrzfdlrvkf44mzjo8bduj9gmqyefo0j3rstt6b0pm2r6r
22name: licenses
33main: src/lib.zig
44license: MIT
5dependencies:
5
6dev_dependencies:
7 - src: git https://github.com/truemedian/zfetch
8 - src: git https://github.com/nektro/zig-json
zigmod.lock created+7
......@@ -0,0 +1,7 @@
12
2git https://github.com/truemedian/zfetch commit-8bbc7b34cd417794841e1432585334bc969dfe83
3git https://github.com/truemedian/hzzp commit-2d30bddae3bf1eaecde5144490307604efe76f2a
4git https://github.com/alexnask/iguanaTLS commit-0d39a361639ad5469f8e4dcdaea35446bbe54b48
5git https://github.com/MasterQ32/zig-network commit-b9c91769d8ebd626c8e45b2abb05cbc28ccc50da
6git https://github.com/MasterQ32/zig-uri commit-52cdd2061bec0579519f0d30280597f3a1db8b75
7git https://github.com/nektro/zig-json commit-72e555fbc0776f2600aee19b01e5ab1855ebec7a
zigmod.sum created+6
......@@ -0,0 +1,6 @@
1blake3-e3072f7fb47e86d53c9a1879e254ba1af55941153fd5f6752ec659b2f14854c9 git/github.com/truemedian/zfetch
2blake3-98982125d0fbedc62e179e62081d2797a2b8a3623c42f9fd5d72cd56d6350714 git/github.com/truemedian/hzzp
3blake3-e6901bd7432450d5b22b01880cc7fa3fa2433e766a527206f18b29c67c1349bb git/github.com/alexnask/iguanaTLS
4blake3-21f91e48333ac0ca7f4704c96352831c25216e7056d02ce24de95d03fc942246 git/github.com/MasterQ32/zig-network
5blake3-030ebb03f1ed21122e681b06786bea6f2f1b810e8eb9f2029d0eee4f4fb3103f git/github.com/MasterQ32/zig-uri
6blake3-1893709ffc6359c5f9cd2f9409abccf78a94ed37bb2c6dd075c603356d17c94b git/github.com/nektro/zig-json