|
| 1 | +const STD = @import("std"); |
| 2 | + |
| 3 | +const CLI = @import("cli.zig"); |
| 4 | +const UTIL = @import("util.zig"); |
| 5 | +const STYLES = @import("styles.zig"); |
| 6 | + |
| 7 | +const PatchSystem = struct { |
| 8 | + list_of_original_file_paths: STD.ArrayList([]const u8) = .empty, |
| 9 | + list_of_modified_file_paths: STD.ArrayList([]const u8) = .empty, |
| 10 | +}; |
| 11 | + |
| 12 | +fn iterateDirs(allocator: STD.mem.Allocator, patch_system: *PatchSystem) !void { |
| 13 | + _ = patch_system; |
| 14 | + |
| 15 | + try UTIL.iterateDirectory(allocator, ".patches/solutions", .{ .is_print_list_of_contents = true }); |
| 16 | + |
| 17 | +} |
| 18 | + |
| 19 | +// TODO: clean up |
| 20 | +fn generatePatches(allocator: STD.mem.Allocator, patch_system: *PatchSystem) !void { |
| 21 | + for (patch_system.list_of_exercises.items, patch_system.list_of_solutions.items) |exercise, solution| { |
| 22 | + const BUFFER_SIZE = comptime STD.math.pow(usize, 2, 16); |
| 23 | + |
| 24 | + // TODO: DRY |
| 25 | + var current_solution_filepath_iterator = STD.mem.splitAny(u8, solution, "/"); |
| 26 | + var current_solution_filepath_list: STD.ArrayList([]const u8) = .empty; |
| 27 | + |
| 28 | + while (current_solution_filepath_iterator.next()) |current_solution_filepath_slice_element| { |
| 29 | + try current_solution_filepath_list.append(allocator, current_solution_filepath_slice_element); |
| 30 | + } |
| 31 | + |
| 32 | + const TOP_LEVEL_DIR = current_solution_filepath_list.items[0]; |
| 33 | + const CHAPTER_DIR = current_solution_filepath_list.items[2]; |
| 34 | + const SOLUTION_FILENAME = current_solution_filepath_list.items[3]; |
| 35 | + |
| 36 | + const OUTPUT_FILENAME = try STD.mem.replaceOwned(u8, allocator, SOLUTION_FILENAME, ".cpp", ".patch"); |
| 37 | + |
| 38 | + const OUTPUT_FILEPATH = try STD.fs.path.join(allocator, &[_][]const u8{ TOP_LEVEL_DIR, "patches", CHAPTER_DIR, OUTPUT_FILENAME }); |
| 39 | + |
| 40 | + STD.debug.print("diff {s} {s} --> {s}{s}{s}\n", .{ |
| 41 | + exercise, |
| 42 | + solution, |
| 43 | + STYLES.ASCII_STYLES.underline, |
| 44 | + OUTPUT_FILEPATH, |
| 45 | + STYLES.ASCII_STYLES.clear_style, |
| 46 | + }); |
| 47 | + |
| 48 | + // TODO: DRY |
| 49 | + var process_args: STD.ArrayList([]const u8) = .empty; |
| 50 | + |
| 51 | + try process_args.appendSlice(allocator, &[_][]const u8{ "diff", "-u", exercise, solution }); |
| 52 | + var process = STD.process.Child.init(process_args.items, allocator); |
| 53 | + |
| 54 | + process.stderr_behavior = .Pipe; |
| 55 | + process.stdout_behavior = .Pipe; |
| 56 | + |
| 57 | + var process_stdout_buffer: STD.ArrayList(u8) = .empty; |
| 58 | + var process_stderr_buffer: STD.ArrayList(u8) = .empty; |
| 59 | + |
| 60 | + process.spawn() catch { |
| 61 | + STD.debug.print("{s}Failed to generate patches!\n{s}", .{ |
| 62 | + STYLES.ASCII_STYLES.red, |
| 63 | + STYLES.ASCII_STYLES.clear_style, |
| 64 | + }); |
| 65 | + return; |
| 66 | + }; |
| 67 | + |
| 68 | + try process.collectOutput(allocator, &process_stdout_buffer, &process_stderr_buffer, BUFFER_SIZE); |
| 69 | + |
| 70 | + const PATCHES_DIR_PATH = try STD.fs.path.join(allocator, &[_][]const u8{ TOP_LEVEL_DIR, "patches" }); |
| 71 | + const PATCHES_CHAPTER_DIR_PATH = try STD.fs.path.join(allocator, &[_][]const u8{ TOP_LEVEL_DIR, "patches", CHAPTER_DIR }); |
| 72 | + |
| 73 | + STD.fs.cwd().access(PATCHES_DIR_PATH, .{}) catch { |
| 74 | + try STD.fs.cwd().makeDir(PATCHES_DIR_PATH); |
| 75 | + }; |
| 76 | + |
| 77 | + STD.fs.cwd().access(PATCHES_CHAPTER_DIR_PATH, .{}) catch { |
| 78 | + try STD.fs.cwd().makeDir(PATCHES_CHAPTER_DIR_PATH); |
| 79 | + }; |
| 80 | + |
| 81 | + try STD.fs.cwd().writeFile(.{ .sub_path = OUTPUT_FILEPATH, .data = process_stdout_buffer.items }); |
| 82 | + } |
| 83 | + |
| 84 | + STD.debug.print("\n{s}Generated patches -> {s}./patches/patches{s}\n", .{ STYLES.ASCII_STYLES.bold, STYLES.ASCII_STYLES.underline, STYLES.ASCII_STYLES.clear_style }); |
| 85 | +} |
| 86 | + |
| 87 | +// TODO: implement patch function |
| 88 | +fn patch( |
| 89 | + allocator: STD.mem.Allocator, |
| 90 | + file: []const u8, |
| 91 | + patch_file: []const u8, |
| 92 | + extra_options: struct { reverse: bool = true }, |
| 93 | +) !void { |
| 94 | + _ = extra_options; |
| 95 | + |
| 96 | + // TODO: DRY |
| 97 | + const BUFFER_SIZE = comptime STD.math.pow(usize, 2, 16); |
| 98 | + |
| 99 | + var process_args: STD.ArrayList([]const u8) = .empty; |
| 100 | + |
| 101 | + try process_args.appendSlice(allocator, &[_][]const u8{ "patch", "-u", file, patch_file }); |
| 102 | + var process = STD.process.Child.init(process_args.items, allocator); |
| 103 | + |
| 104 | + process.stderr_behavior = .Pipe; |
| 105 | + process.stdout_behavior = .Pipe; |
| 106 | + |
| 107 | + var process_stdout_buffer: STD.ArrayList(u8) = .empty; |
| 108 | + var process_stderr_buffer: STD.ArrayList(u8) = .empty; |
| 109 | + |
| 110 | + process.spawn() catch { |
| 111 | + STD.debug.print("{s}Failed to patch {s}file!\n{s}", .{ |
| 112 | + STYLES.ASCII_STYLES.red, |
| 113 | + file, |
| 114 | + STYLES.ASCII_STYLES.clear_style, |
| 115 | + }); |
| 116 | + return; |
| 117 | + }; |
| 118 | + |
| 119 | + try process.collectOutput(allocator, &process_stdout_buffer, &process_stderr_buffer, BUFFER_SIZE); |
| 120 | +} |
| 121 | + |
| 122 | +pub fn run(allocator: STD.mem.Allocator) !void { |
| 123 | + STD.debug.print("{s}\n\n", .{CLI.ASCII_ART}); |
| 124 | + |
| 125 | + STD.debug.print("Creating patch system...\n", .{}); |
| 126 | + const patch_system: *PatchSystem = try allocator.create(PatchSystem); |
| 127 | + defer allocator.destroy(patch_system); |
| 128 | + |
| 129 | + STD.debug.print("Iterating exercises...\n", .{}); |
| 130 | + try iterateDirs(allocator, patch_system); |
| 131 | + |
| 132 | + // STD.debug.print("Generating patches...\n", .{}); |
| 133 | + // try generatePatches(allocator, patch_system); |
| 134 | +} |
| 135 | + |
| 136 | +// tests |
| 137 | + |
| 138 | +const TEST_PATCHES_DIR = "tests/patches"; |
| 139 | +const ORIGINAL_FILES_DIR = TEST_PATCHES_DIR ++ "/" ++ "original"; |
| 140 | +const ORIGINAL_FILE_PATH = ORIGINAL_FILES_DIR ++ "/" ++ "original.txt"; |
| 141 | +const MODIFIED_FILES_DIR = TEST_PATCHES_DIR ++ "/" ++ "modified"; |
| 142 | +const MODIFIED_FILE_PATH = MODIFIED_FILES_DIR ++ "/" ++ "modified.txt"; |
| 143 | + |
| 144 | +fn initTest() !void { |
| 145 | + STD.fs.cwd().access(ORIGINAL_FILES_DIR, .{}) catch { |
| 146 | + try STD.fs.cwd().makePath(ORIGINAL_FILES_DIR); |
| 147 | + }; |
| 148 | + |
| 149 | + STD.fs.cwd().access(MODIFIED_FILES_DIR, .{}) catch { |
| 150 | + try STD.fs.cwd().makePath(MODIFIED_FILES_DIR); |
| 151 | + }; |
| 152 | + |
| 153 | + STD.fs.cwd().access(ORIGINAL_FILE_PATH, .{}) catch { |
| 154 | + try STD.fs.cwd().writeFile(.{ .sub_path = ORIGINAL_FILE_PATH, .data = "hello", .flags = .{ .truncate = true } }); |
| 155 | + }; |
| 156 | + |
| 157 | + STD.fs.cwd().access(MODIFIED_FILE_PATH, .{}) catch { |
| 158 | + try STD.fs.cwd().writeFile(.{ .sub_path = MODIFIED_FILE_PATH, .data = "hey", .flags = .{ .truncate = true } }); |
| 159 | + }; |
| 160 | +} |
| 161 | + |
| 162 | +test "patch system" { |
| 163 | + var allocator: STD.mem.Allocator = STD.testing.allocator; |
| 164 | + |
| 165 | + try PATCH.run(allocator); |
| 166 | +} |
| 167 | + |
| 168 | +test "patch" { |
| 169 | + var allocator: STD.mem.Allocator = STD.testing.allocator; |
| 170 | + |
| 171 | + _ = allocator; |
| 172 | +} |
0 commit comments