use std::{
env, fs,
path::{Path, PathBuf},
};
use anyhow::{Context, Result};
#[derive(Clone, Debug)]
pub struct Workspace {
temp_dir: PathBuf,
clone_root: PathBuf,
}
impl Workspace {
pub fn temp_dir(&self) -> &Path {
&self.temp_dir
}
pub fn clone_root(&self) -> &Path {
&self.clone_root
}
}
pub fn prepare(custom: Option<PathBuf>) -> Result<Workspace> {
match custom {
Some(path) => prepare_custom_workspace(path),
None => prepare_default_workspace(),
}
}
fn prepare_custom_workspace(path: PathBuf) -> Result<Workspace> {
fs::create_dir_all(&path)
.with_context(|| format!("failed to create custom workspace at {}", path.display()))?;
Ok(Workspace {
temp_dir: path.clone(),
clone_root: path,
})
}
fn prepare_default_workspace() -> Result<Workspace> {
let clone_root = env::current_dir().context("failed to resolve current directory")?;
let temp_dir = clone_root.join("revdeprun-work");
fs::create_dir_all(&temp_dir)
.with_context(|| format!("failed to create workspace at {}", temp_dir.display()))?;
Ok(Workspace {
temp_dir,
clone_root,
})
}
pub fn canonicalized(path: &Path) -> Result<PathBuf> {
path.canonicalize()
.with_context(|| format!("failed to canonicalize {}", path.display()))
}
#[cfg(test)]
mod tests {
use super::*;
use tempfile::tempdir;
#[test]
fn custom_workspace_uses_provided_path() {
let tmp = tempdir().expect("tempdir");
let base = tmp.path().join("workspace");
let workspace = prepare(Some(base.clone())).expect("prepare custom workspace");
assert_eq!(workspace.clone_root(), base.as_path());
assert_eq!(workspace.temp_dir(), base.as_path());
assert!(base.exists());
}
}