use crate::project::{package_name, populate_directory, write_support};
use anyhow::{Context, Result};
use std::{fs, path::Path};
use toml::Value;
include!(concat!(env!("OUT_DIR"), "/ashroot-template.rs"));
pub(crate) fn new(path: &Path, toolchain: &str) -> Result<()> {
let name = package_name(path)?;
let mut manifest: Value =
toml::from_str(MANIFEST).context("parse embedded Ashroot manifest")?;
let package = manifest["package"]
.as_table_mut()
.context("missing package metadata")?;
for (field, value) in [
("name", name),
("version", "0.1.0"),
("edition", "2024"),
("license", "Apache-2.0"),
("rust-version", "1.100"),
] {
package.insert(field.into(), Value::String(value.into()));
}
manifest
.as_table_mut()
.unwrap()
.insert("workspace".into(), Value::Table(Default::default()));
let deps = manifest["dependencies"]
.as_table_mut()
.context("missing Ashroot dependencies")?;
let version = format!("={}", env!("CARGO_PKG_VERSION"));
for (_, dependency) in deps.iter_mut() {
let spec = dependency
.as_table_mut()
.context("invalid Ashroot dependency")?;
spec.remove("path");
spec.insert("version".into(), Value::String(version.clone()));
}
let manifest = toml::to_string_pretty(&manifest)?;
populate_directory(path, |staging| {
write_support(staging, toolchain)?;
for (relative, bytes) in SOURCES {
let file = staging.join(relative);
fs::create_dir_all(file.parent().context("missing example source parent")?)?;
fs::write(file, bytes)?;
}
fs::write(staging.join("test/rehearse.mjs"), SCENARIO)?;
fs::write(staging.join("Cargo.toml"), manifest)?;
fs::write(
staging.join("README.md"),
include_str!("../templates/ashroot/README.md.in").replace("@APP_NAME@", name),
)?;
Ok(())
})?;
println!("Created {}", path.display());
Ok(())
}