authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-02-15 15:08:32 -08:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-02-15 15:08:32 -08:00
log2879b10fd69a71ecb33700ff0177f73848d43fe3
tree537b667c9c7faf204d9b284e6d0b52545cac025c
parentc960a0ca49fe83a32b078b02594df799da908341

add license command


4 files changed, 91 insertions(+), 2 deletions(-)

README.md+1
......@@ -102,6 +102,7 @@ dependencies:
102102| `c_include_dirs` | `[]string` | | A list of relative paths to directories with `.h` files |
103103| `c_source_flags` | `[]string` | | A list of clang flags to pass to each of the `.c` files in `c_source_files` |
104104| `c_source_files` | `[]string` | | A list of relative paths to `.c` files to compile along with project |
105| `license` | `string` | | A SPDX License Identifier specifying the license covering the code in this package. |
105106| `dependencies` | `[]Dep` | | An array of dependency objects |
106107
107108#### Dep object
deps.zig+6
......@@ -33,6 +33,7 @@ pub const _ids = .{
3333 "2ta738wrqbaqzl3iwzoo8nj35k9ynwz5p5iyz80ryrpp4ttf",
3434 "2b7mq571jmq31ktmpigopu29480iw245heueajgxzxn7ab8o",
3535 "csbnipaad8n77buaszsnjvlmn6j173fl7pkprsctelswjywe",
36 "0npcrzfdlrvkf44mzjo8bduj9gmqyefo0j3rstt6b0pm2r6r",
3637};
3738
3839pub const _paths = .{
......@@ -42,6 +43,7 @@ pub const _paths = .{
4243 "/v/git/github.com/ziglibs/known-folders/commit-f0f4188/",
4344 "/v/git/github.com/Vexu/zuri/commit-0f9cec8/",
4445 "/v/git/github.com/alexnask/iguanaTLS/commit-58f72f6/",
46 "/v/git/github.com/nektro/zig-licenses/commit-a15ef9b/",
4547};
4648
4749pub const package_data = struct {
......@@ -49,6 +51,7 @@ pub const package_data = struct {
4951 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{ } };
5052 pub const _2b7mq571jmq31ktmpigopu29480iw245heueajgxzxn7ab8o = build.Pkg{ .name = "zuri", .path = cache ++ "/v/git/github.com/Vexu/zuri/commit-0f9cec8/src/zuri.zig", .dependencies = &[_]build.Pkg{ } };
5153 pub const _csbnipaad8n77buaszsnjvlmn6j173fl7pkprsctelswjywe = build.Pkg{ .name = "iguanatls", .path = cache ++ "/v/git/github.com/alexnask/iguanaTLS/commit-58f72f6/src/main.zig", .dependencies = &[_]build.Pkg{ } };
54 pub const _0npcrzfdlrvkf44mzjo8bduj9gmqyefo0j3rstt6b0pm2r6r = build.Pkg{ .name = "licenses", .path = cache ++ "/v/git/github.com/nektro/zig-licenses/commit-a15ef9b/src/lib.zig", .dependencies = &[_]build.Pkg{ } };
5255};
5356
5457pub const packages = &[_]build.Pkg{
......@@ -56,6 +59,7 @@ pub const packages = &[_]build.Pkg{
5659 package_data._2ta738wrqbaqzl3iwzoo8nj35k9ynwz5p5iyz80ryrpp4ttf,
5760 package_data._2b7mq571jmq31ktmpigopu29480iw245heueajgxzxn7ab8o,
5861 package_data._csbnipaad8n77buaszsnjvlmn6j173fl7pkprsctelswjywe,
62 package_data._0npcrzfdlrvkf44mzjo8bduj9gmqyefo0j3rstt6b0pm2r6r,
5963};
6064
6165pub const pkgs = struct {
......@@ -63,6 +67,7 @@ pub const pkgs = struct {
6367 pub const known_folders = packages[2];
6468 pub const zuri = packages[3];
6569 pub const iguanatls = packages[4];
70 pub const licenses = packages[5];
6671};
6772
6873pub const c_include_dirs = &[_][]const u8{
......@@ -76,6 +81,7 @@ pub const c_source_flags = struct {
7681 pub const @"2ta738wrqbaqzl3iwzoo8nj35k9ynwz5p5iyz80ryrpp4ttf" = &[_][]const u8{};
7782 pub const @"2b7mq571jmq31ktmpigopu29480iw245heueajgxzxn7ab8o" = &[_][]const u8{};
7883 pub const @"csbnipaad8n77buaszsnjvlmn6j173fl7pkprsctelswjywe" = &[_][]const u8{};
84 pub const @"0npcrzfdlrvkf44mzjo8bduj9gmqyefo0j3rstt6b0pm2r6r" = &[_][]const u8{};
7985};
8086
8187pub const c_source_files = &[_][2][]const u8{
src/cmd/license.zig created+81
......@@ -0,0 +1,81 @@
1const std = @import("std");
2const gpa = std.heap.c_allocator;
3
4const u = @import("./../util/index.zig");
5const common = @import("./../common.zig");
6
7const ansi = @import("ansi");
8const style = ansi.style;
9
10const licenses = @import("licenses");
11
12const Module = u.Module;
13const List = std.ArrayList(u.Module);
14const Map = std.StringArrayHashMap(*List);
15
16// Inspired by:
17// https://github.com/onur/cargo-license
18
19pub fn execute(args: [][]u8) !void {
20 //
21 const dir = try std.fs.path.join(gpa, &[_][]const u8{".zigmod", "deps"});
22
23 const top_module = try common.collect_deps(dir, "zig.mod", .{
24 .log = false,
25 .update = false,
26 });
27
28 const master_list = &List.init(gpa);
29 try common.collect_pkgs(top_module, master_list);
30
31 const map = &Map.init(gpa);
32
33 const unspecified_list = &List.init(gpa);
34
35 for (master_list.items) |item| {
36 if (item.yaml == null) continue;
37 const license_code = item.yaml.?.get_string("license");
38 if (license_code.len == 0) {
39 try unspecified_list.append(item);
40 continue;
41 }
42 const map_item = try map.getOrPut(license_code);
43 if (!map_item.found_existing) {
44 const temp = try gpa.create(List);
45 temp.* = List.init(gpa);
46 map_item.entry.value = temp;
47 }
48 const tracking_list = map_item.entry.value;
49 try tracking_list.append(item);
50 }
51
52 var iter = map.iterator();
53 while (iter.next()) |entry| {
54 std.debug.print(style.Bold ++ "{s}:\n", .{entry.key});
55 if (get_license(entry.key)) |license| {
56 std.debug.print(style.Faint ++ "= {s}\n", .{license.url});
57 }
58 std.debug.print(style.ResetIntensity, .{});
59 for (entry.value.items) |item| {
60 std.debug.print("- {s}\n", .{if (item.clean_path.len > 0) item.clean_path else "This"});
61 }
62 std.debug.print("\n", .{});
63 }
64 {
65 std.debug.print(style.Bold ++ "Unspecified:\n", .{});
66 std.debug.print(style.ResetIntensity, .{});
67 for (unspecified_list.items) |item| {
68 std.debug.print("- {s}\n", .{if (item.clean_path.len > 0) item.clean_path else "This"});
69 }
70 }
71}
72
73fn get_license(name: []const u8) ?licenses.License {
74 const T = licenses.spdx;
75 inline for (std.meta.declarations(T)) |decl| {
76 if (std.mem.eql(u8, decl.name, name)) {
77 return @field(T, decl.name);
78 }
79 }
80 return null;
81}
zig.mod+3-2
......@@ -1,6 +1,7 @@
11id: 89ujp8gq842x6mzok8feypwze138n2d96zpugw44hcq7406r
22name: zigmod
33main: src/main.zig
4license: MIT
45dependencies:
56 - src: git https://github.com/yaml/libyaml
67 version: tag-0.2.5
......@@ -36,5 +37,5 @@ dependencies:
3637 - src: git https://github.com/alexnask/iguanaTLS
3738 version: commit-58f72f6
3839
39 # - src: git https://github.com/nektro/zig-licenses
40 # version: commit-a15ef9b
40 - src: git https://github.com/nektro/zig-licenses
41 version: commit-a15ef9b