Created
August 15, 2023 07:04
-
-
Save tauoverpi/b7a489e13c1c05a1ade49839c5b85c94 to your computer and use it in GitHub Desktop.
fix all imports
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const std = @import("std"); | |
const mem = std.mem; | |
const assert = std.debug.assert; | |
const Ast = std.zig.Ast; | |
pub fn main() !void { | |
var instance = std.heap.GeneralPurposeAllocator(.{}){}; | |
defer assert(instance.deinit() == .ok); | |
const gpa = instance.allocator(); | |
var here = try std.fs.cwd().openIterableDir("lib/std", .{}); | |
defer here.close(); | |
var walk = try here.walk(gpa); | |
defer walk.deinit(); | |
while (try walk.next()) |entry| if (entry.kind == .file and | |
mem.endsWith(u8, entry.basename, ".zig")) | |
{ | |
std.debug.print("{s}\n", .{entry.path}); | |
const level = mem.count(u8, entry.path, "/"); | |
const source = try here.dir.readFileAllocOptions( | |
gpa, | |
entry.path, | |
0x7fff_ffff, | |
null, | |
64, | |
0, | |
); | |
defer gpa.free(source); | |
var file = try here.dir.createFile(entry.path, .{ .truncate = true }); | |
defer file.close(); | |
var buffered = std.io.bufferedWriter(file.writer()); | |
const w = buffered.writer(); | |
var ast = try Ast.parse(gpa, source, .zig); | |
defer ast.deinit(gpa); | |
const tokens = ast.tokens.items(.start); | |
var last: u32 = 0; | |
for (ast.nodes.items(.tag), ast.nodes.items(.main_token)) |tag, token_index| { | |
if (tag == .builtin_call_two or tag == .builtin_call_two_comma) { | |
const slice = ast.tokenSlice(token_index); | |
if (mem.eql(u8, slice, "@import")) { | |
const next = ast.tokenSlice(token_index + 2); | |
if (mem.eql(u8, next, "\"std\"")) { | |
const offset = tokens[token_index + 2]; | |
const preceding = source[last..offset]; | |
last = offset + 5; | |
try w.writeAll(preceding); | |
try w.writeByte('"'); | |
for (0..level) |_| { | |
try w.writeAll("../"); | |
} | |
try w.writeAll("std.zig\""); | |
} | |
} | |
} | |
} | |
try w.writeAll(source[last..]); | |
try buffered.flush(); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment