use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::sync::atomic::{AtomicU64, Ordering};
use crate::types::{ExitCode, Param, Type};
#[derive(Debug, PartialEq, Eq)]
pub struct ScriptSummary {
pub desc: Option<String>,
pub params: Vec<Param>,
pub output: Option<Type>,
pub exits: Vec<ExitCode>,
}
#[derive(Debug, PartialEq, Eq)]
pub struct ScriptRun {
pub success: bool,
pub code: Option<i32>,
pub output: String,
pub structured_output: Option<String>,
}
pub fn summarize(path: &Path) -> Result<ScriptSummary, Box<dyn std::error::Error>> {
let contents = std::fs::read_to_string(path)?;
Ok(parse_header(&contents)?)
}
fn parse_header(contents: &str) -> Result<ScriptSummary, String> {
let mut desc = None;
let mut params: Option<Vec<Param>> = None;
let mut output: Option<Type> = None;
let mut output_seen = false;
let mut exits: Option<Vec<ExitCode>> = None;
for line in contents.lines() {
let Some(comment) = line.trim_start().strip_prefix('#') else {
continue;
};
let comment = comment.trim();
if let Some(rest) = comment.strip_prefix("desc:") {
desc.get_or_insert_with(|| rest.trim().to_string());
} else if let Some(rest) = comment.strip_prefix("params:") {
if params.is_none() {
params = Some(parse_params(rest)?);
}
} else if let Some(rest) = comment.strip_prefix("output:") {
if !output_seen {
output_seen = true;
let rest = rest.trim();
if !rest.is_empty() {
output = Some(
Type::parse(rest).map_err(|e| format!("invalid `# output:` type: {e}"))?,
);
}
}
} else if let Some(rest) = comment.strip_prefix("exits:") {
if exits.is_none() {
exits = Some(parse_exits(rest)?);
}
}
}
Ok(ScriptSummary {
desc,
params: params.unwrap_or_default(),
output,
exits: exits.unwrap_or_default(),
})
}
fn parse_exits(rest: &str) -> Result<Vec<ExitCode>, String> {
let mut exits: Vec<ExitCode> = Vec::new();
for entry in split_exit_entries(rest) {
let (code_tok, after_code) = split_first_word(&entry);
let code: i32 = code_tok
.parse()
.map_err(|_| format!("invalid exit code `{code_tok}` in `# exits:`"))?;
let (name, after_name) = split_first_word(after_code);
if name.is_empty() {
return Err(format!("exit code {code} in `# exits:` is missing a name"));
}
if !is_dsl_ident(&name) {
return Err(format!(
"exit code name `{name}` in `# exits:` isn't a valid identifier"
));
}
if exits.iter().any(|e| e.name == name) {
return Err(format!("duplicate exit code name `{name}` in `# exits:`"));
}
let desc = parse_exit_desc(after_name, code)?;
exits.push(ExitCode { code, name, desc });
}
Ok(exits)
}
fn parse_exit_desc(rest: &str, code: i32) -> Result<Option<String>, String> {
let rest = rest.trim();
if rest.is_empty() {
return Ok(None);
}
let inner = rest.strip_prefix('"').ok_or_else(|| {
format!("unexpected text after exit code {code} name in `# exits:`; quote the description")
})?;
let (desc, after) = inner
.split_once('"')
.ok_or_else(|| format!("unterminated description for exit code {code} in `# exits:`"))?;
if !after.trim().is_empty() {
return Err(format!(
"unexpected text after exit code {code} description in `# exits:`"
));
}
Ok(Some(desc.to_string()))
}
fn split_exit_entries(rest: &str) -> Vec<String> {
let mut entries = Vec::new();
let mut current = String::new();
let mut in_string = false;
for c in rest.chars() {
match c {
'"' => {
in_string = !in_string;
current.push(c);
}
',' if !in_string => {
if !current.trim().is_empty() {
entries.push(current.trim().to_string());
}
current.clear();
}
_ => current.push(c),
}
}
if !current.trim().is_empty() {
entries.push(current.trim().to_string());
}
entries
}
fn split_first_word(s: &str) -> (String, &str) {
let s = s.trim_start();
let end = s
.find(|c: char| c.is_whitespace() || c == '"')
.unwrap_or(s.len());
(s[..end].to_string(), s[end..].trim_start())
}
fn is_dsl_ident(name: &str) -> bool {
let chars: Vec<char> = name.chars().collect();
match chars.first() {
Some(c) if c.is_ascii_alphabetic() || *c == '_' => {}
_ => return false,
}
for (i, c) in chars.iter().enumerate().skip(1) {
if c.is_ascii_alphanumeric() || *c == '_' {
continue;
}
if *c == '-'
&& chars
.get(i + 1)
.is_some_and(|n| n.is_ascii_alphabetic() || *n == '_')
{
continue;
}
return false;
}
true
}
pub(crate) fn parse_params(rest: &str) -> Result<Vec<Param>, String> {
let mut params = Vec::new();
for token in split_params(rest) {
let param = match token.split_once(':') {
Some((name, ty)) => {
let name = name.trim().to_string();
let ty = Type::parse(ty.trim())
.map_err(|e| format!("invalid type for param `{name}`: {e}"))?;
Param { name, ty }
}
None => Param {
name: token,
ty: Type::String,
},
};
params.push(param);
}
Ok(params)
}
fn split_params(rest: &str) -> Vec<String> {
let mut tokens = Vec::new();
let mut current = String::new();
let mut depth = 0i32;
let mut in_string = false;
let flush = |current: &mut String, tokens: &mut Vec<String>| {
let token = current.trim();
if !token.is_empty() {
tokens.push(token.to_string());
}
current.clear();
};
for c in rest.chars() {
match c {
'"' => {
in_string = !in_string;
current.push(c);
}
'[' | '{' if !in_string => {
depth += 1;
current.push(c);
}
']' | '}' if !in_string => {
depth -= 1;
current.push(c);
}
',' if !in_string && depth == 0 => flush(&mut current, &mut tokens),
c if c.is_whitespace() && !in_string && depth == 0 => flush(&mut current, &mut tokens),
_ => current.push(c),
}
}
flush(&mut current, &mut tokens);
tokens
}
#[derive(Debug)]
pub enum ScriptError {
Unreadable {
path: PathBuf,
source: std::io::Error,
},
NotExecutable { path: PathBuf },
}
impl std::fmt::Display for ScriptError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ScriptError::Unreadable { path, source } => {
write!(f, "tool script {} can't be read: {source}", path.display())
}
ScriptError::NotExecutable { path } => {
write!(f, "tool script {} isn't executable", path.display())
}
}
}
}
impl std::error::Error for ScriptError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
ScriptError::Unreadable { source, .. } => Some(source),
ScriptError::NotExecutable { .. } => None,
}
}
}
pub fn prepare_script(script: &Path) -> Result<PathBuf, ScriptError> {
use std::os::unix::fs::PermissionsExt;
let path = std::fs::canonicalize(script).map_err(|source| ScriptError::Unreadable {
path: script.to_path_buf(),
source,
})?;
let mode = std::fs::metadata(&path)
.map_err(|source| ScriptError::Unreadable {
path: path.clone(),
source,
})?
.permissions()
.mode();
if mode & 0o111 == 0 {
return Err(ScriptError::NotExecutable { path });
}
Ok(path)
}
pub fn run_script(
path: &Path,
args: &HashMap<String, String>,
) -> Result<ScriptRun, Box<dyn std::error::Error>> {
let output_path = fresh_output_path();
let mut cmd = Command::new(path);
for (key, value) in args {
cmd.env(key, value);
}
cmd.env("SP_OUTPUT_PATH", &output_path);
let output = run_with_etxtbsy_retry(&mut cmd)?;
let mut combined = String::from_utf8_lossy(&output.stdout).into_owned();
let stderr = String::from_utf8_lossy(&output.stderr);
if !stderr.is_empty() {
if !combined.is_empty() {
combined.push('\n');
}
combined.push_str(&stderr);
}
let structured_output = std::fs::read_to_string(&output_path)
.ok()
.filter(|s| !s.trim().is_empty());
std::fs::remove_file(&output_path).ok();
Ok(ScriptRun {
success: output.status.success(),
code: output.status.code(),
output: combined,
structured_output,
})
}
fn fresh_output_path() -> PathBuf {
static SEQ: AtomicU64 = AtomicU64::new(0);
let seq = SEQ.fetch_add(1, Ordering::Relaxed);
std::env::temp_dir().join(format!("sp_output_{}_{seq}.json", std::process::id()))
}
fn run_with_etxtbsy_retry(cmd: &mut Command) -> std::io::Result<std::process::Output> {
use std::io::ErrorKind;
const MAX_RETRIES: u32 = 5;
let mut attempt = 0;
loop {
match cmd.output() {
Err(e) if e.kind() == ErrorKind::ExecutableFileBusy && attempt < MAX_RETRIES => {
attempt += 1;
std::thread::sleep(std::time::Duration::from_millis(10 * u64::from(attempt)));
}
other => return other,
}
}
}
#[cfg(test)]
#[path = "script_tests.rs"]
mod tests;