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");...@@ -11,17 +11,18 @@ const common = @import("./../common.zig");
1111
12pub fn execute(self_name: []const u8, args: [][:0]u8) !void {12pub fn execute(self_name: []const u8, args: [][:0]u8) !void {
13 _ = self_name;13 _ = self_name;
14 _ = args;
1514
16 //15 //
17 const gpa = std.heap.c_allocator;16 const gpa = std.heap.c_allocator;
18 const cachepath = try u.find_cachepath();17 const cachepath = try u.find_cachepath();
19 const dir = std.fs.cwd();18 const dir = std.fs.cwd();
19 const should_lock = args.len >= 1 and std.mem.eql(u8, args[0], "--lock");
2020
21 var options = common.CollectOptions{21 var options = common.CollectOptions{
22 .log = false,22 .log = false,
23 .update = false,23 .update = false,
24 .alloc = gpa,24 .alloc = gpa,
25 .lock = if (should_lock) try common.parse_lockfile(gpa, dir) else null,
25 };26 };
26 const top_module = try common.collect_deps_deep(cachepath, dir, &options);27 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 {...@@ -30,10 +31,10 @@ pub fn execute(self_name: []const u8, args: [][:0]u8) !void {
3031
31 std.mem.sort(zigmod.Module, list.items, {}, zigmod.Module.lessThan);32 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);
34}35}
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 {
37 const f = try dir.createFile("deps.zig", .{});38 const f = try dir.createFile("deps.zig", .{});
38 defer f.close();39 defer f.close();
3940
...@@ -104,7 +105,7 @@ pub fn create_depszig(alloc: std.mem.Allocator, cachepath: string, dir: std.fs.D...@@ -104,7 +105,7 @@ pub fn create_depszig(alloc: std.mem.Allocator, cachepath: string, dir: std.fs.D
104 .local => {},105 .local => {},
105 .system_lib => {},106 .system_lib => {},
106 .framework => {},107 .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) }),
108 .hg => @panic("TODO"),109 .hg => @panic("TODO"),
109 .http => @panic("TODO"),110 .http => @panic("TODO"),
110 }111 }
...@@ -226,7 +227,7 @@ pub fn create_depszig(alloc: std.mem.Allocator, cachepath: string, dir: std.fs.D...@@ -226,7 +227,7 @@ pub fn create_depszig(alloc: std.mem.Allocator, cachepath: string, dir: std.fs.D
226 }227 }
227 try duped.append(mod);228 try duped.append(mod);
228 }229 }
229 try print_pkg_data_to(w, alloc, cachepath, &duped, &done);230 try print_pkg_data_to(w, alloc, cachepath, &duped, &done, options);
230 try w.writeAll("};\n\n");231 try w.writeAll("};\n\n");
231232
232 try w.writeAll("pub const packages = ");233 try w.writeAll("pub const packages = ");
...@@ -267,7 +268,7 @@ fn print_deps(w: std.fs.File.Writer, m: zigmod.Module) !void {...@@ -267,7 +268,7 @@ fn print_deps(w: std.fs.File.Writer, m: zigmod.Module) !void {
267 try w.writeAll("}");268 try w.writeAll("}");
268}269}
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 {
271 var len: usize = notdone.items.len;272 var len: usize = notdone.items.len;
272 while (notdone.items.len > 0) {273 while (notdone.items.len > 0) {
273 for (notdone.items, 0..) |mod, i| {274 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:...@@ -282,13 +283,13 @@ fn print_pkg_data_to(w: std.fs.File.Writer, alloc: std.mem.Allocator, cachepath:
282 switch (mod.type) {283 switch (mod.type) {
283 .system_lib, .framework => {},284 .system_lib, .framework => {},
284 .local => {},285 .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) }),
286 .hg => @panic("TODO"),287 .hg => @panic("TODO"),
287 .http => @panic("TODO"),288 .http => @panic("TODO"),
288 }289 }
289 if (mod.main.len > 0 and !std.mem.eql(u8, &mod.id, &zigmod.Module.ROOT)) {290 if (mod.main.len > 0 and !std.mem.eql(u8, &mod.id, &zigmod.Module.ROOT)) {
290 try w.print(" .name = \"{s}\",\n", .{mod.name});291 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
293 if (mod.deps.len != 0) {294 if (mod.deps.len != 0) {
294 try w.writeAll(" .deps = &[_]*Package{");295 try w.writeAll(" .deps = &[_]*Package{");
src/util/module.zig+10-2
...@@ -163,7 +163,7 @@ pub const Module = struct {...@@ -163,7 +163,7 @@ pub const Module = struct {
163 return false;163 return false;
164 }164 }
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 {
167 return switch (self.type) {167 return switch (self.type) {
168 .local => "",168 .local => "",
169 .system_lib => "",169 .system_lib => "",
...@@ -175,7 +175,15 @@ pub const Module = struct {...@@ -175,7 +175,15 @@ pub const Module = struct {
175 defer mdir.close();175 defer mdir.close();
176 return switch (sub) {176 return switch (sub) {
177 .local, .system_lib, .framework => unreachable,177 .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 },
179 .hg => @panic("TODO"),187 .hg => @panic("TODO"),
180 .http => @panic("TODO"),188 .http => @panic("TODO"),
181 };189 };