Repository navigation
Expand file tree
/
Copy pathbuild.rs
More file actions
321 lines (291 loc) · 11.4 KB
/
Copy pathbuild.rs
File metadata and controls
321 lines (291 loc) · 11.4 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
use sha2::{Digest, Sha256};
use std::path::{Path, PathBuf};
use std::process::Command;
use std::{env, fs};
fn main() {
print_git_rerun_paths();
let commit = Command::new("git")
.args(["rev-parse", "--short", "HEAD"])
.output()
.ok()
.filter(|output| output.status.success())
.and_then(|output| String::from_utf8(output.stdout).ok())
.map(|stdout| stdout.trim().to_string())
.filter(|commit| !commit.is_empty())
.unwrap_or_else(|| "unknown".to_string());
println!("cargo:rustc-env=FLUENT_BUILD_COMMIT={commit}");
generate_bundled_skills();
generate_skill_migrations();
}
/// Embed exact historical bundle identities that may be adopted without a
/// managed-installation record.
fn generate_skill_migrations() {
let migrations_dir = PathBuf::from("skill-migrations");
println!("cargo:rerun-if-changed={}", migrations_dir.display());
let fixture_dir = migrations_dir.join("v0.1.4");
let mut manifests = Vec::new();
if fixture_dir.is_dir() {
let mut skills = fs::read_dir(&fixture_dir)
.expect("failed to read skill migration fixture")
.collect::<Result<Vec<_>, _>>()
.expect("failed to iterate skill migration fixture");
skills.sort_by_key(|entry| entry.file_name());
for skill in skills {
if !skill
.file_type()
.expect("failed to inspect skill fixture")
.is_dir()
{
continue;
}
let name = skill.file_name().to_string_lossy().to_string();
let mut entries = Vec::new();
collect_migration_entries(&skill.path(), &skill.path(), &mut entries);
assert!(
!entries.is_empty(),
"migration fixture {name} must not be empty"
);
entries.sort_by(|left, right| left.0.cmp(&right.0));
let mut hasher = Sha256::new();
for (path, bytes) in entries {
hasher.update(if bytes.is_some() {
b"file\0".as_slice()
} else {
b"dir\0".as_slice()
});
hasher.update(path.as_bytes());
hasher.update([0]);
if let Some(bytes) = bytes {
hasher.update((bytes.len() as u64).to_be_bytes());
hasher.update(bytes);
}
}
manifests.push((name, format!("{:x}", hasher.finalize())));
}
}
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
let dest = out_dir.join("skill_migrations.rs");
let mut code = String::from("/// Historical skill bundles eligible for exact migration.\n");
code.push_str("pub const BUNDLED_SKILL_MIGRATION_DIGESTS: &[(&str, &str)] = &[\n");
for (skill, digest) in manifests {
code.push_str(" (");
write_rust_str_literal(&mut code, &skill);
code.push_str(", ");
write_rust_str_literal(&mut code, &digest);
code.push_str("),\n");
}
code.push_str("];\n");
fs::write(dest, code).expect("failed to write skill_migrations.rs");
}
fn collect_migration_entries(
root: &Path,
dir: &Path,
entries: &mut Vec<(String, Option<Vec<u8>>)>,
) {
let mut dir_entries = fs::read_dir(dir)
.unwrap_or_else(|error| panic!("failed to read {}: {error}", dir.display()))
.collect::<Result<Vec<_>, _>>()
.unwrap_or_else(|error| panic!("failed to iterate {}: {error}", dir.display()));
dir_entries.sort_by_key(|entry| entry.file_name());
for entry in dir_entries {
let path = entry.path();
let relative = path
.strip_prefix(root)
.expect("migration entry remains below fixture")
.to_string_lossy()
.replace('\\', "/");
let file_type = entry
.file_type()
.expect("failed to inspect migration entry");
if file_type.is_dir() {
entries.push((relative, None));
collect_migration_entries(root, &path, entries);
} else if file_type.is_file() {
entries.push((
relative,
Some(fs::read(&path).expect("failed to read migration file")),
));
} else {
panic!(
"migration fixture contains non-regular entry: {}",
path.display()
);
}
}
}
fn print_git_rerun_paths() {
for args in [
["rev-parse", "--git-path", "HEAD"].as_slice(),
["rev-parse", "--git-path", "index"].as_slice(),
] {
if let Some(path) = git_stdout(args) {
println!("cargo:rerun-if-changed={path}");
}
}
if let Some(head_ref) = git_stdout(["symbolic-ref", "-q", "HEAD"].as_slice())
&& let Some(path) = git_stdout(["rev-parse", "--git-path", &head_ref].as_slice())
{
println!("cargo:rerun-if-changed={path}");
}
}
fn git_stdout(args: &[&str]) -> Option<String> {
Command::new("git")
.args(args)
.output()
.ok()
.filter(|output| output.status.success())
.and_then(|output| String::from_utf8(output.stdout).ok())
.map(|stdout| stdout.trim().to_string())
.filter(|stdout| !stdout.is_empty())
}
fn generate_bundled_skills() {
let skills_dir = PathBuf::from("skills");
println!("cargo:rerun-if-changed=skills");
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
let dest = out_dir.join("bundled_skills.rs");
let mut entries: Vec<(String, String)> = Vec::new();
// Collect names that have a `.full/` override so we can skip their
// plain directories (the shim) when bundling.
let full_overrides = find_full_overrides(&skills_dir);
if skills_dir.is_dir() {
collect_skill_files(&skills_dir, &skills_dir, &full_overrides, &mut entries);
}
entries.sort_by(|a, b| a.0.cmp(&b.0));
let mut code = String::new();
code.push_str("/// Bundled skill files generated by build.rs.\n");
code.push_str("/// Each entry is (relative_path, file_content).\n");
code.push_str("pub const BUNDLED_SKILL_FILES: &[(&str, &str)] = &[\n");
for (rel_path, content) in &entries {
code.push_str(" (");
write_rust_str_literal(&mut code, rel_path);
code.push_str(", ");
write_rust_str_literal(&mut code, content);
code.push_str("),\n");
}
code.push_str("];\n");
fs::write(&dest, code).expect("failed to write bundled_skills.rs");
}
/// Scan `skills/` for `<name>.full/` directories. Returns the set of `name`
/// strings that have a `.full/` override.
fn find_full_overrides(skills_dir: &Path) -> Vec<String> {
let mut names = Vec::new();
let entries = match fs::read_dir(skills_dir) {
Ok(e) => e,
Err(_) => return names,
};
for entry in entries.flatten() {
let fname = entry.file_name();
let fname = fname.to_string_lossy();
if fname.ends_with(".full") && entry.path().is_dir() {
names.push(fname.trim_end_matches(".full").to_string());
}
}
names
}
fn collect_skill_files(
base: &Path,
dir: &Path,
full_overrides: &[String],
entries: &mut Vec<(String, String)>,
) {
let mut dir_entries: Vec<fs::DirEntry> = fs::read_dir(dir)
.unwrap_or_else(|e| panic!("failed to read {}: {e}", dir.display()))
.collect::<Result<_, _>>()
.unwrap_or_else(|e| panic!("failed to iterate {}: {e}", dir.display()));
dir_entries.sort_by_key(|e| e.file_name());
for entry in dir_entries {
let path = entry.path();
let metadata = fs::metadata(&path)
.unwrap_or_else(|e| panic!("failed to read metadata for {}: {e}", path.display()));
if metadata.is_dir() {
// Skip a top-level `<name>/` directory when `<name>.full/` exists,
// so the shim is never bundled.
if dir == base {
let dir_name = path
.file_name()
.unwrap_or_default()
.to_string_lossy()
.to_string();
if !dir_name.ends_with(".full") && full_overrides.iter().any(|n| *n == dir_name) {
continue;
}
}
// A `.full/` directory maps its contents into `<name>/`:
// `<name>.full/<name>.md` → `<name>/SKILL.md`
// `<name>.full/references/...` → `<name>/references/...`
if dir == base {
let dir_name = path
.file_name()
.unwrap_or_default()
.to_string_lossy()
.to_string();
if let Some(skill_name) = dir_name.strip_suffix(".full") {
collect_full_override_files(skill_name, &path, entries);
continue;
}
}
collect_skill_files(base, &path, full_overrides, entries);
} else if metadata.is_file() {
let rel = path
.strip_prefix(base)
.unwrap()
.to_string_lossy()
.replace('\\', "/");
let content = fs::read_to_string(&path)
.unwrap_or_else(|e| panic!("failed to read {}: {e}", path.display()));
entries.push((rel, content));
}
}
}
/// Collect files from a `<name>.full/` directory, mapping them into
/// `<name>/` paths for bundling.
fn collect_full_override_files(
skill_name: &str,
full_dir: &Path,
entries: &mut Vec<(String, String)>,
) {
// Map `<name>.full/<name>.md` → `<name>/SKILL.md`
let main_file = full_dir.join(format!("{skill_name}.md"));
if main_file.is_file() {
let content = fs::read_to_string(&main_file)
.unwrap_or_else(|e| panic!("failed to read {}: {e}", main_file.display()));
entries.push((format!("{skill_name}/SKILL.md"), content));
}
// Map `<name>.full/references/*` → `<name>/references/*`
let refs_dir = full_dir.join("references");
if refs_dir.is_dir() {
collect_files_recursively(&refs_dir, &format!("{skill_name}/references"), entries);
}
}
fn collect_files_recursively(dir: &Path, prefix: &str, entries: &mut Vec<(String, String)>) {
let mut dir_entries: Vec<fs::DirEntry> = fs::read_dir(dir)
.unwrap_or_else(|e| panic!("failed to read {}: {e}", dir.display()))
.collect::<Result<_, _>>()
.unwrap_or_else(|e| panic!("failed to iterate {}: {e}", dir.display()));
dir_entries.sort_by_key(|e| e.file_name());
for entry in dir_entries {
let path = entry.path();
let fname = path.file_name().unwrap_or_default().to_string_lossy();
if path.is_dir() {
collect_files_recursively(&path, &format!("{prefix}/{fname}"), entries);
} else if path.is_file() {
let content = fs::read_to_string(&path)
.unwrap_or_else(|e| panic!("failed to read {}: {e}", path.display()));
entries.push((format!("{prefix}/{fname}"), content));
}
}
}
fn write_rust_str_literal(out: &mut String, s: &str) {
out.push('"');
for ch in s.chars() {
match ch {
'\\' => out.push_str("\\\\"),
'"' => out.push_str("\\\""),
'\n' => out.push_str("\\n"),
'\r' => out.push_str("\\r"),
'\t' => out.push_str("\\t"),
c => out.push(c),
}
}
out.push('"');
}