use std::fs;
use std::path::Path;
use anyhow::{Context, Result};
use crate::catalog::wally_packages::{self, Realm};
use crate::steps::{rojo, run_in};
use crate::ui;
pub fn ensure_wally_init(project_dir: &Path) -> Result<()> {
if project_dir.join("wally.toml").exists() {
ui::ok("wally.toml already exists");
return Ok(());
}
run_in("wally", &["init"], Some(project_dir))
}
pub fn write_wally_toml(project_dir: &Path, package_name: &str, selected: &[String]) -> Result<()> {
let path = project_dir.join("wally.toml");
if path.exists() {
let content = fs::read_to_string(&path)?;
let has_all = selected
.iter()
.all(|key| content.lines().any(|l| l.trim_start().starts_with(&format!("{key} ="))));
if has_all {
ui::ok("wally.toml already configured");
return Ok(());
}
}
let mut specs = Vec::new();
for key in selected {
specs.push(
wally_packages::find(key)
.with_context(|| format!("unknown package key `{key}` in selection"))?,
);
}
let mut body = format!(
"[package]\nname = \"{package_name}\"\nversion = \"0.1.0\"\nregistry = \"https://github.com/UpliftGames/wally-index\"\nrealm = \"shared\"\n\n[dependencies]\n"
);
for spec in specs.iter().filter(|s| s.realm == Realm::Shared) {
body.push_str(&format!("{} = \"{}\"\n", spec.key, spec.source));
}
let server: Vec<_> = specs.iter().filter(|s| s.realm == Realm::Server).collect();
if !server.is_empty() {
body.push_str("\n[server-dependencies]\n");
for spec in server {
body.push_str(&format!("{} = \"{}\"\n", spec.key, spec.source));
}
}
fs::write(&path, body)?;
ui::ok("wrote wally.toml");
Ok(())
}
pub fn wally_install(project_dir: &Path) -> Result<()> {
run_in("wally", &["install"], Some(project_dir))
}
pub const PACKAGES_DIR: &str = "Packages";
pub const SERVER_PACKAGES_DIR: &str = "ServerPackages";
pub fn wally_package_types(project_dir: &Path) -> Result<()> {
let mut args = vec!["-s", "sourcemap.json"];
for dir in [PACKAGES_DIR, SERVER_PACKAGES_DIR] {
if project_dir.join(dir).is_dir() {
args.push(dir);
}
}
run_in("wally-package-types", &args, Some(project_dir))
}
pub fn sync(project_dir: &Path) -> Result<()> {
wally_install(project_dir)?;
fs::create_dir_all(project_dir.join(PACKAGES_DIR))?;
rojo::generate_sourcemap(project_dir)?;
wally_package_types(project_dir)
}