From 596455f28c48fae9fa09127acfb758ea9f0795aa Mon Sep 17 00:00:00 2001 From: vrinek Date: Tue, 23 Feb 2016 14:03:45 +0000 Subject: [PATCH] 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;