-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathDirectoryFileInput.zig
More file actions
138 lines (124 loc) · 5.02 KB
/
DirectoryFileInput.zig
File metadata and controls
138 lines (124 loc) · 5.02 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
//! DirectoryFileInput adds files within a directory to the dependencies of the given Step.Run command
//! This is required so that generated directories will work.
const androidbuild = @import("androidbuild.zig");
const builtin = @import("builtin");
const Build = @import("std").Build;
const Step = Build.Step;
const Run = Build.Step.Run;
const LazyPath = Build.LazyPath;
const fs = @import("std").fs;
const mem = @import("std").mem;
const debug = @import("std").debug;
step: Step,
/// Runner to update
run: *Build.Step.Run,
/// The directory that will contain the files to glob
dir: LazyPath,
/// Track the files added to the Run step for --watch
file_input_range: ?FileInputRange,
const FileInputRange = struct {
start_value: []const u8,
len: u32,
};
pub fn create(owner: *Build, run: *Run, dir: LazyPath) void {
const self = owner.allocator.create(DirectoryFileInput) catch @panic("OOM");
self.* = .{
.step = Step.init(.{
.id = .custom,
.name = androidbuild.runNameContext("directory-file-input"),
.owner = owner,
.makeFn = make,
}),
.run = run,
.dir = dir,
.file_input_range = null,
};
// Run step relies on DirectoryFileInput finishing
run.step.dependOn(&self.step);
// If dir is generated then this will wait for that dir to generate
dir.addStepDependencies(&self.step);
}
fn make(step: *Step, options: Build.Step.MakeOptions) !void {
const b = step.owner;
const gpa = if (builtin.zig_version.major == 0 and builtin.zig_version.minor <= 14)
// Deprecated: Zig 0.14.X doesn't have options.gpa
b.allocator
else
options.gpa;
const arena = b.allocator;
const self: *DirectoryFileInput = @fieldParentPtr("step", step);
const run = self.run;
// Add the directory to --watch input so that if any files are updated or changed
// this step will re-trigger
const need_derived_inputs = try step.addDirectoryWatchInput(self.dir);
// triggers on --watch if a file is modified.
if (self.file_input_range) |file_input_range| {
const start_index: usize = blk: {
for (run.file_inputs.items, 0..) |lp, file_input_index| {
switch (lp) {
.cwd_relative => |cwd_relative| {
if (mem.eql(u8, file_input_range.start_value, cwd_relative)) {
break :blk file_input_index;
}
},
else => continue,
}
}
return error.MissingFileInputWatchArgument;
};
try run.file_inputs.replaceRange(run.step.owner.allocator, start_index, file_input_range.len, &.{});
}
const dir_path = if (builtin.zig_version.major == 0 and builtin.zig_version.minor <= 15)
self.dir.getPath3(b, step)
else
try self.dir.getPath4(b, step);
// NOTE(jae): 2025-07-23
// As of Zig 0.15.0-dev.1092+d772c0627, package_name_path.openDir("") is not possible as it assumes you're appending a sub-path
var dir = if (builtin.zig_version.major == 0 and builtin.zig_version.minor <= 15)
try dir_path.root_dir.handle.openDir(dir_path.sub_path, .{ .iterate = true })
else
try dir_path.root_dir.handle.openDir(b.graph.io, dir_path.sub_path, .{ .iterate = true });
defer if (builtin.zig_version.major == 0 and builtin.zig_version.minor <= 15)
dir.close()
else
dir.close(b.graph.io);
var optional_file_input_value: ?[]const u8 = null;
var optional_file_input_start_index: ?usize = null;
var walker = try dir.walk(arena);
defer walker.deinit();
while (if (builtin.zig_version.major == 0 and builtin.zig_version.minor <= 15)
try walker.next()
else
try walker.next(b.graph.io)) |entry|
{
switch (entry.kind) {
.directory => {
if (need_derived_inputs) {
const entry_path = try dir_path.join(arena, entry.path);
try step.addDirectoryWatchInputFromPath(entry_path);
}
},
.file => {
// Add file as dependency to run command
const file_path = try dir_path.root_dir.join(gpa, &.{ dir_path.sub_path, entry.path });
if (optional_file_input_value == null) {
// Set index and value of first file
optional_file_input_start_index = run.file_inputs.items.len;
optional_file_input_value = file_path;
}
run.addFileInput(LazyPath{
.cwd_relative = file_path,
});
},
else => continue,
}
}
if (optional_file_input_value) |file_input_value| {
const file_input_start_index = optional_file_input_start_index orelse unreachable;
self.file_input_range = .{
.start_value = file_input_value,
.len = @intCast(run.file_inputs.items.len - file_input_start_index),
};
}
}
const DirectoryFileInput = @This();