| ... | @@ -1,33 +1,34 @@ | ... | @@ -1,33 +1,34 @@ |
| 1 | //! Run this with `zig build gen` | 1 | //! Run this with `zig build gen` |
| 2 | | 2 | |
| 3 | const std = @import("std"); | 3 | const std = @import("std"); |
| 4 | const zfetch = @import("zfetch"); | | |
| 5 | const json = @import("json"); | 4 | const json = @import("json"); |
| 6 | | 5 | |
| 7 | pub fn main() !void { | 6 | pub 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(); |
| 10 | | 9 | |
| 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(); |
| 13 | | 12 | |
| 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(); |
| 17 | | 18 | |
| 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"); |
| 22 | | 23 | |
| 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); |
| 25 | | 26 | |
| 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 | } |
| 43 | | 44 | |
| 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"); |
| 52 | | 56 | |
| 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 | } |
| 67 | | 71 | |
| 68 | pub fn simple_fetch(alloc: *std.mem.Allocator, url: []const u8) !json.Value { | 72 | pub 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 | } |
| 77 | | 87 | |
| 78 | fn spdxlicenseLessThan(context: void, lhs: json.Value, rhs: json.Value) bool { | 88 | fn 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 | |
| | 95 | fn 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 | } |