zig/tools/update-license-headers.zig
Andrew Kelley d29871977f remove redundant license headers from zig standard library
We already have a LICENSE file that covers the Zig Standard Library. We
no longer need to remind everyone that the license is MIT in every single
file.

Previously this was introduced to clarify the situation for a fork of
Zig that made Zig's LICENSE file harder to find, and replaced it with
their own license that required annual payments to their company.
However that fork now appears to be dead. So there is no need to
reinforce the copyright notice in every single file.
2021-08-24 12:25:09 -07:00

48 lines
1.6 KiB
Zig

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{};
const root_node = try progress.start("", 0);
defer root_node.end();
var arena_allocator = std.heap.ArenaAllocator.init(std.heap.page_allocator);
const arena = &arena_allocator.allocator;
const args = try std.process.argsAlloc(arena);
const path_to_walk = args[1];
const dir = try std.fs.cwd().openDir(path_to_walk, .{ .iterate = true });
var walker = try dir.walk(arena);
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();
const source = try dir.readFileAlloc(arena, entry.path, 20 * 1024 * 1024);
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);
try dir.writeFile(entry.path, new_source);
}
}