This repository was archived by the owner on Oct 16, 2022. It is now read-only.
forked from bootradev/zig-fetch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch.zig
More file actions
464 lines (396 loc) · 15.8 KB
/
Copy pathfetch.zig
File metadata and controls
464 lines (396 loc) · 15.8 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
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
// 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,
shallow_branch: ?[]const u8 = null,
};
pub const Dependency = struct {
name: []const u8,
vcs: union(enum) {
git: GitDependency,
},
recursive_fetch: bool = true,
};
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,
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,
};
const git_available = checkGitAvailable(builder);
if (!fetch_skip) {
const fetch_cache = try readFetchCache(builder);
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 });
switch (dep.vcs) {
.git => |git_dep| {
if (!git_available) {
return error.GitNotAvailable;
}
const git_fetch = try GitFetch.init(builder, fetch_dir, git_dep);
if (dep.recursive_fetch) {
const recursive_fetch = try RecursiveFetch.init(builder, fetch_dir, fetch_force);
fetch_and_build.step.dependOn(&recursive_fetch.step);
recursive_fetch.step.dependOn(&git_fetch.step);
} else {
fetch_and_build.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(builder, 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) []const u8 {
return builder.pathJoin(&.{ builder.build_root, builder.cache_root, "fetch_cache" });
}
fn readFetchCache(builder: *std.build.Builder) !?[]const Dependency {
const cache_path = getFetchCachePath(builder);
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(builder: *std.build.Builder, deps: []const Dependency) !void {
const cache_path = getFetchCachePath(builder);
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;
const result = try runChildProcessExec(builder, recursive_fetch.dir, build_args);
defer builder.allocator.free(result.stdout);
defer builder.allocator.free(result.stderr);
try logChildProcessOutput(result.stdout);
// only log error if it's not related to missing zig-fetch functionality
if (!std.mem.startsWith(u8, result.stderr, "error: Invalid option: -Dfetch-only")) {
try logChildProcessOutput(result.stderr);
}
} else |_| {}
}
};
pub 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;
// TODO: the logging behavior here suppresses important Git progress reports, like:
//
// remote: Enumerating objects: 34369, done.
// remote: Counting objects: 100% (34369/34369), done.
// remote: Compressing objects: 100% (14619/14619), done.
// remote: Total 34369 (delta 20763), reused 30144 (delta 19405), pack-reused 0
// Receiving objects: 100% (34369/34369), 56.13 MiB | 21.65 MiB/s, done.
// Resolving deltas: 100% (20763/20763), done.
//
std.log.info("fetching from git into {s}...", .{git_fetch.dir});
std.fs.accessAbsolute(git_fetch.dir, .{}) catch {
const clone_args: []const []const u8 = if (git_fetch.dep.shallow_branch) |branch|
&.{ "git", "clone", "--depth", "1", "-b", branch, git_fetch.dep.url, git_fetch.dir }
else
&.{ "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);
}
// TODO: zig-fetch does not currently correctly handle updating the git repo if it already
// exists and the revision has changed. It should run something akin to `git fetch` (adding
// `--depth 1` if a shallow clone) followed by `git checkout`. This is a network access,
// though, so should be ran rarely.
if (git_fetch.dep.shallow_branch) |_| {
// Works around an issue where a shallow clone gets the last commit, but that commit is
// in fact not the one we want (it's a newer one and we want an older one) e.g.:
// fatal: reference is not a tree: fff6ea92a00c5f6092b896d754a932b8b88149ff
const args = &.{ "git", "fetch", "--depth", "1", "origin", git_fetch.dep.commit };
try runChildProcess(builder, git_fetch.dir, 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);
var child_process = std.ChildProcess.init(args, builder.allocator);
child_process.cwd = cwd;
child_process.env_map = builder.env_map;
child_process.stdin_behavior = .Ignore;
if (!log_output) {
child_process.stdout_behavior = .Ignore;
child_process.stderr_behavior = .Ignore;
}
switch (try child_process.spawnAndWait()) {
.Exited => |code| if (code != 0) {
return error.RunChildProcessFailed;
},
else => {
return error.RunChildProcessFailed;
},
}
}
fn runChildProcessExec(
builder: *std.build.Builder,
cwd: []const u8,
args: []const []const u8,
) !std.ChildProcess.ExecResult {
try logCommand(builder, args);
return try std.ChildProcess.exec(.{
.allocator = builder.allocator,
.argv = args,
.cwd = cwd,
.env_map = builder.env_map,
});
}
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});
}
}
fn logChildProcessOutput(output: []const u8) !void {
try std.io.getStdOut().writer().writeAll(output);
}
pub fn dependencyEql(a: Dependency, b: Dependency) bool {
return std.mem.eql(u8, a.name, b.name) 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;
}
}