Ask user to create `.gitignore` and skip on `--force`

This commit is contained in:
vrinek 2016-02-28 15:28:11 +00:00
parent 596455f28c
commit 1eb59428e6
3 changed files with 51 additions and 31 deletions

View File

@ -94,7 +94,7 @@ fn init(args: &ArgMatches) -> Result<(), Box<Error>> {
// If flag `--theme` is present, copy theme to src
if args.is_present("theme") {
// Skip this id `--force` is present
// Skip this if `--force` is present
if !args.is_present("force") {
// Print warning
print!("\nCopying the default theme to {:?}", book.get_src());
@ -115,6 +115,22 @@ fn init(args: &ArgMatches) -> Result<(), Box<Error>> {
}
// Because of `src/book/mdbook.rs#L37-L39`, the following will always evaluate to `true`
let is_dest_inside_root = book.get_dest().starts_with(book.get_root());
if !args.is_present("force") && is_dest_inside_root {
let gitignore = book.get_dest().join(".gitignore");
println!("\nCreating default .gitignore at {:?}", gitignore);
print!("\nAre you sure you want to continue? (y/n) ");
if confirm() {
book.create_gitignore();
println!("\n.gitignore created.");
} else {
println!("\nSkipping...\n");
}
}
println!("\nAll done, no errors...");
Ok(())

View File

@ -100,32 +100,6 @@ impl MDBook {
output!("{:?} created", self.config.get_root());
}
{
let root = self.config.get_root();
let gitignore = root.join(".gitignore");
if !gitignore.exists() {
// Gitignore does not exist, create it
debug!("[*]: {:?} does not exist, trying to create .gitignore", root.join(".gitignore"));
let dest = self.config.get_dest();
// `relative_from` is marked as unstable
// http://doc.rust-lang.org/std/path/struct.PathBuf.html#method.relative_from
let dest = dest.relative_from(root)
.expect("Destination path does not start with root path.");
let dest = dest.to_str()
.expect("No destination path found.");
let mut f = try!(File::create(&root.join(".gitignore")));
debug!("[*]: Writing to .gitignore");
try!(writeln!(f, "{}", dest));
}
}
{
let dest = self.config.get_dest();
let src = self.config.get_src();
@ -186,12 +160,37 @@ impl MDBook {
Ok(())
}
pub fn create_gitignore(&self) {
let gitignore = self.get_gitignore();
if !gitignore.exists() {
// Gitignore does not exist, create it
debug!("[*]: {:?} does not exist, trying to create .gitignore", gitignore);
let mut f = File::create(&gitignore)
.expect("Could not create file.");
debug!("[*]: Writing to .gitignore");
writeln!(f, "# Ignore everything within this folder")
.expect("Could not write to file.");
writeln!(f, "*")
.expect("Could not write to file.");
writeln!(f, "")
.expect("Could not write to file.");
writeln!(f, "# Except this file")
.expect("Could not write to file.");
writeln!(f, "!.gitignore")
.expect("Could not write to file.");
}
}
/// The `build()` method is the one where everything happens. First it parses `SUMMARY.md` to
/// construct the book's structure in the form of a `Vec<BookItem>` and then calls `render()`
/// method of the current renderer.
///
/// It is the renderer who generates all the output files.
pub fn build(&mut self) -> Result<(), Box<Error>> {
debug!("[fn]: build");
@ -206,6 +205,10 @@ impl MDBook {
}
pub fn get_gitignore(&self) -> PathBuf {
self.config.get_dest().join(".gitignore")
}
pub fn copy_theme(&self) -> Result<(), Box<Error>> {
debug!("[fn]: copy_theme");
@ -323,6 +326,10 @@ impl MDBook {
Ok(())
}
pub fn get_root(&self) -> &Path {
self.config.get_root()
}
pub fn set_dest(mut self, dest: &Path) -> Self {
// Handle absolute and relative paths

View File

@ -69,9 +69,6 @@
//!
//! Make sure to take a look at it.
// Used in `MDBook.init()`
#![feature(path_relative_from)]
#[macro_use]
pub mod macros;
pub mod book;