use super::{Executor, WorkspaceTarget};
use crate::config::Recipe;
use crate::container::{Backend, ExecOptions};
use crate::runtime::error::RuntimeError;
use crate::runtime::ports::{RecipeProvider, ScriptProvider};
use std::collections::HashSet;
use tokio::process::Command;
use tracing::debug;
impl Executor {
pub(crate) async fn run_step(
&self,
recipe: &Recipe,
step: &str,
forwarded: &[String],
in_progress: HashSet<String>,
) -> Result<i32, RuntimeError> {
if !step.chars().any(char::is_whitespace) {
if self.config.has_recipe(step) {
debug!(recipe_ref = step, "step is recipe reference");
return self
.run_recipe_inner(step.to_string(), forwarded.to_vec(), in_progress)
.await;
}
if self.config.has_script(step) {
debug!(script_ref = step, "step is script reference");
return self
.run_script_inner(step.to_string(), forwarded.to_vec(), in_progress)
.await;
}
}
let env = self.effective_env(Some(&recipe.env)).await?;
let mut argv = shell_words::split(step).map_err(|e| RuntimeError::ArgvParse {
input: step.into(),
message: e.to_string(),
})?;
if recipe.forward_args {
argv.extend(forwarded.iter().cloned());
}
if argv.is_empty() {
return Ok(0);
}
if let Some(service) = &recipe.service {
let opts = ExecOptions {
tty: recipe.tty,
env: env.project_only_map(),
workdir: None,
};
let argv_refs: Vec<&str> = argv.iter().map(String::as_str).collect();
if self.sink.capture() {
let child = self.backend.spawn_exec(service, &argv_refs, &opts).await?;
return self.stream_child_to_sink(child).await;
}
return self
.backend
.exec(service, &argv_refs, &opts)
.await
.map_err(RuntimeError::from);
}
if let WorkspaceTarget::Devcontainer(dc) = &self.workspace {
let opts = ExecOptions {
tty: recipe.tty,
env: env.project_only_map(),
workdir: None,
};
let argv_refs: Vec<&str> = argv.iter().map(String::as_str).collect();
if self.sink.capture() {
let child = dc
.spawn_exec(dc.container_name(), &argv_refs, &opts)
.await?;
return self.stream_child_to_sink(child).await;
}
return dc
.exec(dc.container_name(), &argv_refs, &opts)
.await
.map_err(RuntimeError::from);
}
let (program, rest) = argv.split_first().expect("non-empty argv");
let mut cmd = Command::new(program);
cmd.args(rest);
cmd.current_dir(self.project_root.as_ref());
env.apply_to(&mut cmd);
self.spawn_host(cmd).await
}
}