| ... | @@ -1,47 +0,0 @@ |
| 1 | const std = @import("std"); |
| 2 | |
| 3 | /// This script replaces a matching license header from .zig source files in a directory tree |
| 4 | /// with the `new_header` below. |
| 5 | const new_header = ""; |
| 6 | |
| 7 | pub fn main() !void { |
| 8 | var progress = std.Progress{}; |
| 9 | const root_node = progress.start("", 0); |
| 10 | defer root_node.end(); |
| 11 | |
| 12 | var arena_allocator = std.heap.ArenaAllocator.init(std.heap.page_allocator); |
| 13 | const arena = arena_allocator.allocator(); |
| 14 | |
| 15 | const args = try std.process.argsAlloc(arena); |
| 16 | const path_to_walk = args[1]; |
| 17 | const dir = try std.fs.cwd().openDir(path_to_walk, .{ .iterate = true }); |
| 18 | |
| 19 | var walker = try dir.walk(arena); |
| 20 | defer walker.deinit(); |
| 21 | |
| 22 | var buffer: [500]u8 = undefined; |
| 23 | const expected_header = buffer[0..try std.io.getStdIn().readAll(&buffer)]; |
| 24 | |
| 25 | while (try walker.next()) |entry| { |
| 26 | if (!std.mem.endsWith(u8, entry.basename, ".zig")) |
| 27 | continue; |
| 28 | |
| 29 | var node = root_node.start(entry.basename, 0); |
| 30 | node.activate(); |
| 31 | defer node.end(); |
| 32 | |
| 33 | const source = try dir.readFileAlloc(arena, entry.path, 20 * 1024 * 1024); |
| 34 | if (!std.mem.startsWith(u8, source, expected_header)) { |
| 35 | std.debug.print("no match: {s}\n", .{entry.path}); |
| 36 | continue; |
| 37 | } |
| 38 | |
| 39 | const truncated_source = source[expected_header.len..]; |
| 40 | |
| 41 | const new_source = try arena.alloc(u8, truncated_source.len + new_header.len); |
| 42 | @memcpy(new_source[0..new_source.len], new_header); |
| 43 | @memcpy(new_source[new_header.len .. new_header.len + truncated_source.len], truncated_source); |
| 44 | |
| 45 | try dir.writeFile(entry.path, new_source); |
| 46 | } |
| 47 | } |