use super::*;
use std::io::Write;
use std::path::Path;
use std::sync::Arc;
use crate::incremental::{CachePolicy, scope, scope_async};
use crate::reporting::{Event, Reporter, Reporting};
#[derive(Debug, Clone, Default)]
pub struct CommandRunner {
pub(super) reporting: Reporting,
cache_policy: CachePolicy,
}
impl CommandRunner {
pub fn with_reporter(reporter: Arc<dyn Reporter>) -> Self {
Self {
reporting: Reporting::new(reporter),
cache_policy: CachePolicy::default(),
}
}
pub fn with_cache_policy(mut self, policy: CachePolicy) -> Self {
self.cache_policy = policy;
self
}
pub fn build(&self, options: BuildOptions) -> Result<(), CommandError> {
let command = self.reporting.command("build");
let site = pipeline::runtime()?.block_on(scope_async(
self.cache_policy,
pipeline::build_attempt(
&options,
ExecutionDeadlines::default(),
Box::pin(watching::termination()),
&self.reporting,
),
))?;
command.finish(Some(&options.output), Some(site.page_count()));
Ok(())
}
pub fn check(&self, options: CheckOptions) -> Result<CheckReport, CommandError> {
let command = self.reporting.command("check");
let report = scope(self.cache_policy, || {
pipeline::check_attempt(options, &self.reporting)
})?;
for diagnostic in &report.diagnostics {
self.reporting.emit(Event::Diagnostic(diagnostic));
}
command.finish(None, None);
Ok(report)
}
pub fn export_bundle(&self, options: BundleExportOptions) -> Result<(), CommandError> {
let command = self.reporting.command("bundle export");
let output = options.output.clone();
pipeline::runtime()?.block_on(scope_async(
self.cache_policy,
pipeline::export_bundle(options, Box::pin(watching::termination()), &self.reporting),
))?;
command.finish(Some(&output), None);
Ok(())
}
pub fn extract(&self, options: ExtractOptions) -> Result<(), CommandError> {
pipeline::runtime()?.block_on(self.extract_with(
options,
ExecutionDeadlines::default(),
Box::pin(watching::termination()),
))?;
Ok(())
}
pub async fn extract_with(
&self,
options: ExtractOptions,
deadlines: ExecutionDeadlines,
cancellation: ExecutionCancellation<'_>,
) -> Result<PathBuf, CommandError> {
let command = self.reporting.command("extract");
let output = scope_async(
self.cache_policy,
pipeline::extract_attempt(options, None, deadlines, cancellation, &self.reporting),
)
.await?;
command.finish(Some(&output), None);
Ok(output)
}
pub fn watch_extract(&self, options: ExtractOptions) -> Result<(), CommandError> {
pipeline::runtime()?.block_on(self.watch_extract_with(
options,
ExecutionDeadlines::default(),
Box::pin(watching::termination()),
|path| {
let event =
serde_json::json!({"format_version": 1, "event": "snapshot", "path": path});
let mut output = std::io::stdout().lock();
writeln!(output, "{event}")?;
output.flush()
},
))
}
pub async fn watch_extract_with(
&self,
options: ExtractOptions,
deadlines: ExecutionDeadlines,
cancellation: ExecutionCancellation<'_>,
on_snapshot: impl FnMut(&Path) -> std::io::Result<()>,
) -> Result<(), CommandError> {
scope_async(
self.cache_policy,
watching::watch_extract_reported(
options,
deadlines,
cancellation,
on_snapshot,
&self.reporting,
),
)
.await
}
pub fn generate(&self, options: GenerateOptions) -> Result<(), CommandError> {
self.generate_with_components(options, false)
}
pub fn generate_with_components(
&self,
options: GenerateOptions,
allow_components: bool,
) -> Result<(), CommandError> {
let command = self.reporting.command("generate");
let site = pipeline::generate_attempt(
&options.input,
&options.output,
true,
allow_components,
&self.reporting,
)?;
command.finish(Some(&options.output), Some(site.page_count()));
Ok(())
}
pub fn generate_fragments(
&self,
options: GenerateOptions,
url_map: Option<PathBuf>,
) -> Result<(), CommandError> {
let command = self.reporting.command("generate");
let site = pipeline::generate_fragments_attempt(
&options.input,
&options.output,
url_map.as_deref(),
&self.reporting,
)?;
command.finish(Some(&options.output), Some(site.page_count()));
Ok(())
}
pub fn serve(&self, options: ServeOptions) -> Result<(), CommandError> {
pipeline::runtime()?.block_on(self.serve_with(
options,
ExecutionDeadlines::default(),
Box::pin(watching::termination()),
))
}
pub async fn serve_with(
&self,
options: ServeOptions,
deadlines: ExecutionDeadlines,
cancellation: ExecutionCancellation<'_>,
) -> Result<(), CommandError> {
scope_async(
self.cache_policy,
preview::serve_reported(options, deadlines, cancellation, &self.reporting),
)
.await
}
pub fn report_error(&self, error: &CommandError) {
self.reporting.error(error);
}
}