authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2025-12-31 23:45:48 -08:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2025-12-31 23:45:48 -08:00
log0e0c69c1964176e06e63328dc74b777281eed737
tree27b1f26fec3de4db4639145201a44cf99e52e988
parenta95c8a1b74a3d7f2251b3302901da523f560bab6

cmd/generate: add a --lock option


2 files changed, 19 insertions(+), 10 deletions(-)

src/cmd/generate.zig+9-8
......@@ -11,17 +11,18 @@ const common = @import("./../common.zig");
1111
1212pub fn execute(self_name: []const u8, args: [][:0]u8) !void {
1313 _ = self_name;
14 _ = args;
1514
1615 //
1716 const gpa = std.heap.c_allocator;
1817 const cachepath = try u.find_cachepath();
1918 const dir = std.fs.cwd();
19 const should_lock = args.len >= 1 and std.mem.eql(u8, args[0], "--lock");
2020
2121 var options = common.CollectOptions{
2222 .log = false,
2323 .update = false,
2424 .alloc = gpa,
25 .lock = if (should_lock) try common.parse_lockfile(gpa, dir) else null,
2526 };
2627 const top_module = try common.collect_deps_deep(cachepath, dir, &options);
2728
......@@ -30,10 +31,10 @@ pub fn execute(self_name: []const u8, args: [][:0]u8) !void {
3031
3132 std.mem.sort(zigmod.Module, list.items, {}, zigmod.Module.lessThan);
3233
33 try create_depszig(gpa, cachepath, dir, top_module, list.items);
34 try create_depszig(gpa, cachepath, dir, top_module, list.items, &options);
3435}
3536
36pub fn create_depszig(alloc: std.mem.Allocator, cachepath: string, dir: std.fs.Dir, top_module: zigmod.Module, list: []const zigmod.Module) !void {
37pub fn create_depszig(alloc: std.mem.Allocator, cachepath: string, dir: std.fs.Dir, top_module: zigmod.Module, list: []const zigmod.Module, options: *common.CollectOptions) !void {
3738 const f = try dir.createFile("deps.zig", .{});
3839 defer f.close();
3940
......@@ -104,7 +105,7 @@ pub fn create_depszig(alloc: std.mem.Allocator, cachepath: string, dir: std.fs.D
104105 .local => {},
105106 .system_lib => {},
106107 .framework => {},
107 .git => try w.print(" step.dependOn(&GitExactStep.create(b, \"{s}\", \"{s}\").step);\n", .{ module.dep.?.path, try module.pin(alloc, cachepath) }),
108 .git => try w.print(" step.dependOn(&GitExactStep.create(b, \"{s}\", \"{s}\").step);\n", .{ module.dep.?.path, try module.pin(alloc, cachepath, options) }),
108109 .hg => @panic("TODO"),
109110 .http => @panic("TODO"),
110111 }
......@@ -226,7 +227,7 @@ pub fn create_depszig(alloc: std.mem.Allocator, cachepath: string, dir: std.fs.D
226227 }
227228 try duped.append(mod);
228229 }
229 try print_pkg_data_to(w, alloc, cachepath, &duped, &done);
230 try print_pkg_data_to(w, alloc, cachepath, &duped, &done, options);
230231 try w.writeAll("};\n\n");
231232
232233 try w.writeAll("pub const packages = ");
......@@ -267,7 +268,7 @@ fn print_deps(w: std.fs.File.Writer, m: zigmod.Module) !void {
267268 try w.writeAll("}");
268269}
269270
270fn print_pkg_data_to(w: std.fs.File.Writer, alloc: std.mem.Allocator, cachepath: string, notdone: *std.ArrayList(zigmod.Module), done: *std.ArrayList(zigmod.Module)) !void {
271fn print_pkg_data_to(w: std.fs.File.Writer, alloc: std.mem.Allocator, cachepath: string, notdone: *std.ArrayList(zigmod.Module), done: *std.ArrayList(zigmod.Module), options: *common.CollectOptions) !void {
271272 var len: usize = notdone.items.len;
272273 while (notdone.items.len > 0) {
273274 for (notdone.items, 0..) |mod, i| {
......@@ -282,13 +283,13 @@ fn print_pkg_data_to(w: std.fs.File.Writer, alloc: std.mem.Allocator, cachepath:
282283 switch (mod.type) {
283284 .system_lib, .framework => {},
284285 .local => {},
285 .git => try w.print(" .store = \"/{}/{s}\",\n", .{ std.zig.fmtEscapes(fixed_path), try mod.pin(alloc, cachepath) }),
286 .git => try w.print(" .store = \"/{}/{s}\",\n", .{ std.zig.fmtEscapes(fixed_path), try mod.pin(alloc, cachepath, options) }),
286287 .hg => @panic("TODO"),
287288 .http => @panic("TODO"),
288289 }
289290 if (mod.main.len > 0 and !std.mem.eql(u8, &mod.id, &zigmod.Module.ROOT)) {
290291 try w.print(" .name = \"{s}\",\n", .{mod.name});
291 try w.print(" .entry = \"/{}/{s}/{s}\",\n", .{ std.zig.fmtEscapes(fixed_path), try mod.pin(alloc, cachepath), mod.main });
292 try w.print(" .entry = \"/{}/{s}/{s}\",\n", .{ std.zig.fmtEscapes(fixed_path), try mod.pin(alloc, cachepath, options), mod.main });
292293
293294 if (mod.deps.len != 0) {
294295 try w.writeAll(" .deps = &[_]*Package{");
src/util/module.zig+10-2
......@@ -163,7 +163,7 @@ pub const Module = struct {
163163 return false;
164164 }
165165
166 pub fn pin(self: Module, alloc: std.mem.Allocator, cachepath: string) !string {
166 pub fn pin(self: Module, alloc: std.mem.Allocator, cachepath: string, options: *common.CollectOptions) !string {
167167 return switch (self.type) {
168168 .local => "",
169169 .system_lib => "",
......@@ -175,7 +175,15 @@ pub const Module = struct {
175175 defer mdir.close();
176176 return switch (sub) {
177177 .local, .system_lib, .framework => unreachable,
178 .git => try u.git_rev_HEAD(alloc, mdir),
178 .git => {
179 if (options.lock == null) return try u.git_rev_HEAD(alloc, mdir);
180 for (options.lock.?) |item| {
181 if (!std.mem.eql(u8, item[1], "git")) continue;
182 if (!std.mem.eql(u8, item[2], self.dep.?.path)) continue;
183 return item[3][std.mem.indexOfScalar(u8, item[3], '-').? + 1 ..];
184 }
185 @panic("unreachable");
186 },
179187 .hg => @panic("TODO"),
180188 .http => @panic("TODO"),
181189 };