From 596455f28c48fae9fa09127acfb758ea9f0795aa Mon Sep 17 00:00:00 2001 From: vrinek Date: Tue, 23 Feb 2016 14:03:45 +0000 Subject: [PATCH 1/2] Generate simple .gitignore on init --- src/book/mdbook.rs | 26 ++++++++++++++++++++++++++ src/lib.rs | 3 +++ 2 files changed, 29 insertions(+) diff --git a/src/book/mdbook.rs b/src/book/mdbook.rs index 521606d3..a8ffc437 100644 --- a/src/book/mdbook.rs +++ b/src/book/mdbook.rs @@ -100,6 +100,32 @@ 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(); diff --git a/src/lib.rs b/src/lib.rs index edc2fe9e..eb45041f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -69,6 +69,9 @@ //! //! Make sure to take a look at it. +// Used in `MDBook.init()` +#![feature(path_relative_from)] + #[macro_use] pub mod macros; pub mod book; From 1eb59428e67052fea3d6cdb907e20fbab63442c9 Mon Sep 17 00:00:00 2001 From: vrinek Date: Sun, 28 Feb 2016 15:28:11 +0000 Subject: [PATCH 2/2] Ask user to create `.gitignore` and skip on `--force` --- src/bin/mdbook.rs | 18 +++++++++++++- src/book/mdbook.rs | 61 ++++++++++++++++++++++++++-------------------- src/lib.rs | 3 --- 3 files changed, 51 insertions(+), 31 deletions(-) diff --git a/src/bin/mdbook.rs b/src/bin/mdbook.rs index 50b9a2e2..357a0efb 100644 --- a/src/bin/mdbook.rs +++ b/src/bin/mdbook.rs @@ -94,7 +94,7 @@ fn init(args: &ArgMatches) -> Result<(), Box> { // 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> { } + // 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(()) diff --git a/src/book/mdbook.rs b/src/book/mdbook.rs index a8ffc437..8f964b26 100644 --- a/src/book/mdbook.rs +++ b/src/book/mdbook.rs @@ -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` 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> { 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> { 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 diff --git a/src/lib.rs b/src/lib.rs index eb45041f..edc2fe9e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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;