diff --git a/src/init.rs b/src/init.rs new file mode 100644 index 0000000..8d6c4d0 --- /dev/null +++ b/src/init.rs @@ -0,0 +1,62 @@ +use anyhow::Result; +use dialoguer::{Confirm, Select}; +use std::path::Path; + +use crate::config::EngineKind; + +pub fn handle_init(game_id: String, branch: String, engine: Option) -> Result<()> { + let path = Path::new("./wavedash.toml"); + + if path.exists() { + let overwrite = Confirm::new() + .with_prompt("wavedash.toml already exists. Overwrite?") + .default(false) + .interact()?; + + if !overwrite { + println!("Aborted."); + return Ok(()); + } + } + + let engine = match engine { + Some(e) => e, + None => { + let items = &["Godot", "Unity", "Custom"]; + let selection = Select::new() + .with_prompt("Select engine") + .items(items) + .default(0) + .interact()?; + + match selection { + 0 => EngineKind::Godot, + 1 => EngineKind::Unity, + 2 => EngineKind::Custom, + _ => unreachable!(), + } + } + }; + + let engine_section = match engine { + EngineKind::Godot => "[godot]\nversion = \"4.3\"\n".to_string(), + EngineKind::Unity => "[unity]\nversion = \"6000.0\"\n".to_string(), + EngineKind::Custom => { + "[custom]\nversion = \"1.0\"\nentrypoint = \"index.html\"\n".to_string() + } + }; + + let content = format!( + "game_id = \"{game_id}\"\n\ + branch = \"{branch}\"\n\ + upload_dir = \"./build\"\n\ + version = \"0.0.1\"\n\ + \n\ + {engine_section}" + ); + + std::fs::write(path, &content)?; + println!("✓ Created wavedash.toml"); + + Ok(()) +} diff --git a/src/main.rs b/src/main.rs index 8851585..3be2be0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ mod builds; mod config; mod dev; mod file_staging; +mod init; mod updater; use anyhow::Result; @@ -12,6 +13,23 @@ use clap::{Parser, Subcommand}; use dev::handle_dev; use std::path::PathBuf; +#[derive(Clone, clap::ValueEnum)] +enum EngineArg { + Godot, + Unity, + Custom, +} + +impl From for config::EngineKind { + fn from(arg: EngineArg) -> Self { + match arg { + EngineArg::Godot => config::EngineKind::Godot, + EngineArg::Unity => config::EngineKind::Unity, + EngineArg::Custom => config::EngineKind::Custom, + } + } +} + fn mask_token(token: &str) -> String { if token.len() > 10 { format!("{}...{}", &token[..6], &token[token.len() - 3..]) @@ -51,6 +69,15 @@ enum Commands { #[arg(long = "no-open", help = "Don't open the sandbox URL in the browser")] no_open: bool, }, + #[command(about = "Initialize a new wavedash.toml config file")] + Init { + #[arg(long)] + game_id: String, + #[arg(long)] + branch: String, + #[arg(long, value_enum)] + engine: Option, + }, #[command(about = "Check for and install updates")] Update, } @@ -153,6 +180,13 @@ async fn main() -> Result<()> { Commands::Dev { config, no_open } => { handle_dev(config, cli.verbose, no_open).await?; } + Commands::Init { + game_id, + branch, + engine, + } => { + init::handle_init(game_id, branch, engine.map(|e| e.into()))?; + } Commands::Update => { updater::run_update().await?; }