| ... | ... | @@ -1,33 +1,34 @@ |
| 1 | 1 | //! Run this with `zig build gen` |
| 2 | 2 | |
| 3 | 3 | const std = @import("std"); |
| 4 | | const zfetch = @import("zfetch"); |
| 5 | 4 | const json = @import("json"); |
| 6 | 5 | |
| 7 | 6 | pub fn main() !void { |
| 8 | 7 | var gpa = std.heap.GeneralPurposeAllocator(.{}){}; |
| 9 | | const alloc = &gpa.allocator; |
| 8 | const alloc = gpa.allocator(); |
| 10 | 9 | |
| 11 | 10 | const f = try std.fs.cwd().createFile("src/lib.zig", .{}); |
| 12 | 11 | const w = f.writer(); |
| 13 | 12 | |
| 14 | 13 | { |
| 15 | 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 | 19 | try w.writeAll("// SPDX License Data generated from https://github.com/spdx/license-list-data\n"); |
| 19 | 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 | 22 | try w.writeAll("//\n"); |
| 22 | 23 | |
| 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); |
| 25 | 26 | |
| 26 | 27 | try w.writeAll("\n"); |
| 27 | 28 | try w.writeAll("pub const spdx = &[_][]const u8{\n"); |
| 28 | 29 | for (licenses) |lic| { |
| 29 | 30 | try w.print(" \"{s}\",\n", .{ |
| 30 | | lic.get("licenseId").?.String, |
| 31 | lic.object().getS("licenseId").?, |
| 31 | 32 | }); |
| 32 | 33 | } |
| 33 | 34 | try w.writeAll("};\n"); |
| ... | ... | @@ -35,28 +36,31 @@ pub fn main() !void { |
| 35 | 36 | try w.writeAll("\n"); |
| 36 | 37 | try w.writeAll("pub const osi = &[_][]const u8{\n"); |
| 37 | 38 | 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").?}); |
| 40 | 41 | } |
| 41 | 42 | try w.writeAll("};\n"); |
| 42 | 43 | } |
| 43 | 44 | |
| 44 | 45 | { |
| 45 | 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 | 51 | try w.writeAll("\n"); |
| 48 | 52 | try w.writeAll("// Blue Oak Council data generated from https://blueoakcouncil.org/list\n"); |
| 49 | 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 | 55 | try w.writeAll("//\n"); |
| 52 | 56 | |
| 53 | 57 | try w.writeAll("\n"); |
| 54 | 58 | 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| { |
| 58 | 62 | try w.print(" \"{s}\",\n", .{ |
| 59 | | lic.get("id").?.String, |
| 63 | lic.object().getS("id").?, |
| 60 | 64 | }); |
| 61 | 65 | } |
| 62 | 66 | try w.print(" }};\n", .{}); |
| ... | ... | @@ -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 { |
| 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; |
| 72 | pub 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); |
| 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 | 89 | _ = 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").?; |
| 82 | 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 | } |