From 2879b10fd69a71ecb33700ff0177f73848d43fe3 Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Mon, 15 Feb 2021 15:08:32 -0800 Subject: [PATCH] add license command --- README.md | 1 + deps.zig | 6 ++++ src/cmd/license.zig | 81 +++++++++++++++++++++++++++++++++++++++++++++ zig.mod | 5 +-- 4 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 src/cmd/license.zig diff --git a/README.md b/README.md index 83311bafa14f2a7efef90a6bf0bcb4128412917f..d9b8be5cfafef92c8c5dc1250aeab0677b1017a6 100644 --- a/README.md +++ b/README.md @@ -102,6 +102,7 @@ dependencies: | `c_include_dirs` | `[]string` | | A list of relative paths to directories with `.h` files | | `c_source_flags` | `[]string` | | A list of clang flags to pass to each of the `.c` files in `c_source_files` | | `c_source_files` | `[]string` | | A list of relative paths to `.c` files to compile along with project | +| `license` | `string` | | A SPDX License Identifier specifying the license covering the code in this package. | | `dependencies` | `[]Dep` | | An array of dependency objects | #### Dep object diff --git a/deps.zig b/deps.zig index f573bbe5016842f6ce7df340225ef6cd2b77a45c..a1673e1165a8d77c3bb4d7555fe829e4d4223e8f 100644 --- a/deps.zig +++ b/deps.zig @@ -33,6 +33,7 @@ pub const _ids = .{ "2ta738wrqbaqzl3iwzoo8nj35k9ynwz5p5iyz80ryrpp4ttf", "2b7mq571jmq31ktmpigopu29480iw245heueajgxzxn7ab8o", "csbnipaad8n77buaszsnjvlmn6j173fl7pkprsctelswjywe", + "0npcrzfdlrvkf44mzjo8bduj9gmqyefo0j3rstt6b0pm2r6r", }; pub const _paths = .{ @@ -42,6 +43,7 @@ pub const _paths = .{ "/v/git/github.com/ziglibs/known-folders/commit-f0f4188/", "/v/git/github.com/Vexu/zuri/commit-0f9cec8/", "/v/git/github.com/alexnask/iguanaTLS/commit-58f72f6/", + "/v/git/github.com/nektro/zig-licenses/commit-a15ef9b/", }; pub const package_data = struct { @@ -49,6 +51,7 @@ pub const package_data = struct { pub const _2ta738wrqbaqzl3iwzoo8nj35k9ynwz5p5iyz80ryrpp4ttf = build.Pkg{ .name = "known-folders", .path = cache ++ "/v/git/github.com/ziglibs/known-folders/commit-f0f4188/known-folders.zig", .dependencies = &[_]build.Pkg{ } }; pub const _2b7mq571jmq31ktmpigopu29480iw245heueajgxzxn7ab8o = build.Pkg{ .name = "zuri", .path = cache ++ "/v/git/github.com/Vexu/zuri/commit-0f9cec8/src/zuri.zig", .dependencies = &[_]build.Pkg{ } }; pub const _csbnipaad8n77buaszsnjvlmn6j173fl7pkprsctelswjywe = build.Pkg{ .name = "iguanatls", .path = cache ++ "/v/git/github.com/alexnask/iguanaTLS/commit-58f72f6/src/main.zig", .dependencies = &[_]build.Pkg{ } }; + pub const _0npcrzfdlrvkf44mzjo8bduj9gmqyefo0j3rstt6b0pm2r6r = build.Pkg{ .name = "licenses", .path = cache ++ "/v/git/github.com/nektro/zig-licenses/commit-a15ef9b/src/lib.zig", .dependencies = &[_]build.Pkg{ } }; }; pub const packages = &[_]build.Pkg{ @@ -56,6 +59,7 @@ pub const packages = &[_]build.Pkg{ package_data._2ta738wrqbaqzl3iwzoo8nj35k9ynwz5p5iyz80ryrpp4ttf, package_data._2b7mq571jmq31ktmpigopu29480iw245heueajgxzxn7ab8o, package_data._csbnipaad8n77buaszsnjvlmn6j173fl7pkprsctelswjywe, + package_data._0npcrzfdlrvkf44mzjo8bduj9gmqyefo0j3rstt6b0pm2r6r, }; pub const pkgs = struct { @@ -63,6 +67,7 @@ pub const pkgs = struct { pub const known_folders = packages[2]; pub const zuri = packages[3]; pub const iguanatls = packages[4]; + pub const licenses = packages[5]; }; pub const c_include_dirs = &[_][]const u8{ @@ -76,6 +81,7 @@ pub const c_source_flags = struct { pub const @"2ta738wrqbaqzl3iwzoo8nj35k9ynwz5p5iyz80ryrpp4ttf" = &[_][]const u8{}; pub const @"2b7mq571jmq31ktmpigopu29480iw245heueajgxzxn7ab8o" = &[_][]const u8{}; pub const @"csbnipaad8n77buaszsnjvlmn6j173fl7pkprsctelswjywe" = &[_][]const u8{}; + pub const @"0npcrzfdlrvkf44mzjo8bduj9gmqyefo0j3rstt6b0pm2r6r" = &[_][]const u8{}; }; pub const c_source_files = &[_][2][]const u8{ diff --git a/src/cmd/license.zig b/src/cmd/license.zig new file mode 100644 index 0000000000000000000000000000000000000000..c8369ad9312e29d12df3c9a77990e09c68540e68 --- /dev/null +++ b/src/cmd/license.zig @@ -0,0 +1,81 @@ +const std = @import("std"); +const gpa = std.heap.c_allocator; + +const u = @import("./../util/index.zig"); +const common = @import("./../common.zig"); + +const ansi = @import("ansi"); +const style = ansi.style; + +const licenses = @import("licenses"); + +const Module = u.Module; +const List = std.ArrayList(u.Module); +const Map = std.StringArrayHashMap(*List); + +// Inspired by: +// https://github.com/onur/cargo-license + +pub fn execute(args: [][]u8) !void { + // + const dir = try std.fs.path.join(gpa, &[_][]const u8{".zigmod", "deps"}); + + const top_module = try common.collect_deps(dir, "zig.mod", .{ + .log = false, + .update = false, + }); + + const master_list = &List.init(gpa); + try common.collect_pkgs(top_module, master_list); + + const map = &Map.init(gpa); + + const unspecified_list = &List.init(gpa); + + for (master_list.items) |item| { + if (item.yaml == null) continue; + const license_code = item.yaml.?.get_string("license"); + if (license_code.len == 0) { + try unspecified_list.append(item); + continue; + } + const map_item = try map.getOrPut(license_code); + if (!map_item.found_existing) { + const temp = try gpa.create(List); + temp.* = List.init(gpa); + map_item.entry.value = temp; + } + const tracking_list = map_item.entry.value; + try tracking_list.append(item); + } + + var iter = map.iterator(); + while (iter.next()) |entry| { + std.debug.print(style.Bold ++ "{s}:\n", .{entry.key}); + if (get_license(entry.key)) |license| { + std.debug.print(style.Faint ++ "= {s}\n", .{license.url}); + } + std.debug.print(style.ResetIntensity, .{}); + for (entry.value.items) |item| { + std.debug.print("- {s}\n", .{if (item.clean_path.len > 0) item.clean_path else "This"}); + } + std.debug.print("\n", .{}); + } + { + std.debug.print(style.Bold ++ "Unspecified:\n", .{}); + std.debug.print(style.ResetIntensity, .{}); + for (unspecified_list.items) |item| { + std.debug.print("- {s}\n", .{if (item.clean_path.len > 0) item.clean_path else "This"}); + } + } +} + +fn get_license(name: []const u8) ?licenses.License { + const T = licenses.spdx; + inline for (std.meta.declarations(T)) |decl| { + if (std.mem.eql(u8, decl.name, name)) { + return @field(T, decl.name); + } + } + return null; +} diff --git a/zig.mod b/zig.mod index 38e947851c5754321c7143a5bc4c29924b9f071a..5c8ab79da5cdf2400fcf00e31f42c7fc1f59bf6b 100644 --- a/zig.mod +++ b/zig.mod @@ -1,6 +1,7 @@ id: 89ujp8gq842x6mzok8feypwze138n2d96zpugw44hcq7406r name: zigmod main: src/main.zig +license: MIT dependencies: - src: git https://github.com/yaml/libyaml version: tag-0.2.5 @@ -36,5 +37,5 @@ dependencies: - src: git https://github.com/alexnask/iguanaTLS version: commit-58f72f6 - # - src: git https://github.com/nektro/zig-licenses - # version: commit-a15ef9b + - src: git https://github.com/nektro/zig-licenses + version: commit-a15ef9b -- 2.54.0