From c8f50e0f0659d91a4bb8d6b22f5f8768490e6d65 Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Mon, 19 Jul 2021 06:45:48 -0700 Subject: [PATCH] add `generate.zig` --- build.zig | 18 +++++++++++++ generate.zig | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++ zig.mod | 5 +++- zigmod.lock | 7 ++++++ zigmod.sum | 6 +++++ 5 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 generate.zig create mode 100644 zigmod.lock create mode 100644 zigmod.sum diff --git a/build.zig b/build.zig index da571d971404e6d13064ed35ea593d15c75409ec..11d18262368447f65df3e81927ab709237fb8cba 100644 --- a/build.zig +++ b/build.zig @@ -1,4 +1,5 @@ const std = @import("std"); +const deps = @import("./deps.zig"); pub fn build(b: *std.build.Builder) void { // Standard target options allows the person running `zig build` to choose @@ -24,4 +25,21 @@ pub fn build(b: *std.build.Builder) void { const run_step = b.step("run", "Run the app"); run_step.dependOn(&run_cmd.step); + + // + + const exe2 = b.addExecutable("generate", "generate.zig"); + exe2.setTarget(target); + exe2.setBuildMode(mode); + deps.addAllTo(exe2); + exe2.install(); + + const run_cmd2 = exe2.run(); + run_cmd2.step.dependOn(b.getInstallStep()); + if (b.args) |args| { + run_cmd2.addArgs(args); + } + + const run_step2 = b.step("gen", "generate the list"); + run_step2.dependOn(&run_cmd2.step); } diff --git a/generate.zig b/generate.zig new file mode 100644 index 0000000000000000000000000000000000000000..31331dd29e17f41c7547436fb6ef241cf12a8ee9 --- /dev/null +++ b/generate.zig @@ -0,0 +1,71 @@ +//! Run this with `zig build gen` + +const std = @import("std"); +const zfetch = @import("zfetch"); +const json = @import("json"); + +pub fn main() !void { + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; + const alloc = &gpa.allocator; + + const f = try std.fs.cwd().createFile("src/lib.zig", .{}); + const w = f.writer(); + + { + std.log.info("spdx", .{}); + const val = try simple_fetch(alloc, "https://raw.githubusercontent.com/spdx/license-list-data/master/json/licenses.json"); + + try w.writeAll("// SPDX License Data generated from https://github.com/spdx/license-list-data\n"); + try w.writeAll("//\n"); + try w.print("// Last generated from version {s}\n", .{val.get("licenseListVersion").?.String}); + try w.writeAll("//\n"); + + try w.writeAll("\n"); + try w.writeAll( + \\pub const License = struct { + \\ isOsiApproved: bool, + \\ url: []const u8, + \\}; + \\ + ); + + var licenses = val.get("licenses").?.Array; + std.sort.sort(json.Value, licenses, {}, spdxlicenseLessThan); + + try w.writeAll("\n"); + try w.writeAll("pub const spdx = struct {\n"); + for (licenses) |lic| { + try w.print(" pub const @\"{s}\" = License{{.isOsiApproved = {}, .url = \"{s}\"}};\n", .{ + lic.get("licenseId").?.String, + lic.get("isOsiApproved").?.Bool, + lic.get("seeAlso").?.Array[0].String, + }); + } + try w.writeAll("};\n"); + + try w.writeAll("\n"); + try w.writeAll("pub const osi = &[_][]const u8{\n"); + for (licenses) |lic| { + if (!lic.get("isOsiApproved").?.Bool) continue; + try w.print(" \"{s}\",\n", .{lic.get("licenseId").?.String}); + } + try w.writeAll("};\n"); + } +} + +pub fn simple_fetch(alloc: *std.mem.Allocator, url: []const u8) !json.Value { + const req = try zfetch.Request.init(alloc, url, null); + defer req.deinit(); + try req.do(.GET, null, null); + const r = req.reader(); + const body_content = try r.readAllAlloc(alloc, std.math.maxInt(usize)); + const val = try json.parse(alloc, body_content); + return val; +} + +fn spdxlicenseLessThan(context: void, lhs: json.Value, rhs: json.Value) bool { + _ = context; + const l = lhs.get("licenseId").?.String; + const r = rhs.get("licenseId").?.String; + return std.mem.lessThan(u8, l, r); +} diff --git a/zig.mod b/zig.mod index 93eebaea604dc156291e34f94ce971c88f23bfa7..7e22ffea99b05198a680a701f0505fa44c995d5f 100644 --- a/zig.mod +++ b/zig.mod @@ -2,4 +2,7 @@ id: 0npcrzfdlrvkf44mzjo8bduj9gmqyefo0j3rstt6b0pm2r6r name: licenses main: src/lib.zig license: MIT -dependencies: + +dev_dependencies: + - src: git https://github.com/truemedian/zfetch + - src: git https://github.com/nektro/zig-json diff --git a/zigmod.lock b/zigmod.lock new file mode 100644 index 0000000000000000000000000000000000000000..123ab4ebd7e94bbd0abcdf067d24790fc842478b --- /dev/null +++ b/zigmod.lock @@ -0,0 +1,7 @@ +2 +git https://github.com/truemedian/zfetch commit-8bbc7b34cd417794841e1432585334bc969dfe83 +git https://github.com/truemedian/hzzp commit-2d30bddae3bf1eaecde5144490307604efe76f2a +git https://github.com/alexnask/iguanaTLS commit-0d39a361639ad5469f8e4dcdaea35446bbe54b48 +git https://github.com/MasterQ32/zig-network commit-b9c91769d8ebd626c8e45b2abb05cbc28ccc50da +git https://github.com/MasterQ32/zig-uri commit-52cdd2061bec0579519f0d30280597f3a1db8b75 +git https://github.com/nektro/zig-json commit-72e555fbc0776f2600aee19b01e5ab1855ebec7a diff --git a/zigmod.sum b/zigmod.sum new file mode 100644 index 0000000000000000000000000000000000000000..2f2804269b18fc1979c2c3b5e87dbeddfd414163 --- /dev/null +++ b/zigmod.sum @@ -0,0 +1,6 @@ +blake3-e3072f7fb47e86d53c9a1879e254ba1af55941153fd5f6752ec659b2f14854c9 git/github.com/truemedian/zfetch +blake3-98982125d0fbedc62e179e62081d2797a2b8a3623c42f9fd5d72cd56d6350714 git/github.com/truemedian/hzzp +blake3-e6901bd7432450d5b22b01880cc7fa3fa2433e766a527206f18b29c67c1349bb git/github.com/alexnask/iguanaTLS +blake3-21f91e48333ac0ca7f4704c96352831c25216e7056d02ce24de95d03fc942246 git/github.com/MasterQ32/zig-network +blake3-030ebb03f1ed21122e681b06786bea6f2f1b810e8eb9f2029d0eee4f4fb3103f git/github.com/MasterQ32/zig-uri +blake3-1893709ffc6359c5f9cd2f9409abccf78a94ed37bb2c6dd075c603356d17c94b git/github.com/nektro/zig-json -- 2.54.0