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