| ... | ... | @@ -1,7 +1,6 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | 2 | pub const licenses = @import("licenses-text"); |
| 3 | 3 | const leven = @import("leven"); |
| 4 | | const fscheck = @import("fs-check"); |
| 5 | 4 | |
| 6 | 5 | pub fn detect(alloc: std.mem.Allocator, license_src: []const u8) ![]const u8 { |
| 7 | 6 | var min: ?usize = null; |
| ... | ... | @@ -20,21 +19,19 @@ pub fn detect(alloc: std.mem.Allocator, license_src: []const u8) ![]const u8 { |
| 20 | 19 | } |
| 21 | 20 | |
| 22 | 21 | 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; |
| 22 | const b = (try testLicenseFile(alloc, dir, "LICENSE")) orelse |
| 23 | (try testLicenseFile(alloc, dir, "LICENSE.md")) orelse |
| 24 | return null; |
| 26 | 25 | |
| 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 | 26 | defer alloc.free(b); |
| 32 | 27 | return try detect(alloc, b); |
| 33 | 28 | } |
| 34 | 29 | |
| 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; |
| 30 | pub fn testLicenseFile(alloc: std.mem.Allocator, dir: std.fs.Dir, name: []const u8) !?[]const u8 { |
| 31 | const file = dir.openFile(name, .{}) catch |err| switch (err) { |
| 32 | error.FileNotFound => return null, |
| 33 | else => |e| return e, |
| 34 | }; |
| 35 | defer file.close(); |
| 36 | return try file.reader().readAllAlloc(alloc, 1024 * 1024); |
| 40 | 37 | } |