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