Skip to content

Commit

Permalink
Add initial compilation (-i) flag to tecomp CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
kosude committed Apr 15, 2024
1 parent d708219 commit fa3b4f5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
4 changes: 4 additions & 0 deletions compiler/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ pub struct WatchArgs {
/// Path to a file or directory to watch
pub watch: String,

/// Initially compile the document when watch is started
#[arg(short = 'i', long, action)]
pub initial_make: bool,

#[command(flatten)]
pub com: CommonArgGroup,
}
1 change: 1 addition & 0 deletions compiler/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ fn main() {
&str_to_pathbuf(&o.com.outdir, true)?,
&str_to_pathbuf(&o.watch, true)?,
args_to_comp_verbosity_enum(o.com.verbose),
o.initial_make,
)?;

return Ok(());
Expand Down
29 changes: 20 additions & 9 deletions compiler/src/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub fn watch_sync<P: AsRef<Path> + Debug>(
outdir: P,
watch: P,
verbosity: compiler::OutputVerbosity,
initial_make: bool,
) -> CompResult<()> {
let (tx, rx) = std::sync::mpsc::channel();
let texpdfc = &texpdfc.as_ref().to_path_buf();
Expand All @@ -48,6 +49,12 @@ pub fn watch_sync<P: AsRef<Path> + Debug>(
.verbosity(verbosity)
.to_owned();

// first compilation done here, if it is specified
if initial_make {
log::info("Initial pre-watch compilation (-i) specified...");
watch_compile(&compiler);
}

// set up debouncer watcher
let mut debouncer = new_debouncer(
Duration::from_millis(500), // this value seems to debounce successfully without too long of a delay
Expand Down Expand Up @@ -88,15 +95,7 @@ pub fn watch_sync<P: AsRef<Path> + Debug>(

// check if any of the recieved errors are to be tracked
if evs.iter().any(|ev| tracked_evs.contains(&ev.kind)) {
// handle the error here (safely) instead of in main -- we don't want to exit the program on error when watching.
if let Err(e) = || -> CompResult<CompileOutput> {
compiler
.compile()
.map_err(|_| CompError::CompilationError("Compile error".to_string()))
}() {
// this just prints the error without stopping the process
e.handle_safe();
}
watch_compile(&compiler);
}
}
Err(errs) => {
Expand All @@ -109,3 +108,15 @@ pub fn watch_sync<P: AsRef<Path> + Debug>(

Ok(())
}

fn watch_compile(compiler: &Compiler) {
// handle the error here (safely) instead of in main -- we don't want to exit the program on error when watching.
if let Err(e) = || -> CompResult<CompileOutput> {
compiler
.compile()
.map_err(|_| CompError::CompilationError("Compile error".to_string()))
}() {
// this just prints the error without stopping the process
e.handle_safe();
}
}

0 comments on commit fa3b4f5

Please sign in to comment.