| author | |
| committer | |
| log | 6931e3e3e4ade3a526c662dffc1710fe1863174a |
| tree | e5c489e93622e9d19862912ecac49905caab6414 |
| parent | 52396d84739259235cde5e2c35780a07cfac960c |
7 files changed, 98 insertions(+), 0 deletions(-)
.gitignore created+3| ... | ... | @@ -0,0 +1,3 @@ |
| 1 | /zig-* | |
| 2 | /.zigmod | |
| 3 | /deps.zig |
build.zig created+23| ... | ... | @@ -0,0 +1,23 @@ |
| 1 | const std = @import("std"); | |
| 2 | const deps = @import("./deps.zig"); | |
| 3 | ||
| 4 | pub fn build(b: *std.build.Builder) void { | |
| 5 | const target = b.standardTargetOptions(.{}); | |
| 6 | ||
| 7 | const mode = b.standardReleaseOptions(); | |
| 8 | ||
| 9 | const exe = b.addExecutable("zig-license-detect", "src/main.zig"); | |
| 10 | exe.setTarget(target); | |
| 11 | exe.setBuildMode(mode); | |
| 12 | deps.addAllTo(exe); | |
| 13 | exe.install(); | |
| 14 | ||
| 15 | const run_cmd = exe.run(); | |
| 16 | run_cmd.step.dependOn(b.getInstallStep()); | |
| 17 | if (b.args) |args| { | |
| 18 | run_cmd.addArgs(args); | |
| 19 | } | |
| 20 | ||
| 21 | const run_step = b.step("run", "Run the app"); | |
| 22 | run_step.dependOn(&run_cmd.step); | |
| 23 | } |
src/lib.zig created+40| ... | ... | @@ -0,0 +1,40 @@ |
| 1 | const std = @import("std"); | |
| 2 | const licenses = @import("licenses-text"); | |
| 3 | const leven = @import("leven"); | |
| 4 | const fscheck = @import("fs-check"); | |
| 5 | ||
| 6 | pub fn detect(alloc: *std.mem.Allocator, license_src: []const u8) ![]const u8 { | |
| 7 | var min: ?usize = null; | |
| 8 | var ind: ?usize = null; | |
| 9 | ||
| 10 | for (licenses.spdx) |item, i| { | |
| 11 | const distance = try leven.leven(u8, alloc, license_src, item[1], min); | |
| 12 | ||
| 13 | if (min == null or distance < min.?) { | |
| 14 | min = distance; | |
| 15 | ind = i; | |
| 16 | } | |
| 17 | } | |
| 18 | ||
| 19 | return licenses.spdx[ind.?][0]; | |
| 20 | } | |
| 21 | ||
| 22 | pub fn detectInDir(alloc: *std.mem.Allocator, dir: std.fs.Dir) !?[]const u8 { | |
| 23 | const lic_filename = (try testLicenseFile(dir, "LICENSE")) orelse | |
| 24 | (try testLicenseFile(dir, "LICENSE.md")) orelse | |
| 25 | null; | |
| 26 | ||
| 27 | if (lic_filename == null) return null; | |
| 28 | const f = try dir.openFile(lic_filename.?, .{}); | |
| 29 | defer f.close(); | |
| 30 | const b = try f.reader().readAllAlloc(alloc, 1024 * 1024); | |
| 31 | defer alloc.free(b); | |
| 32 | return try detect(alloc, b); | |
| 33 | } | |
| 34 | ||
| 35 | // | |
| 36 | // | |
| 37 | ||
| 38 | pub fn testLicenseFile(dir: std.fs.Dir, name: []const u8) !?[]const u8 { | |
| 39 | return if (try fscheck.doesFileExist(dir, name)) name else null; | |
| 40 | } |
src/main.zig created+14| ... | ... | @@ -0,0 +1,14 @@ |
| 1 | const std = @import("std"); | |
| 2 | ||
| 3 | const detectlicense = @import("detect-license"); | |
| 4 | ||
| 5 | pub fn main() !void { | |
| 6 | std.log.info("All your codebase are belong to us.", .{}); | |
| 7 | ||
| 8 | var gpa = std.heap.GeneralPurposeAllocator(.{}){}; | |
| 9 | const alloc = &gpa.allocator; | |
| 10 | ||
| 11 | const license = try detectlicense.detectInDir(alloc, std.fs.cwd()); | |
| 12 | std.debug.assert(std.mem.eql(u8, license.?, "MIT")); | |
| 13 | std.log.info("OK", .{}); | |
| 14 | } |
zig.mod created+9| ... | ... | @@ -0,0 +1,9 @@ |
| 1 | id: 2ovav391ivakpt5uh706c38jy7wddi6tqo5rmbdwig9ngkh7 | |
| 2 | name: detect-license | |
| 3 | main: src/lib.zig | |
| 4 | license: MIT | |
| 5 | description: Given an input text guess which SPDX license it most likely is an instance of | |
| 6 | dependencies: | |
| 7 | - src: git https://github.com/nektro/zig-licenses-text | |
| 8 | - src: git https://github.com/nektro/zig-leven | |
| 9 | - src: git https://github.com/nektro/zig-fs-check |
zigmod.lock created+5| ... | ... | @@ -0,0 +1,5 @@ |
| 1 | 2 | |
| 2 | git https://github.com/nektro/zig-licenses-text commit-3c07c6e4eb0965dafd0b029c632f823631b3169c | |
| 3 | git https://github.com/nektro/zig-leven commit-8e9f827117ab1418578b692a2325754cc3ee692d | |
| 4 | git https://github.com/nektro/zig-range commit-890ca308fe09b3d5c866d5cfb3b3d7a95dbf939f | |
| 5 | git https://github.com/nektro/zig-fs-check commit-dcd8da90fcb8bf3c9887e713bf0ec072bc897dd5 |
zigmod.sum created+4| ... | ... | @@ -0,0 +1,4 @@ |
| 1 | blake3-20ceb9e27bdb93540e93006628fb94e0540f3f69a2304a8f73ad2989b8e27226 git/github.com/nektro/zig-licenses-text | |
| 2 | blake3-cf68eaad66254b89c8bc18578f58607aa2e87ae59dd5696c2394eb5a0a31c455 git/github.com/nektro/zig-leven | |
| 3 | blake3-09698753782139ab4877d08f33235170836f68b73e482b65cdee5637a6addf86 git/github.com/nektro/zig-range | |
| 4 | blake3-10460da714f5c8436d09c268795597b0b2686ffc1e789c9f50366c2cbb0e1ff3 git/github.com/nektro/zig-fs-check |