Skip to content

Commit 59395c7

Browse files
committed
support command and commands for script builder
1 parent cf53ef5 commit 59395c7

1 file changed

Lines changed: 57 additions & 40 deletions

File tree

lib/icp-adapter/src/script.rs

Lines changed: 57 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,56 +5,73 @@ use serde::Deserialize;
55
use snafu::{OptionExt, ResultExt, Snafu};
66
use std::process::{Command, Stdio};
77

8+
#[derive(Debug, Deserialize)]
9+
#[serde(rename_all = "lowercase")]
10+
pub enum CommandField {
11+
/// Command used to build a canister
12+
Command(String),
13+
14+
/// Set of commands used to build a canister
15+
Commands(Vec<String>),
16+
}
17+
818
/// Configuration for a custom canister build adapter.
919
#[derive(Debug, Deserialize)]
1020
pub struct ScriptAdapter {
1121
/// Command used to build a canister
12-
pub command: String,
22+
#[serde(flatten)]
23+
pub command: CommandField,
1324
}
1425

1526
#[async_trait]
1627
impl Adapter for ScriptAdapter {
1728
async fn compile(&self, path: Utf8PathBuf) -> Result<(), AdapterCompileError> {
18-
// Parse command input
19-
let input = shellwords::split(&self.command).context(CommandParseSnafu {
20-
command: &self.command,
21-
})?;
22-
23-
// Separate command and args
24-
let (cmd, args) = input.split_first().context(InvalidCommandSnafu {
25-
command: &self.command,
26-
reason: "command must include at least one element".to_string(),
27-
})?;
28-
29-
// Command
30-
let mut cmd = Command::new(cmd);
31-
32-
// Args
33-
cmd.args(args);
34-
35-
// Set directory
36-
cmd.current_dir(&path);
37-
38-
// Output
39-
cmd.stdin(Stdio::inherit());
40-
cmd.stdout(Stdio::inherit());
41-
cmd.stderr(Stdio::inherit());
42-
43-
// Execute
44-
let status = cmd.status().context(CommandInvokeSnafu {
45-
command: &self.command,
46-
})?;
47-
48-
// Status
49-
if !status.success() {
50-
return Err(ScriptAdapterCompileError::CommandStatus {
51-
command: self.command.to_owned(),
52-
code: status
53-
.code()
54-
.map(|c| c.to_string())
55-
.unwrap_or("N/A".to_string()),
29+
let cmds = match &self.command {
30+
CommandField::Command(cmd) => std::slice::from_ref(cmd),
31+
CommandField::Commands(cmds) => cmds,
32+
};
33+
34+
for input_cmd in cmds {
35+
// Parse command input
36+
let input =
37+
shellwords::split(input_cmd).context(CommandParseSnafu { command: input_cmd })?;
38+
39+
// Separate command and args
40+
let (cmd, args) = input.split_first().context(InvalidCommandSnafu {
41+
command: input_cmd,
42+
reason: "command must include at least one element".to_string(),
43+
})?;
44+
45+
// Command
46+
let mut cmd = Command::new(cmd);
47+
48+
// Args
49+
cmd.args(args);
50+
51+
// Set directory
52+
cmd.current_dir(&path);
53+
54+
// Output
55+
cmd.stdin(Stdio::inherit());
56+
cmd.stdout(Stdio::inherit());
57+
cmd.stderr(Stdio::inherit());
58+
59+
// Execute
60+
let status = cmd
61+
.status()
62+
.context(CommandInvokeSnafu { command: input_cmd })?;
63+
64+
// Status
65+
if !status.success() {
66+
return Err(ScriptAdapterCompileError::CommandStatus {
67+
command: input_cmd.to_owned(),
68+
code: status
69+
.code()
70+
.map(|c| c.to_string())
71+
.unwrap_or("N/A".to_string()),
72+
}
73+
.into());
5674
}
57-
.into());
5875
}
5976

6077
Ok(())

0 commit comments

Comments
 (0)