Skip to content

Commit 74af1af

Browse files
Simplify distribution of version-switcher.js
1 parent b174ed1 commit 74af1af

File tree

2 files changed

+26
-26
lines changed

2 files changed

+26
-26
lines changed

src/bootstrap/doc.rs

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use std::ffi::OsStr;
1111
use std::fs;
1212
use std::io;
1313
use std::path::{Path, PathBuf};
14-
use std::str::FromStr;
1514

1615
use crate::builder::{Builder, Compiler, Kind, RunConfig, ShouldRun, Step};
1716
use crate::cache::{Interned, INTERNER};
@@ -401,18 +400,6 @@ impl Step for Standalone {
401400
}
402401
}
403402

404-
fn get_doc_max_version(builder: &Builder<'_>) -> String {
405-
let reduce_by = match builder.config.channel.as_str() {
406-
"dev" => 3,
407-
"nightly" => 2,
408-
"beta" => 1,
409-
_ => 0,
410-
};
411-
let parts = builder.version.split(".").collect::<Vec<_>>();
412-
let medium = t!(u32::from_str(parts[1]));
413-
format!("{}.{}.{}", parts[0], medium - reduce_by, parts[2..].join("."))
414-
}
415-
416403
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
417404
pub struct Std {
418405
pub stage: u32,
@@ -443,12 +430,9 @@ impl Step for Std {
443430
t!(fs::create_dir_all(&out));
444431
t!(fs::copy(builder.src.join("src/doc/rust.css"), out.join("rust.css")));
445432

446-
let version = get_doc_max_version(&builder);
447-
448-
let content = t!(fs::read_to_string(builder.src.join("src/doc/version-switcher.js")));
449-
t!(fs::write(
450-
out.join("version-switcher.js"),
451-
content.replace("/* VERSION TO BE REPLACED */", &version),
433+
t!(fs::copy(
434+
builder.src.join("src/doc/version-switcher.js"),
435+
out.join("version-switcher.js")
452436
));
453437

454438
let index_page = builder.src.join("src/doc/index.md").into_os_string();

src/doc/version-switcher.js

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,21 @@
1-
const CURRENT_VERSION = "/* VERSION TO BE REPLACED */";
1+
(function() {
2+
3+
let CURRENT_VERSION = -1;
4+
5+
function get_current_version() {
6+
if (CURRENT_VERSION !== -1) {
7+
return CURRENT_VERSION;
8+
}
9+
const now = Date.now();
10+
// Month is 0-indexed.
11+
// First release of Rust, 15 may 2015.
12+
const first_release = new Date(2015, 4, 15);
13+
const diff_time = Math.abs(now - first_release);
14+
const nb_days = Math.ceil(diff_time / (1000 * 60 * 60 * 24));
15+
const nb_weeks = nb_days / 7;
16+
CURRENT_VERSION = Math.floor(nb_weeks / 6);
17+
return CURRENT_VERSION;
18+
}
219

320
function checkIfIsOldVersion() {
421
if (["http:", "https:"].indexOf(window.location.protocol) === -1) {
@@ -159,12 +176,9 @@ function showSwitcher(isOldVersion) {
159176
version_picker.appendChild(createOption("beta", current_doc_version === "beta"));
160177
version_picker.appendChild(createOption("nightly", current_doc_version === "nightly"));
161178

162-
const version_parts = CURRENT_VERSION.split(".");
163-
for (let major = parseInt(version_parts[0]); major >= 1; --major) {
164-
for (let medium = parseInt(version_parts[1]); medium >= 0; --medium) {
165-
const version = `${major}.${medium}.0`;
166-
version_picker.appendChild(createOption(version, version === current_doc_version));
167-
}
179+
for (let medium = get_current_version(); medium >= 0; --medium) {
180+
const version = `1.${medium}.0`;
181+
version_picker.appendChild(createOption(version, version === current_doc_version));
168182
}
169183

170184
version_picker.style.color = "#000";
@@ -208,3 +222,5 @@ function showSwitcher(isOldVersion) {
208222
}
209223

210224
showSwitcher(checkIfIsOldVersion());
225+
226+
}());

0 commit comments

Comments
 (0)