Updated the binary to use the new signature

This commit is contained in:
Michael Bryan 2017-07-08 20:25:59 +08:00
parent 65a60bf81f
commit ed83e0ec10
6 changed files with 29 additions and 22 deletions

View File

@ -17,7 +17,7 @@ pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> {
// Build command implementation // Build command implementation
pub fn execute(args: &ArgMatches) -> Result<()> { pub fn execute(args: &ArgMatches) -> Result<()> {
let book_dir = get_book_dir(args); let book_dir = get_book_dir(args);
let book = MDBook::new(&book_dir).read_config()?; let book = MDBook::new(&book_dir)?.read_config()?;
let mut book = match args.value_of("dest-dir") { let mut book = match args.value_of("dest-dir") {
Some(dest_dir) => book.with_destination(dest_dir), Some(dest_dir) => book.with_destination(dest_dir),

View File

@ -19,10 +19,7 @@ pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> {
pub fn execute(args: &ArgMatches) -> Result<()> { pub fn execute(args: &ArgMatches) -> Result<()> {
let book_dir = get_book_dir(args); let book_dir = get_book_dir(args);
let mut book = MDBook::new(&book_dir); let book = MDBook::init(&book_dir)?;
// Call the function that does the initialization
book.init()?;
// If flag `--theme` is present, copy theme to src // If flag `--theme` is present, copy theme to src
if args.is_present("theme") { if args.is_present("theme") {

View File

@ -33,7 +33,7 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
const RELOAD_COMMAND: &'static str = "reload"; const RELOAD_COMMAND: &'static str = "reload";
let book_dir = get_book_dir(args); let book_dir = get_book_dir(args);
let book = MDBook::new(&book_dir).read_config()?; let book = MDBook::new(&book_dir)?.read_config()?;
let mut book = match args.value_of("dest-dir") { let mut book = match args.value_of("dest-dir") {
Some(dest_dir) => book.with_destination(Path::new(dest_dir)), Some(dest_dir) => book.with_destination(Path::new(dest_dir)),
@ -53,7 +53,8 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
let address = format!("{}:{}", interface, port); let address = format!("{}:{}", interface, port);
let ws_address = format!("{}:{}", interface, ws_port); let ws_address = format!("{}:{}", interface, ws_port);
book.set_livereload(format!(r#" book.set_livereload(format!(
r#"
<script type="text/javascript"> <script type="text/javascript">
var socket = new WebSocket("ws://{}:{}"); var socket = new WebSocket("ws://{}:{}");
socket.onmessage = function (event) {{ socket.onmessage = function (event) {{
@ -70,7 +71,8 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
"#, "#,
public_address, public_address,
ws_port, ws_port,
RELOAD_COMMAND)); RELOAD_COMMAND
));
book.build()?; book.build()?;

View File

@ -14,7 +14,7 @@ pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> {
pub fn execute(args: &ArgMatches) -> Result<()> { pub fn execute(args: &ArgMatches) -> Result<()> {
let library_paths: Vec<&str> = args.values_of("library-path").map(|v| v.collect()).unwrap_or_default(); let library_paths: Vec<&str> = args.values_of("library-path").map(|v| v.collect()).unwrap_or_default();
let book_dir = get_book_dir(args); let book_dir = get_book_dir(args);
let mut book = MDBook::new(&book_dir).read_config()?; let mut book = MDBook::new(&book_dir)?.read_config()?;
book.test(library_paths)?; book.test(library_paths)?;

View File

@ -24,7 +24,7 @@ pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> {
// Watch command implementation // Watch command implementation
pub fn execute(args: &ArgMatches) -> Result<()> { pub fn execute(args: &ArgMatches) -> Result<()> {
let book_dir = get_book_dir(args); let book_dir = get_book_dir(args);
let book = MDBook::new(&book_dir).read_config()?; let book = MDBook::new(&book_dir)?.read_config()?;
let mut book = match args.value_of("dest-dir") { let mut book = match args.value_of("dest-dir") {
Some(dest_dir) => book.with_destination(dest_dir), Some(dest_dir) => book.with_destination(dest_dir),
@ -53,7 +53,8 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
// Calls the closure when a book source file is changed. This is blocking! // Calls the closure when a book source file is changed. This is blocking!
pub fn trigger_on_change<F>(book: &mut MDBook, closure: F) -> () pub fn trigger_on_change<F>(book: &mut MDBook, closure: F) -> ()
where F: Fn(&Path, &mut MDBook) -> () where
F: Fn(&Path, &mut MDBook) -> (),
{ {
use self::notify::RecursiveMode::*; use self::notify::RecursiveMode::*;
use self::notify::DebouncedEvent::*; use self::notify::DebouncedEvent::*;
@ -76,19 +77,23 @@ pub fn trigger_on_change<F>(book: &mut MDBook, closure: F) -> ()
}; };
// Add the theme directory to the watcher // Add the theme directory to the watcher
watcher.watch(book.get_theme_path(), Recursive).unwrap_or_default(); watcher
.watch(book.get_theme_path(), Recursive)
.unwrap_or_default();
// Add the book.{json,toml} file to the watcher if it exists, because it's not // Add the book.{json,toml} file to the watcher if it exists, because it's not
// located in the source directory // located in the source directory
if watcher if watcher
.watch(book.get_root().join("book.json"), NonRecursive) .watch(book.get_root().join("book.json"), NonRecursive)
.is_err() { .is_err()
{
// do nothing if book.json is not found // do nothing if book.json is not found
} }
if watcher if watcher
.watch(book.get_root().join("book.toml"), NonRecursive) .watch(book.get_root().join("book.toml"), NonRecursive)
.is_err() { .is_err()
{
// do nothing if book.toml is not found // do nothing if book.toml is not found
} }

View File

@ -7,13 +7,15 @@ use std::io::Write;
use mdbook::MDBook; use mdbook::MDBook;
use tempdir::TempDir; use tempdir::TempDir;
// Tests that config values unspecified in the configuration file do not overwrite // Tests that config values unspecified in the configuration file do not
// overwrite
// values specified earlier. // values specified earlier.
#[test] #[test]
fn do_not_overwrite_unspecified_config_values() { fn do_not_overwrite_unspecified_config_values() {
let dir = TempDir::new("mdbook").expect("Could not create a temp dir"); let dir = TempDir::new("mdbook").expect("Could not create a temp dir");
let book = MDBook::new(dir.path()) let book = MDBook::init(dir.path())
.unwrap()
.with_source("bar") .with_source("bar")
.with_destination("baz") .with_destination("baz")
.with_mathjax_support(true); .with_mathjax_support(true);
@ -33,7 +35,9 @@ fn do_not_overwrite_unspecified_config_values() {
// Try with a partial config file // Try with a partial config file
let file_path = dir.path().join("book.toml"); let file_path = dir.path().join("book.toml");
let mut f = File::create(file_path).expect("Could not create config file"); let mut f = File::create(file_path).expect("Could not create config file");
f.write_all(br#"source = "barbaz""#).expect("Could not write to config file"); f.write_all(br#"source = "barbaz""#).expect(
"Could not write to config file",
);
f.sync_all().expect("Could not sync the file"); f.sync_all().expect("Could not sync the file");
let book = book.read_config().expect("Error reading the config file"); let book = book.read_config().expect("Error reading the config file");
@ -43,4 +47,3 @@ fn do_not_overwrite_unspecified_config_values() {
assert_eq!(book.get_destination(), dir.path().join("baz")); assert_eq!(book.get_destination(), dir.path().join("baz"));
assert_eq!(book.get_mathjax_support(), true); assert_eq!(book.get_mathjax_support(), true);
} }