-
-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathupdate.zig
More file actions
72 lines (68 loc) · 2.56 KB
/
update.zig
File metadata and controls
72 lines (68 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const std = @import("std");
const Allocator = std.mem.Allocator;
const Child = std.process.Child;
// url must be something like: https://github.com/nat3Github/zig-lib-dvui-dev-fork
// branch must be something like main or dev
pub fn get_hash(alloc: Allocator, url: []const u8, branch: []const u8) ![]const u8 {
const get_commit = &.{ "git", "ls-remote", url };
const dat = try exec(alloc, get_commit);
var tokenizer = std.mem.tokenizeAny(u8, dat, "\r\n");
var hash: []const u8 = "";
const refs_heads = "refs/heads/";
var arlist = std.array_list.Managed([]const u8).init(alloc);
defer arlist.deinit();
while (tokenizer.next()) |token| {
hash = token[0..40];
var ref = std.mem.trim(u8, token[40..], " \t");
if (std.ascii.startsWithIgnoreCase(ref, refs_heads)) ref = ref[refs_heads.len..];
if (std.mem.eql(u8, branch, ref)) return alloc.dupe(u8, hash);
try arlist.append(ref);
}
const branches = arlist.items;
std.log.err("url: {s} BRANCH: '{s}' NOT FOUND", .{ url, branch });
std.log.info("there are {} other branches:", .{branches.len});
for (branches[0..@min(10, branches.len)]) |s| {
std.log.info("{s}", .{s});
}
return error.BranchNotFound;
}
pub fn get_zig_fetch_repo_string(alloc: Allocator, url: []const u8, branch: []const u8) ![]const u8 {
const hash = try get_hash(alloc, url, branch);
const repo = try std.fmt.allocPrint(alloc, "git+{s}#{s}", .{ url, hash });
return repo;
}
pub const GitDependency = struct {
url: []const u8,
branch: []const u8,
};
pub fn update_dependency(alloc: Allocator, deps: []const GitDependency) !void {
for (deps) |dep| {
const rep = try get_zig_fetch_repo_string(alloc, dep.url, dep.branch);
std.log.info("running zig fetch --save {s}", .{rep});
_ = try exec(alloc, &.{
"zig",
"fetch",
"--save",
rep,
});
}
std.log.info("ok", .{});
}
pub fn exec(alloc: Allocator, args: []const []const u8) ![]const u8 {
var caller = Child.init(args, alloc);
caller.stdout_behavior = .Pipe;
caller.stderr_behavior = .Pipe;
var stdout = std.ArrayListUnmanaged(u8){};
var stderr = std.ArrayListUnmanaged(u8){};
errdefer stdout.deinit(alloc);
defer stderr.deinit(alloc);
try caller.spawn();
try caller.collectOutput(alloc, &stdout, &stderr, 1024 * 1024);
const res = try caller.wait();
if (res.Exited > 0) {
std.log.err("{s}\n", .{stderr.items});
return error.Failed;
} else {
return stdout.items;
}
}