use std::path::{Path, PathBuf};
use anyhow::{Context, Result, bail, ensure};
use typst::syntax::VirtualPath;
use crate::foundation::config::AsterConfig;
#[derive(Clone)]
pub struct Project {
root: PathBuf,
}
impl Project {
pub fn find(dir: &Path) -> Option<Self> {
let dir = std::path::absolute(dir).ok()?;
let mut current = Some(dir.as_path());
while let Some(path) = current {
if path.join("aster.toml").is_file() {
return Some(Self {
root: path.to_owned(),
});
}
current = path.parent();
}
None
}
pub fn open(root: impl Into<PathBuf>) -> Result<Self> {
let root =
std::path::absolute(root.into()).context("failed to make project root absolute")?;
if !root.join("aster.toml").is_file() {
bail!("no aster.toml found in {}", root.display());
}
Ok(Self { root })
}
pub fn root(&self) -> &Path {
&self.root
}
pub fn config_file(&self) -> PathBuf {
self.root.join("aster.toml")
}
pub fn config_path(&self) -> VirtualPath {
VirtualPath::new("aster.toml").expect("the manifest path is a valid virtual path")
}
pub fn realize(&self, path: &VirtualPath) -> PathBuf {
path.realize(&self.root)
.expect("validated project path must realize within the project root")
}
}
#[derive(Clone)]
pub struct ProjectLayout {
pages: VirtualPath,
generate: VirtualPath,
content: VirtualPath,
public: VirtualPath,
output: VirtualPath,
generated_assets: VirtualPath,
fonts: Vec<VirtualPath>,
watch_paths: Vec<VirtualPath>,
}
impl ProjectLayout {
pub fn new(config: &AsterConfig) -> Result<Self> {
let pages = project_directory(&config.paths.pages, "pages")?;
let generate = project_directory(&config.paths.generate, "generate")?;
let content = project_directory(&config.paths.content, "content")?;
let public = project_directory(&config.paths.public, "public")?;
let output = project_directory(&config.paths.output, "output")?;
ensure_disjoint(&pages, "pages", &content, "content")?;
ensure_disjoint(&pages, "pages", &public, "public")?;
ensure_disjoint(&pages, "pages", &output, "output")?;
ensure_disjoint(&generate, "generate", &pages, "pages")?;
ensure_disjoint(&generate, "generate", &content, "content")?;
ensure_disjoint(&generate, "generate", &public, "public")?;
ensure_disjoint(&generate, "generate", &output, "output")?;
ensure_disjoint(&content, "content", &public, "public")?;
ensure_disjoint(&content, "content", &output, "output")?;
ensure_disjoint(&public, "public", &output, "output")?;
let generated_assets = project_directory(&config.output.assets, "generated assets")?;
let fonts = config
.typst
.fonts
.paths
.iter()
.map(|path| project_path(path, "font"))
.collect::<Result<_>>()?;
for font in &fonts {
ensure_disjoint(font, "font", &output, "output")?;
}
let watch_paths = config
.watch
.paths
.iter()
.chain(
config
.postprocess
.iter()
.flat_map(|processor| processor.watch.iter()),
)
.map(|value| {
let path = project_path(value, "watch")?;
ensure!(!path.is_root(), "watch path cannot be the project root");
ensure!(
!overlaps(&path, &output),
"watch path `{}` must not overlap the output directory",
path.get_without_slash()
);
Ok(path)
})
.collect::<Result<_>>()?;
Ok(Self {
pages,
generate,
content,
public,
output,
generated_assets,
fonts,
watch_paths,
})
}
pub fn pages(&self) -> &VirtualPath {
&self.pages
}
pub fn generate(&self) -> &VirtualPath {
&self.generate
}
pub fn content(&self) -> &VirtualPath {
&self.content
}
pub fn public(&self) -> &VirtualPath {
&self.public
}
pub fn generated_assets(&self) -> &VirtualPath {
&self.generated_assets
}
pub fn output(&self) -> &VirtualPath {
&self.output
}
pub fn font_dirs(&self) -> impl Iterator<Item = &VirtualPath> {
self.fonts.iter()
}
pub fn watch_paths(&self) -> impl Iterator<Item = &VirtualPath> {
self.watch_paths.iter()
}
}
fn project_directory(value: &str, name: &str) -> Result<VirtualPath> {
let path = project_path(value, name)?;
ensure!(
!path.is_root(),
"{name} directory cannot be the project root"
);
Ok(path)
}
fn project_path(value: &str, name: &str) -> Result<VirtualPath> {
ensure!(
!Path::new(value).is_absolute(),
"{name} path must be relative"
);
VirtualPath::new(value).with_context(|| format!("invalid {name} path `{value}`"))
}
fn ensure_disjoint(
left: &VirtualPath,
left_name: &str,
right: &VirtualPath,
right_name: &str,
) -> Result<()> {
ensure!(
!overlaps(left, right),
"{left_name} and {right_name} directories must not overlap"
);
Ok(())
}
fn overlaps(left: &VirtualPath, right: &VirtualPath) -> bool {
let left = Path::new(left.get_without_slash());
let right = Path::new(right.get_without_slash());
left.starts_with(right) || right.starts_with(left)
}