From 254cb608f66ebbf7109491903f06c2ff41cf9dbc Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Mon, 14 Aug 2023 14:39:45 -0700 Subject: [PATCH] update files/root_files attributes to be compatible with Zig 0.11 --- src/cmd/generate.zig | 2 ++ src/cmd/init.zig | 1 + src/common.zig | 45 +++++++++++++++++++------------------------ src/util/dep.zig | 4 ++-- src/util/dep_type.zig | 5 +++++ src/util/module.zig | 1 + 6 files changed, 31 insertions(+), 27 deletions(-) diff --git a/src/cmd/generate.zig b/src/cmd/generate.zig index 980663e9e84f08f21fa7f19a1a9e77743dc8e517..a41cb426471f8c2351fa8dac71462bdc3b936fe6 100644 --- a/src/cmd/generate.zig +++ b/src/cmd/generate.zig @@ -106,6 +106,7 @@ pub fn create_depszig(alloc: std.mem.Allocator, cachepath: string, dir: std.fs.D .git => try w.print(" exe.step.dependOn(&GitExactStep.create(b, \"{s}\", \"{s}\").step);\n", .{ module.dep.?.path, try module.pin(alloc, cachepath) }), .hg => @panic("TODO"), .http => @panic("TODO"), + .files => {}, } } try w.writeAll( @@ -273,6 +274,7 @@ fn print_pkg_data_to(w: std.fs.File.Writer, alloc: std.mem.Allocator, cachepath: .git => try w.print(" .store = \"/{}/{s}\",\n", .{ std.zig.fmtEscapes(fixed_path), try mod.pin(alloc, cachepath) }), .hg => @panic("TODO"), .http => @panic("TODO"), + .files => {}, } if (mod.main.len > 0 and !std.mem.eql(u8, mod.id, "root")) { try w.print(" .name = \"{s}\",\n", .{mod.name}); diff --git a/src/cmd/init.zig b/src/cmd/init.zig index 7fb80173b76f1c6ee794cbd83c67cc22fd729fbb..13fdf161204a7d8ce426a48269005a960042695f 100644 --- a/src/cmd/init.zig +++ b/src/cmd/init.zig @@ -148,6 +148,7 @@ pub fn execute(args: [][]u8) !void { if (!exists) try w.writeAll("zig-*\n"); try w.writeAll(".zigmod\n"); try w.writeAll("deps.zig\n"); + try w.writeAll("files.zig\n"); } } diff --git a/src/common.zig b/src/common.zig index d25388e95ce4f0d846461c2caed2366d3e7066e8..1488aeb9becc6c93cbff9957e6cbe3d7d7da5bda 100644 --- a/src/common.zig +++ b/src/common.zig @@ -31,8 +31,7 @@ pub fn collect_deps_deep(cachepath: string, mdir: std.fs.Dir, options: *CollectO var moduledeps = std.ArrayList(zigmod.Module).init(options.alloc); errdefer moduledeps.deinit(); if (m.root_files.len > 0) { - const builddotzig = try extras.hashFile(mdir, "build.zig", std.crypto.hash.Sha1); - try moduledeps.append(try add_files_package(options.alloc, cachepath, "root_" ++ &builddotzig, mdir, m.root_files)); + try moduledeps.append(try add_files_package(options.alloc, cachepath, mdir, m.root_files)); } try moduledeps.append(try collect_deps(cachepath, mdir, .local, options)); for (m.rootdeps) |*d| { @@ -66,7 +65,7 @@ pub fn collect_deps(cachepath: string, mdir: std.fs.Dir, dtype: zigmod.Dep.Type, var moduledeps = std.ArrayList(zigmod.Module).init(options.alloc); errdefer moduledeps.deinit(); if (m.files.len > 0) { - try moduledeps.append(try add_files_package(options.alloc, cachepath, m.id, mdir, m.files)); + try moduledeps.append(try add_files_package(options.alloc, cachepath, mdir, m.files)); } for (m.deps) |*d| { if (try get_module_from_dep(d, cachepath, options)) |founddep| { @@ -118,7 +117,7 @@ pub fn get_modpath(cachepath: string, d: zigmod.Dep, options: *CollectOptions) ! } } switch (d.type) { - .local => { + .local, .files => { if (!std.mem.endsWith(u8, d.main, ".zig")) { return d.main; } @@ -217,7 +216,7 @@ pub fn get_module_from_dep(d: *zigmod.Dep, cachepath: string, options: *CollectO } if (!d.is_for_this()) return null; const modpath = try get_modpath(cachepath, d.*, options); - const moddir = if (std.mem.eql(u8, modpath, "files") or modpath.len == 0) try std.fs.cwd().openDir(cachepath, .{}) else try std.fs.cwd().openDir(modpath, .{}); + const moddir = if (d.type == .files or modpath.len == 0) try std.fs.cwd().openDir(cachepath, .{}) else try std.fs.cwd().openDir(modpath, .{}); const nocache = d.type.isLocal(); if (!nocache) try options.already_fetched.append(modpath); @@ -245,7 +244,7 @@ pub fn get_module_from_dep(d: *zigmod.Dep, cachepath: string, options: *CollectO error.ManifestNotFound => { if (d.main.len > 0 or d.c_include_dirs.len > 0 or d.c_source_files.len > 0 or d.keep) { var mod_from = try zigmod.Module.from(options.alloc, d.*, cachepath, options); - if (d.type != .local) mod_from.clean_path = extras.trimPrefix(modpath, cachepath)[1..]; + if (d.type != .local and d.type != .files) mod_from.clean_path = extras.trimPrefix(modpath, cachepath)[1..]; if (mod_from.is_for_this()) return mod_from; return null; } @@ -259,7 +258,7 @@ pub fn get_module_from_dep(d: *zigmod.Dep, cachepath: string, options: *CollectO d.*.name = tryname; d.*.main = trymain.?; var mod_from = try zigmod.Module.from(options.alloc, d.*, cachepath, options); - if (d.type != .local) mod_from.clean_path = extras.trimPrefix(modpath, cachepath)[1..]; + if (d.type != .local and d.type != .files) mod_from.clean_path = extras.trimPrefix(modpath, cachepath)[1..]; if (mod_from.is_for_this()) return mod_from; return null; } @@ -270,7 +269,7 @@ pub fn get_module_from_dep(d: *zigmod.Dep, cachepath: string, options: *CollectO dd.dep = d.*; dd.for_build = d.for_build; const save = dd; - if (d.type != .local) dd.clean_path = extras.trimPrefix(modpath, cachepath)[1..]; + if (d.type != .local and d.type != .files) dd.clean_path = extras.trimPrefix(modpath, cachepath)[1..]; if (dd.id.len == 0) dd.id = try u.random_string(options.alloc, 48); if (d.name.len > 0) dd.name = d.name; if (d.main.len > 0) dd.main = d.main; @@ -279,17 +278,15 @@ pub fn get_module_from_dep(d: *zigmod.Dep, cachepath: string, options: *CollectO if (d.c_source_files.len > 0) dd.c_source_files = d.c_source_files; if (d.only_os.len > 0) dd.only_os = d.only_os; if (d.except_os.len > 0) dd.except_os = d.except_os; - if (d.type == .local) dd.main = try std.fs.path.join(options.alloc, &.{ d.main, save.main }); - if (std.mem.eql(u8, modpath, "files")) dd.clean_path = modpath; + if (d.type == .local or d.type == .files) dd.main = try std.fs.path.join(options.alloc, &.{ d.main, save.main }); + if (d.type == .files) dd.clean_path = modpath; if (dd.is_for_this()) return dd; return null; }, } } -pub fn add_files_package(alloc: std.mem.Allocator, cachepath: string, pkg_name: string, mdir: std.fs.Dir, dirs: []const string) !zigmod.Module { - const fname = try std.mem.join(alloc, "", &.{ pkg_name, ".zig" }); - +pub fn add_files_package(alloc: std.mem.Allocator, cachepath: string, mdir: std.fs.Dir, dirs: []const string) !zigmod.Module { var map = std.StringHashMap(string).init(alloc); defer map.deinit(); @@ -306,24 +303,23 @@ pub fn add_files_package(alloc: std.mem.Allocator, cachepath: string, pkg_name: } } - const fpath = try mdir.realpathAlloc(alloc, "."); - var cachedir = try std.fs.cwd().openDir(cachepath, .{}); - defer cachedir.close(); - try cachedir.makePath("files"); - var destdir = try cachedir.openDir("files", .{}); - defer destdir.close(); + var dpath: string = try mdir.realpathAlloc(alloc, "."); + const is_not_root = std.mem.indexOf(u8, dpath, cachepath); + dpath = if (is_not_root) |idx| dpath[idx..] else "../.."; + + const fname = "files.zig"; + const destdir = mdir; const rff = try destdir.createFile(fname, .{}); defer rff.close(); const w = rff.writer(); - try w.print("const srcpath = \"/{}\";\n\n", .{std.zig.fmtEscapes(fpath[1..])}); var iter = map.iterator(); while (iter.next()) |item| { - try w.print("pub const @\"/{}\" = @embedFile(srcpath ++ \"/{}\");\n", .{ std.zig.fmtEscapes(item.key_ptr.*), std.zig.fmtEscapes(item.value_ptr.*) }); + try w.print("pub const @\"/{}\" = @embedFile(\"{}\");\n", .{ std.zig.fmtEscapes(item.key_ptr.*), std.zig.fmtEscapes(item.value_ptr.*) }); } var d: zigmod.Dep = .{ - .type = .local, - .path = "files", + .type = .files, + .path = extras.trimPrefix(extras.trimPrefix(dpath, cachepath), "/"), .id = "", .name = "self/files", .main = fname, @@ -337,8 +333,7 @@ pub fn add_files_package(alloc: std.mem.Allocator, cachepath: string, pkg_name: .update = false, .alloc = alloc, }; - const filesdestpath = try std.fs.path.join(alloc, &.{ cachepath, "files" }); - return (try get_module_from_dep(&d, filesdestpath, &options)).?; + return (try get_module_from_dep(&d, cachepath, &options)).?; } pub fn parse_lockfile(alloc: std.mem.Allocator, dir: std.fs.Dir) ![]const [4]string { diff --git a/src/util/dep.zig b/src/util/dep.zig index 7a9ba83f0c4834ca7d99e162dfcb787c23639395..d50dcf34b21fea06c4043f5fdaa96b554d91e45a 100644 --- a/src/util/dep.zig +++ b/src/util/dep.zig @@ -34,8 +34,8 @@ pub const Dep = struct { pub const Type = @import("./dep_type.zig").DepType; pub fn clean_path(self: Dep, alloc: std.mem.Allocator) !string { - if (self.type == .local) { - return if (self.path.len == 0) "../.." else self.path; + if (self.type == .local or self.type == .files) { + return self.path; } var p = self.path; p = extras.trimPrefix(p, "http://"); diff --git a/src/util/dep_type.zig b/src/util/dep_type.zig index 865b826028abbcb6175d6a050892da786115389a..ca06f61139451f8f6d7636d248bac90464b4210f 100644 --- a/src/util/dep_type.zig +++ b/src/util/dep_type.zig @@ -14,6 +14,7 @@ pub const DepType = enum { git, // https://git-scm.com/ hg, // https://www.mercurial-scm.org/ http, // https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol + files, // TEMP also local // zig fmt: on pub fn pull(self: DepType, alloc: std.mem.Allocator, rpath: string, dpath: string) !void { @@ -38,6 +39,7 @@ pub const DepType = enum { u.assert((try u.run_cmd(alloc, dpath, &.{ "tar", "-xf", f, "-C", "." })) == 0, "un-tar {s} failed", .{f}); } }, + .files => {}, } } @@ -58,6 +60,7 @@ pub const DepType = enum { .http => { // }, + .files => {}, } } @@ -71,6 +74,7 @@ pub const DepType = enum { .git => try std.fmt.allocPrint(alloc, "commit-{s}", .{(try u.git_rev_HEAD(alloc, mdir))}), .hg => "", .http => "", + .files => "", }; } @@ -82,6 +86,7 @@ pub const DepType = enum { .git => false, .hg => false, .http => false, + .files => true, }; } diff --git a/src/util/module.zig b/src/util/module.zig index ac1c03de7ee97351e5f827088da9ecab731a7b7b..6bb8c5c9c869f0db81558ccfa273d87fedf374c2 100644 --- a/src/util/module.zig +++ b/src/util/module.zig @@ -174,6 +174,7 @@ pub const Module = struct { .git => try u.git_rev_HEAD(alloc, mdir), .hg => @panic("TODO"), .http => @panic("TODO"), + .files => unreachable, }; }, }; -- 2.54.0