| ... | ... | @@ -1,13 +1,11 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | 2 | const string = []const u8; |
| 3 | | const gpa = std.heap.c_allocator; |
| 4 | 3 | |
| 5 | 4 | const zigmod = @import("../lib.zig"); |
| 6 | 5 | const u = @import("./../util/index.zig"); |
| 7 | 6 | const common = @import("./../common.zig"); |
| 8 | 7 | |
| 9 | 8 | const ansi = @import("ansi"); |
| 10 | | |
| 11 | 9 | const root = @import("root"); |
| 12 | 10 | const build_options = if (@hasDecl(root, "build_options")) root.build_options else struct {}; |
| 13 | 11 | const bootstrap = if (@hasDecl(build_options, "bootstrap")) build_options.bootstrap else false; |
| ... | ... | @@ -17,6 +15,7 @@ const bootstrap = if (@hasDecl(build_options, "bootstrap")) build_options.bootst |
| 17 | 15 | |
| 18 | 16 | pub fn execute(args: [][]u8) !void { |
| 19 | 17 | // |
| 18 | const gpa = std.heap.c_allocator; |
| 20 | 19 | const cachepath = try std.fs.path.join(gpa, &.{ ".zigmod", "deps" }); |
| 21 | 20 | const dir = std.fs.cwd(); |
| 22 | 21 | const should_update = !(args.len >= 1 and std.mem.eql(u8, args[0], "--no-update")); |
| ... | ... | @@ -31,16 +30,16 @@ pub fn execute(args: [][]u8) !void { |
| 31 | 30 | var list = std.ArrayList(zigmod.Module).init(gpa); |
| 32 | 31 | try common.collect_pkgs(top_module, &list); |
| 33 | 32 | |
| 34 | | try create_depszig(cachepath, dir, top_module, &list); |
| 33 | try create_depszig(gpa, cachepath, dir, top_module, &list); |
| 35 | 34 | |
| 36 | 35 | if (bootstrap) return; |
| 37 | 36 | |
| 38 | | try create_lockfile(&list, cachepath, dir); |
| 37 | try create_lockfile(gpa, &list, cachepath, dir); |
| 39 | 38 | |
| 40 | | try diff_lockfile(); |
| 39 | try diff_lockfile(gpa); |
| 41 | 40 | } |
| 42 | 41 | |
| 43 | | pub fn create_depszig(cachepath: string, dir: std.fs.Dir, top_module: zigmod.Module, list: *std.ArrayList(zigmod.Module)) !void { |
| 42 | pub fn create_depszig(alloc: std.mem.Allocator, cachepath: string, dir: std.fs.Dir, top_module: zigmod.Module, list: *std.ArrayList(zigmod.Module)) !void { |
| 44 | 43 | const f = try dir.createFile("deps.zig", .{}); |
| 45 | 44 | defer f.close(); |
| 46 | 45 | |
| ... | ... | @@ -98,14 +97,14 @@ pub fn create_depszig(cachepath: string, dir: std.fs.Dir, top_module: zigmod.Mod |
| 98 | 97 | try w.writeAll("};\n\n"); |
| 99 | 98 | |
| 100 | 99 | try w.writeAll("pub const package_data = struct {\n"); |
| 101 | | var duped = std.ArrayList(zigmod.Module).init(gpa); |
| 100 | var duped = std.ArrayList(zigmod.Module).init(alloc); |
| 102 | 101 | for (list.items) |mod| { |
| 103 | 102 | if (mod.is_sys_lib) { |
| 104 | 103 | continue; |
| 105 | 104 | } |
| 106 | 105 | try duped.append(mod); |
| 107 | 106 | } |
| 108 | | try print_pkg_data_to(w, &duped, &std.ArrayList(zigmod.Module).init(gpa)); |
| 107 | try print_pkg_data_to(w, &duped, &std.ArrayList(zigmod.Module).init(alloc)); |
| 109 | 108 | try w.writeAll("};\n\n"); |
| 110 | 109 | |
| 111 | 110 | try w.writeAll("pub const packages = "); |
| ... | ... | @@ -113,15 +112,15 @@ pub fn create_depszig(cachepath: string, dir: std.fs.Dir, top_module: zigmod.Mod |
| 113 | 112 | try w.writeAll(";\n\n"); |
| 114 | 113 | |
| 115 | 114 | try w.writeAll("pub const pkgs = "); |
| 116 | | try print_pkgs(w, top_module); |
| 115 | try print_pkgs(alloc, w, top_module); |
| 117 | 116 | try w.writeAll(";\n\n"); |
| 118 | 117 | |
| 119 | 118 | try w.writeAll("pub const imports = struct {\n"); |
| 120 | | try print_imports(w, top_module, cachepath); |
| 119 | try print_imports(alloc, w, top_module, cachepath); |
| 121 | 120 | try w.writeAll("};\n"); |
| 122 | 121 | } |
| 123 | 122 | |
| 124 | | fn create_lockfile(list: *std.ArrayList(zigmod.Module), path: string, dir: std.fs.Dir) !void { |
| 123 | fn create_lockfile(alloc: std.mem.Allocator, list: *std.ArrayList(zigmod.Module), path: string, dir: std.fs.Dir) !void { |
| 125 | 124 | const fl = try dir.createFile("zigmod.lock", .{}); |
| 126 | 125 | defer fl.close(); |
| 127 | 126 | |
| ... | ... | @@ -133,7 +132,7 @@ fn create_lockfile(list: *std.ArrayList(zigmod.Module), path: string, dir: std.f |
| 133 | 132 | continue; |
| 134 | 133 | } |
| 135 | 134 | if (md.type == .system_lib) continue; |
| 136 | | const mpath = try std.fs.path.join(gpa, &.{ path, m.clean_path }); |
| 135 | const mpath = try std.fs.path.join(alloc, &.{ path, m.clean_path }); |
| 137 | 136 | const version = try md.exact_version(mpath); |
| 138 | 137 | try wl.print("{s} {s} {s}\n", .{ @tagName(md.type), md.path, version }); |
| 139 | 138 | } |
| ... | ... | @@ -145,25 +144,25 @@ const DiffChange = struct { |
| 145 | 144 | to: string, |
| 146 | 145 | }; |
| 147 | 146 | |
| 148 | | fn diff_lockfile() !void { |
| 147 | fn diff_lockfile(alloc: std.mem.Allocator) !void { |
| 149 | 148 | const max = std.math.maxInt(usize); |
| 150 | 149 | |
| 151 | 150 | if (try u.does_folder_exist(".git")) { |
| 152 | | const result = try u.run_cmd_raw(gpa, null, &.{ "git", "diff", "zigmod.lock" }); |
| 151 | const result = try u.run_cmd_raw(alloc, null, &.{ "git", "diff", "zigmod.lock" }); |
| 153 | 152 | const r = std.io.fixedBufferStream(result.stdout).reader(); |
| 154 | | while (try r.readUntilDelimiterOrEofAlloc(gpa, '\n', max)) |line| { |
| 153 | while (try r.readUntilDelimiterOrEofAlloc(alloc, '\n', max)) |line| { |
| 155 | 154 | if (std.mem.startsWith(u8, line, "@@")) break; |
| 156 | 155 | } |
| 157 | 156 | |
| 158 | | var rems = std.ArrayList(string).init(gpa); |
| 159 | | var adds = std.ArrayList(string).init(gpa); |
| 160 | | while (try r.readUntilDelimiterOrEofAlloc(gpa, '\n', max)) |line| { |
| 157 | var rems = std.ArrayList(string).init(alloc); |
| 158 | var adds = std.ArrayList(string).init(alloc); |
| 159 | while (try r.readUntilDelimiterOrEofAlloc(alloc, '\n', max)) |line| { |
| 161 | 160 | if (line[0] == ' ') continue; |
| 162 | 161 | if (line[0] == '-') try rems.append(line[1..]); |
| 163 | 162 | if (line[0] == '+') if (line[1] == '2') continue else try adds.append(line[1..]); |
| 164 | 163 | } |
| 165 | 164 | |
| 166 | | var changes = std.StringHashMap(DiffChange).init(gpa); |
| 165 | var changes = std.StringHashMap(DiffChange).init(alloc); |
| 167 | 166 | |
| 168 | 167 | var didbreak = false; |
| 169 | 168 | var i: usize = 0; |
| ... | ... | @@ -349,7 +348,7 @@ fn contains_all(needles: []zigmod.Module, haystack: []const zigmod.Module) bool |
| 349 | 348 | return true; |
| 350 | 349 | } |
| 351 | 350 | |
| 352 | | fn print_pkgs(w: std.fs.File.Writer, m: zigmod.Module) !void { |
| 351 | fn print_pkgs(alloc: std.mem.Allocator, w: std.fs.File.Writer, m: zigmod.Module) !void { |
| 353 | 352 | try w.writeAll("struct {\n"); |
| 354 | 353 | for (m.deps) |d| { |
| 355 | 354 | if (d.main.len == 0) { |
| ... | ... | @@ -358,13 +357,13 @@ fn print_pkgs(w: std.fs.File.Writer, m: zigmod.Module) !void { |
| 358 | 357 | if (d.for_build) { |
| 359 | 358 | continue; |
| 360 | 359 | } |
| 361 | | const ident = try zig_name_from_pkg_name(d.name); |
| 360 | const ident = try zig_name_from_pkg_name(alloc, d.name); |
| 362 | 361 | try w.print(" pub const {s} = package_data._{s};\n", .{ ident, d.id[0..12] }); |
| 363 | 362 | } |
| 364 | 363 | try w.writeAll("}"); |
| 365 | 364 | } |
| 366 | 365 | |
| 367 | | fn print_imports(w: std.fs.File.Writer, m: zigmod.Module, path: string) !void { |
| 366 | fn print_imports(alloc: std.mem.Allocator, w: std.fs.File.Writer, m: zigmod.Module, path: string) !void { |
| 368 | 367 | for (m.deps) |d| { |
| 369 | 368 | if (d.main.len == 0) { |
| 370 | 369 | continue; |
| ... | ... | @@ -372,15 +371,15 @@ fn print_imports(w: std.fs.File.Writer, m: zigmod.Module, path: string) !void { |
| 372 | 371 | if (!d.for_build) { |
| 373 | 372 | continue; |
| 374 | 373 | } |
| 375 | | const ident = try zig_name_from_pkg_name(d.name); |
| 374 | const ident = try zig_name_from_pkg_name(alloc, d.name); |
| 376 | 375 | try w.print(" pub const {s} = @import(\"{}/{}/{s}\");\n", .{ ident, std.zig.fmtEscapes(path), std.zig.fmtEscapes(d.clean_path), d.main }); |
| 377 | 376 | } |
| 378 | 377 | } |
| 379 | 378 | |
| 380 | | fn zig_name_from_pkg_name(name: string) !string { |
| 379 | fn zig_name_from_pkg_name(alloc: std.mem.Allocator, name: string) !string { |
| 381 | 380 | var legal = name; |
| 382 | | legal = try std.mem.replaceOwned(u8, gpa, legal, "-", "_"); |
| 383 | | legal = try std.mem.replaceOwned(u8, gpa, legal, "/", "_"); |
| 384 | | legal = try std.mem.replaceOwned(u8, gpa, legal, ".", "_"); |
| 381 | legal = try std.mem.replaceOwned(u8, alloc, legal, "-", "_"); |
| 382 | legal = try std.mem.replaceOwned(u8, alloc, legal, "/", "_"); |
| 383 | legal = try std.mem.replaceOwned(u8, alloc, legal, ".", "_"); |
| 385 | 384 | return legal; |
| 386 | 385 | } |