diff options
author | Ilion Beyst <ilion.beyst@gmail.com> | 2021-12-25 15:36:23 +0100 |
---|---|---|
committer | Ilion Beyst <ilion.beyst@gmail.com> | 2021-12-25 17:48:54 +0100 |
commit | 92a13bac9289fd00a6fa2c70129490fa22ef0661 (patch) | |
tree | ceba13a471891b03be10fe20b768db37f4cb3c71 /planetwars-localdev/src/lib.rs | |
parent | d0a0fcfdeda2c315a13ddd96b4fca958da0d9858 (diff) | |
download | planetwars.dev-92a13bac9289fd00a6fa2c70129490fa22ef0661.tar.xz planetwars.dev-92a13bac9289fd00a6fa2c70129490fa22ef0661.zip |
add init-project command
Diffstat (limited to 'planetwars-localdev/src/lib.rs')
-rw-r--r-- | planetwars-localdev/src/lib.rs | 40 |
1 files changed, 39 insertions, 1 deletions
diff --git a/planetwars-localdev/src/lib.rs b/planetwars-localdev/src/lib.rs index 3b77efb..562e9a6 100644 --- a/planetwars-localdev/src/lib.rs +++ b/planetwars-localdev/src/lib.rs @@ -22,6 +22,8 @@ struct Cli { #[derive(Subcommand)] enum Commands { + /// Initialize a new project + InitProject(InitProjectCommand), /// Run a match RunMatch(RunMatchCommand), } @@ -34,6 +36,12 @@ struct RunMatchCommand { bots: Vec<String>, } +#[derive(Parser)] +struct InitProjectCommand { + /// project root directory + path: String, +} + #[derive(Serialize, Deserialize, Debug)] struct ProjectConfig { bots: HashMap<String, BotConfig>, @@ -49,6 +57,7 @@ pub async fn run() { let matches = Cli::parse(); let res = match matches.command { Commands::RunMatch(command) => run_match(command).await, + Commands::InitProject(command) => init_project(command), }; if let Err(err) = res { eprintln!("{}", err); @@ -89,7 +98,10 @@ async fn run_match(command: RunMatchCommand) -> io::Result<()> { }; match_runner::run_match(match_config).await; - + println!("match completed successfully"); + // TODO: don't hardcode match path. + // maybe print the match result as well? + println!("wrote match log to matches/{}.log", timestamp); Ok(()) } @@ -101,3 +113,29 @@ fn resolve_bot_config(project_dir: &Path, config: BotConfig) -> BotConfig { argv: config.argv, } } + +macro_rules! copy_asset { + ($path:expr, $file_name:literal) => { + ::std::fs::write( + $path.join($file_name), + include_bytes!(concat!("../assets/", $file_name)), + )?; + }; +} + +fn init_project(command: InitProjectCommand) -> io::Result<()> { + let path = PathBuf::from(&command.path); + + // create directories + std::fs::create_dir_all(&path)?; + std::fs::create_dir(path.join("maps"))?; + std::fs::create_dir(path.join("matches"))?; + std::fs::create_dir_all(path.join("bots/simplebot"))?; + + // create files + copy_asset!(path, "pw_project.toml"); + copy_asset!(path.join("maps"), "hex.json"); + copy_asset!(path.join("bots/simplebot"), "simplebot.py"); + + Ok(()) +} |