1const std = @import("std");
2const string = []const u8;
3const nio = @import("nio");
4const nfs = @import("nfs");
5
6const u = @import("./funcs.zig");
7
8//
9//
10
11// zig fmt: off
12pub const DepType = enum {
13 local, // A 'package' derived from files in the same repository.
14 system_lib, // std.build.LibExeObjStep.linkSystemLibrary
15 framework, // std.build.LibExeObjStep.linkFramework
16 git, // https://git-scm.com/
17 hg, // https://www.mercurial-scm.org/
18 http, // https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol
19
20 // zig fmt: on
21 pub fn pull(self: DepType, alloc: std.mem.Allocator, rpath: [:0]const u8, dpath: [:0]const u8) !void {
22 switch (self) {
23 .local => {},
24 .system_lib => {},
25 .framework => {},
26 .git => {
27 u.assert((try u.run_cmd(alloc, null, &.{ "git", "clone", "--recurse-submodules", rpath, dpath })) == 0, "git clone {s} failed", .{rpath});
28 },
29 .hg => {
30 u.assert((try u.run_cmd(alloc, null, &.{ "hg", "clone", rpath, dpath })) == 0, "hg clone {s} failed", .{rpath});
31 },
32 .http => {
33 try nfs.cwd().makePath(dpath);
34 u.assert((try u.run_cmd(alloc, dpath, &.{ "wget", rpath })) == 0, "wget {s} failed", .{rpath});
35 const f = rpath[std.mem.lastIndexOf(u8, rpath, "/").? + 1 ..];
36 if (std.mem.endsWith(u8, f, ".zip")) {
37 u.assert((try u.run_cmd(alloc, dpath, &.{ "unzip", f, "-d", "." })) == 0, "unzip {s} failed", .{f});
38 }
39 if (std.mem.endsWith(u8, f, ".tar") or std.mem.endsWith(u8, f, ".tar.gz") or std.mem.endsWith(u8, f, ".tar.xz") or std.mem.endsWith(u8, f, ".tar.zst")) {
40 u.assert((try u.run_cmd(alloc, dpath, &.{ "tar", "-xf", f, "-C", "." })) == 0, "un-tar {s} failed", .{f});
41 }
42 },
43 }
44 }
45
46 pub fn update(self: DepType, alloc: std.mem.Allocator, dpath: string, rpath: string) !void {
47 _ = rpath;
48
49 switch (self) {
50 .local => {},
51 .system_lib => {},
52 .framework => {},
53 .git => {
54 u.assert((try u.run_cmd(alloc, dpath, &.{ "git", "fetch" })) == 0, "git fetch failed", .{});
55 u.assert((try u.run_cmd(alloc, dpath, &.{ "git", "pull" })) == 0, "git pull failed", .{});
56 },
57 .hg => {
58 u.assert((try u.run_cmd(alloc, dpath, &.{ "hg", "pull" })) == 0, "hg pull failed", .{});
59 },
60 .http => {
61 //
62 },
63 }
64 }
65
66 pub fn exact_version(self: DepType, alloc: std.mem.Allocator, mpath: [:0]const u8) !string {
67 var mdir = try nfs.cwd().openDir(mpath, .{});
68 defer mdir.close();
69 return switch (self) {
70 .local => "",
71 .system_lib => "",
72 .framework => "",
73 .git => try nio.fmt.allocPrint(alloc, "commit-{s}", .{(try u.git_rev_HEAD(alloc, mdir))}),
74 .hg => "",
75 .http => "",
76 };
77 }
78
79 pub fn isLocal(self: DepType) bool {
80 return switch (self) {
81 .local => true,
82 .system_lib => true,
83 .framework => true,
84 .git => false,
85 .hg => false,
86 .http => false,
87 };
88 }
89
90 pub const Version = union(DepType) {
91 local: void,
92 system_lib: void,
93 framework: void,
94 git: Git,
95 hg: void,
96 http: void,
97
98 pub const Git = enum {
99 branch,
100 tag,
101 commit,
102
103 pub fn frozen(self: Git) bool {
104 return switch (self) {
105 .branch => false,
106 .tag => true,
107 .commit => true,
108 };
109 }
110 };
111 };
112};