Generate docs basde on package root .roc file

This commit is contained in:
Richard Feldman 2022-12-01 12:58:33 -05:00
parent e549456668
commit 15590fb31b
No known key found for this signature in database
GPG key ID: F1F21AA5B1D9E43B
6 changed files with 115 additions and 188 deletions

View file

@ -1,51 +1,27 @@
//! Provides a binary that is only used for static build servers.
use clap::{Arg, Command};
use roc_docs::generate_docs_html;
use std::fs::{self, FileType};
use std::io;
use std::path::{Path, PathBuf};
use std::path::PathBuf;
pub const DIRECTORY_OR_FILES: &str = "DIRECTORY_OR_FILES";
pub const ROC_FILE: &str = "ROC_FILE";
const DEFAULT_ROC_FILENAME: &str = "main.roc";
fn main() -> io::Result<()> {
let matches = Command::new("roc-docs")
.about("Build HTML documentation files from the given .roc files")
.about("Generate documentation for a Roc package")
.arg(
Arg::new(DIRECTORY_OR_FILES)
Arg::new(ROC_FILE)
.multiple_values(true)
.required(true)
.help("The directory or files to build documentation for")
.allow_invalid_utf8(true),
.help("The package's main .roc file")
.allow_invalid_utf8(true)
.required(false)
.default_value(DEFAULT_ROC_FILENAME),
)
.get_matches();
let mut roc_files = Vec::new();
// Populate roc_files
for os_str in matches.values_of_os(DIRECTORY_OR_FILES).unwrap() {
let metadata = fs::metadata(os_str)?;
roc_files_recursive(os_str, metadata.file_type(), &mut roc_files)?;
}
generate_docs_html(roc_files);
Ok(())
}
fn roc_files_recursive<P: AsRef<Path>>(
path: P,
file_type: FileType,
roc_files: &mut Vec<PathBuf>,
) -> io::Result<()> {
if file_type.is_dir() {
for entry_res in fs::read_dir(path)? {
let entry = entry_res?;
roc_files_recursive(entry.path(), entry.file_type()?, roc_files)?;
}
} else {
roc_files.push(path.as_ref().to_path_buf());
}
generate_docs_html(PathBuf::from(matches.value_of_os(ROC_FILE).unwrap()));
Ok(())
}