pub mod audit;
pub mod brief;
pub mod change;
pub mod check;
pub mod claim;
pub mod init;
pub mod journal;
pub mod ledger;
pub mod lint;
pub mod locate;
pub mod new;
pub mod pin;
pub mod probe;
pub mod questions;
pub mod readset;
pub mod run;
pub mod run_status;
pub mod schema;
pub mod trace;
use std::path::{Path, PathBuf};
use std::process::Command;
use vitrify_core::CoreError;
use vitrify_core::audit as core_audit;
use vitrify_core::config::{self, Config, Overrides};
use vitrify_core::contract::{self, Peers};
use vitrify_core::graph::UnitGraph;
use vitrify_core::model::{self, Instance, SddHandle};
pub fn governed_files(root: &Path, inst: &Instance) -> Option<Vec<String>> {
crate::git::Git::new(root)
.committable_files(&inst.meta.get_list("scope_roots"))
.ok()
}
fn committable_files(root: &Path) -> Option<Vec<String>> {
crate::git::Git::new(root)
.committable_files(&[".".to_string()])
.ok()
}
pub fn resolve_sdd(root: &Path, sdd_arg: Option<&str>, cwd: &Path) -> Result<SddHandle, CoreError> {
if sdd_arg.is_some() {
return model::resolve_sdd(root, sdd_arg, cwd);
}
model::resolve_sdd_with(root, None, cwd, committable_files(root).as_deref())
}
pub fn discover(root: &Path) -> Vec<SddHandle> {
model::discover_with(root, committable_files(root).as_deref())
}
pub fn peers(root: &Path) -> Peers<'_> {
Peers::new(move || discover(root))
}
pub fn loaded(root: &Path, own: Option<&Instance>) -> (Vec<Instance>, Vec<(SddHandle, CoreError)>) {
let mut insts: Vec<Instance> = Vec::new();
let mut refused: Vec<(SddHandle, CoreError)> = Vec::new();
for h in discover(root) {
if let Some(own) = own
&& same_root(&h.root, &own.root)
{
insts.push(own.clone());
continue;
}
match Instance::load(h.clone()) {
Ok(inst) => insts.push(inst),
Err(e) => refused.push((h, e)),
}
}
if let Some(own) = own
&& !insts.iter().any(|i| same_root(&i.root, &own.root))
{
insts.push(own.clone());
}
(insts, refused)
}
fn same_root(a: &Path, b: &Path) -> bool {
a == b || matches!((a.canonicalize(), b.canonicalize()), (Ok(x), Ok(y)) if x == y)
}
pub fn repo_graph(root: &Path, own: Option<&Instance>) -> Result<UnitGraph, CoreError> {
UnitGraph::build(&loaded(root, own).0)
}
pub fn reaches_out(inst: &Instance) -> bool {
inst.units.iter().any(|u| {
u.meta
.get_list("depends_on")
.iter()
.any(|d| inst.unit(d).is_none())
})
}
pub fn targets(
root: &Path,
sdd_arg: Option<&str>,
all: bool,
cwd: &Path,
) -> Result<Vec<SddHandle>, CoreError> {
if !all {
return Ok(vec![resolve_sdd(root, sdd_arg, cwd)?]);
}
let found = discover(root);
if found.is_empty() {
return Err(CoreError::refusal(4, model::NO_SDD));
}
Ok(found)
}
pub fn shown(root: &Path, path: &Path) -> String {
let rel = path
.strip_prefix(root)
.ok()
.map(Path::to_path_buf)
.or_else(|| {
let (root, path) = (root.canonicalize().ok()?, path.canonicalize().ok()?);
path.strip_prefix(&root).ok().map(Path::to_path_buf)
});
match rel {
Some(rel) if rel.as_os_str().is_empty() => ".".to_string(),
Some(rel) => rel.to_string_lossy().replace('\\', "/"),
None => path.display().to_string(),
}
}
pub fn label(sdd: &SddHandle, root: &Path) -> String {
match contract::slug(sdd) {
"" => shown(root, &sdd.root),
slug => slug.to_string(),
}
}
pub fn each(
root: &Path,
sdds: &[SddHandle],
mut one: impl FnMut(&SddHandle) -> Result<u8, CoreError>,
) -> u8 {
let mut codes: Vec<u8> = Vec::new();
for sdd in sdds {
codes.push(match one(sdd) {
Ok(code) => code,
Err(e) => {
let text = e.to_string();
crate::out::diag(&format!(
"{}: {}",
label(sdd, root),
text.strip_prefix("error: ").unwrap_or(&text)
));
e.exit_code()
}
});
}
if codes.contains(&1) {
1
} else {
codes.into_iter().max().unwrap_or(0)
}
}
pub fn sdd_rel(checkout: &Path, sdd_root: &Path) -> Option<PathBuf> {
if let Ok(rel) = sdd_root.strip_prefix(checkout) {
return Some(rel.to_path_buf());
}
let checkout = checkout.canonicalize().ok()?;
let sdd_root = sdd_root.canonicalize().ok()?;
sdd_root.strip_prefix(&checkout).ok().map(Path::to_path_buf)
}
pub fn resolve_sdd_in(checkout: &Path, rel: &Path) -> Result<SddHandle, CoreError> {
let at = checkout.join(rel);
model::resolve_sdd(checkout, Some(&at.to_string_lossy()), checkout)
}
pub fn sdd_arg(rel: &Path) -> String {
let s = rel.to_string_lossy().replace('\\', "/");
if s.is_empty() { ".".to_string() } else { s }
}
pub fn audit_options(
mut opts: core_audit::Options,
root: &Path,
inst: &Instance,
) -> core_audit::Options {
opts.scope_files = governed_files(root, inst);
opts
}
pub fn repo_root() -> PathBuf {
let out = Command::new("git")
.args(["rev-parse", "--show-toplevel"])
.output();
if let Ok(o) = out
&& o.status.success()
&& let Ok(s) = String::from_utf8(o.stdout)
{
let trimmed = s.trim();
if !trimmed.is_empty() {
return PathBuf::from(trimmed);
}
}
cwd()
}
pub fn cwd() -> PathBuf {
std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."))
}
pub fn home_dir() -> Option<PathBuf> {
if let Ok(h) = std::env::var("VITRIFY_HOME")
&& !h.is_empty()
{
return Some(PathBuf::from(h));
}
std::env::var("HOME")
.ok()
.filter(|h| !h.is_empty())
.map(|h| PathBuf::from(h).join(".vitrify"))
}
pub fn load_config(overrides: &Overrides) -> Result<Config, CoreError> {
let env: Vec<(String, String)> = std::env::vars()
.filter(|(k, _)| k.starts_with("VITRIFY_"))
.collect();
config::load(Some(&repo_root()), home_dir().as_deref(), &env, overrides)
}