Skip to content

Commit

Permalink
doc: try to make it dir-depth agnostic
Browse files Browse the repository at this point in the history
Still ironing out some problems
  • Loading branch information
psyomn committed Nov 9, 2015
1 parent 84a51ee commit f84b349
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
*.swp
target
Binary file removed src/.main.rs.swp
Binary file not shown.
26 changes: 25 additions & 1 deletion src/doc.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::env;
use std::fs::File;
use std::path::PathBuf;
use std::io::prelude::*;
use std::io::BufReader;
use regex::Regex;
Expand Down Expand Up @@ -131,7 +132,10 @@ fn process_template<T: Read>(template: &mut T,
}

fn get_crate_info() -> Result<CrateInfo, String> {
let current_dir = env::current_dir().unwrap();
let current_dir = match project_root_dir() {
Some(v) => v,
None => return Err("Not in a rust project".into()),
};

let mut cargo_toml = match File::open(current_dir.join("Cargo.toml")) {
Ok(file) => file,
Expand Down Expand Up @@ -193,3 +197,23 @@ fn append_license(readme: String, license: &str) -> String {

new_readme
}

/// Given the current directory, start from there, and go up, and up, until a Cargo.toml file has
/// been found. If a Cargo.toml folder has been found, then we have found the project dir. If not,
/// nothing is found, and we return None.
pub fn project_root_dir() -> Option<PathBuf> {
let mut currpath= env::current_dir().unwrap();

while currpath.parent().is_some() {
currpath.push("Cargo.toml");
if currpath.is_file() {
currpath.pop(); // found, remove toml, return project root
println!("return : {:?}", currpath);
return Some(currpath);
}
currpath.pop(); // remove toml filename
currpath.pop(); // next dir
}

None
}
17 changes: 14 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,20 @@
//!
//! You can override the displaying of your crate's name and license using `{{crate}}`
//! and `{{license}}`.
#![feature(path_ext)]

#[macro_use]
extern crate clap;
extern crate toml;
extern crate regex;

mod doc;

use std::env;
use std::io::{Write, ErrorKind};
use std::fs::File;
use std::fs::{File};
use clap::{Arg, ArgMatches, App, AppSettings, SubCommand};
use doc::project_root_dir;

const DEFAULT_TEMPLATE: &'static str = "README.tpl";

Expand Down Expand Up @@ -165,7 +168,13 @@ fn main() {
}

fn execute(m: &ArgMatches) {
let current_dir = env::current_dir().unwrap();
let current_dir = match project_root_dir() {
Some(v) => v,
None => {
println!("This doesn't look like a Rust/Cargo project");
return;
},
};

let input = m.value_of("INPUT");
let output = m.value_of("OUTPUT");
Expand Down Expand Up @@ -235,6 +244,7 @@ fn execute(m: &ArgMatches) {
},
};


match dest.as_mut() {
Some(dest) => {
dest.write_all(doc_string.as_bytes())
Expand All @@ -247,3 +257,4 @@ fn execute(m: &ArgMatches) {
None => println!("{}", doc_string),
}
}

0 comments on commit f84b349

Please sign in to comment.