authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-02-10 20:52:53 -08:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-02-10 20:52:53 -08:00
loga285cec47d49d8a24626002fb9d7daa1588e0f7d
tree151fbd1a23bd56bb5c48049dbf6f196a4edc5423
parent14b8705a3ba28a8144db3a73021f7606fc9e1823

fix common.collect_deps and add options


1 files changed, 87 insertions(+), 11 deletions(-)

src/common.zig+87-11
...@@ -7,24 +7,100 @@ const u = @import("./util/index.zig");...@@ -7,24 +7,100 @@ const u = @import("./util/index.zig");
7//7//
8//8//
99
10pub fn collect_deps(dir: []const u8, mpath: []const u8) anyerror!u.Module {10pub const CollectOptions = struct {
11 log: bool,
12 update: bool,
13};
14
15pub fn collect_deps(dir: []const u8, mpath: []const u8, comptime options: CollectOptions) anyerror!u.Module {
11 const m = try u.ModFile.init(gpa, mpath);16 const m = try u.ModFile.init(gpa, mpath);
12 const moduledeps = &std.ArrayList(u.Module).init(gpa);17 const moduledeps = &std.ArrayList(u.Module).init(gpa);
13 var moddir: []const u8 = undefined;18 var moddir: []const u8 = undefined;
14 for (m.deps) |d| {19 for (m.deps) |d| {
15 const p = try fs.path.join(gpa, &[_][]const u8{dir, try d.clean_path()});20 const p = try fs.path.join(gpa, &[_][]const u8{dir, try d.clean_path()});
16 const pv = try fs.path.join(gpa, &[_][]const u8{dir, "v", try d.clean_path(), d.version});21 const pv = try fs.path.join(gpa, &[_][]const u8{dir, try d.clean_path_v()});
17 if (try u.does_file_exist(pv)) {22 if (options.log) { u.print("fetch: {s}: {s}: {s}", .{m.name, @tagName(d.type), d.path}); }
18 moddir = pv;23 moddir = p;
19 } else {24 switch (d.type) {
20 u.assert(try u.does_file_exist(p), "unable to find dep: {s} {s}, please run zigmod fetch", .{d.path, d.version});25 .system_lib => {
21 moddir = p;26 // no op
27 },
28 .git => blk: {
29 if (!try u.does_folder_exist(p)) {
30 try d.type.pull(d.path, p);
31 }
32 else {
33 if (options.update) { try d.type.update(p, d.path); }
34 }
35 if (d.version.len > 0) {
36 const vers = u.parse_split(u.GitVersionType, "-").do(d.version) catch |e| switch (e) {
37 error.IterEmpty => unreachable,
38 error.NoMemberFound => {
39 const vtype = d.version[0..std.mem.indexOf(u8, d.version, "-").?];
40 u.assert(false, "fetch: git: version type '{s}' is invalid.", .{vtype});
41 unreachable;
42 },
43 };
44 if (try u.does_folder_exist(pv)) {
45 if (vers.id == .branch) {
46 if (options.update) { try d.type.update(p, d.path); }
47 }
48 moddir = pv;
49 break :blk;
50 }
51 if ((try u.run_cmd(p, &[_][]const u8{"git", "checkout", vers.string})) > 0) {
52 u.assert(false, "fetch: git: {s}: {s} {s} does not exist", .{d.path, @tagName(vers.id), vers.string});
53 } else {
54 _ = try u.run_cmd(p, &[_][]const u8{"git", "checkout", "-"});
55 }
56 try d.type.pull(d.path, pv);
57 _ = try u.run_cmd(pv, &[_][]const u8{"git", "checkout", vers.string});
58 if (vers.id != .branch) {
59 const pvd = try std.fs.cwd().openDir(pv, .{});
60 try pvd.deleteTree(".git");
61 }
62 moddir = pv;
63 }
64 },
65 .hg => {
66 if (!try u.does_folder_exist(p)) {
67 try d.type.pull(d.path, p);
68 }
69 else {
70 if (options.update) { try d.type.update(p, d.path); }
71 }
72 },
73 .http => blk: {
74 if (try u.does_folder_exist(pv)) {
75 moddir = pv;
76 break :blk;
77 }
78 const file_name = try u.last(try u.split(d.path, "/"));
79 if (d.version.len > 0) {
80 const file_path = try std.fs.path.join(gpa, &[_][]const u8{pv, file_name});
81 try d.type.pull(d.path, pv);
82 if (try u.validate_hash(try u.last(try u.split(pv, "/")), file_path)) {
83 try std.fs.deleteFileAbsolute(file_path);
84 moddir = pv;
85 break :blk;
86 }
87 try u.rm_recv(pv);
88 u.assert(false, "{s} does not match hash {s}", .{d.path, d.version});
89 break :blk;
90 }
91 if (try u.does_folder_exist(p)) {
92 try u.rm_recv(p);
93 }
94 const file_path = try std.fs.path.join(gpa, &[_][]const u8{p, file_name});
95 try d.type.pull(d.path, p);
96 try std.fs.deleteFileAbsolute(file_path);
97 },
22 }98 }
23 switch (d.type) {99 switch (d.type) {
24 .system_lib => {100 .system_lib => {
25 if (d.is_for_this()) try moduledeps.append(u.Module{101 if (d.is_for_this()) try moduledeps.append(u.Module{
26 .is_sys_lib = true,102 .is_sys_lib = true,
27 .id = d.id,103 .id = "",
28 .name = d.path,104 .name = d.path,
29 .only_os = d.only_os,105 .only_os = d.only_os,
30 .except_os = d.except_os,106 .except_os = d.except_os,
...@@ -33,13 +109,13 @@ pub fn collect_deps(dir: []const u8, mpath: []const u8) anyerror!u.Module {...@@ -33,13 +109,13 @@ pub fn collect_deps(dir: []const u8, mpath: []const u8) anyerror!u.Module {
33 .c_source_flags = &[_][]const u8{},109 .c_source_flags = &[_][]const u8{},
34 .c_source_files = &[_][]const u8{},110 .c_source_files = &[_][]const u8{},
35 .deps = &[_]u.Module{},111 .deps = &[_]u.Module{},
36 .clean_path = "",112 .clean_path = d.path,
37 });113 });
38 },114 },
39 else => blk: {115 else => blk: {
40 var dd = try collect_deps(dir, try u.concat(&[_][]const u8{moddir, "/zig.mod"})) catch |e| switch (e) {116 var dd = try collect_deps(dir, try u.concat(&[_][]const u8{moddir, "/zig.mod"}), options) catch |e| switch (e) {
41 error.FileNotFound => {117 error.FileNotFound => {
42 if (d.c_include_dirs.len > 0 or d.c_source_files.len > 0) {118 if (d.main.len > 0 or d.c_include_dirs.len > 0 or d.c_source_files.len > 0) {
43 var mod_from = try u.Module.from(d);119 var mod_from = try u.Module.from(d);
44 mod_from.id = try u.random_string(48);120 mod_from.id = try u.random_string(48);
45 mod_from.clean_path = u.trim_prefix(moddir, dir)[1..];121 mod_from.clean_path = u.trim_prefix(moddir, dir)[1..];