From a96752a65a29a6216770e4ab6446f8937224992c Mon Sep 17 00:00:00 2001 From: Richard Feldman Date: Thu, 19 Oct 2023 17:21:27 -0400 Subject: [PATCH] Add --output CLI flag for `roc docs` --- crates/cli/src/lib.rs | 9 +++++++++ crates/cli/src/main.rs | 7 ++++--- crates/docs/src/lib.rs | 5 +---- crates/docs_cli/src/main.rs | 5 ++++- 4 files changed, 18 insertions(+), 8 deletions(-) diff --git a/crates/cli/src/lib.rs b/crates/cli/src/lib.rs index 85473f95f0..3b09184282 100644 --- a/crates/cli/src/lib.rs +++ b/crates/cli/src/lib.rs @@ -63,6 +63,7 @@ pub const FLAG_LINKER: &str = "linker"; pub const FLAG_PREBUILT: &str = "prebuilt-platform"; pub const FLAG_CHECK: &str = "check"; pub const FLAG_WASM_STACK_SIZE_KB: &str = "wasm-stack-size-kb"; +pub const FLAG_OUTPUT: &str = "output"; pub const ROC_FILE: &str = "ROC_FILE"; pub const ROC_DIR: &str = "ROC_DIR"; pub const GLUE_DIR: &str = "GLUE_DIR"; @@ -71,6 +72,7 @@ pub const DIRECTORY_OR_FILES: &str = "DIRECTORY_OR_FILES"; pub const ARGS_FOR_APP: &str = "ARGS_FOR_APP"; const VERSION: &str = include_str!("../../../version.txt"); +const DEFAULT_GENERATED_DOCS_DIR: &str = "generated-docs"; pub fn build_app() -> Command { let flag_optimize = Arg::new(FLAG_OPTIMIZE) @@ -276,6 +278,13 @@ pub fn build_app() -> Command { .subcommand( Command::new(CMD_DOCS) .about("Generate documentation for a Roc package") + .arg(Arg::new(FLAG_OUTPUT) + .long(FLAG_OUTPUT) + .help("Output directory for the generated documentation files.") + .value_parser(value_parser!(OsString)) + .required(false) + .default_value(DEFAULT_GENERATED_DOCS_DIR), + ) .arg(Arg::new(ROC_FILE) .help("The package's main .roc file") .value_parser(value_parser!(PathBuf)) diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index e0ced7b2c0..db5f286415 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -4,8 +4,8 @@ use roc_build::program::{check_file, CodeGenBackend}; use roc_cli::{ build_app, format, test, BuildConfig, FormatMode, CMD_BUILD, CMD_CHECK, CMD_DEV, CMD_DOCS, CMD_FORMAT, CMD_GEN_STUB_LIB, CMD_GLUE, CMD_REPL, CMD_RUN, CMD_TEST, CMD_VERSION, - DIRECTORY_OR_FILES, FLAG_CHECK, FLAG_DEV, FLAG_LIB, FLAG_NO_LINK, FLAG_TARGET, FLAG_TIME, - GLUE_DIR, GLUE_SPEC, ROC_FILE, + DIRECTORY_OR_FILES, FLAG_CHECK, FLAG_DEV, FLAG_LIB, FLAG_NO_LINK, FLAG_OUTPUT, FLAG_TARGET, + FLAG_TIME, GLUE_DIR, GLUE_SPEC, ROC_FILE, }; use roc_docs::generate_docs_html; use roc_error_macros::user_error; @@ -213,8 +213,9 @@ fn main() -> io::Result<()> { Some((CMD_REPL, _)) => Ok(roc_repl_cli::main()), Some((CMD_DOCS, matches)) => { let root_path = matches.get_one::(ROC_FILE).unwrap(); + let out_dir = matches.get_one::(FLAG_OUTPUT).unwrap(); - generate_docs_html(root_path.to_owned()); + generate_docs_html(root_path.to_owned(), out_dir.as_ref()); Ok(0) } diff --git a/crates/docs/src/lib.rs b/crates/docs/src/lib.rs index c642615678..f2b312f712 100644 --- a/crates/docs/src/lib.rs +++ b/crates/docs/src/lib.rs @@ -17,12 +17,9 @@ use roc_region::all::Region; use std::fs; use std::path::{Path, PathBuf}; -const BUILD_DIR: &str = "./generated-docs"; - const LINK_SVG: &str = include_str!("./static/link.svg"); -pub fn generate_docs_html(root_file: PathBuf) { - let build_dir = Path::new(BUILD_DIR); +pub fn generate_docs_html(root_file: PathBuf, build_dir: &Path) { let loaded_module = load_module_for_docs(root_file); // TODO get these from the platform's source file rather than hardcoding them! diff --git a/crates/docs_cli/src/main.rs b/crates/docs_cli/src/main.rs index 40f67f560c..d7f8a931d7 100644 --- a/crates/docs_cli/src/main.rs +++ b/crates/docs_cli/src/main.rs @@ -20,7 +20,10 @@ fn main() -> io::Result<()> { .get_matches(); // Populate roc_files - generate_docs_html(matches.get_one::(ROC_FILE).unwrap().to_owned()); + generate_docs_html( + matches.get_one::(ROC_FILE).unwrap().to_owned(), + &PathBuf::from("./generated-docs"), + ); Ok(()) }