authorgravatar for hello@nektro.netMeghan <hello@nektro.net> 2020-11-28 13:09:22 -08:00
committergravatar for hello@nektro.netMeghan <hello@nektro.net> 2020-11-28 13:09:22 -08:00
logde608bf1d26bc8b5ac39c6e657d5d9b70cb0c9b0
tree364c2cd0681317b7d2093b79c1eb5a3589b3bc6b
parent27c6c3754ca8e00b61371c3ea10e1efa38767183

add pull and update methods to deptype


3 files changed, 45 insertions(+), 22 deletions(-)

src/cmd_fetch.zig+9-22
......@@ -75,11 +75,10 @@ fn fetch_deps(dir: []const u8, mpath: []const u8) anyerror!u.Module {
7575 switch (d.type) {
7676 .git => blk: {
7777 if (!try u.does_file_exist(p)) {
78 _ = try run_cmd(null, &[_][]const u8{"git", "clone", d.path, p});
78 _ = try d.type.pull(d.path, p);
7979 }
8080 else {
81 _ = try run_cmd(p, &[_][]const u8{"git", "fetch"});
82 _ = try run_cmd(p, &[_][]const u8{"git", "pull"});
81 _ = try d.type.update(p, d.path);
8382 }
8483 if (d.version.len > 0) {
8584 const iter = &std.mem.split(d.version, "-");
......@@ -88,19 +87,18 @@ fn fetch_deps(dir: []const u8, mpath: []const u8) anyerror!u.Module {
8887 const ref = iter.rest();
8988 if (try u.does_file_exist(pv)) {
9089 if (v_type == .branch) {
91 _ = try run_cmd(pv, &[_][]const u8{"git", "fetch"});
92 _ = try run_cmd(pv, &[_][]const u8{"git", "pull"});
90 try d.type.update(p, d.path);
9391 }
9492 moddir = pv;
9593 break :blk;
9694 }
97 if ((try run_cmd(p, &[_][]const u8{"git", "checkout", ref})) > 0) {
95 if ((try u.run_cmd(p, &[_][]const u8{"git", "checkout", ref})) > 0) {
9896 u.assert(false, "fetch: git: {}: {} {} does not exist", .{d.path, @tagName(v_type), ref});
9997 } else {
100 _ = try run_cmd(p, &[_][]const u8{"git", "checkout", "-"});
98 _ = try u.run_cmd(p, &[_][]const u8{"git", "checkout", "-"});
10199 }
102 _ = try run_cmd(null, &[_][]const u8{"git", "clone", d.path, pv});
103 _ = try run_cmd(pv, &[_][]const u8{"git", "checkout", ref});
100 _ = try u.run_cmd(null, &[_][]const u8{"git", "clone", d.path, pv});
101 _ = try u.run_cmd(pv, &[_][]const u8{"git", "checkout", ref});
104102 if (v_type != .branch) {
105103 const pvd = try u.open_dir_absolute(pv);
106104 try pvd.deleteTree(".git");
......@@ -114,10 +112,10 @@ fn fetch_deps(dir: []const u8, mpath: []const u8) anyerror!u.Module {
114112 },
115113 .hg => {
116114 if (!try u.does_file_exist(p)) {
117 _= try run_cmd(null, &[_][]const u8{"hg", "clone", d.path, p});
115 _= try u.run_cmd(null, &[_][]const u8{"hg", "clone", d.path, p});
118116 }
119117 else {
120 _= try run_cmd(p, &[_][]const u8{"hg", "pull"});
118 _= try u.run_cmd(p, &[_][]const u8{"hg", "pull"});
121119 }
122120 },
123121 }
......@@ -157,17 +155,6 @@ fn fetch_deps(dir: []const u8, mpath: []const u8) anyerror!u.Module {
157155 };
158156}
159157
160fn run_cmd(dir: ?[]const u8, args: []const []const u8) !u32 {
161 const result = std.ChildProcess.exec(.{ .allocator = gpa, .cwd = dir, .argv = args, }) catch |e| switch(e) {
162 error.FileNotFound => {
163 u.assert(false, "\"{}\" command not found", .{args[0]});
164 unreachable;
165 },
166 else => return e,
167 };
168 return result.term.Exited;
169}
170
171158fn print_deps(w: std.fs.File.Writer, dir: []const u8, m: u.Module, tabs: i32) anyerror!void {
172159 if (m.deps.len == 0 and tabs > 0) {
173160 try w.print("null", .{});
src/util/dep_type.zig+25
......@@ -1,9 +1,34 @@
1const u = @import("./index.zig");
2
13//
24//
35
46pub const DepType = enum {
57 git, // https://git-scm.com/
68 hg, // https://www.mercurial-scm.org/
9
10 pub fn pull(self: DepType, rpath: []const u8, dpath: []const u8) !void {
11 switch (self) {
12 .git => {
13 _ = try u.run_cmd(null, &[_][]const u8{"git", "clone", rpath, dpath});
14 },
15 else => {
16 u.assert(false, "dep type {} not configured for pull: {}", .{@tagName(self), rpath});
17 },
18 }
19 }
20
21 pub fn update(self: DepType, dpath: []const u8, rpath: []const u8) !void {
22 switch (self) {
23 .git => {
24 _ = try u.run_cmd(dpath, &[_][]const u8{"git", "fetch"});
25 _ = try u.run_cmd(dpath, &[_][]const u8{"git", "pull"});
26 },
27 else => {
28 u.assert(false, "dep type {} not configured for update: {}", .{@tagName(self), rpath});
29 },
30 }
31 }
732};
833
934pub const GitVersionType = enum {
src/util/funcs.zig+11
......@@ -145,3 +145,14 @@ pub fn file_list(dpath: []const u8, list: *std.ArrayList([]const u8)) !void {
145145 }
146146 }
147147}
148
149pub fn run_cmd(dir: ?[]const u8, args: []const []const u8) !u32 {
150 const result = std.ChildProcess.exec(.{ .allocator = gpa, .cwd = dir, .argv = args, }) catch |e| switch(e) {
151 error.FileNotFound => {
152 u.assert(false, "\"{}\" command not found", .{args[0]});
153 unreachable;
154 },
155 else => return e,
156 };
157 return result.term.Exited;
158}