|
| 1 | +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +use std::path::Path; |
| 12 | +use std::fs::{self, File}; |
| 13 | +use std::io::prelude::*; |
| 14 | + |
| 15 | +use build::{Build, Compiler}; |
| 16 | +use build::util::up_to_date; |
| 17 | + |
| 18 | +pub fn rustbook(build: &Build, stage: u32, host: &str, name: &str, out: &Path) { |
| 19 | + t!(fs::create_dir_all(out)); |
| 20 | + |
| 21 | + let out = out.join(name); |
| 22 | + let compiler = Compiler::new(stage, host); |
| 23 | + let src = build.src.join("src/doc").join(name); |
| 24 | + let index = out.join("index.html"); |
| 25 | + let rustbook = build.tool(&compiler, "rustbook"); |
| 26 | + if up_to_date(&src, &index) && up_to_date(&rustbook, &index) { |
| 27 | + return |
| 28 | + } |
| 29 | + println!("Rustbook stage{} ({}) - {}", stage, host, name); |
| 30 | + let _ = fs::remove_dir_all(&out); |
| 31 | + build.run(build.tool_cmd(&compiler, "rustbook") |
| 32 | + .arg("build") |
| 33 | + .arg(&src) |
| 34 | + .arg(out)); |
| 35 | +} |
| 36 | + |
| 37 | +pub fn standalone(build: &Build, stage: u32, host: &str, out: &Path) { |
| 38 | + println!("Documenting stage{} standalone ({})", stage, host); |
| 39 | + t!(fs::create_dir_all(out)); |
| 40 | + |
| 41 | + let compiler = Compiler::new(stage, host); |
| 42 | + |
| 43 | + let favicon = build.src.join("src/doc/favicon.inc"); |
| 44 | + let footer = build.src.join("src/doc/footer.inc"); |
| 45 | + let full_toc = build.src.join("src/doc/full-toc.inc"); |
| 46 | + t!(fs::copy(build.src.join("src/doc/rust.css"), out.join("rust.css"))); |
| 47 | + |
| 48 | + let version_input = build.src.join("src/doc/version_info.html.template"); |
| 49 | + let version_info = out.join("version_info.html"); |
| 50 | + |
| 51 | + if !up_to_date(&version_input, &version_info) { |
| 52 | + let mut info = String::new(); |
| 53 | + t!(t!(File::open(&version_input)).read_to_string(&mut info)); |
| 54 | + let blank = String::new(); |
| 55 | + let short = build.short_ver_hash.as_ref().unwrap_or(&blank); |
| 56 | + let hash = build.ver_hash.as_ref().unwrap_or(&blank); |
| 57 | + let info = info.replace("VERSION", &build.release) |
| 58 | + .replace("SHORT_HASH", short) |
| 59 | + .replace("STAMP", hash); |
| 60 | + t!(t!(File::create(&version_info)).write_all(info.as_bytes())); |
| 61 | + } |
| 62 | + |
| 63 | + for file in t!(fs::read_dir(build.src.join("src/doc"))) { |
| 64 | + let file = t!(file); |
| 65 | + let path = file.path(); |
| 66 | + let filename = path.file_name().unwrap().to_str().unwrap(); |
| 67 | + if !filename.ends_with(".md") || filename == "README.md" { |
| 68 | + continue |
| 69 | + } |
| 70 | + |
| 71 | + let html = out.join(filename).with_extension("html"); |
| 72 | + let rustdoc = build.tool(&compiler, "rustdoc"); |
| 73 | + if up_to_date(&path, &html) && |
| 74 | + up_to_date(&footer, &html) && |
| 75 | + up_to_date(&favicon, &html) && |
| 76 | + up_to_date(&full_toc, &html) && |
| 77 | + up_to_date(&version_info, &html) && |
| 78 | + up_to_date(&rustdoc, &html) { |
| 79 | + continue |
| 80 | + } |
| 81 | + |
| 82 | + let mut cmd = build.tool_cmd(&compiler, "rustdoc"); |
| 83 | + cmd.arg("--html-after-content").arg(&footer) |
| 84 | + .arg("--html-before-content").arg(&version_info) |
| 85 | + .arg("--html-in-header").arg(&favicon) |
| 86 | + .arg("--markdown-playground-url") |
| 87 | + .arg("https://play.rust-lang.org/") |
| 88 | + .arg("-o").arg(out) |
| 89 | + .arg(&path); |
| 90 | + |
| 91 | + if filename == "reference.md" { |
| 92 | + cmd.arg("--html-in-header").arg(&full_toc); |
| 93 | + } |
| 94 | + |
| 95 | + if filename == "not_found.md" { |
| 96 | + cmd.arg("--markdown-no-toc") |
| 97 | + .arg("--markdown-css") |
| 98 | + .arg("https://doc.rust-lang.org/rust.css"); |
| 99 | + } else { |
| 100 | + cmd.arg("--markdown-css").arg("rust.css"); |
| 101 | + } |
| 102 | + build.run(&mut cmd); |
| 103 | + } |
| 104 | +} |
0 commit comments