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 {...@@ -75,11 +75,10 @@ fn fetch_deps(dir: []const u8, mpath: []const u8) anyerror!u.Module {
75 switch (d.type) {75 switch (d.type) {
76 .git => blk: {76 .git => blk: {
77 if (!try u.does_file_exist(p)) {77 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);
79 }79 }
80 else {80 else {
81 _ = try run_cmd(p, &[_][]const u8{"git", "fetch"});81 _ = try d.type.update(p, d.path);
82 _ = try run_cmd(p, &[_][]const u8{"git", "pull"});
83 }82 }
84 if (d.version.len > 0) {83 if (d.version.len > 0) {
85 const iter = &std.mem.split(d.version, "-");84 const iter = &std.mem.split(d.version, "-");
...@@ -88,19 +87,18 @@ fn fetch_deps(dir: []const u8, mpath: []const u8) anyerror!u.Module {...@@ -88,19 +87,18 @@ fn fetch_deps(dir: []const u8, mpath: []const u8) anyerror!u.Module {
88 const ref = iter.rest();87 const ref = iter.rest();
89 if (try u.does_file_exist(pv)) {88 if (try u.does_file_exist(pv)) {
90 if (v_type == .branch) {89 if (v_type == .branch) {
91 _ = try run_cmd(pv, &[_][]const u8{"git", "fetch"});90 try d.type.update(p, d.path);
92 _ = try run_cmd(pv, &[_][]const u8{"git", "pull"});
93 }91 }
94 moddir = pv;92 moddir = pv;
95 break :blk;93 break :blk;
96 }94 }
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) {
98 u.assert(false, "fetch: git: {}: {} {} does not exist", .{d.path, @tagName(v_type), ref});96 u.assert(false, "fetch: git: {}: {} {} does not exist", .{d.path, @tagName(v_type), ref});
99 } else {97 } else {
100 _ = try run_cmd(p, &[_][]const u8{"git", "checkout", "-"});98 _ = try u.run_cmd(p, &[_][]const u8{"git", "checkout", "-"});
101 }99 }
102 _ = try run_cmd(null, &[_][]const u8{"git", "clone", d.path, pv});100 _ = try u.run_cmd(null, &[_][]const u8{"git", "clone", d.path, pv});
103 _ = try run_cmd(pv, &[_][]const u8{"git", "checkout", ref});101 _ = try u.run_cmd(pv, &[_][]const u8{"git", "checkout", ref});
104 if (v_type != .branch) {102 if (v_type != .branch) {
105 const pvd = try u.open_dir_absolute(pv);103 const pvd = try u.open_dir_absolute(pv);
106 try pvd.deleteTree(".git");104 try pvd.deleteTree(".git");
...@@ -114,10 +112,10 @@ fn fetch_deps(dir: []const u8, mpath: []const u8) anyerror!u.Module {...@@ -114,10 +112,10 @@ fn fetch_deps(dir: []const u8, mpath: []const u8) anyerror!u.Module {
114 },112 },
115 .hg => {113 .hg => {
116 if (!try u.does_file_exist(p)) {114 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});
118 }116 }
119 else {117 else {
120 _= try run_cmd(p, &[_][]const u8{"hg", "pull"});118 _= try u.run_cmd(p, &[_][]const u8{"hg", "pull"});
121 }119 }
122 },120 },
123 }121 }
...@@ -157,17 +155,6 @@ fn fetch_deps(dir: []const u8, mpath: []const u8) anyerror!u.Module {...@@ -157,17 +155,6 @@ fn fetch_deps(dir: []const u8, mpath: []const u8) anyerror!u.Module {
157 };155 };
158}156}
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
171fn print_deps(w: std.fs.File.Writer, dir: []const u8, m: u.Module, tabs: i32) anyerror!void {158fn print_deps(w: std.fs.File.Writer, dir: []const u8, m: u.Module, tabs: i32) anyerror!void {
172 if (m.deps.len == 0 and tabs > 0) {159 if (m.deps.len == 0 and tabs > 0) {
173 try w.print("null", .{});160 try w.print("null", .{});
src/util/dep_type.zig+25
...@@ -1,9 +1,34 @@...@@ -1,9 +1,34 @@
1const u = @import("./index.zig");
2
1//3//
2//4//
35
4pub const DepType = enum {6pub const DepType = enum {
5 git, // https://git-scm.com/7 git, // https://git-scm.com/
6 hg, // https://www.mercurial-scm.org/8 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 }
7};32};
833
9pub const GitVersionType = enum {34pub 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 {...@@ -145,3 +145,14 @@ pub fn file_list(dpath: []const u8, list: *std.ArrayList([]const u8)) !void {
145 }145 }
146 }146 }
147}147}
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}