#![allow(dead_code)]
use std::io::Write as _;
use std::path::{Path, PathBuf};
use tempfile::TempDir;
pub struct TempProject {
pub project_dir: TempDir,
pub global_dir: Option<TempDir>,
}
impl TempProject {
pub fn new() -> Self {
let project_dir = tempfile::tempdir().expect("create temp project dir");
Self {
project_dir,
global_dir: None,
}
}
pub fn with_global_recipes(content: &str) -> Self {
let project_dir = tempfile::tempdir().expect("create temp project dir");
let global_dir = tempfile::tempdir().expect("create temp global dir");
let global_justfile = global_dir.path().join("justfile");
let mut f = std::fs::File::create(&global_justfile).expect("create global justfile");
f.write_all(content.as_bytes())
.expect("write global justfile content");
Self {
project_dir,
global_dir: Some(global_dir),
}
}
pub fn write_project_justfile(&self, content: &str) {
let path = self.project_justfile();
let mut f = std::fs::File::create(&path).expect("create project justfile");
f.write_all(content.as_bytes())
.expect("write project justfile content");
}
pub fn project_path(&self) -> &Path {
self.project_dir.path()
}
pub fn project_justfile(&self) -> PathBuf {
self.project_dir.path().join("justfile")
}
pub fn global_justfile(&self) -> Option<PathBuf> {
self.global_dir.as_ref().map(|d| d.path().join("justfile"))
}
}