authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2024-12-28 19:27:55 -08:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2024-12-28 19:27:55 -08:00
log3c8d7f0f22d2cc214849c1d2b4dd7d6a6e1b1bbd
treed4ffb9555740a6fa1092b02c18f3f3fd41a926ae
parenta6682626e50219f04571cb2d9af8d77bf2fa97ca

update to zig 0.13


5 files changed, 74 insertions(+), 49 deletions(-)

.gitignore+1-1
...@@ -1,6 +1,6 @@...@@ -1,6 +1,6 @@
1zig-cache
2.zig-cache1.zig-cache
3zig-out2zig-out
4deps.zig3deps.zig
5.zigmod4.zigmod
5files.zig
6zigmod.lock6zigmod.lock
build.zig+21-21
...@@ -1,23 +1,20 @@...@@ -1,23 +1,20 @@
1const std = @import("std");1const std = @import("std");
2const deps = @import("./deps.zig");2const deps = @import("./deps.zig");
33
4pub fn build(b: *std.build.Builder) void {4pub fn build(b: *std.Build) void {
5 // Standard target options allows the person running `zig build` to choose
6 // what target to build for. Here we do not override the defaults, which
7 // means any target is allowed, and the default is native. Other options
8 // for restricting supported target set are available.
9 const target = b.standardTargetOptions(.{});5 const target = b.standardTargetOptions(.{});
106 const mode = b.option(std.builtin.Mode, "mode", "") orelse .Debug;
11 // Standard release options allow the person running `zig build` to select7
12 // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.8 const exe = b.addExecutable(.{
13 const mode = b.standardReleaseOptions();9 .name = "zig-licenses",
1410 .root_source_file = b.path("src/main.zig"),
15 const exe = b.addExecutable("zig-licenses", "src/main.zig");11 .target = target,
16 exe.setTarget(target);12 .optimize = mode,
17 exe.setBuildMode(mode);13 });
18 exe.install();14 deps.addAllTo(exe);
1915 b.installArtifact(exe);
20 const run_cmd = exe.run();16
17 const run_cmd = b.addRunArtifact(exe);
21 run_cmd.step.dependOn(b.getInstallStep());18 run_cmd.step.dependOn(b.getInstallStep());
22 if (b.args) |args| {19 if (b.args) |args| {
23 run_cmd.addArgs(args);20 run_cmd.addArgs(args);
...@@ -28,13 +25,16 @@ pub fn build(b: *std.build.Builder) void {...@@ -28,13 +25,16 @@ pub fn build(b: *std.build.Builder) void {
2825
29 //26 //
3027
31 const exe2 = b.addExecutable("generate", "generate.zig");28 const exe2 = b.addExecutable(.{
32 exe2.setTarget(target);29 .name = "generate",
33 exe2.setBuildMode(mode);30 .root_source_file = b.path("generate.zig"),
31 .target = target,
32 .optimize = mode,
33 });
34 deps.addAllTo(exe2);34 deps.addAllTo(exe2);
35 exe2.install();35 b.installArtifact(exe2);
3636
37 const run_cmd2 = exe2.run();37 const run_cmd2 = b.addRunArtifact(exe2);
38 run_cmd2.step.dependOn(b.getInstallStep());38 run_cmd2.step.dependOn(b.getInstallStep());
39 if (b.args) |args| {39 if (b.args) |args| {
40 run_cmd2.addArgs(args);40 run_cmd2.addArgs(args);
generate.zig+42-26
...@@ -1,33 +1,34 @@...@@ -1,33 +1,34 @@
1//! Run this with `zig build gen`1//! Run this with `zig build gen`
22
3const std = @import("std");3const std = @import("std");
4const zfetch = @import("zfetch");
5const json = @import("json");4const json = @import("json");
65
7pub fn main() !void {6pub fn main() !void {
8 var gpa = std.heap.GeneralPurposeAllocator(.{}){};7 var gpa = std.heap.GeneralPurposeAllocator(.{}){};
9 const alloc = &gpa.allocator;8 const alloc = gpa.allocator();
109
11 const f = try std.fs.cwd().createFile("src/lib.zig", .{});10 const f = try std.fs.cwd().createFile("src/lib.zig", .{});
12 const w = f.writer();11 const w = f.writer();
1312
14 {13 {
15 std.log.info("spdx", .{});14 std.log.info("spdx", .{});
16 const val = try simple_fetch(alloc, "https://raw.githubusercontent.com/spdx/license-list-data/master/json/licenses.json");15 const doc = try simple_fetch(alloc, "https://raw.githubusercontent.com/spdx/license-list-data/master/json/licenses.json");
16 defer doc.deinit(alloc);
17 const val = doc.root.object();
1718
18 try w.writeAll("// SPDX License Data generated from https://github.com/spdx/license-list-data\n");19 try w.writeAll("// SPDX License Data generated from https://github.com/spdx/license-list-data\n");
19 try w.writeAll("//\n");20 try w.writeAll("//\n");
20 try w.print("// Last generated from version {s}\n", .{val.get("licenseListVersion").?.String});21 try w.print("// Last generated from version {s}\n", .{val.getS("licenseListVersion").?});
21 try w.writeAll("//\n");22 try w.writeAll("//\n");
2223
23 var licenses = val.get("licenses").?.Array;24 const licenses = try mutdupe(alloc, json.ValueIndex, val.getA("licenses").?);
24 std.sort.sort(json.Value, licenses, {}, spdxlicenseLessThan);25 std.mem.sort(json.ValueIndex, licenses, {}, spdxlicenseLessThan);
2526
26 try w.writeAll("\n");27 try w.writeAll("\n");
27 try w.writeAll("pub const spdx = &[_][]const u8{\n");28 try w.writeAll("pub const spdx = &[_][]const u8{\n");
28 for (licenses) |lic| {29 for (licenses) |lic| {
29 try w.print(" \"{s}\",\n", .{30 try w.print(" \"{s}\",\n", .{
30 lic.get("licenseId").?.String,31 lic.object().getS("licenseId").?,
31 });32 });
32 }33 }
33 try w.writeAll("};\n");34 try w.writeAll("};\n");
...@@ -35,28 +36,31 @@ pub fn main() !void {...@@ -35,28 +36,31 @@ pub fn main() !void {
35 try w.writeAll("\n");36 try w.writeAll("\n");
36 try w.writeAll("pub const osi = &[_][]const u8{\n");37 try w.writeAll("pub const osi = &[_][]const u8{\n");
37 for (licenses) |lic| {38 for (licenses) |lic| {
38 if (!lic.get("isOsiApproved").?.Bool) continue;39 if (!lic.object().getB("isOsiApproved").?) continue;
39 try w.print(" \"{s}\",\n", .{lic.get("licenseId").?.String});40 try w.print(" \"{s}\",\n", .{lic.object().getS("licenseId").?});
40 }41 }
41 try w.writeAll("};\n");42 try w.writeAll("};\n");
42 }43 }
4344
44 {45 {
45 std.log.info("blueoak", .{});46 std.log.info("blueoak", .{});
46 const val = try simple_fetch(alloc, "https://blueoakcouncil.org/list.json");47 const doc = try simple_fetch(alloc, "https://blueoakcouncil.org/list.json");
48 defer doc.deinit(alloc);
49 const val = doc.root.object();
50
47 try w.writeAll("\n");51 try w.writeAll("\n");
48 try w.writeAll("// Blue Oak Council data generated from https://blueoakcouncil.org/list\n");52 try w.writeAll("// Blue Oak Council data generated from https://blueoakcouncil.org/list\n");
49 try w.writeAll("//\n");53 try w.writeAll("//\n");
50 try w.print("// Last generated from version {s}\n", .{val.get("version").?.String});54 try w.print("// Last generated from version {s}\n", .{val.getS("version").?});
51 try w.writeAll("//\n");55 try w.writeAll("//\n");
5256
53 try w.writeAll("\n");57 try w.writeAll("\n");
54 try w.writeAll("pub const blueoak = struct {\n");58 try w.writeAll("pub const blueoak = struct {\n");
55 for (val.get("ratings").?.Array) |rating| {59 for (val.getA("ratings").?) |rating| {
56 try w.print(" pub const {s} = &[_][]const u8{{\n", .{std.ascii.allocLowerString(alloc, rating.get("name").?.String)});60 try w.print(" pub const {s} = &[_][]const u8{{\n", .{try std.ascii.allocLowerString(alloc, rating.object().getS("name").?)});
57 for (rating.get("licenses").?.Array) |lic| {61 for (rating.object().getA("licenses").?) |lic| {
58 try w.print(" \"{s}\",\n", .{62 try w.print(" \"{s}\",\n", .{
59 lic.get("id").?.String,63 lic.object().getS("id").?,
60 });64 });
61 }65 }
62 try w.print(" }};\n", .{});66 try w.print(" }};\n", .{});
...@@ -65,19 +69,31 @@ pub fn main() !void {...@@ -65,19 +69,31 @@ pub fn main() !void {
65 }69 }
66}70}
6771
68pub fn simple_fetch(alloc: *std.mem.Allocator, url: []const u8) !json.Value {72pub fn simple_fetch(alloc: std.mem.Allocator, url: []const u8) !json.Document {
69 const req = try zfetch.Request.init(alloc, url, null);73 var client = std.http.Client{ .allocator = alloc };
70 defer req.deinit();74 defer client.deinit();
71 try req.do(.GET, null, null);75
72 const r = req.reader();76 var list = std.ArrayList(u8).init(alloc);
73 const body_content = try r.readAllAlloc(alloc, std.math.maxInt(usize));77 defer list.deinit();
74 const val = try json.parse(alloc, body_content);78
75 return val;79 const fetch = try client.fetch(.{
80 .location = .{ .url = url },
81 .response_storage = .{ .dynamic = &list },
82 });
83 const opts = json.Parser.Options{ .maximum_depth = 100, .support_trailing_commas = true };
84 if (fetch.status != .ok) return try json.parseFromSlice(alloc, url, "{}", opts);
85 return try json.parseFromSlice(alloc, url, list.items, opts);
76}86}
7787
78fn spdxlicenseLessThan(context: void, lhs: json.Value, rhs: json.Value) bool {88fn spdxlicenseLessThan(context: void, lhs: json.ValueIndex, rhs: json.ValueIndex) bool {
79 _ = context;89 _ = context;
80 const l = lhs.get("licenseId").?.String;90 const l = lhs.object().getS("licenseId").?;
81 const r = rhs.get("licenseId").?.String;91 const r = rhs.object().getS("licenseId").?;
82 return std.mem.lessThan(u8, l, r);92 return std.mem.lessThan(u8, l, r);
83}93}
94
95fn mutdupe(alloc: std.mem.Allocator, comptime T: type, original: anytype) ![]T {
96 const slice = try alloc.alloc(T, original.len);
97 for (original, 0..) |item, i| slice[i] = item;
98 return slice;
99}
licenses.txt created+10
...@@ -0,0 +1,10 @@
1MIT:
2= https://spdx.org/licenses/MIT
3- This
4- git https://github.com/nektro/zig-extras
5- git https://github.com/nektro/zig-json
6- git https://github.com/nektro/zig-tracer
7
8MPL-2.0:
9= https://spdx.org/licenses/MPL-2.0
10- git https://github.com/nektro/zig-intrusive-parser
zig.mod-1
...@@ -4,5 +4,4 @@ main: src/lib.zig...@@ -4,5 +4,4 @@ main: src/lib.zig
4license: MIT4license: MIT
55
6dev_dependencies:6dev_dependencies:
7 - src: git https://github.com/truemedian/zfetch
8 - src: git https://github.com/nektro/zig-json7 - src: git https://github.com/nektro/zig-json