-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.rs
More file actions
413 lines (367 loc) · 15.1 KB
/
Copy pathbuild.rs
File metadata and controls
413 lines (367 loc) · 15.1 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
use reqwest::blocking::Client;
use std::fs;
use std::path::{Path, PathBuf};
// Reuse HTML parsing logic from src/html_utils.rs
include!("src/html_utils.rs");
/// Generates test fixtures by fetching real data from package registries.
/// Runs during build if DOC2SKILL_REGEN_FIXTURES=1 or if fixtures don't exist.
fn main() {
// Generate only when explicitly asked. Writing to the source tree from a
// build script is forbidden by `cargo publish` (verification builds a copy
// of the package and rejects source-tree mutations outside OUT_DIR), and
// fetching from the network on every `cargo install`/CI build job is both
// slow and fragile. CI's test job and devs set DOC2SKILL_REGEN_FIXTURES=1.
let should_regen = std::env::var("DOC2SKILL_REGEN_FIXTURES")
.map(|v| v == "1" || v.to_lowercase() == "true")
.unwrap_or(false);
if !should_regen {
return;
}
let fixtures_dir = PathBuf::from("tests/fixtures_gen");
println!("cargo:warning=Regenerating test fixtures from real APIs...");
let client = Client::builder()
.user_agent("doc2skill/0.1 (https://github.com/odonno/doc2skill)")
.build()
.expect("Failed to create HTTP client");
// Rust packages
generate_rust_fixtures(&client, &fixtures_dir, "clap", Some("4.6.1"));
generate_rust_fixtures(&client, &fixtures_dir, "clap", None); // latest
generate_rust_fixtures(&client, &fixtures_dir, "serde", None); // latest
generate_rust_fixtures(&client, &fixtures_dir, "color-eyre", Some("0.6.5"));
// TypeScript packages
generate_typescript_fixtures(&client, &fixtures_dir, "react", Some("19.2.7"));
generate_typescript_fixtures(&client, &fixtures_dir, "react", None); // latest
// C# packages
generate_csharp_fixtures(&client, &fixtures_dir, "Newtonsoft.Json", None);
generate_csharp_fixtures(&client, &fixtures_dir, "System.Text.Json", Some("10.0.10"));
// llms.txt sources (real external docs)
generate_llmtxt_fixtures(
&client,
&fixtures_dir,
"https://www.fastht.ml/docs/llms.txt",
);
generate_llmtxt_fixtures(&client, &fixtures_dir, "https://likec4.dev/llms-full.txt");
println!("cargo:warning=Test fixtures generated successfully");
}
/// Fetches a real llms.txt (index) or llms-full.txt (self-contained) doc and writes
/// it under tests/fixtures_gen/llms-txt/ so the mock server can serve it.
///
/// For an index, each `## `-section link line is downloaded and the link URL is
/// rewritten to a relative path (the URL minus scheme+host) so the test stays
/// hermetic — url_to_slug of that relative path equals url_to_slug of the real
/// URL, so reference filenames match real runtime output.
fn generate_llmtxt_fixtures(client: &Client, fixtures_dir: &Path, url: &str) {
println!("cargo:warning=Generating llms.txt fixtures for '{}'", url);
let dest_dir = fixtures_dir.join("llms-txt");
fs::create_dir_all(&dest_dir).unwrap();
let resp = match client.get(url).send() {
Ok(r) if r.status().is_success() => r,
Ok(r) => {
println!("cargo:warning= skipped '{}' (status {})", url, r.status());
return;
}
Err(e) => {
println!("cargo:warning= skipped '{}' ({})", url, e);
return;
}
};
let body = resp.text().expect("Failed to read llms.txt response");
let basename = url.rsplit('/').next().unwrap_or("llms.txt");
let is_index = basename == "llms.txt";
if !is_index {
// Self-contained full doc: save as-is.
fs::write(dest_dir.join(basename), &body).expect("Failed to write llms-full fixture");
return;
}
// Index form: parse link lines, fetch each linked page, rewrite URLs to relative.
let mut rewritten = String::with_capacity(body.len());
for line in body.lines() {
let trimmed = line.trim_start();
let entry = trimmed.strip_prefix("- [");
let Some(rest) = entry else {
rewritten.push_str(line);
rewritten.push('\n');
continue;
};
let Some(close) = rest.find(']') else {
rewritten.push_str(line);
rewritten.push('\n');
continue;
};
let after = &rest[close + 1..];
let Some(paren_start) = after.find('(') else {
rewritten.push_str(line);
rewritten.push('\n');
continue;
};
let Some(paren_end) = after[paren_start + 1..].find(')') else {
rewritten.push_str(line);
rewritten.push('\n');
continue;
};
let link_url = &after[paren_start + 1..paren_start + 1 + paren_end];
if !link_url.starts_with("http://") && !link_url.starts_with("https://") {
rewritten.push_str(line);
rewritten.push('\n');
continue;
}
let rel = to_relative_path(link_url);
fetch_and_save(client, link_url, &dest_dir, rel);
rewritten.push_str(&line.replace(link_url, rel));
rewritten.push('\n');
}
fs::write(dest_dir.join("llms.txt"), rewritten).expect("Failed to write llms.txt fixture");
}
/// Strip scheme and host, returning the path (no leading slash).
fn to_relative_path(url: &str) -> &str {
let after = url.split("://").nth(1).unwrap_or(url);
after.find('/').map(|i| &after[i + 1..]).unwrap_or(after)
}
fn fetch_and_save(client: &Client, url: &str, dest_dir: &Path, rel: &str) {
let Ok(resp) = client.get(url).send() else {
println!(
"cargo:warning= skipped linked page '{}' (send failed)",
url
);
return;
};
if !resp.status().is_success() {
println!(
"cargo:warning= skipped linked page '{}' (status {})",
url,
resp.status()
);
return;
}
let body = resp.text().expect("Failed to read linked page");
let path = dest_dir.join(rel);
fs::create_dir_all(path.parent().unwrap()).unwrap();
fs::write(&path, &body).expect("Failed to write linked page fixture");
}
fn generate_rust_fixtures(client: &Client, fixtures_dir: &Path, name: &str, version: Option<&str>) {
println!(
"cargo:warning=Generating Rust fixtures for '{}' {:?}",
name, version
);
// Fetch crate metadata
let url = format!("https://crates.io/api/v1/crates/{}", name);
let resp = client
.get(&url)
.send()
.expect("Failed to fetch crate metadata");
let body = resp.text().expect("Failed to read response");
let path = fixtures_dir.join(format!("crates.io/api/v1/crates/{}.json", name));
fs::create_dir_all(path.parent().unwrap()).unwrap();
fs::write(&path, &body).expect("Failed to write fixture");
// Parse version from metadata or use provided version
let metadata: serde_json::Value = serde_json::from_str(&body).unwrap();
let version = version.map(|v| v.to_string()).unwrap_or_else(|| {
metadata["crate"]["newest_version"]
.as_str()
.unwrap_or("latest")
.to_string()
});
// Fetch crate owners
let url = format!("https://crates.io/api/v1/crates/{}/owners", name);
let resp = client
.get(&url)
.send()
.expect("Failed to fetch crate owners");
let body = resp.text().expect("Failed to read response");
let path = fixtures_dir.join(format!("crates.io/api/v1/crates/{}/owners.json", name));
fs::create_dir_all(path.parent().unwrap()).unwrap();
fs::write(&path, &body).expect("Failed to write fixture");
// Fetch docs index
let module = name.replace('-', "_");
let url = format!("https://docs.rs/{}/{}/{}/index.html", name, version, module);
if let Ok(resp) = client.get(&url).send()
&& resp.status().is_success()
{
let body = resp.text().unwrap();
let path = fixtures_dir.join(format!(
"docs.rs/{}/{}/{}/index.html",
name, version, module
));
fs::create_dir_all(path.parent().unwrap()).unwrap();
fs::write(&path, &body).expect("Failed to write fixture");
let crate_base = format!("https://docs.rs/{}/{}/{}", name, version, module);
// Extract reference URLs from index.html
let mut reference_urls: Vec<(String, String)> = extract_html_references(&body, &crate_base);
// Fetch all.html to get complete item list
let all_url = format!("{}/all.html", crate_base);
if let Ok(resp) = client.get(&all_url).send()
&& resp.status().is_success()
{
let body = resp.text().unwrap();
let path =
fixtures_dir.join(format!("docs.rs/{}/{}/{}/all.html", name, version, module));
fs::create_dir_all(path.parent().unwrap()).unwrap();
fs::write(&path, &body).expect("Failed to write fixture");
let all_references = extract_html_references(&body, &crate_base);
reference_urls.extend(all_references);
}
// Deduplicate by relative path
let mut seen = HashSet::new();
reference_urls.retain(|(_, rel)| seen.insert(rel.clone()));
// Fetch each reference page
for (full_url, rel_path) in reference_urls {
if let Ok(resp) = client.get(&full_url).send()
&& resp.status().is_success()
{
let body = resp.text().unwrap();
let path = fixtures_dir.join(format!(
"docs.rs/{}/{}/{}/{}",
name, version, module, rel_path
));
fs::create_dir_all(path.parent().unwrap()).unwrap();
fs::write(&path, &body).expect("Failed to write reference fixture");
}
}
}
}
fn generate_typescript_fixtures(
client: &Client,
fixtures_dir: &Path,
name: &str,
version: Option<&str>,
) {
println!(
"cargo:warning=Generating TypeScript fixtures for '{}' {:?}",
name, version
);
// Fetch package metadata (specific or latest version)
let url = if let Some(v) = version {
format!("https://registry.npmjs.org/{}/{}", name, v)
} else {
format!("https://registry.npmjs.org/{}/latest", name)
};
let resp = client
.get(&url)
.send()
.expect("Failed to fetch package metadata");
let body = resp.text().expect("Failed to read response");
let path = if let Some(v) = version {
fixtures_dir.join(format!("npm/{}/{}.json", name, v))
} else {
fixtures_dir.join(format!("npm/{}/latest.json", name))
};
fs::create_dir_all(path.parent().unwrap()).unwrap();
fs::write(&path, &body).expect("Failed to write fixture");
// Parse version from metadata
let metadata: serde_json::Value = serde_json::from_str(&body).unwrap();
let version = version
.map(|v| v.to_string())
.unwrap_or_else(|| metadata["version"].as_str().unwrap_or("latest").to_string());
// Fetch README from jsdelivr
let url = format!(
"https://cdn.jsdelivr.net/npm/{}@{}/README.md",
name, version
);
if let Ok(resp) = client.get(&url).send()
&& resp.status().is_success()
{
let body = resp.text().unwrap();
let path = fixtures_dir.join(format!("jsdelivr/npm/{}@{}/README.md", name, version));
fs::create_dir_all(path.parent().unwrap()).unwrap();
fs::write(&path, &body).expect("Failed to write fixture");
}
// Fetch npmx docs
let url = format!("https://npmx.dev/api/registry/docs/{}/v/{}", name, version);
if let Ok(resp) = client.get(&url).send()
&& resp.status().is_success()
{
let body = resp.text().unwrap();
let path = fixtures_dir.join(format!(
"npmx/api/registry/docs/{}/v/{}.json",
name, version
));
fs::create_dir_all(path.parent().unwrap()).unwrap();
fs::write(&path, &body).expect("Failed to write fixture");
}
}
fn generate_csharp_fixtures(client: &Client, fixtures_dir: &Path, id: &str, version: Option<&str>) {
println!(
"cargo:warning=Generating C# fixtures for '{}' {:?}",
id, version
);
let id_lower = id.to_lowercase();
// Fetch versions
let url = format!(
"https://api.nuget.org/v3-flatcontainer/{}/index.json",
id_lower
);
let resp = client.get(&url).send().expect("Failed to fetch versions");
let body = resp.text().expect("Failed to read response");
let path = fixtures_dir.join(format!("nuget/v3-flatcontainer/{}/index.json", id_lower));
fs::create_dir_all(path.parent().unwrap()).unwrap();
fs::write(&path, &body).expect("Failed to write fixture");
// Parse version from parameter or use latest stable
let versions: serde_json::Value = serde_json::from_str(&body).unwrap();
let version = version.map(|v| v.to_string()).unwrap_or_else(|| {
versions["versions"]
.as_array()
.and_then(|vs| {
vs.iter()
.rev()
.find(|v| v.as_str().is_some_and(|s| !s.contains('-')))
})
.and_then(|v| v.as_str())
.unwrap_or("latest")
.to_string()
});
// Fetch registration leaf
let url = format!(
"https://api.nuget.org/v3/registration5-semver1/{}/{}.json",
id_lower, version
);
let resp = client
.get(&url)
.send()
.expect("Failed to fetch registration leaf");
if !resp.status().is_success() {
println!(
"cargo:warning=Failed to fetch registration leaf for {} (status: {})",
id,
resp.status()
);
return;
}
let body = resp.text().expect("Failed to read response");
let path = fixtures_dir.join(format!(
"nuget/v3/registration5-semver1/{}/{}.json",
id_lower, version
));
fs::create_dir_all(path.parent().unwrap()).unwrap();
fs::write(&path, &body).expect("Failed to write fixture");
// Parse catalog URL and fetch catalog entry
let leaf: serde_json::Value =
serde_json::from_str(&body).expect("Failed to parse registration leaf JSON");
if let Some(catalog_url) = leaf["catalogEntry"].as_str() {
let resp = client
.get(catalog_url)
.send()
.expect("Failed to fetch catalog entry");
let body = resp.text().expect("Failed to read response");
// Extract the path from the catalog URL (after .org/)
let catalog_path = catalog_url.split(".org/").nth(1).unwrap_or("catalog.json");
let path = fixtures_dir.join(format!("nuget/{}.json", catalog_path));
fs::create_dir_all(path.parent().unwrap()).unwrap();
fs::write(&path, &body).expect("Failed to write fixture");
}
// Fetch readme
let url = format!(
"https://api.nuget.org/v3-flatcontainer/{}/{}/readme",
id_lower, version
);
if let Ok(resp) = client.get(&url).send()
&& resp.status().is_success()
{
let body = resp.text().unwrap();
let path = fixtures_dir.join(format!(
"nuget/v3-flatcontainer/{}/{}/readme.md",
id_lower, version
));
fs::create_dir_all(path.parent().unwrap()).unwrap();
fs::write(&path, &body).expect("Failed to write fixture");
}
}