Merge pull request #115 from vrinek/init-with-gitignore-take-2

Move `.gitignore` directly under the root folder
This commit is contained in:
Mathieu David 2016-03-07 12:09:25 +01:00
commit f23a5f2729
2 changed files with 14 additions and 11 deletions

View File

@ -115,7 +115,7 @@ fn init(args: &ArgMatches) -> Result<(), Box<Error>> {
} }
// Because of `src/book/mdbook.rs#L37-L39`, the following will always evaluate to `true` // Because of `src/book/mdbook.rs#L37-L39`, `dest` will always start with `root`
let is_dest_inside_root = book.get_dest().starts_with(book.get_root()); let is_dest_inside_root = book.get_dest().starts_with(book.get_root());
if !args.is_present("force") && is_dest_inside_root { if !args.is_present("force") && is_dest_inside_root {

View File

@ -166,6 +166,17 @@ impl MDBook {
if !gitignore.exists() { if !gitignore.exists() {
// Gitignore does not exist, create it // Gitignore does not exist, create it
// Because of `src/book/mdbook.rs#L37-L39`, `dest` will always start with `root`. If it
// is not, `strip_prefix` will return an Error.
if !self.get_dest().starts_with(self.get_root()) {
return;
}
let relative = self.get_dest().strip_prefix(self.get_root())
.expect("Destination is not relative to root.");
let relative = relative.to_str()
.expect("Path could not be yielded into a string slice.");
debug!("[*]: {:?} does not exist, trying to create .gitignore", gitignore); debug!("[*]: {:?} does not exist, trying to create .gitignore", gitignore);
let mut f = File::create(&gitignore) let mut f = File::create(&gitignore)
@ -173,15 +184,7 @@ impl MDBook {
debug!("[*]: Writing to .gitignore"); debug!("[*]: Writing to .gitignore");
writeln!(f, "# Ignore everything within this folder") writeln!(f, "{}", relative)
.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."); .expect("Could not write to file.");
} }
} }
@ -206,7 +209,7 @@ impl MDBook {
pub fn get_gitignore(&self) -> PathBuf { pub fn get_gitignore(&self) -> PathBuf {
self.config.get_dest().join(".gitignore") self.config.get_root().join(".gitignore")
} }
pub fn copy_theme(&self) -> Result<(), Box<Error>> { pub fn copy_theme(&self) -> Result<(), Box<Error>> {