2014-12-02 09:20:48 -05:00
|
|
|
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
//! Implementation of the `build` subcommand, used to compile a book.
|
|
|
|
|
2015-02-16 16:04:02 +02:00
|
|
|
use std::env;
|
std: Stabilize the `fs` module
This commit performs a stabilization pass over the `std::fs` module now that
it's had some time to bake. The change was largely just adding `#[stable]` tags,
but there are a few APIs that remain `#[unstable]`.
The following apis are now marked `#[stable]`:
* `std::fs` (the name)
* `File`
* `Metadata`
* `ReadDir`
* `DirEntry`
* `OpenOptions`
* `Permissions`
* `File::{open, create}`
* `File::{sync_all, sync_data}`
* `File::set_len`
* `File::metadata`
* Trait implementations for `File` and `&File`
* `OpenOptions::new`
* `OpenOptions::{read, write, append, truncate, create}`
* `OpenOptions::open` - this function was modified, however, to not attempt to
reject cross-platform openings of directories. This means that some platforms
will succeed in opening a directory and others will fail.
* `Metadata::{is_dir, is_file, len, permissions}`
* `Permissions::{readonly, set_readonly}`
* `Iterator for ReadDir`
* `DirEntry::path`
* `remove_file` - like with `OpenOptions::open`, the extra windows code to
remove a readonly file has been removed. This means that removing a readonly
file will succeed on some platforms but fail on others.
* `metadata`
* `rename`
* `copy`
* `hard_link`
* `soft_link`
* `read_link`
* `create_dir`
* `create_dir_all`
* `remove_dir`
* `remove_dir_all`
* `read_dir`
The following apis remain `#[unstable]`.
* `WalkDir` and `walk` - there are many methods by which a directory walk can be
constructed, and it's unclear whether the current semantics are the right
ones. For example symlinks are not handled super well currently. This is now
behind a new `fs_walk` feature.
* `File::path` - this is an extra abstraction which the standard library
provides on top of what the system offers and it's unclear whether we should
be doing so. This is now behind a new `file_path` feature.
* `Metadata::{accessed, modified}` - we do not currently have a good
abstraction for a moment in time which is what these APIs should likely be
returning, so these remain `#[unstable]` for now. These are now behind a new
`fs_time` feature
* `set_file_times` - like with `Metadata::accessed`, we do not currently have
the appropriate abstraction for the arguments here so this API remains
unstable behind the `fs_time` feature gate.
* `PathExt` - the precise set of methods on this trait may change over time and
some methods may be removed. This API remains unstable behind the `path_ext`
feature gate.
* `set_permissions` - we may wish to expose a more granular ability to set the
permissions on a file instead of just a blanket "set all permissions" method.
This function remains behind the `fs` feature.
The following apis are now `#[deprecated]`
* The `TempDir` type is now entirely deprecated and is [located on
crates.io][tempdir] as the `tempdir` crate with [its source][github] at
rust-lang/tempdir.
[tempdir]: https://crates.io/crates/tempdir
[github]: https://github.com/rust-lang/tempdir
The stability of some of these APIs has been questioned over the past few weeks
in using these APIs, and it is intentional that the majority of APIs here are
marked `#[stable]`. The `std::fs` module has a lot of room to grow and the
material is [being tracked in a RFC issue][rfc-issue].
[rfc-issue]: https://github.com/rust-lang/rfcs/issues/939
[breaking-change]
2015-03-03 19:18:29 -08:00
|
|
|
use std::fs::{self, File};
|
2015-02-26 21:00:43 -08:00
|
|
|
use std::io::prelude::*;
|
|
|
|
use std::io::{self, BufWriter};
|
|
|
|
use std::path::{Path, PathBuf};
|
std: Stabilize the `fs` module
This commit performs a stabilization pass over the `std::fs` module now that
it's had some time to bake. The change was largely just adding `#[stable]` tags,
but there are a few APIs that remain `#[unstable]`.
The following apis are now marked `#[stable]`:
* `std::fs` (the name)
* `File`
* `Metadata`
* `ReadDir`
* `DirEntry`
* `OpenOptions`
* `Permissions`
* `File::{open, create}`
* `File::{sync_all, sync_data}`
* `File::set_len`
* `File::metadata`
* Trait implementations for `File` and `&File`
* `OpenOptions::new`
* `OpenOptions::{read, write, append, truncate, create}`
* `OpenOptions::open` - this function was modified, however, to not attempt to
reject cross-platform openings of directories. This means that some platforms
will succeed in opening a directory and others will fail.
* `Metadata::{is_dir, is_file, len, permissions}`
* `Permissions::{readonly, set_readonly}`
* `Iterator for ReadDir`
* `DirEntry::path`
* `remove_file` - like with `OpenOptions::open`, the extra windows code to
remove a readonly file has been removed. This means that removing a readonly
file will succeed on some platforms but fail on others.
* `metadata`
* `rename`
* `copy`
* `hard_link`
* `soft_link`
* `read_link`
* `create_dir`
* `create_dir_all`
* `remove_dir`
* `remove_dir_all`
* `read_dir`
The following apis remain `#[unstable]`.
* `WalkDir` and `walk` - there are many methods by which a directory walk can be
constructed, and it's unclear whether the current semantics are the right
ones. For example symlinks are not handled super well currently. This is now
behind a new `fs_walk` feature.
* `File::path` - this is an extra abstraction which the standard library
provides on top of what the system offers and it's unclear whether we should
be doing so. This is now behind a new `file_path` feature.
* `Metadata::{accessed, modified}` - we do not currently have a good
abstraction for a moment in time which is what these APIs should likely be
returning, so these remain `#[unstable]` for now. These are now behind a new
`fs_time` feature
* `set_file_times` - like with `Metadata::accessed`, we do not currently have
the appropriate abstraction for the arguments here so this API remains
unstable behind the `fs_time` feature gate.
* `PathExt` - the precise set of methods on this trait may change over time and
some methods may be removed. This API remains unstable behind the `path_ext`
feature gate.
* `set_permissions` - we may wish to expose a more granular ability to set the
permissions on a file instead of just a blanket "set all permissions" method.
This function remains behind the `fs` feature.
The following apis are now `#[deprecated]`
* The `TempDir` type is now entirely deprecated and is [located on
crates.io][tempdir] as the `tempdir` crate with [its source][github] at
rust-lang/tempdir.
[tempdir]: https://crates.io/crates/tempdir
[github]: https://github.com/rust-lang/tempdir
The stability of some of these APIs has been questioned over the past few weeks
in using these APIs, and it is intentional that the majority of APIs here are
marked `#[stable]`. The `std::fs` module has a lot of room to grow and the
material is [being tracked in a RFC issue][rfc-issue].
[rfc-issue]: https://github.com/rust-lang/rfcs/issues/939
[breaking-change]
2015-03-03 19:18:29 -08:00
|
|
|
use rustc_back::tempdir::TempDir;
|
2014-12-02 09:20:48 -05:00
|
|
|
|
|
|
|
use subcommand::Subcommand;
|
|
|
|
use term::Term;
|
2015-02-26 21:00:43 -08:00
|
|
|
use error::{err, CliResult, CommandResult};
|
2014-12-02 09:20:48 -05:00
|
|
|
use book;
|
|
|
|
use book::{Book, BookItem};
|
|
|
|
use css;
|
2015-01-12 16:51:31 -08:00
|
|
|
use javascript;
|
2014-12-02 09:20:48 -05:00
|
|
|
|
|
|
|
use rustdoc;
|
|
|
|
|
|
|
|
struct Build;
|
|
|
|
|
|
|
|
pub fn parse_cmd(name: &str) -> Option<Box<Subcommand>> {
|
|
|
|
if name == "build" {
|
2015-02-26 21:00:43 -08:00
|
|
|
Some(Box::new(Build))
|
2014-12-02 09:20:48 -05:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-26 21:00:43 -08:00
|
|
|
fn write_toc(book: &Book, path_to_root: &Path, out: &mut Write) -> io::Result<()> {
|
2014-12-02 09:20:48 -05:00
|
|
|
fn walk_items(items: &[BookItem],
|
|
|
|
section: &str,
|
|
|
|
path_to_root: &Path,
|
2015-02-26 21:00:43 -08:00
|
|
|
out: &mut Write) -> io::Result<()> {
|
2014-12-02 09:20:48 -05:00
|
|
|
for (i, item) in items.iter().enumerate() {
|
2015-02-23 00:20:42 +02:00
|
|
|
try!(walk_item(item, &format!("{}{}.", section, i + 1)[..], path_to_root, out));
|
2014-12-02 09:20:48 -05:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
fn walk_item(item: &BookItem,
|
|
|
|
section: &str,
|
|
|
|
path_to_root: &Path,
|
2015-02-26 21:00:43 -08:00
|
|
|
out: &mut Write) -> io::Result<()> {
|
2014-12-02 09:20:48 -05:00
|
|
|
try!(writeln!(out, "<li><a href='{}'><b>{}</b> {}</a>",
|
2015-02-26 21:00:43 -08:00
|
|
|
path_to_root.join(&item.path.with_extension("html")).display(),
|
2014-12-02 09:20:48 -05:00
|
|
|
section,
|
|
|
|
item.title));
|
|
|
|
if !item.children.is_empty() {
|
|
|
|
try!(writeln!(out, "<ul class='section'>"));
|
2015-02-23 00:20:42 +02:00
|
|
|
let _ = walk_items(&item.children[..], section, path_to_root, out);
|
2014-12-02 09:20:48 -05:00
|
|
|
try!(writeln!(out, "</ul>"));
|
|
|
|
}
|
|
|
|
try!(writeln!(out, "</li>"));
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2015-01-12 16:51:31 -08:00
|
|
|
try!(writeln!(out, "<div id='toc' class='mobile-hidden'>"));
|
2014-12-02 09:20:48 -05:00
|
|
|
try!(writeln!(out, "<ul class='chapter'>"));
|
2015-02-23 00:20:42 +02:00
|
|
|
try!(walk_items(&book.chapters[..], "", path_to_root, out));
|
2014-12-02 09:20:48 -05:00
|
|
|
try!(writeln!(out, "</ul>"));
|
|
|
|
try!(writeln!(out, "</div>"));
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn render(book: &Book, tgt: &Path) -> CliResult<()> {
|
2015-01-09 20:25:45 +11:00
|
|
|
let tmp = try!(TempDir::new("rust-book"));
|
2014-12-02 09:20:48 -05:00
|
|
|
|
2015-02-26 21:00:43 -08:00
|
|
|
for (_section, item) in book.iter() {
|
|
|
|
let out_path = match item.path.parent() {
|
|
|
|
Some(p) => tgt.join(p),
|
|
|
|
None => tgt.to_path_buf(),
|
|
|
|
};
|
2014-12-02 09:20:48 -05:00
|
|
|
|
|
|
|
let src;
|
2015-02-16 16:04:02 +02:00
|
|
|
if env::args().len() < 3 {
|
2015-02-26 21:00:43 -08:00
|
|
|
src = env::current_dir().unwrap().clone();
|
2014-12-02 09:20:48 -05:00
|
|
|
} else {
|
2015-03-18 09:14:54 -07:00
|
|
|
src = PathBuf::from(&env::args().nth(2).unwrap());
|
2014-12-02 09:20:48 -05:00
|
|
|
}
|
2015-04-07 15:22:57 -07:00
|
|
|
// preprocess the markdown, rerouting markdown references to html
|
|
|
|
// references
|
2015-02-26 21:00:43 -08:00
|
|
|
let mut markdown_data = String::new();
|
|
|
|
try!(File::open(&src.join(&item.path)).and_then(|mut f| {
|
|
|
|
f.read_to_string(&mut markdown_data)
|
|
|
|
}));
|
|
|
|
let preprocessed_path = tmp.path().join(item.path.file_name().unwrap());
|
2014-12-02 09:20:48 -05:00
|
|
|
{
|
2015-01-20 10:45:29 -08:00
|
|
|
let urls = markdown_data.replace(".md)", ".html)");
|
2015-02-26 21:00:43 -08:00
|
|
|
try!(File::create(&preprocessed_path).and_then(|mut f| {
|
|
|
|
f.write_all(urls.as_bytes())
|
|
|
|
}));
|
2014-12-02 09:20:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// write the prelude to a temporary HTML file for rustdoc inclusion
|
|
|
|
let prelude = tmp.path().join("prelude.html");
|
|
|
|
{
|
2015-02-26 21:00:43 -08:00
|
|
|
let mut toc = BufWriter::new(try!(File::create(&prelude)));
|
2015-01-12 16:51:31 -08:00
|
|
|
try!(writeln!(&mut toc, r#"<div id="nav">
|
|
|
|
<button id="toggle-nav">
|
|
|
|
<span class="sr-only">Toggle navigation</span>
|
|
|
|
<span class="bar"></span>
|
|
|
|
<span class="bar"></span>
|
|
|
|
<span class="bar"></span>
|
|
|
|
</button>
|
|
|
|
</div>"#));
|
2014-12-02 09:20:48 -05:00
|
|
|
let _ = write_toc(book, &item.path_to_root, &mut toc);
|
|
|
|
try!(writeln!(&mut toc, "<div id='page-wrapper'>"));
|
|
|
|
try!(writeln!(&mut toc, "<div id='page'>"));
|
|
|
|
}
|
|
|
|
|
|
|
|
// write the postlude to a temporary HTML file for rustdoc inclusion
|
|
|
|
let postlude = tmp.path().join("postlude.html");
|
|
|
|
{
|
2015-02-26 21:00:43 -08:00
|
|
|
let mut toc = BufWriter::new(try!(File::create(&postlude)));
|
|
|
|
try!(toc.write_all(javascript::JAVASCRIPT.as_bytes()));
|
2014-12-02 09:20:48 -05:00
|
|
|
try!(writeln!(&mut toc, "</div></div>"));
|
|
|
|
}
|
|
|
|
|
2015-02-26 21:00:43 -08:00
|
|
|
try!(fs::create_dir_all(&out_path));
|
2014-12-02 09:20:48 -05:00
|
|
|
|
|
|
|
let rustdoc_args: &[String] = &[
|
|
|
|
"".to_string(),
|
|
|
|
preprocessed_path.display().to_string(),
|
|
|
|
format!("-o{}", out_path.display()),
|
|
|
|
format!("--html-before-content={}", prelude.display()),
|
|
|
|
format!("--html-after-content={}", postlude.display()),
|
2015-04-07 11:23:14 -07:00
|
|
|
format!("--markdown-playground-url=http://play.rust-lang.org"),
|
2014-12-02 09:20:48 -05:00
|
|
|
format!("--markdown-css={}", item.path_to_root.join("rust-book.css").display()),
|
|
|
|
"--markdown-no-toc".to_string(),
|
|
|
|
];
|
|
|
|
let output_result = rustdoc::main_args(rustdoc_args);
|
|
|
|
if output_result != 0 {
|
2015-01-08 10:27:03 -08:00
|
|
|
let message = format!("Could not execute `rustdoc` with {:?}: {}",
|
|
|
|
rustdoc_args, output_result);
|
2015-02-26 21:00:43 -08:00
|
|
|
return Err(err(&message));
|
2014-12-02 09:20:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// create index.html from the root README
|
|
|
|
try!(fs::copy(&tgt.join("README.html"), &tgt.join("index.html")));
|
2015-04-07 11:23:14 -07:00
|
|
|
|
|
|
|
// Copy some js for playpen
|
|
|
|
let mut jquery = try!(File::create(tgt.join("jquery.js")));
|
|
|
|
let js = include_bytes!("../librustdoc/html/static/jquery-2.1.0.min.js");
|
|
|
|
try!(jquery.write_all(js));
|
|
|
|
let mut playpen = try!(File::create(tgt.join("playpen.js")));
|
|
|
|
let js = include_bytes!("../librustdoc/html/static/playpen.js");
|
|
|
|
try!(playpen.write_all(js));
|
2014-12-02 09:20:48 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Subcommand for Build {
|
|
|
|
fn parse_args(&mut self, _: &[String]) -> CliResult<()> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
fn usage(&self) {}
|
|
|
|
fn execute(&mut self, term: &mut Term) -> CommandResult<()> {
|
2015-02-26 21:00:43 -08:00
|
|
|
let cwd = env::current_dir().unwrap();
|
2014-12-02 09:20:48 -05:00
|
|
|
let src;
|
|
|
|
let tgt;
|
|
|
|
|
2015-02-16 16:04:02 +02:00
|
|
|
if env::args().len() < 3 {
|
2014-12-02 09:20:48 -05:00
|
|
|
src = cwd.clone();
|
|
|
|
} else {
|
2015-03-18 09:14:54 -07:00
|
|
|
src = PathBuf::from(&env::args().nth(2).unwrap());
|
2014-12-02 09:20:48 -05:00
|
|
|
}
|
|
|
|
|
2015-02-16 16:04:02 +02:00
|
|
|
if env::args().len() < 4 {
|
2014-12-02 09:20:48 -05:00
|
|
|
tgt = cwd.join("_book");
|
|
|
|
} else {
|
2015-03-18 09:14:54 -07:00
|
|
|
tgt = PathBuf::from(&env::args().nth(3).unwrap());
|
2014-12-02 09:20:48 -05:00
|
|
|
}
|
|
|
|
|
2015-05-13 00:06:52 -04:00
|
|
|
// `_book` directory may already exist from previous runs. Check and
|
|
|
|
// delete it if it exists.
|
|
|
|
for entry in try!(fs::read_dir(&cwd)) {
|
|
|
|
let path = try!(entry).path();
|
|
|
|
if path == tgt { try!(fs::remove_dir_all(&tgt)) }
|
|
|
|
}
|
2015-02-26 21:00:43 -08:00
|
|
|
try!(fs::create_dir(&tgt));
|
2014-12-02 09:20:48 -05:00
|
|
|
|
2015-02-26 21:00:43 -08:00
|
|
|
try!(File::create(&tgt.join("rust-book.css")).and_then(|mut f| {
|
|
|
|
f.write_all(css::STYLE.as_bytes())
|
|
|
|
}));
|
2014-12-02 09:20:48 -05:00
|
|
|
|
2015-02-26 21:00:43 -08:00
|
|
|
let mut summary = try!(File::open(&src.join("SUMMARY.md")));
|
|
|
|
match book::parse_summary(&mut summary, &src) {
|
2014-12-02 09:20:48 -05:00
|
|
|
Ok(book) => {
|
|
|
|
// execute rustdoc on the whole book
|
2015-01-09 20:25:45 +11:00
|
|
|
render(&book, &tgt)
|
2014-12-02 09:20:48 -05:00
|
|
|
}
|
|
|
|
Err(errors) => {
|
2015-01-09 20:25:45 +11:00
|
|
|
let n = errors.len();
|
2015-01-31 20:03:04 -05:00
|
|
|
for err in errors {
|
2015-02-23 00:20:42 +02:00
|
|
|
term.err(&format!("error: {}", err)[..]);
|
2014-12-02 09:20:48 -05:00
|
|
|
}
|
2015-01-09 20:25:45 +11:00
|
|
|
|
2015-02-26 21:00:43 -08:00
|
|
|
Err(err(&format!("{} errors occurred", n)))
|
2014-12-02 09:20:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|