use std::path::{Path, PathBuf};
use crate::tools::canonical_tool_names;
use rho_sdk::hooks::HookEventKind;
use super::{
config::{
parse_hooks_file, ConfiguredHookEvent, HookConfigError, HookDefinition,
WorkflowHookEventKind,
},
matcher::ToolMatcher,
HookSource,
};
pub use crate::workspace::{ProjectTrust, TRUST_PROJECT_HOOKS_ENV};
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SkippedProjectHooks {
pub path: PathBuf,
definitions: Vec<HookDefinition>,
error: Option<HookConfigError>,
}
impl SkippedProjectHooks {
pub fn error(&self) -> Option<&HookConfigError> {
self.error.as_ref()
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct HookCatalog {
hooks: Vec<HookDefinition>,
files: Vec<PathBuf>,
skipped_untrusted: Option<SkippedProjectHooks>,
}
impl HookCatalog {
pub fn discover(
rho_home: Option<&Path>,
project_root: Option<&Path>,
trust: ProjectTrust,
) -> Result<Self, HookConfigError> {
let mut catalog = Self::default();
if let Some(home) = rho_home {
catalog.add(HooksFileRead::user(home))?;
}
if let Some(root) = project_root {
catalog.add(HooksFileRead::project(root, trust))?;
}
Ok(catalog)
}
fn add(&mut self, file: HooksFileRead) -> Result<(), HookConfigError> {
match file.outcome {
HooksFileOutcome::Absent => {}
HooksFileOutcome::Loaded(hooks) => {
self.files.push(file.path);
self.hooks.extend(hooks);
}
HooksFileOutcome::Untrusted { definitions, error } => {
self.skipped_untrusted = Some(SkippedProjectHooks {
path: file.path,
definitions,
error,
});
}
HooksFileOutcome::Invalid(error) => return Err(error),
}
Ok(())
}
pub fn is_empty(&self) -> bool {
self.hooks.is_empty()
}
pub fn len(&self) -> usize {
self.hooks.len()
}
pub(crate) fn retain_blocking(&mut self) {
self.hooks.retain(|hook| hook.event().is_blocking());
}
pub fn has_blocking_hooks(&self) -> bool {
self.hooks.iter().any(|hook| hook.event().is_blocking())
}
pub fn has_observational_hooks(&self) -> bool {
self.hooks.iter().any(|hook| !hook.event().is_blocking())
}
#[cfg(test)]
pub fn hooks(&self) -> &[HookDefinition] {
&self.hooks
}
pub fn files(&self) -> &[PathBuf] {
&self.files
}
pub fn skipped_untrusted(&self) -> Option<&SkippedProjectHooks> {
self.skipped_untrusted.as_ref()
}
pub fn matching(&self, event: HookEventKind, tool: Option<&str>) -> Vec<&HookDefinition> {
self.matching_configured(ConfiguredHookEvent::Sdk(event), tool)
}
pub(crate) fn matching_workflow(&self, event: WorkflowHookEventKind) -> Vec<&HookDefinition> {
self.matching_configured(ConfiguredHookEvent::Workflow(event), None)
}
pub(crate) fn matching_configured(
&self,
event: ConfiguredHookEvent,
tool: Option<&str>,
) -> Vec<&HookDefinition> {
self.hooks
.iter()
.filter(|hook| hook.event() == event)
.filter(|hook| match tool {
Some(name) => hook.tools().matches(name),
None => true,
})
.collect()
}
pub fn spawn_contract(&self) -> Vec<HookSpawnContract> {
self.hooks
.iter()
.map(|hook| spawn_contract(hook, true))
.chain(
self.skipped_untrusted
.iter()
.flat_map(|skipped| skipped.definitions.iter())
.map(|hook| spawn_contract(hook, false)),
)
.collect()
}
}
pub(super) struct HooksFileRead {
pub(super) path: PathBuf,
pub(super) outcome: HooksFileOutcome,
}
pub(super) enum HooksFileOutcome {
Absent,
Loaded(Vec<HookDefinition>),
Untrusted {
definitions: Vec<HookDefinition>,
error: Option<HookConfigError>,
},
Invalid(HookConfigError),
}
impl HooksFileRead {
pub(super) fn user(rho_home: &Path) -> Self {
Self::read(
user_hooks_path(rho_home),
HookSource::User,
None,
ProjectTrust::Trusted,
)
}
pub(super) fn project(root: &Path, trust: ProjectTrust) -> Self {
Self::read(
root.join(".rho/hooks.toml"),
HookSource::Project,
Some(root),
trust,
)
}
fn read(
path: PathBuf,
source: HookSource,
project_root: Option<&Path>,
trust: ProjectTrust,
) -> Self {
let outcome = match (
read_definitions(&path, source, project_root),
trust.is_trusted(),
) {
(Ok(None), _) => HooksFileOutcome::Absent,
(Ok(Some(hooks)), true) => HooksFileOutcome::Loaded(hooks),
(Ok(Some(definitions)), false) => HooksFileOutcome::Untrusted {
definitions,
error: None,
},
(Err(error), true) => HooksFileOutcome::Invalid(error),
(Err(error), false) => HooksFileOutcome::Untrusted {
definitions: Vec::new(),
error: Some(error),
},
};
Self { path, outcome }
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct HookSpawnContract {
pub active: bool,
pub id: String,
pub(crate) event: ConfiguredHookEvent,
pub tools: ToolMatcher,
pub command: Vec<String>,
pub working_directory: PathBuf,
pub timeout: std::time::Duration,
pub environment: Vec<String>,
pub missing_edit_tools: Vec<&'static str>,
}
pub(crate) fn user_hooks_path(rho_home: &Path) -> PathBuf {
rho_home.join("hooks.toml")
}
pub(crate) fn check_user_hooks(path: &Path, text: &str) -> Result<(), HookConfigError> {
parse_hooks_file(path, text, HookSource::User, None, canonical_tool_names()).map(drop)
}
fn read_definitions(
path: &Path,
source: HookSource,
project_root: Option<&Path>,
) -> Result<Option<Vec<HookDefinition>>, HookConfigError> {
let contents = match std::fs::read_to_string(path) {
Ok(contents) => contents,
Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(None),
Err(error) => {
return Err(HookConfigError::at_file(
path,
format!("cannot read hooks file: {error}"),
));
}
};
parse_hooks_file(
path,
&contents,
source,
project_root,
canonical_tool_names(),
)
.map(Some)
}
pub(super) fn spawn_contract(hook: &HookDefinition, active: bool) -> HookSpawnContract {
HookSpawnContract {
active,
id: hook.qualified_id(),
event: hook.event(),
tools: hook.tools().clone(),
command: hook.command().to_vec(),
working_directory: hook.working_directory().to_path_buf(),
timeout: hook.timeout(),
environment: crate::child_env::base_names()
.chain([super::IN_HOOK_ENV])
.map(str::to_owned)
.chain(hook.env().iter().cloned())
.collect(),
missing_edit_tools: hook.tools().missing_edit_tools(),
}
}
#[cfg(test)]
#[path = "catalog_tests.rs"]
mod tests;