implement build command

This commit is contained in:
Ilion Beyst 2021-12-28 19:28:19 +01:00
parent 6d176ac99f
commit ee5af8d076
4 changed files with 46 additions and 1 deletions

View file

@ -0,0 +1,27 @@
use clap::Parser;
use std::io;
use tokio::process;
use crate::workspace::Workspace;
#[derive(Parser)]
pub struct BuildCommand {
/// Name of the bot to build
bot: String,
}
impl BuildCommand {
pub async fn run(self) -> io::Result<()> {
let workspace = Workspace::open_current_dir()?;
let bot = workspace.get_bot(&self.bot)?;
if let Some(argv) = bot.config.get_build_argv() {
process::Command::new(&argv[0])
.args(&argv[1..])
.current_dir(&bot.path)
.spawn()?
.wait()
.await?;
}
Ok(())
}
}

View file

@ -1,3 +1,4 @@
mod build;
mod init;
mod run_match;
mod serve;
@ -21,6 +22,7 @@ impl Cli {
Command::Init(command) => command.run().await,
Command::RunMatch(command) => command.run().await,
Command::Serve(command) => command.run().await,
Command::Build(command) => command.run().await,
}
}
}
@ -33,4 +35,6 @@ enum Command {
RunMatch(run_match::RunMatchCommand),
/// Host local webserver
Serve(serve::ServeCommand),
/// Run build command for a bot
Build(build::BuildCommand),
}

View file

@ -40,7 +40,12 @@ impl RunMatchCommand {
match_runner::run_match(match_config).await;
println!("match completed successfully");
// TODO: maybe print the match result as well?
println!("wrote match log to {}", log_path.to_str().unwrap());
let relative_path = match log_path.strip_prefix(&workspace.root_path) {
Ok(path) => path.to_str().unwrap(),
Err(_) => log_path.to_str().unwrap(),
};
println!("wrote match log to {}", relative_path);
Ok(())
}
}

View file

@ -33,9 +33,18 @@ pub struct BotConfig {
}
impl BotConfig {
// TODO: these commands should not be here
pub fn get_run_argv(&self) -> Vec<String> {
// TODO: proper error handling
shlex::split(&self.run_command)
.expect("Failed to parse bot run command. Check for unterminated quotes.")
}
pub fn get_build_argv(&self) -> Option<Vec<String>> {
// TODO: proper error handling
self.build_command.as_ref().map(|cmd| {
shlex::split(cmd)
.expect("Failed to parse bot build command. Check for unterminated quotes.")
})
}
}