-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfetch.zig
420 lines (357 loc) · 13.8 KB
/
fetch.zig
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
// fetch.zig - a dependency management solution for zig projects!
// see the repo at https://github.com/bootradev/zig-fetch for more info
const std = @import("std");
// adds a step that will be passed through to the build file
pub fn addStep(
builder: *std.build.Builder,
name: []const u8,
description: []const u8,
) void {
builder.step(name, description).dependOn(builder.getInstallStep());
}
// adds an option that will be passed through to the build file
pub fn addOption(
builder: *std.build.Builder,
comptime T: type,
name: []const u8,
description: []const u8,
) void {
_ = builder.option(T, name, description);
}
pub const GitDependency = struct {
url: []const u8,
commit: []const u8,
recursive: bool = false, // set to true to have this dependency fetch git submodules
};
pub const Dependency = struct {
name: []const u8,
recursive: bool = false, // set to true when the dependency also uses zig-fetch
vcs: union(enum) {
git: GitDependency,
},
};
pub fn fetchAndBuild(
builder: *std.build.Builder,
deps_dir: []const u8,
deps: []const Dependency,
build_file: []const u8,
) !void {
// no-op standard options to pass through to build file
_ = builder.standardTargetOptions(.{});
_ = builder.standardReleaseOptions();
const fetch_and_build = try FetchAndBuild.init(builder, deps_dir, deps, build_file);
builder.getInstallStep().dependOn(&fetch_and_build.step);
}
const FetchAndBuild = struct {
builder: *std.build.Builder,
step: std.build.Step,
deps: []const Dependency,
build_file: []const u8,
write_fetch_cache: bool,
run_zig_build: bool,
fetch_cache_path: []const u8,
fn init(
builder: *std.build.Builder,
deps_dir: []const u8,
deps: []const Dependency,
build_file: []const u8,
) !*FetchAndBuild {
const fetch_skip = builder.option(
bool,
"fetch-skip",
"Skip fetch dependencies",
) orelse false;
const fetch_only = builder.option(
bool,
"fetch-only",
"Only fetch dependencies",
) orelse false;
const fetch_force = builder.option(
bool,
"fetch-force",
"Force fetch dependencies",
) orelse false;
if (fetch_skip and fetch_only) {
std.log.err("fetch-skip and fetch-only are mutually exclusive!", .{});
return error.InvalidOptions;
}
var fetch_and_build = try builder.allocator.create(FetchAndBuild);
fetch_and_build.* = .{
.builder = builder,
.step = std.build.Step.init(.custom, "fetch and build", builder.allocator, make),
.deps = try builder.allocator.dupe(Dependency, deps),
.build_file = builder.dupe(build_file),
.write_fetch_cache = false,
.run_zig_build = !fetch_only,
.fetch_cache_path = getFetchCachePath(builder, deps_dir),
};
const git_available = checkGitAvailable(builder);
if (!fetch_skip) {
const fetch_cache = try readFetchCache(builder, fetch_and_build.fetch_cache_path);
for (deps) |dep| {
if (!fetch_force) {
if (fetch_cache) |cache| {
var dep_in_cache = false;
for (cache) |cache_dep| {
if (dependencyEql(dep, cache_dep)) {
dep_in_cache = true;
break;
}
}
if (dep_in_cache) {
continue;
}
}
}
const fetch_dir = builder.pathJoin(&.{ builder.build_root, deps_dir, dep.name });
var fetch_step = &fetch_and_build.step;
if (dep.recursive) {
const recursive = try RecursiveFetch.init(builder, fetch_dir, fetch_force);
fetch_step.dependOn(&recursive.step);
fetch_step = &recursive.step;
}
switch (dep.vcs) {
.git => |git_dep| {
if (!git_available) {
return error.GitNotAvailable;
}
const git_fetch = try GitFetch.init(builder, fetch_dir, git_dep);
fetch_step.dependOn(&git_fetch.step);
},
}
fetch_and_build.write_fetch_cache = true;
}
}
return fetch_and_build;
}
fn make(step: *std.build.Step) !void {
const fetch_and_build = @fieldParentPtr(FetchAndBuild, "step", step);
const builder = fetch_and_build.builder;
if (fetch_and_build.write_fetch_cache) {
try writeFetchCache(fetch_and_build.fetch_cache_path, fetch_and_build.deps);
}
if (fetch_and_build.run_zig_build) {
const args = try std.process.argsAlloc(builder.allocator);
defer std.process.argsFree(builder.allocator, args);
// TODO: this might be platform specific.
// on windows, 5 args are prepended before the user defined args
const args_offset = 5;
var build_args_list = std.ArrayList([]const u8).init(builder.allocator);
defer build_args_list.deinit();
try build_args_list.appendSlice(
&.{ "zig", "build", "--build-file", fetch_and_build.build_file },
);
for (args[args_offset..]) |arg| {
if (std.mem.startsWith(u8, arg, "-Dfetch-skip=") or
std.mem.startsWith(u8, arg, "-Dfetch-only=") or
std.mem.startsWith(u8, arg, "-Dfetch-force="))
{
continue;
}
try build_args_list.append(arg);
}
if (fetch_and_build.write_fetch_cache or builder.verbose) {
std.log.info("building with build file {s}...", .{fetch_and_build.build_file});
}
const build_args = build_args_list.items;
runChildProcess(builder, builder.build_root, build_args, true) catch return;
}
}
};
fn getFetchCachePath(builder: *std.build.Builder, deps_dir: []const u8) []const u8 {
return builder.pathJoin(&.{ builder.build_root, deps_dir, "fetch_cache" });
}
fn readFetchCache(builder: *std.build.Builder, cache_path: []const u8) !?[]const Dependency {
const cache_file = std.fs.cwd().openFile(cache_path, .{}) catch return null;
defer cache_file.close();
const reader = cache_file.reader();
var dependencies = std.ArrayList(Dependency).init(builder.allocator);
var read_buf: [256]u8 = undefined;
while (true) {
const name = builder.dupe(reader.readUntilDelimiter(&read_buf, '\n') catch |e| {
if (e == error.EndOfStream) {
break;
} else {
return e;
}
});
var dependency: Dependency = undefined;
dependency.name = name;
const vcs_type = try reader.readUntilDelimiter(&read_buf, '\n');
if (std.mem.eql(u8, vcs_type, "git")) {
const url = builder.dupe(try reader.readUntilDelimiter(&read_buf, '\n'));
const commit = builder.dupe(try reader.readUntilDelimiter(&read_buf, '\n'));
const recursive = try parseBool(try reader.readUntilDelimiter(&read_buf, '\n'));
dependency.vcs = .{
.git = .{
.url = url,
.commit = commit,
.recursive = recursive,
},
};
} else {
return error.InvalidVcsType;
}
try dependencies.append(dependency);
}
return dependencies.toOwnedSlice();
}
fn writeFetchCache(cache_path: []const u8, deps: []const Dependency) !void {
try std.fs.cwd().makePath(std.fs.path.dirname(cache_path) orelse unreachable);
const cache_file = try std.fs.cwd().createFile(cache_path, .{});
const writer = cache_file.writer();
for (deps) |dep| {
try writer.print("{s}\n", .{dep.name});
switch (dep.vcs) {
.git => |git_dep| {
try writer.print("git\n", .{});
try writer.print("{s}\n", .{git_dep.url});
try writer.print("{s}\n", .{git_dep.commit});
try writer.print("{}\n", .{git_dep.recursive});
},
}
}
}
const RecursiveFetch = struct {
builder: *std.build.Builder,
step: std.build.Step,
dir: []const u8,
fetch_force: bool,
pub fn init(
builder: *std.build.Builder,
dir: []const u8,
fetch_force: bool,
) !*RecursiveFetch {
var recursive_fetch = try builder.allocator.create(RecursiveFetch);
recursive_fetch.* = .{
.builder = builder,
.step = std.build.Step.init(.custom, "recursive fetch", builder.allocator, make),
.dir = dir,
.fetch_force = fetch_force,
};
return recursive_fetch;
}
pub fn make(step: *std.build.Step) !void {
const recursive_fetch = @fieldParentPtr(RecursiveFetch, "step", step);
const builder = recursive_fetch.builder;
var dir = try std.fs.openDirAbsolute(recursive_fetch.dir, .{});
defer dir.close();
if (dir.openFile("build.zig", .{})) |file| {
file.close();
std.log.info("recursively fetching within {s}...", .{recursive_fetch.dir});
var build_args_list = std.ArrayList([]const u8).init(builder.allocator);
defer build_args_list.deinit();
try build_args_list.appendSlice(&.{ "zig", "build", "-Dfetch-only=true" });
if (builder.verbose) {
try build_args_list.append("--verbose");
}
if (recursive_fetch.fetch_force) {
try build_args_list.append("-Dfetch-force=true");
}
const build_args = build_args_list.items;
try runChildProcess(builder, recursive_fetch.dir, build_args, true);
} else |_| {}
}
};
const GitFetch = struct {
builder: *std.build.Builder,
step: std.build.Step,
dep: GitDependency,
dir: []const u8,
pub fn init(
builder: *std.build.Builder,
dir: []const u8,
dep: GitDependency,
) !*GitFetch {
var git_fetch = try builder.allocator.create(GitFetch);
git_fetch.* = .{
.builder = builder,
.step = std.build.Step.init(.custom, "git fetch", builder.allocator, make),
.dep = dep,
.dir = dir,
};
return git_fetch;
}
pub fn make(step: *std.build.Step) !void {
const git_fetch = @fieldParentPtr(GitFetch, "step", step);
const builder = git_fetch.builder;
std.log.info("fetching from git into {s}...", .{git_fetch.dir});
var dir_exists = true;
std.fs.accessAbsolute(git_fetch.dir, .{}) catch {
dir_exists = false;
};
if (dir_exists) {
const fetch_args = &.{ "git", "fetch" };
try runChildProcess(builder, builder.build_root, fetch_args, builder.verbose);
} else {
const clone_args = &.{ "git", "clone", git_fetch.dep.url, git_fetch.dir };
try runChildProcess(builder, builder.build_root, clone_args, builder.verbose);
}
if (git_fetch.dep.recursive) {
const submodule_args = &.{ "git", "submodule", "update", "--init", "--recursive" };
try runChildProcess(builder, git_fetch.dir, submodule_args, builder.verbose);
}
const checkout_args = &.{ "git", "checkout", git_fetch.dep.commit };
try runChildProcess(builder, git_fetch.dir, checkout_args, builder.verbose);
}
};
fn checkGitAvailable(builder: *std.build.Builder) bool {
const args = &.{ "git", "--version" };
runChildProcess(builder, builder.build_root, args, builder.verbose) catch return false;
return true;
}
fn runChildProcess(
builder: *std.build.Builder,
cwd: []const u8,
args: []const []const u8,
log_output: bool,
) !void {
try logCommand(builder, args);
const result = try std.ChildProcess.exec(.{
.allocator = builder.allocator,
.argv = args,
.cwd = cwd,
.env_map = builder.env_map,
});
defer builder.allocator.free(result.stdout);
defer builder.allocator.free(result.stderr);
const err = result.term != .Exited or result.term.Exited != 0;
if (log_output or err) {
try std.io.getStdOut().writer().writeAll(result.stdout);
try std.io.getStdErr().writer().writeAll(result.stderr);
}
if (err) {
return error.RunChildProcessFailed;
}
}
fn logCommand(builder: *std.build.Builder, args: []const []const u8) !void {
if (builder.verbose) {
var command = std.ArrayList(u8).init(builder.allocator);
defer command.deinit();
try command.appendSlice("RUNNING COMMAND:");
for (args) |arg| {
try command.append(' ');
try command.appendSlice(arg);
}
std.log.info("{s}", .{command.items});
}
}
pub fn dependencyEql(a: Dependency, b: Dependency) bool {
return std.mem.eql(u8, a.name, b.name) and
a.recursive == b.recursive and
std.meta.activeTag(a.vcs) == std.meta.activeTag(b.vcs) and
switch (a.vcs) {
.git => std.mem.eql(u8, a.vcs.git.url, b.vcs.git.url) and
std.mem.eql(u8, a.vcs.git.commit, b.vcs.git.commit) and
a.vcs.git.recursive == b.vcs.git.recursive,
};
}
fn parseBool(str: []const u8) !bool {
if (std.mem.eql(u8, str, "true")) {
return true;
} else if (std.mem.eql(u8, str, "false")) {
return false;
} else {
return error.ParseBoolFailed;
}
}