Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate simple .gitignore on init #111

Merged
merged 2 commits into from
Mar 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/bin/mdbook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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(())
Expand Down
35 changes: 34 additions & 1 deletion src/book/mdbook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,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");

Expand All @@ -180,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");

Expand Down Expand Up @@ -297,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
Expand Down