Skip to content

Commit 1eb5942

Browse files
committed
Ask user to create .gitignore and skip on --force
1 parent 596455f commit 1eb5942

File tree

3 files changed

+51
-31
lines changed

3 files changed

+51
-31
lines changed

src/bin/mdbook.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ fn init(args: &ArgMatches) -> Result<(), Box<Error>> {
9494
// If flag `--theme` is present, copy theme to src
9595
if args.is_present("theme") {
9696

97-
// Skip this id `--force` is present
97+
// Skip this if `--force` is present
9898
if !args.is_present("force") {
9999
// Print warning
100100
print!("\nCopying the default theme to {:?}", book.get_src());
@@ -115,6 +115,22 @@ fn init(args: &ArgMatches) -> Result<(), Box<Error>> {
115115

116116
}
117117

118+
// Because of `src/book/mdbook.rs#L37-L39`, the following will always evaluate to `true`
119+
let is_dest_inside_root = book.get_dest().starts_with(book.get_root());
120+
121+
if !args.is_present("force") && is_dest_inside_root {
122+
let gitignore = book.get_dest().join(".gitignore");
123+
println!("\nCreating default .gitignore at {:?}", gitignore);
124+
print!("\nAre you sure you want to continue? (y/n) ");
125+
126+
if confirm() {
127+
book.create_gitignore();
128+
println!("\n.gitignore created.");
129+
} else {
130+
println!("\nSkipping...\n");
131+
}
132+
}
133+
118134
println!("\nAll done, no errors...");
119135

120136
Ok(())

src/book/mdbook.rs

+34-27
Original file line numberDiff line numberDiff line change
@@ -100,32 +100,6 @@ impl MDBook {
100100
output!("{:?} created", self.config.get_root());
101101
}
102102

103-
{
104-
let root = self.config.get_root();
105-
let gitignore = root.join(".gitignore");
106-
107-
if !gitignore.exists() {
108-
109-
// Gitignore does not exist, create it
110-
111-
debug!("[*]: {:?} does not exist, trying to create .gitignore", root.join(".gitignore"));
112-
113-
let dest = self.config.get_dest();
114-
// `relative_from` is marked as unstable
115-
// http://doc.rust-lang.org/std/path/struct.PathBuf.html#method.relative_from
116-
let dest = dest.relative_from(root)
117-
.expect("Destination path does not start with root path.");
118-
let dest = dest.to_str()
119-
.expect("No destination path found.");
120-
121-
let mut f = try!(File::create(&root.join(".gitignore")));
122-
123-
debug!("[*]: Writing to .gitignore");
124-
125-
try!(writeln!(f, "{}", dest));
126-
}
127-
}
128-
129103
{
130104
let dest = self.config.get_dest();
131105
let src = self.config.get_src();
@@ -186,12 +160,37 @@ impl MDBook {
186160
Ok(())
187161
}
188162

163+
pub fn create_gitignore(&self) {
164+
let gitignore = self.get_gitignore();
165+
166+
if !gitignore.exists() {
167+
// Gitignore does not exist, create it
168+
169+
debug!("[*]: {:?} does not exist, trying to create .gitignore", gitignore);
170+
171+
let mut f = File::create(&gitignore)
172+
.expect("Could not create file.");
173+
174+
debug!("[*]: Writing to .gitignore");
175+
176+
writeln!(f, "# Ignore everything within this folder")
177+
.expect("Could not write to file.");
178+
writeln!(f, "*")
179+
.expect("Could not write to file.");
180+
writeln!(f, "")
181+
.expect("Could not write to file.");
182+
writeln!(f, "# Except this file")
183+
.expect("Could not write to file.");
184+
writeln!(f, "!.gitignore")
185+
.expect("Could not write to file.");
186+
}
187+
}
188+
189189
/// The `build()` method is the one where everything happens. First it parses `SUMMARY.md` to
190190
/// construct the book's structure in the form of a `Vec<BookItem>` and then calls `render()`
191191
/// method of the current renderer.
192192
///
193193
/// It is the renderer who generates all the output files.
194-
195194
pub fn build(&mut self) -> Result<(), Box<Error>> {
196195
debug!("[fn]: build");
197196

@@ -206,6 +205,10 @@ impl MDBook {
206205
}
207206

208207

208+
pub fn get_gitignore(&self) -> PathBuf {
209+
self.config.get_dest().join(".gitignore")
210+
}
211+
209212
pub fn copy_theme(&self) -> Result<(), Box<Error>> {
210213
debug!("[fn]: copy_theme");
211214

@@ -323,6 +326,10 @@ impl MDBook {
323326
Ok(())
324327
}
325328

329+
pub fn get_root(&self) -> &Path {
330+
self.config.get_root()
331+
}
332+
326333
pub fn set_dest(mut self, dest: &Path) -> Self {
327334

328335
// Handle absolute and relative paths

src/lib.rs

-3
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,6 @@
6969
//!
7070
//! Make sure to take a look at it.
7171
72-
// Used in `MDBook.init()`
73-
#![feature(path_relative_from)]
74-
7572
#[macro_use]
7673
pub mod macros;
7774
pub mod book;

0 commit comments

Comments
 (0)