Something went wrong. Try again.
personal ssg
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172//! Copy the embedded templates and theme into a project.//!//! Eject never overwrites and never builds: it exists so a project can edit//! the defaults without recompiling uteri. The built output must stay the//! same before and after an eject.
use std::fs;use std::path::{Path, PathBuf};
use crate::config;use crate::init::write_atomic;use crate::template;
/// What an eject wrote.pub struct Summary { /// Templates written, not counting existing ones. pub templates: usize, /// Files left alone because they already existed. pub skipped: usize, /// Whether `style.css` was written. pub stylesheet: bool, /// The project root the files went to. pub root: PathBuf,}
/// Write the embedded templates and theme when they are missing.pub fn run(config_path: Option<&Path>) -> Result<Summary, String> { let config = config::load(config_path)?;
let mut templates = 0; let mut skipped = 0;
for name in template::TEMPLATES { let path = config.templates.join(name); if path.exists() { skipped += 1; continue; } if let Some(parent) = path.parent() { fs::create_dir_all(parent).map_err(|error| format!("{}: {error}", parent.display()))?; } write_atomic(&path, template::embedded(name)?.as_bytes())?; templates += 1; }
// `build` prefers a project stylesheet over `static/style.css`, so writing // one next to an active static stylesheet would change the site. let style = config.root.join("style.css"); let static_style = config.static_dir.join("style.css"); let stylesheet = if style.exists() { skipped += 1; false } else if static_style.is_file() { eprintln!( "warning: {}: static stylesheet is in use; leaving style.css alone", static_style.display() ); skipped += 1; false } else { write_atomic(&style, config.theme.style().as_bytes())?; true };
Ok(Summary { templates, skipped, stylesheet, root: config.root, })}