use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;
use auro_core::{AuroError, Config};
const CARGO_TOML: &str = include_str!("../templates/Cargo.toml.in");
const AURO_TOML: &str = include_str!("../templates/Auro.toml");
const DIOXUS_TOML: &str = include_str!("../templates/Dioxus.toml");
const MAIN_RS: &str = include_str!("../templates/main.rs");
const UI_CARGO_TOML: &str = include_str!("../templates/ui/Cargo.toml.in");
const UI_LIB_RS: &str = include_str!("../templates/ui/lib.rs");
const SERVICE_RS: &str = include_str!("../templates/ui/services.rs");
const DATA_CARGO_TOML: &str = include_str!("../templates/data/Cargo.toml.in");
const DATA_LIB_RS: &str = include_str!("../templates/data/lib.rs");
const TYPES_CARGO_TOML: &str = include_str!("../templates/types/Cargo.toml.in");
const TYPES_LIB_RS: &str = include_str!("../templates/types/lib.rs");
const ICON_PNG: &[u8] = include_bytes!("../templates/icon.png");
const GITIGNORE: &str = "/target\n";
pub fn new(path: &Path) -> Result<(), AuroError> {
if path.exists() && fs::read_dir(path)?.next().is_some() {
return Err(AuroError::Message(format!(
"`{}` is not empty; refusing to scaffold over it",
path.display()
)));
}
fs::create_dir_all(path)?;
let name = package_name(path);
let crate_name = name.replace('-', "_");
let identifier = format!("com.example.{}", name.replace('-', ""));
let auro = auro_dependency(
Path::new(env!("CARGO_MANIFEST_DIR")),
env!("CARGO_PKG_VERSION"),
);
let patch = xattr_patch();
let fill = |template: &str| {
template
.replace("{{name}}", &name)
.replace("{{crate}}", &crate_name)
.replace("{{identifier}}", &identifier)
.replace("{{auro_dependency}}", &auro)
.replace("{{patch}}", &patch)
};
write(&path.join("Cargo.toml"), &fill(CARGO_TOML))?;
write(&path.join("Auro.toml"), &fill(AURO_TOML))?;
write(&path.join("Dioxus.toml"), &fill(DIOXUS_TOML))?;
write(&path.join("src/main.rs"), &fill(MAIN_RS))?;
write(&path.join("ui/Cargo.toml"), &fill(UI_CARGO_TOML))?;
write(&path.join("ui/src/lib.rs"), &fill(UI_LIB_RS))?;
write(&path.join("ui/src/services.rs"), &fill(SERVICE_RS))?;
write(&path.join("data/Cargo.toml"), &fill(DATA_CARGO_TOML))?;
write(&path.join("data/src/lib.rs"), &fill(DATA_LIB_RS))?;
write(&path.join("types/Cargo.toml"), &fill(TYPES_CARGO_TOML))?;
write(&path.join("types/src/lib.rs"), &fill(TYPES_LIB_RS))?;
write_bytes(&path.join("icons/icon.png"), ICON_PNG)?;
write(&path.join(".gitignore"), GITIGNORE)?;
println!("Created {} at {}", name, path.display());
println!("Next: cd {} && auro dev", path.display());
Ok(())
}
pub fn build(dir: &Path) -> Result<(), AuroError> {
let dir = dir.canonicalize()?;
let config = load_checked(&dir)?;
build_release(&dir, &config)
}
pub fn bundle(dir: &Path) -> Result<(), AuroError> {
let dir = plain_path(dir.canonicalize()?);
let config = load_checked(&dir)?;
ensure_bundle_active(&config)?;
build_release(&dir, &config)?;
let binary = binary_name(&dir)?;
let target = target_dir(&dir)?;
let artifacts = auro_bundle::bundle_in(&dir, &config, &binary, &target)?;
if let Some(profile) = config.bundle.macos.notarize_profile.as_deref() {
println!("notarizing macOS artifacts with profile `{profile}`");
auro_bundle::MacNotarizer::new().notarize_artifacts(&artifacts, profile)?;
}
for artifact in artifacts {
println!("{}", artifact.display());
}
Ok(())
}
fn ensure_bundle_active(config: &Config) -> Result<(), AuroError> {
if config.bundle.active {
Ok(())
} else {
Err(AuroError::bundle(
"`[bundle] active = false` disables bundling for this app",
))
}
}
fn build_release(dir: &Path, _config: &Config) -> Result<(), AuroError> {
let target = target_dir(dir)?;
let status = Command::new("cargo")
.arg("build")
.arg("--release")
.arg("--target-dir")
.arg(&target)
.arg("--manifest-path")
.arg(dir.join("Cargo.toml"))
.status()?;
if !status.success() {
return Err(AuroError::Message("cargo build failed".to_owned()));
}
Ok(())
}
pub(crate) fn target_dir(dir: &Path) -> Result<PathBuf, AuroError> {
Ok(resolve_target_dir(
dir,
std::env::var_os("CARGO_TARGET_DIR"),
&std::env::current_dir()?,
))
}
fn resolve_target_dir(dir: &Path, value: Option<std::ffi::OsString>, cwd: &Path) -> PathBuf {
match value {
Some(value) => {
let target = PathBuf::from(value);
let rooted = target.is_absolute() || target.to_string_lossy().starts_with(['/', '\\']);
if rooted { target } else { cwd.join(target) }
}
None => dir.join("target"),
}
}
fn plain_path(path: PathBuf) -> PathBuf {
#[cfg(windows)]
{
let text = path.to_string_lossy();
if let Some(rest) = text.strip_prefix(r"\\?\UNC\") {
return PathBuf::from(format!(r"\\{rest}"));
}
if let Some(rest) = text.strip_prefix(r"\\?\") {
return PathBuf::from(rest);
}
}
path
}
pub fn binary_name(dir: &Path) -> Result<String, AuroError> {
let manifest = dir.join("Cargo.toml");
let text = fs::read_to_string(&manifest)?;
let value: toml::Value = toml::from_str(&text).map_err(|error: toml::de::Error| {
AuroError::config(&manifest, None, format!("invalid Cargo.toml: {error}"))
})?;
let package = value
.get("package")
.ok_or_else(|| AuroError::config(&manifest, None, "missing [package]"))?;
let name = package
.get("default-run")
.or_else(|| package.get("name"))
.and_then(toml::Value::as_str)
.ok_or_else(|| AuroError::config(&manifest, Some("package.name"), "missing name"))?;
Ok(name.to_owned())
}
pub fn load_checked(dir: &Path) -> Result<Config, AuroError> {
let config = Config::load_from(&dir.join(auro_core::config::CONFIG_FILE))?;
check_version(dir, &config)?;
Ok(config)
}
pub fn check_version(dir: &Path, config: &Config) -> Result<(), AuroError> {
let manifest = dir.join("Cargo.toml");
let text = fs::read_to_string(&manifest)?;
let value: toml::Value = toml::from_str(&text).map_err(|error: toml::de::Error| {
AuroError::config(&manifest, None, format!("invalid Cargo.toml: {error}"))
})?;
let declared = value
.get("package")
.and_then(|package| package.get("version"))
.and_then(|version| version.as_str())
.unwrap_or_default();
if declared != config.app.version {
return Err(AuroError::config(
&manifest,
Some("package.version"),
format!(
"Cargo.toml says `{declared}` but Auro.toml app.version says `{}`",
config.app.version
),
));
}
Ok(())
}
pub fn declares_hot_feature(dir: &Path) -> Result<bool, AuroError> {
let manifest = dir.join("Cargo.toml");
let text = fs::read_to_string(&manifest)?;
let value: toml::Value = toml::from_str(&text).map_err(|error: toml::de::Error| {
AuroError::config(&manifest, None, format!("invalid Cargo.toml: {error}"))
})?;
Ok(value
.get("features")
.and_then(|features| features.get("hot-reload"))
.is_some())
}
fn toml_path(path: &Path) -> String {
path.display().to_string().replace('\\', "/")
}
fn write(path: &Path, contents: &str) -> Result<(), AuroError> {
write_bytes(path, contents.as_bytes())
}
fn write_bytes(path: &Path, contents: &[u8]) -> Result<(), AuroError> {
if let Some(parent) = path.parent() {
fs::create_dir_all(parent)?;
}
fs::write(path, contents)?;
Ok(())
}
fn package_name(path: &Path) -> String {
let raw = path
.file_name()
.map(|name| name.to_string_lossy().to_string())
.unwrap_or_else(|| "app".to_owned());
let mut name: String = raw
.chars()
.map(|character| {
if character.is_ascii_alphanumeric() {
character.to_ascii_lowercase()
} else {
'-'
}
})
.collect();
while name.contains("--") {
name = name.replace("--", "-");
}
let name = name.trim_matches('-').to_owned();
let name = if name.is_empty() {
"app".to_owned()
} else {
name
};
if name.starts_with(|character: char| character.is_ascii_digit()) {
format!("app-{name}")
} else {
name
}
}
fn auro_dependency(manifest_dir: &Path, version: &str) -> String {
if let Some(repo) = manifest_dir.parent().and_then(Path::parent) {
let candidate = repo.join("crates").join("auro");
if candidate.is_dir() {
return format!("{{ path = \"{}\" }}", toml_path(&candidate));
}
}
format!("\"{version}\"")
}
fn xattr_patch() -> String {
let manifest = Path::new(env!("CARGO_MANIFEST_DIR"));
let Some(repo) = manifest.parent().and_then(Path::parent) else {
return String::new();
};
let vendored = repo.join("vendor").join("xattr");
if !vendored.is_dir() {
return String::new();
}
format!(
"\n# Temporary: gpui -> gpui_http_client -> zed-async-tar -> xattr 0.2.3 does not\n\
# compile against libc 0.2.191 on Linux (`ENOATTR` was removed). Drop this when\n\
# the dependency chain stops pulling xattr 0.2.\n\
[patch.crates-io]\n\
xattr = {{ path = \"{}\" }}\n",
toml_path(&vendored)
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn scaffolds_a_new_app() {
let temp = tempfile::tempdir().expect("tempdir");
let target = temp.path().join("demo");
new(&target).expect("scaffolds");
for file in [
"Cargo.toml",
"Auro.toml",
"Dioxus.toml",
"src/main.rs",
"ui/Cargo.toml",
"ui/src/lib.rs",
"ui/src/services.rs",
"data/Cargo.toml",
"data/src/lib.rs",
"types/Cargo.toml",
"types/src/lib.rs",
"icons/icon.png",
] {
assert!(target.join(file).is_file(), "{file} missing");
}
let icon = std::fs::read(target.join("icons/icon.png")).expect("icon");
assert!(icon.starts_with(b"\x89PNG\r\n\x1a\n"), "icon is a PNG");
assert_eq!(
u32::from_be_bytes([icon[16], icon[17], icon[18], icon[19]]),
512,
"the AppImage preflight needs a square PNG"
);
let config = Config::load_from(&target.join("Auro.toml")).expect("valid config");
assert_eq!(config.app.product_name, "demo");
assert_eq!(
config.default_window().map(|window| window.label.as_str()),
Some("main")
);
let manifest =
std::fs::read_to_string(target.join("Cargo.toml")).expect("generated manifest");
assert!(
manifest.contains("members = [\".\", \"ui\", \"data\", \"types\"]"),
"the scaffold is a four-crate workspace: {manifest}"
);
assert!(
manifest.contains("[workspace]"),
"the app must be its own workspace: {manifest}"
);
assert!(
manifest.contains("strip = \"symbols\""),
"release symbols are stripped by default: {manifest}"
);
let ui =
std::fs::read_to_string(target.join("ui/Cargo.toml")).expect("generated ui manifest");
assert!(
ui.contains("auro = { path ="),
"in-repo scaffolds point at the workspace: {ui}"
);
assert!(
!ui.contains("demo-data"),
"the ui crate must not depend on the boundary: {ui}"
);
let data = std::fs::read_to_string(target.join("data/Cargo.toml"))
.expect("generated data manifest");
assert!(
!data.contains("auro ="),
"the boundary crate stays framework-free: {data}"
);
let main = std::fs::read_to_string(target.join("src/main.rs")).expect("main");
assert!(
main.contains("demo_ui::run(demo_data::view_data)"),
"the bin wires the boundary: {main}"
);
let dioxus = std::fs::read_to_string(target.join("Dioxus.toml")).expect("Dioxus.toml");
assert!(
dioxus.contains("name = \"demo\""),
"dx needs the app name: {dioxus}"
);
let view = std::fs::read_to_string(target.join("ui/src/lib.rs")).expect("view");
assert!(
view.contains("com.example.demo"),
"identifier must reach the view: {view}"
);
assert!(
!view.contains("{{identifier}}"),
"placeholder must be substituted: {view}"
);
}
#[test]
fn refuses_a_non_empty_directory() {
let temp = tempfile::tempdir().expect("tempdir");
std::fs::write(temp.path().join("keep.txt"), "x").expect("write");
let error = new(temp.path()).expect_err("refuses");
assert!(error.to_string().contains("not empty"), "{error}");
}
#[test]
fn detects_the_hot_reload_feature() {
let temp = tempfile::tempdir().expect("tempdir");
std::fs::write(
temp.path().join("Cargo.toml"),
"[package]\nname = \"demo\"\nversion = \"0.1.0\"\n\n[features]\nhot-reload = [\"auro/hot-reload\"]\n",
)
.expect("write");
assert!(declares_hot_feature(temp.path()).expect("reads"));
std::fs::write(
temp.path().join("Cargo.toml"),
"[package]\nname = \"demo\"\nversion = \"0.1.0\"\n",
)
.expect("write");
assert!(!declares_hot_feature(temp.path()).expect("reads"));
}
#[test]
fn resolves_the_cargo_target_directory() {
let app = Path::new("/app");
let cwd = Path::new("/work");
assert_eq!(
resolve_target_dir(app, None, cwd),
Path::new("/app").join("target")
);
assert_eq!(
resolve_target_dir(app, Some(std::ffi::OsString::from("/tmp/auro-target")), cwd),
PathBuf::from("/tmp/auro-target")
);
assert_eq!(
resolve_target_dir(app, Some(std::ffi::OsString::from("local-target")), cwd),
Path::new("/work").join("local-target")
);
}
#[test]
fn generated_manifest_paths_use_forward_slashes() {
assert_eq!(toml_path(Path::new(r"C:\Users\a\auro")), "C:/Users/a/auro");
assert_eq!(toml_path(Path::new("/home/a/auro")), "/home/a/auro");
}
#[test]
fn normalises_names() {
assert_eq!(package_name(Path::new("/tmp/My App!")), "my-app");
assert_eq!(package_name(Path::new("/tmp/2cool")), "app-2cool");
}
#[test]
fn published_cli_uses_the_crate_version() {
let temp = tempfile::tempdir().expect("tempdir");
let manifest = temp.path().join("registry").join("auro-cli-0.1.0-alpha.1");
std::fs::create_dir_all(&manifest).expect("manifest dir");
assert_eq!(
auro_dependency(&manifest, "0.1.0-alpha.1"),
"\"0.1.0-alpha.1\""
);
}
#[test]
fn in_repo_cli_uses_the_workspace_path() {
let temp = tempfile::tempdir().expect("tempdir");
let manifest = temp.path().join("crates").join("auro-cli");
std::fs::create_dir_all(temp.path().join("crates").join("auro")).expect("workspace crate");
std::fs::create_dir_all(&manifest).expect("manifest dir");
let dependency = auro_dependency(&manifest, "0.1.0-alpha.1");
assert!(dependency.starts_with("{ path = \""), "{dependency}");
}
fn app_with_versions(config_version: &str, manifest_version: &str) -> tempfile::TempDir {
let temp = tempfile::tempdir().expect("tempdir");
std::fs::write(
temp.path().join("Auro.toml"),
format!(
r#"
[app]
identifier = "com.example.versioned"
product-name = "Versioned"
version = "{config_version}"
[[app.windows]]
label = "main"
"#
),
)
.expect("config");
std::fs::write(
temp.path().join("Cargo.toml"),
format!(
r#"
[package]
name = "versioned"
version = "{manifest_version}"
"#
),
)
.expect("manifest");
temp
}
#[test]
fn refuses_a_config_version_that_disagrees_with_the_manifest() {
let temp = app_with_versions("9.9.9", "0.1.0");
let error = load_checked(temp.path()).expect_err("version mismatch");
let message = error.to_string();
assert!(message.contains("9.9.9"), "{message}");
assert!(message.contains("0.1.0"), "{message}");
}
#[test]
fn refuses_to_bundle_when_the_config_disables_it() {
let temp = app_with_versions("0.1.0", "0.1.0");
let mut config = std::fs::read_to_string(temp.path().join("Auro.toml")).expect("config");
config.push_str("\n[bundle]\nactive = false\n");
std::fs::write(temp.path().join("Auro.toml"), config).expect("config");
let error = bundle(temp.path()).expect_err("bundling is disabled");
assert!(error.to_string().contains("active = false"), "{error}");
}
#[test]
fn accepts_matching_versions() {
let temp = app_with_versions("0.1.0", "0.1.0");
load_checked(temp.path()).expect("matching versions");
}
#[cfg(windows)]
#[test]
fn strips_the_windows_verbatim_prefix() {
assert_eq!(
plain_path(PathBuf::from(r"\\?\C:\foo")),
PathBuf::from(r"C:\foo")
);
}
#[test]
fn reads_the_binary_name_from_the_manifest() {
let temp = tempfile::tempdir().expect("tempdir");
std::fs::write(
temp.path().join("Cargo.toml"),
"[package]\nname = \"demo\"\nversion = \"0.1.0\"\n",
)
.expect("manifest");
assert_eq!(binary_name(temp.path()).expect("name"), "demo");
std::fs::write(
temp.path().join("Cargo.toml"),
"[package]\nname = \"demo\"\nversion = \"0.1.0\"\ndefault-run = \"server\"\n",
)
.expect("manifest");
assert_eq!(binary_name(temp.path()).expect("default-run"), "server");
}
}