-
Notifications
You must be signed in to change notification settings - Fork 178
/
Copy pathlib.rs
502 lines (433 loc) · 17.1 KB
/
lib.rs
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
mod channel;
use std::{
collections::BTreeMap,
fmt::Write,
io::{BufRead, BufReader},
time::SystemTime,
};
use mdbook::{
book::{Book, BookItem},
errors::Error,
preprocess::{Preprocessor, PreprocessorContext},
};
const CHANNELS: &[&str] = &["stable", "beta", "nightly"];
const CHANNEL_URL_PREFIX: &str = "https://static.rust-lang.org/dist/channel-rust-";
const MANIFESTS_URL: &str = "https://static.rust-lang.org/manifests.txt";
const RUSTUP_URLS: &str =
"https://raw.githubusercontent.com/rust-lang/rustup.rs/stable/ci/cloudfront-invalidation.txt";
/// A representation of a rust target platform. `stable`, `beta`, and `nightly`
/// represent whether the platform is available on that channel. `stable` also
/// carries the specific stable compiler version.
#[derive(serde::Serialize, serde::Deserialize)]
pub struct Platform {
stable: Option<String>,
beta: bool,
nightly: bool,
}
impl Default for Platform {
fn default() -> Self {
Self {
stable: None,
beta: false,
nightly: false,
}
}
}
/// `Blacksmith` builds dynamic tables and lists to display in the Rust Forge.
#[derive(Default, serde::Serialize, serde::Deserialize)]
pub struct Blacksmith {
last_update: Option<u64>,
rustup: Vec<String>,
stable_version: Option<String>,
platforms: BTreeMap<String, Platform>,
#[serde(default)]
previous_stable_versions: Vec<(String, Vec<String>)>,
}
impl Blacksmith {
/// Creates a new instance of `Blacksmith`.
pub fn new() -> Self {
Self::default()
}
/// Check if the data in this `Blacksmith` instance was not updated within the TTL.
pub fn is_stale(&self, ttl: u64) -> bool {
if let (Some(last_update), Some(now)) = (self.last_update, unix_time()) {
last_update + ttl < now
} else {
true
}
}
/// Populates a `Blacksmith` instance with data gathered from Rust's CI and
/// distribution channels.
pub fn init() -> Result<Self, Box<dyn std::error::Error>> {
let mut blacksmith = Self::new();
let rustup_url_regex =
regex::Regex::new(r"^rustup/dist/([^/]+)/rustup-init(?:\.exe)?$").unwrap();
for line in BufReader::new(reqwest::blocking::get(RUSTUP_URLS)?).lines() {
if let Some(m) = rustup_url_regex.captures(&(line?)) {
blacksmith
.rustup
.push(m.get(1).unwrap().as_str().to_string());
}
}
log::info!("Found {} targets for rustup", blacksmith.rustup.len());
for &channel_name in CHANNELS {
let channel_url = format!("{}{}.toml", CHANNEL_URL_PREFIX, channel_name);
let content = reqwest::blocking::get(&channel_url)?.text()?;
let rust = toml::from_str::<crate::channel::Channel>(&content)?
.pkg
.rust;
log::info!(
"Found {} targets for {} channel (v{})",
rust.target.len(),
channel_name,
rust.version
);
let vers = rust.version.split(' ').next().unwrap().to_string();
let platforms = rust
.target
.into_iter()
.filter_map(|(target, content)| {
if content.available {
Some(target)
} else {
None
}
})
.collect::<Vec<_>>();
if channel_name == "stable" {
blacksmith.stable_version = Some(vers.clone());
}
for platform in platforms {
let entry = blacksmith
.platforms
.entry(platform)
.or_insert_with(Platform::default);
match channel_name {
"stable" => entry.stable = Some(vers.clone()),
"beta" => entry.beta = true,
"nightly" => entry.nightly = true,
_ => unreachable!(),
}
}
}
let latest_stable_version = &blacksmith.stable_version.clone().unwrap();
// We need to use a map to deduplicate entries and sort them in the correct order.
// There are multiple entries for stable 1.8.0, 1.14.0, 1.15.1, 1.49.0 in MANIFESTS_URL.
// Keys contain (minor_version, patch_version) and values contain (full_version, platforms).
let mut previous_stable_version_map: BTreeMap<(u32, u32), (String, Vec<String>)> =
BTreeMap::new();
// Go over stable versions in https://static.rust-lang.org/manifests.txt in reverse order.
let manifests_content = reqwest::blocking::get(MANIFESTS_URL)?.text()?;
let stable_manifest_url_regex = regex::Regex::new(
r"^static\.rust-lang\.org/dist/\d{4}-\d{2}-\d{2}/channel-rust-1\.(\d+)\.(\d+)\.toml$",
)
.unwrap();
for manifest_url in manifests_content.lines().rev() {
let minor;
let patch;
// Check if it's a stable version.
if let Some(captures) = stable_manifest_url_regex.captures(&(manifest_url)) {
minor = captures.get(1).unwrap().as_str().parse::<u32>().unwrap();
patch = captures.get(2).unwrap().as_str().parse::<u32>().unwrap();
} else {
continue;
}
let full_version = format!("1.{}.{}", minor, patch);
// Check if we already processed that version.
if previous_stable_version_map.contains_key(&(minor, patch)) {
continue;
}
// Skip latest stable version.
if &full_version == latest_stable_version {
continue;
}
// Download https://static.rust-lang.org/dist/channel-rust-{major.minor.patch}.toml and process it.
let channel_url = format!("{}{}.toml", CHANNEL_URL_PREFIX, full_version);
let content = reqwest::blocking::get(&channel_url)?.text()?;
let rust = toml::from_str::<crate::channel::Channel>(&content)?
.pkg
.rust;
log::info!(
"Found {} targets for stable v{}",
rust.target.len(),
rust.version
);
let version = rust.version.split(' ').next().unwrap().to_string();
// Sanity check.
assert_eq!(&full_version, &version);
let platforms = rust
.target
.into_iter()
.filter_map(|(target, content)| {
if content.available {
Some(target)
} else {
None
}
})
.collect::<Vec<_>>();
previous_stable_version_map.insert((minor, patch), (version, platforms));
}
// There are no manifests for stable versions before 1.8.0,
// so we hardcode them instead.
// i686-pc-windows-msvc wasn't supported until version 1.3.0.
for minor in 3..8 {
previous_stable_version_map.insert(
(minor, 0),
(
format!("1.{}.0", minor),
Vec::from([
"i686-apple-darwin".to_string(),
"i686-pc-windows-gnu".to_string(),
"i686-pc-windows-msvc".to_string(),
"i686-unknown-linux-gnu".to_string(),
"x86_64-apple-darwin".to_string(),
"x86_64-pc-windows-gnu".to_string(),
"x86_64-pc-windows-msvc".to_string(),
"x86_64-unknown-linux-gnu".to_string(),
]),
),
);
}
// x86_64-pc-windows-msvc wasn't supported until version 1.2.0.
previous_stable_version_map.insert(
(2, 0),
(
"1.2.0".to_string(),
Vec::from([
"i686-apple-darwin".to_string(),
"i686-pc-windows-gnu".to_string(),
"i686-unknown-linux-gnu".to_string(),
"x86_64-apple-darwin".to_string(),
"x86_64-pc-windows-gnu".to_string(),
"x86_64-pc-windows-msvc".to_string(),
"x86_64-unknown-linux-gnu".to_string(),
]),
),
);
for minor in 0..2 {
previous_stable_version_map.insert(
(minor, 0),
(
format!("1.{}.0", minor),
Vec::from([
"i686-apple-darwin".to_string(),
"i686-pc-windows-gnu".to_string(),
"i686-unknown-linux-gnu".to_string(),
"x86_64-apple-darwin".to_string(),
"x86_64-pc-windows-gnu".to_string(),
"x86_64-unknown-linux-gnu".to_string(),
]),
),
);
}
for (_, (version, platforms)) in previous_stable_version_map.into_iter().rev() {
blacksmith
.previous_stable_versions
.push((version, platforms));
}
blacksmith.last_update = unix_time();
Ok(blacksmith)
}
/// Creates a list of hyperlinks to `rustup-init` based on what targets
/// rustup provided using the following URL schema. Where `target` is the
/// platforms target tuple (`x86_64-apple-darwin`) and `suffix` is a target
/// specific file extension.
/// ```url
/// https://static.rust-lang.org/rustup/dist/{target}/rustup-init{suffix}
/// ```
fn generate_rustup_init_list(&self) -> String {
let mut buffer = String::new();
for target in &self.rustup {
let suffix = if target.contains("windows") {
".exe"
} else {
""
};
writeln!(
buffer,
"- [{target}](https://static.rust-lang.org/rustup/dist/{target}/rustup-init{suffix})",
target = target,
suffix = suffix,
).unwrap();
}
buffer
}
/// Generates a table of links to the standalone installer packages for
/// each platform.
fn generate_standalone_installers_table(&self) -> String {
let mut buffer = String::new();
writeln!(
buffer,
"platform | stable ({}) | beta | nightly",
self.stable_version.as_ref().unwrap()
)
.unwrap();
writeln!(buffer, "---------|--------|------|--------").unwrap();
for (name, platform) in &self.platforms {
let extensions: &[&str] = if name.contains("windows") {
&["msi", "tar.xz"]
} else if name.contains("darwin") {
&["pkg", "tar.xz"]
} else {
&["tar.xz"]
};
let stable_links = platform
.stable
.as_ref()
.map(|version| generate_standalone_links("rust", version, name, extensions))
.unwrap_or_else(String::new);
let beta_links = if platform.beta {
generate_standalone_links("rust", "beta", name, extensions)
} else {
String::new()
};
let nightly_links = if platform.nightly {
generate_standalone_links("rust", "nightly", name, extensions)
} else {
String::new()
};
writeln!(
buffer,
"`{name}` | {stable} | {beta} | {nightly}",
name = name,
stable = stable_links,
beta = beta_links,
nightly = nightly_links
)
.unwrap();
}
buffer
}
/// Generates tables of links to the previous stable standalone installer packages for
/// each platform.
fn generate_previous_stable_standalone_installers_tables(&self) -> String {
let mut buffer = String::new();
for (stable_version, platforms) in &self.previous_stable_versions {
writeln!(buffer, "## Stable ({})", stable_version).unwrap();
writeln!(buffer, "").unwrap();
writeln!(buffer, "platform | stable ({})", stable_version).unwrap();
writeln!(buffer, "---------|--------").unwrap();
for name in platforms {
let extensions: &[&str] = if name.contains("windows") {
&["msi", "tar.gz"]
} else if name.contains("darwin") {
&["pkg", "tar.gz"]
} else {
&["tar.gz"]
};
let stable_links =
generate_standalone_links("rust", stable_version, name, extensions);
writeln!(
buffer,
"`{name}` | {stable}",
name = name,
stable = stable_links,
)
.unwrap();
}
writeln!(buffer, "").unwrap();
}
buffer
}
/// Generates a similar table to `generate_standalone_installers_table`
/// except for the rust source code packages.
fn generate_source_code_table(&self) -> String {
let mut buffer = String::new();
writeln!(buffer, "Channel | Archives + Signatures").unwrap();
writeln!(buffer, "--------|----------------------").unwrap();
for &channel in CHANNELS {
if channel == "stable" {
let stable_version = self.stable_version.as_ref().unwrap();
writeln!(
buffer,
"stable ({}) | {}",
stable_version,
generate_standalone_links("rustc", stable_version, "src", &["tar.xz"])
)
.unwrap();
} else {
writeln!(
buffer,
"{} | {}",
channel,
generate_standalone_links("rustc", &channel, "src", &["tar.xz"])
)
.unwrap();
}
}
buffer
}
}
/// Generates links to standalone installers provided a rust version or channel,
/// target name, and file extension.
fn generate_standalone_links(base: &str, stem: &str, name: &str, extensions: &[&str]) -> String {
let mut result = String::new();
for extension in extensions {
if !result.is_empty() {
result.push_str(" <br> ");
}
let url = format!(
"https://static.rust-lang.org/dist/{base}-{stem}-{name}.{extension}",
extension = extension,
name = name,
stem = stem,
base = base,
);
result.push_str(&format!(
"[{extension}]({url}) <br> [{extension}.asc]({url}.asc)",
extension = extension,
url = url,
));
}
result
}
fn unix_time() -> Option<u64> {
SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.ok()
.map(|d| d.as_secs())
}
impl Preprocessor for Blacksmith {
fn name(&self) -> &str {
"blacksmith"
}
fn run(&self, _: &PreprocessorContext, mut book: Book) -> Result<Book, Error> {
fn recursive_replace(book_item: &mut BookItem, old: &str, new: &str) {
let chapter = match book_item {
BookItem::Chapter(chapter) => chapter,
_ => return,
};
chapter.content = chapter.content.replace(old, new);
for sub_item in &mut chapter.sub_items {
recursive_replace(sub_item, old, new);
}
}
let rustup_init_list = self.generate_rustup_init_list();
let standalone_installers_table = self.generate_standalone_installers_table();
let previous_stable_standalone_installers_tables =
self.generate_previous_stable_standalone_installers_tables();
let source_code_table = self.generate_source_code_table();
// TODO: Currently we're performing a global search for any of the
// variables as that's the most flexible for adding more dynamic
// content, and the time to traverse is fast enough to not be noticeable.
// However if the processing time begins to become a bottleneck this
// should change.
for item in &mut book.sections {
recursive_replace(item, "{{#rustup_init_list}}", &rustup_init_list);
recursive_replace(item, "{{#installer_table}}", &standalone_installers_table);
recursive_replace(
item,
"{{#previous_stable_standalone_installers_tables}}",
&previous_stable_standalone_installers_tables,
);
recursive_replace(item, "{{#source_code_table}}", &source_code_table);
}
Ok(book)
}
/// `Blacksmith`'s operations are renderer independent as they output
/// markdown.
fn supports_renderer(&self, _renderer: &str) -> bool {
true
}
}