2021-08-24 20:25:09 +01:00
|
|
|
const std = @import("std");
|
|
|
|
|
|
|
|
/// This script replaces a matching license header from .zig source files in a directory tree
|
|
|
|
/// with the `new_header` below.
|
|
|
|
const new_header = "";
|
|
|
|
|
|
|
|
pub fn main() !void {
|
|
|
|
var progress = std.Progress{};
|
2022-02-09 00:49:40 +00:00
|
|
|
const root_node = progress.start("", 0);
|
2021-08-24 20:25:09 +01:00
|
|
|
defer root_node.end();
|
|
|
|
|
|
|
|
var arena_allocator = std.heap.ArenaAllocator.init(std.heap.page_allocator);
|
2021-10-29 02:08:41 +01:00
|
|
|
const arena = arena_allocator.allocator();
|
2021-08-24 20:25:09 +01:00
|
|
|
|
|
|
|
const args = try std.process.argsAlloc(arena);
|
|
|
|
const path_to_walk = args[1];
|
2022-07-09 16:59:21 +01:00
|
|
|
const iterable_dir = try std.fs.cwd().openIterableDir(path_to_walk, .{});
|
2021-08-24 20:25:09 +01:00
|
|
|
|
2022-07-09 16:59:21 +01:00
|
|
|
var walker = try iterable_dir.walk(arena);
|
2021-08-24 20:25:09 +01:00
|
|
|
defer walker.deinit();
|
|
|
|
|
|
|
|
var buffer: [500]u8 = undefined;
|
|
|
|
const expected_header = buffer[0..try std.io.getStdIn().readAll(&buffer)];
|
|
|
|
|
|
|
|
while (try walker.next()) |entry| {
|
|
|
|
if (!std.mem.endsWith(u8, entry.basename, ".zig"))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
var node = root_node.start(entry.basename, 0);
|
|
|
|
node.activate();
|
|
|
|
defer node.end();
|
|
|
|
|
2022-07-09 16:59:21 +01:00
|
|
|
const source = try iterable_dir.dir.readFileAlloc(arena, entry.path, 20 * 1024 * 1024);
|
2021-08-24 20:25:09 +01:00
|
|
|
if (!std.mem.startsWith(u8, source, expected_header)) {
|
|
|
|
std.debug.print("no match: {s}\n", .{entry.path});
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const truncated_source = source[expected_header.len..];
|
|
|
|
|
|
|
|
const new_source = try arena.alloc(u8, truncated_source.len + new_header.len);
|
|
|
|
std.mem.copy(u8, new_source, new_header);
|
|
|
|
std.mem.copy(u8, new_source[new_header.len..], truncated_source);
|
|
|
|
|
2022-07-09 16:59:21 +01:00
|
|
|
try iterable_dir.dir.writeFile(entry.path, new_source);
|
2021-08-24 20:25:09 +01:00
|
|
|
}
|
|
|
|
}
|