use super::Layout;
use crate::{Lock, Reader, Store, documents};
use std::{
collections::BTreeSet,
path::{Component, Path, PathBuf},
};
use workyard_core::{Control, Problem, ProblemCode};
#[derive(Clone, Copy)]
pub enum RootEdit {
Track,
Untrack,
}
impl Reader {
pub fn tracked_only(&self) -> Result<bool, Problem> {
Ok(Layout::read(self.state())?.tracked_only)
}
pub fn project_registration_path(&self, path: &Path) -> Result<PathBuf, Problem> {
let workspace = self.state().parent().expect("workspace marker");
Ok(workspace.join(registration(workspace, path)?))
}
pub fn project_roots(&self) -> Result<Vec<PathBuf>, Problem> {
let workspace = self.state().parent().expect("workspace marker");
Ok(Layout::read(self.state())?
.located_roots(workspace)?
.into_iter()
.map(|root| workspace.join(root))
.collect())
}
}
impl Store {
pub async fn remember_workspace(&self, control: &Control) -> Result<bool, Problem> {
let state = self.state_dir();
let workspace = state.parent().expect("workspace marker");
control.check()?;
if Layout::read(state)?.is_at(workspace)? {
return Ok(false);
}
let lock = Lock::wait(&state.join("locks/projects.lock"), control).await?;
self.verify()?;
let mut layout = Layout::read(state)?;
if !layout.update_workspace(workspace)? {
return Ok(false);
}
lock.verify()?;
control.check()?;
documents::publish(&state.join("workspace.json"), &layout)?;
Ok(true)
}
pub async fn edit_project_roots(
&self,
edit: RootEdit,
paths: &[PathBuf],
control: &Control,
) -> Result<Vec<PathBuf>, Problem> {
if paths.is_empty() {
return Ok(Vec::new());
}
let workspace = self.state_dir().parent().expect("workspace marker");
let lock = Lock::wait(&self.state_dir().join("locks/projects.lock"), control).await?;
self.verify()?;
let mut layout = Layout::read(self.state_dir())?;
let roots = paths
.iter()
.map(|path| edit.registration(workspace, path))
.collect::<Result<BTreeSet<_>, _>>()?;
layout.update_workspace(workspace)?;
let mut registered = layout.project_roots()?;
let mut changed = Vec::new();
for root in roots {
let matches: Vec<_> = registered
.iter()
.filter(|stored| {
**stored == root
|| (stored.components().any(|part| part == Component::ParentDir)
&& registration(workspace, &workspace.join(stored))
.is_ok_and(|path| path == root))
})
.cloned()
.collect();
match edit {
RootEdit::Track if matches.is_empty() => {
registered.insert(root.clone());
}
RootEdit::Untrack if !matches.is_empty() => {
for stored in matches {
registered.remove(&stored);
}
}
_ => continue,
}
changed.push(workspace.join(root));
}
if !changed.is_empty() {
control.check()?;
lock.verify()?;
layout.set_project_roots(®istered);
documents::publish(&self.state_dir().join("workspace.json"), &layout)?;
}
Ok(changed)
}
}
pub(super) fn initial_roots(
workspace: &Path,
paths: &[&Path],
) -> Result<Option<BTreeSet<PathBuf>>, Problem> {
if paths.is_empty() {
return Ok(None);
}
let roots = paths
.iter()
.map(|&path| {
if path.is_absolute() {
return RootEdit::Track.registration(workspace, path);
}
if path.as_os_str().is_empty()
|| path
.components()
.any(|part| matches!(part, Component::RootDir | Component::Prefix(_)))
{
return Err(Problem::new(
ProblemCode::InvalidInput,
"Supply a relative or absolute project path.",
));
}
let resolved = crate::canonical_future(&workspace.join(path))?;
validate_track_path(&resolved)?;
Ok(path.to_owned())
})
.collect::<Result<BTreeSet<_>, _>>()?;
Ok(Some(roots))
}
impl RootEdit {
fn registration(self, workspace: &Path, path: &Path) -> Result<PathBuf, Problem> {
if matches!(self, Self::Track) {
validate_track_path(path)?;
}
registration(workspace, path)
}
}
fn validate_track_path(path: &Path) -> Result<(), Problem> {
match std::fs::metadata(path) {
Ok(meta) if !meta.is_dir() => Err(Problem::new(
ProblemCode::InvalidInput,
format!("Project root {path:?} must be a directory."),
)),
Err(error) if error.kind() != std::io::ErrorKind::NotFound => {
Err(Problem::io(format!("Inspect project root {path:?}"), error))
}
_ => Ok(()),
}
}
fn registration(workspace: &Path, path: &Path) -> Result<PathBuf, Problem> {
if path.as_os_str().is_empty() {
return Err(Problem::new(
workyard_core::ProblemCode::InvalidInput,
"Project root cannot be empty.",
));
}
let absolute =
std::path::absolute(path).map_err(|error| Problem::io("Resolve project root", error))?;
let inside = absolute.starts_with(workspace)
&& !absolute
.components()
.any(|part| part == std::path::Component::ParentDir);
let absolute = match (absolute.parent(), absolute.file_name()) {
_ if inside => absolute,
(Some(parent), Some(name)) => crate::canonical_future(parent)?.join(name),
_ => crate::canonical_future(&absolute)?,
};
let relative = absolute.strip_prefix(workspace).unwrap_or(&absolute);
Ok(if relative.as_os_str().is_empty() {
PathBuf::from(".")
} else {
relative.to_owned()
})
}