Skip to main content

mk_lib/schema/
mod.rs

1mod command;
2mod include;
3mod plan;
4mod precondition;
5mod shell;
6mod task;
7mod task_context;
8mod task_dependency;
9mod task_root;
10mod use_cargo;
11mod use_npm;
12mod validation;
13
14use std::collections::HashSet;
15use std::fmt;
16use std::process::Stdio;
17use std::sync::{
18  Arc,
19  Mutex,
20};
21
22use once_cell::sync::Lazy;
23use regex::Regex;
24
25pub type ActiveTasks = Arc<Mutex<HashSet<String>>>;
26pub type CompletedTasks = Arc<Mutex<HashSet<String>>>;
27
28#[derive(Debug)]
29pub struct ExecutionInterrupted;
30
31impl fmt::Display for ExecutionInterrupted {
32  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33    write!(f, "Execution interrupted")
34  }
35}
36
37impl std::error::Error for ExecutionInterrupted {}
38
39pub use command::*;
40pub use include::*;
41pub use plan::*;
42pub use precondition::*;
43pub use shell::*;
44pub use task::*;
45pub use task_context::*;
46pub use task_dependency::*;
47pub use task_root::*;
48pub use use_cargo::*;
49pub use use_npm::*;
50pub use validation::*;
51
52use crate::secrets::load_secret_value;
53
54static TEMPLATE_COMMAND_RE: Lazy<Regex> =
55  Lazy::new(|| Regex::new(r"^\$\{\{.+\}\}$").expect("valid template regex"));
56static TEMPLATE_EXPR_RE: Lazy<Regex> =
57  Lazy::new(|| Regex::new(r"\$\{\{\s*(.+?)\s*\}\}").expect("valid template expression regex"));
58
59pub fn is_shell_command(value: &str) -> anyhow::Result<bool> {
60  let re = Regex::new(r"^\$\(.+\)$")?;
61  Ok(re.is_match(value))
62}
63
64pub fn is_template_command(value: &str) -> anyhow::Result<bool> {
65  Ok(TEMPLATE_COMMAND_RE.is_match(value))
66}
67
68pub fn resolve_template_command_value(value: &str, context: &TaskContext) -> anyhow::Result<String> {
69  let value = value.trim_start_matches("${{").trim_end_matches("}}").trim();
70  resolve_template_expression(value, context)
71}
72
73pub fn resolve_template_expression(value: &str, context: &TaskContext) -> anyhow::Result<String> {
74  if value.starts_with("env.") {
75    let value = value.trim_start_matches("env.");
76    let value = context
77      .env_vars
78      .get(value)
79      .ok_or_else(|| anyhow::anyhow!("Failed to find environment variable"))?;
80    Ok(value.to_string())
81  } else if value.starts_with("secrets.") {
82    let path = value.trim_start_matches("secrets.");
83    load_secret_value(
84      path,
85      &context.task_root.config_base_dir(),
86      context.secret_vault_location.as_deref(),
87      context.secret_keys_location.as_deref(),
88      context.secret_key_name.as_deref(),
89    )
90  } else if value.starts_with("outputs.") {
91    let name = value.trim_start_matches("outputs.");
92    context
93      .get_task_output(name)?
94      .ok_or_else(|| anyhow::anyhow!("Failed to find task output - {}", name))
95  } else {
96    Ok(value.to_string())
97  }
98}
99
100pub fn interpolate_template_string(value: &str, context: &TaskContext) -> anyhow::Result<String> {
101  let mut result = String::with_capacity(value.len());
102  let mut last_end = 0usize;
103  for captures in TEMPLATE_EXPR_RE.captures_iter(value) {
104    let Some(full_match) = captures.get(0) else {
105      continue;
106    };
107    let Some(expr) = captures.get(1) else {
108      continue;
109    };
110    result.push_str(&value[last_end..full_match.start()]);
111    result.push_str(&resolve_template_expression(expr.as_str().trim(), context)?);
112    last_end = full_match.end();
113  }
114  result.push_str(&value[last_end..]);
115  Ok(result)
116}
117
118pub fn extract_output_references(value: &str) -> Vec<String> {
119  TEMPLATE_EXPR_RE
120    .captures_iter(value)
121    .filter_map(|captures| captures.get(1))
122    .map(|expr| expr.as_str().trim())
123    .filter_map(|expr| expr.strip_prefix("outputs."))
124    .map(str::to_string)
125    .collect()
126}
127
128pub fn contains_output_reference(value: &str) -> bool {
129  !extract_output_references(value).is_empty()
130}
131
132pub fn get_output_handler(verbose: bool) -> Stdio {
133  if verbose {
134    Stdio::piped()
135  } else {
136    Stdio::null()
137  }
138}
139
140#[cfg(test)]
141mod test {
142  use std::sync::Arc;
143
144  use super::*;
145
146  #[test]
147  fn test_interpolate_template_string_resolves_outputs() -> anyhow::Result<()> {
148    let root = Arc::new(TaskRoot::default());
149    let context = TaskContext::empty_with_root(root);
150    context.insert_task_output("version", "v1.2.3")?;
151    assert_eq!(
152      interpolate_template_string("tag=${{ outputs.version }}", &context)?,
153      "tag=v1.2.3"
154    );
155    Ok(())
156  }
157
158  #[test]
159  fn test_extract_output_references_finds_all_output_templates() {
160    assert_eq!(
161      extract_output_references("${{ outputs.first }}-${{ outputs.second }}"),
162      vec!["first".to_string(), "second".to_string()]
163    );
164  }
165}