Skip to content

Commit

Permalink
update files/root_files attributes to be compatible with Zig 0.11
Browse files Browse the repository at this point in the history
  • Loading branch information
nektro committed Aug 14, 2023
1 parent 62a4e5c commit 254cb60
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 27 deletions.
2 changes: 2 additions & 0 deletions src/cmd/generate.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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});
Expand Down
1 change: 1 addition & 0 deletions src/cmd/init.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}

Expand Down
45 changes: 20 additions & 25 deletions src/common.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand Down Expand Up @@ -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| {
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
Expand All @@ -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();

Expand All @@ -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,
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions src/util/dep.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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://");
Expand Down
5 changes: 5 additions & 0 deletions src/util/dep_type.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 => {},
}
}

Expand All @@ -58,6 +60,7 @@ pub const DepType = enum {
.http => {
//
},
.files => {},
}
}

Expand All @@ -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 => "",
};
}

Expand All @@ -82,6 +86,7 @@ pub const DepType = enum {
.git => false,
.hg => false,
.http => false,
.files => true,
};
}

Expand Down
1 change: 1 addition & 0 deletions src/util/module.zig
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ pub const Module = struct {
.git => try u.git_rev_HEAD(alloc, mdir),
.hg => @panic("TODO"),
.http => @panic("TODO"),
.files => unreachable,
};
},
};
Expand Down

0 comments on commit 254cb60

Please sign in to comment.