diff --git a/cli/src/lib.rs b/cli/src/lib.rs index 1cd2ea0eab..c7efdbc1a5 100644 --- a/cli/src/lib.rs +++ b/cli/src/lib.rs @@ -94,7 +94,7 @@ pub fn build_app<'a>() -> App<'a> { .arg(Arg::with_name(DIRECTORY_OR_FILES) .index(1) .multiple(true) - .required(true) + .required(false) .help("The directory or files to build documentation for") ) diff --git a/cli/src/main.rs b/cli/src/main.rs index fccf700c96..c4d659a5be 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -9,6 +9,7 @@ use target_lexicon::Triple; #[cfg(feature = "llvm")] use roc_cli::build; +use std::ffi::{OsStr, OsString}; #[cfg(not(feature = "llvm"))] fn build(_target: &Triple, _matches: &clap::ArgMatches, _config: BuildConfig) -> io::Result { @@ -68,19 +69,34 @@ fn main() -> io::Result<()> { Ok(0) } Some(CMD_DOCS) => { - let values = matches + let maybe_values = matches .subcommand_matches(CMD_DOCS) .unwrap() - .values_of_os(DIRECTORY_OR_FILES) - .unwrap(); + .values_of_os(DIRECTORY_OR_FILES); + + let mut values: Vec = Vec::new(); + + match maybe_values { + None => { + let mut os_string_values: Vec = Vec::new(); + read_all_roc_files(&OsStr::new("./").to_os_string(), &mut os_string_values)?; + for os_string in os_string_values { + values.push(os_string); + } + } + Some(os_values) => { + for os_str in os_values { + values.push(os_str.to_os_string()); + } + } + } let mut roc_files = Vec::new(); // Populate roc_files for os_str in values { - let metadata = fs::metadata(os_str)?; - - roc_files_recursive(os_str, metadata.file_type(), &mut roc_files)?; + let metadata = fs::metadata(os_str.clone())?; + roc_files_recursive(os_str.as_os_str(), metadata.file_type(), &mut roc_files)?; } docs(roc_files); @@ -93,6 +109,26 @@ fn main() -> io::Result<()> { std::process::exit(exit_code); } +fn read_all_roc_files( + dir: &OsString, + mut roc_file_paths: &mut Vec, +) -> Result<(), std::io::Error> { + let entries = fs::read_dir(dir)?; + + for entry in entries { + let path = entry?.path(); + + if path.is_dir() { + read_all_roc_files(&path.into_os_string(), &mut roc_file_paths)?; + } else if path.extension().and_then(OsStr::to_str) == Some("roc") { + let file_path = path.into_os_string(); + roc_file_paths.push(file_path); + } + } + + Ok(()) +} + fn roc_files_recursive>( path: P, file_type: FileType,