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 @@
1zig-cache
21.zig-cache
32zig-out
43deps.zig
54.zigmod
5files.zig
66zigmod.lock
build.zig+21-21
......@@ -1,23 +1,20 @@
11const std = @import("std");
22const deps = @import("./deps.zig");
33
4pub fn build(b: *std.build.Builder) 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.
4pub fn build(b: *std.Build) void {
95 const target = b.standardTargetOptions(.{});
10
11 // Standard release options allow the person running `zig build` to select
12 // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
13 const mode = b.standardReleaseOptions();
14
15 const exe = b.addExecutable("zig-licenses", "src/main.zig");
16 exe.setTarget(target);
17 exe.setBuildMode(mode);
18 exe.install();
19
20 const run_cmd = exe.run();
6 const mode = b.option(std.builtin.Mode, "mode", "") orelse .Debug;
7
8 const exe = b.addExecutable(.{
9 .name = "zig-licenses",
10 .root_source_file = b.path("src/main.zig"),
11 .target = target,
12 .optimize = mode,
13 });
14 deps.addAllTo(exe);
15 b.installArtifact(exe);
16
17 const run_cmd = b.addRunArtifact(exe);
2118 run_cmd.step.dependOn(b.getInstallStep());
2219 if (b.args) |args| {
2320 run_cmd.addArgs(args);
......@@ -28,13 +25,16 @@ pub fn build(b: *std.build.Builder) void {
2825
2926 //
3027
31 const exe2 = b.addExecutable("generate", "generate.zig");
32 exe2.setTarget(target);
33 exe2.setBuildMode(mode);
28 const exe2 = b.addExecutable(.{
29 .name = "generate",
30 .root_source_file = b.path("generate.zig"),
31 .target = target,
32 .optimize = mode,
33 });
3434 deps.addAllTo(exe2);
35 exe2.install();
35 b.installArtifact(exe2);
3636
37 const run_cmd2 = exe2.run();
37 const run_cmd2 = b.addRunArtifact(exe2);
3838 run_cmd2.step.dependOn(b.getInstallStep());
3939 if (b.args) |args| {
4040 run_cmd2.addArgs(args);
generate.zig+42-26
......@@ -1,33 +1,34 @@
11//! Run this with `zig build gen`
22
33const std = @import("std");
4const zfetch = @import("zfetch");
54const json = @import("json");
65
76pub fn main() !void {
87 var gpa = std.heap.GeneralPurposeAllocator(.{}){};
9 const alloc = &gpa.allocator;
8 const alloc = gpa.allocator();
109
1110 const f = try std.fs.cwd().createFile("src/lib.zig", .{});
1211 const w = f.writer();
1312
1413 {
1514 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
1819 try w.writeAll("// SPDX License Data generated from https://github.com/spdx/license-list-data\n");
1920 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").?});
2122 try w.writeAll("//\n");
2223
23 var licenses = val.get("licenses").?.Array;
24 std.sort.sort(json.Value, licenses, {}, spdxlicenseLessThan);
24 const licenses = try mutdupe(alloc, json.ValueIndex, val.getA("licenses").?);
25 std.mem.sort(json.ValueIndex, licenses, {}, spdxlicenseLessThan);
2526
2627 try w.writeAll("\n");
2728 try w.writeAll("pub const spdx = &[_][]const u8{\n");
2829 for (licenses) |lic| {
2930 try w.print(" \"{s}\",\n", .{
30 lic.get("licenseId").?.String,
31 lic.object().getS("licenseId").?,
3132 });
3233 }
3334 try w.writeAll("};\n");
......@@ -35,28 +36,31 @@ pub fn main() !void {
3536 try w.writeAll("\n");
3637 try w.writeAll("pub const osi = &[_][]const u8{\n");
3738 for (licenses) |lic| {
38 if (!lic.get("isOsiApproved").?.Bool) continue;
39 try w.print(" \"{s}\",\n", .{lic.get("licenseId").?.String});
39 if (!lic.object().getB("isOsiApproved").?) continue;
40 try w.print(" \"{s}\",\n", .{lic.object().getS("licenseId").?});
4041 }
4142 try w.writeAll("};\n");
4243 }
4344
4445 {
4546 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
4751 try w.writeAll("\n");
4852 try w.writeAll("// Blue Oak Council data generated from https://blueoakcouncil.org/list\n");
4953 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").?});
5155 try w.writeAll("//\n");
5256
5357 try w.writeAll("\n");
5458 try w.writeAll("pub const blueoak = struct {\n");
55 for (val.get("ratings").?.Array) |rating| {
56 try w.print(" pub const {s} = &[_][]const u8{{\n", .{std.ascii.allocLowerString(alloc, rating.get("name").?.String)});
57 for (rating.get("licenses").?.Array) |lic| {
59 for (val.getA("ratings").?) |rating| {
60 try w.print(" pub const {s} = &[_][]const u8{{\n", .{try std.ascii.allocLowerString(alloc, rating.object().getS("name").?)});
61 for (rating.object().getA("licenses").?) |lic| {
5862 try w.print(" \"{s}\",\n", .{
59 lic.get("id").?.String,
63 lic.object().getS("id").?,
6064 });
6165 }
6266 try w.print(" }};\n", .{});
......@@ -65,19 +69,31 @@ pub fn main() !void {
6569 }
6670}
6771
68pub fn simple_fetch(alloc: *std.mem.Allocator, url: []const u8) !json.Value {
69 const req = try zfetch.Request.init(alloc, url, null);
70 defer req.deinit();
71 try req.do(.GET, null, null);
72 const r = req.reader();
73 const body_content = try r.readAllAlloc(alloc, std.math.maxInt(usize));
74 const val = try json.parse(alloc, body_content);
75 return val;
72pub fn simple_fetch(alloc: std.mem.Allocator, url: []const u8) !json.Document {
73 var client = std.http.Client{ .allocator = alloc };
74 defer client.deinit();
75
76 var list = std.ArrayList(u8).init(alloc);
77 defer list.deinit();
78
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);
7686}
7787
78fn spdxlicenseLessThan(context: void, lhs: json.Value, rhs: json.Value) bool {
88fn spdxlicenseLessThan(context: void, lhs: json.ValueIndex, rhs: json.ValueIndex) bool {
7989 _ = context;
80 const l = lhs.get("licenseId").?.String;
81 const r = rhs.get("licenseId").?.String;
90 const l = lhs.object().getS("licenseId").?;
91 const r = rhs.object().getS("licenseId").?;
8292 return std.mem.lessThan(u8, l, r);
8393}
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
44license: MIT
55
66dev_dependencies:
7 - src: git https://github.com/truemedian/zfetch
87 - src: git https://github.com/nektro/zig-json