-
Notifications
You must be signed in to change notification settings - Fork 175
/
lib.rs
312 lines (269 loc) · 9.85 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
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 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>,
}
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!(),
}
}
}
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 extension = if name.contains("windows") {
"msi"
} else if name.contains("darwin") {
"pkg"
} else {
"tar.gz"
};
let stable_links = platform
.stable
.as_ref()
.map(|version| generate_standalone_links("rust", version, name, extension))
.unwrap_or_else(String::new);
let beta_links = if platform.beta {
generate_standalone_links("rust", "beta", name, extension)
} else {
String::new()
};
let nightly_links = if platform.nightly {
generate_standalone_links("rust", "nightly", name, extension)
} else {
String::new()
};
writeln!(
buffer,
"`{name}` | {stable} | {beta} | {nightly}",
name = name,
stable = stable_links,
beta = beta_links,
nightly = nightly_links
)
.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.gz")
)
.unwrap();
} else {
writeln!(
buffer,
"{} | {}",
channel,
generate_standalone_links("rustc", &channel, "src", "tar.gz")
)
.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, extension: &str) -> String {
let url = format!(
"https://static.rust-lang.org/dist/{base}-{stem}-{name}.{extension}",
extension = extension,
name = name,
stem = stem,
base = base,
);
format!(
"[{extension}]({url}) <br> [{extension}.asc]({url}.asc)",
extension = extension,
url = url,
)
}
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 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 noticable.
// 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, "{{#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
}
}