diff --git a/src/cmd_fetch.zig b/src/cmd_fetch.zig index f2e6897a9d32aaceb9b7bd859a7d0ef993160a4f..717cbef456ca3e346fc4a2e6e96008becf2e15d6 100644 --- a/src/cmd_fetch.zig +++ b/src/cmd_fetch.zig @@ -75,11 +75,10 @@ fn fetch_deps(dir: []const u8, mpath: []const u8) anyerror!u.Module { switch (d.type) { .git => blk: { if (!try u.does_file_exist(p)) { - _ = try run_cmd(null, &[_][]const u8{"git", "clone", d.path, p}); + _ = try d.type.pull(d.path, p); } else { - _ = try run_cmd(p, &[_][]const u8{"git", "fetch"}); - _ = try run_cmd(p, &[_][]const u8{"git", "pull"}); + _ = try d.type.update(p, d.path); } if (d.version.len > 0) { const iter = &std.mem.split(d.version, "-"); @@ -88,19 +87,18 @@ fn fetch_deps(dir: []const u8, mpath: []const u8) anyerror!u.Module { const ref = iter.rest(); if (try u.does_file_exist(pv)) { if (v_type == .branch) { - _ = try run_cmd(pv, &[_][]const u8{"git", "fetch"}); - _ = try run_cmd(pv, &[_][]const u8{"git", "pull"}); + try d.type.update(p, d.path); } moddir = pv; break :blk; } - if ((try run_cmd(p, &[_][]const u8{"git", "checkout", ref})) > 0) { + if ((try u.run_cmd(p, &[_][]const u8{"git", "checkout", ref})) > 0) { u.assert(false, "fetch: git: {}: {} {} does not exist", .{d.path, @tagName(v_type), ref}); } else { - _ = try run_cmd(p, &[_][]const u8{"git", "checkout", "-"}); + _ = try u.run_cmd(p, &[_][]const u8{"git", "checkout", "-"}); } - _ = try run_cmd(null, &[_][]const u8{"git", "clone", d.path, pv}); - _ = try run_cmd(pv, &[_][]const u8{"git", "checkout", ref}); + _ = try u.run_cmd(null, &[_][]const u8{"git", "clone", d.path, pv}); + _ = try u.run_cmd(pv, &[_][]const u8{"git", "checkout", ref}); if (v_type != .branch) { const pvd = try u.open_dir_absolute(pv); try pvd.deleteTree(".git"); @@ -114,10 +112,10 @@ fn fetch_deps(dir: []const u8, mpath: []const u8) anyerror!u.Module { }, .hg => { if (!try u.does_file_exist(p)) { - _= try run_cmd(null, &[_][]const u8{"hg", "clone", d.path, p}); + _= try u.run_cmd(null, &[_][]const u8{"hg", "clone", d.path, p}); } else { - _= try run_cmd(p, &[_][]const u8{"hg", "pull"}); + _= try u.run_cmd(p, &[_][]const u8{"hg", "pull"}); } }, } @@ -157,17 +155,6 @@ fn fetch_deps(dir: []const u8, mpath: []const u8) anyerror!u.Module { }; } -fn run_cmd(dir: ?[]const u8, args: []const []const u8) !u32 { - const result = std.ChildProcess.exec(.{ .allocator = gpa, .cwd = dir, .argv = args, }) catch |e| switch(e) { - error.FileNotFound => { - u.assert(false, "\"{}\" command not found", .{args[0]}); - unreachable; - }, - else => return e, - }; - return result.term.Exited; -} - fn print_deps(w: std.fs.File.Writer, dir: []const u8, m: u.Module, tabs: i32) anyerror!void { if (m.deps.len == 0 and tabs > 0) { try w.print("null", .{}); diff --git a/src/util/dep_type.zig b/src/util/dep_type.zig index ed3018306a8e66e88a3a9f32f52de6f9983f268e..201b5d996831636211b9bbaf008f5183b4dc5ea0 100644 --- a/src/util/dep_type.zig +++ b/src/util/dep_type.zig @@ -1,9 +1,34 @@ +const u = @import("./index.zig"); + // // pub const DepType = enum { git, // https://git-scm.com/ hg, // https://www.mercurial-scm.org/ + + pub fn pull(self: DepType, rpath: []const u8, dpath: []const u8) !void { + switch (self) { + .git => { + _ = try u.run_cmd(null, &[_][]const u8{"git", "clone", rpath, dpath}); + }, + else => { + u.assert(false, "dep type {} not configured for pull: {}", .{@tagName(self), rpath}); + }, + } + } + + pub fn update(self: DepType, dpath: []const u8, rpath: []const u8) !void { + switch (self) { + .git => { + _ = try u.run_cmd(dpath, &[_][]const u8{"git", "fetch"}); + _ = try u.run_cmd(dpath, &[_][]const u8{"git", "pull"}); + }, + else => { + u.assert(false, "dep type {} not configured for update: {}", .{@tagName(self), rpath}); + }, + } + } }; pub const GitVersionType = enum { diff --git a/src/util/funcs.zig b/src/util/funcs.zig index 40703c648c49dd11cfeeba672fb2cefe744d9e87..840c4d478f24176d4c17a5a63f4d074c41740a39 100644 --- a/src/util/funcs.zig +++ b/src/util/funcs.zig @@ -145,3 +145,14 @@ pub fn file_list(dpath: []const u8, list: *std.ArrayList([]const u8)) !void { } } } + +pub fn run_cmd(dir: ?[]const u8, args: []const []const u8) !u32 { + const result = std.ChildProcess.exec(.{ .allocator = gpa, .cwd = dir, .argv = args, }) catch |e| switch(e) { + error.FileNotFound => { + u.assert(false, "\"{}\" command not found", .{args[0]}); + unreachable; + }, + else => return e, + }; + return result.term.Exited; +}