cargo_image_runner/core/
context.rs1use crate::config::Config;
2use crate::core::error::Result;
3use std::collections::HashMap;
4use std::path::PathBuf;
5
6pub struct Context {
8 pub config: Config,
10
11 pub workspace_root: PathBuf,
13
14 pub target_dir: PathBuf,
16
17 pub executable: PathBuf,
19
20 pub is_test: bool,
22
23 pub cache_dir: PathBuf,
25
26 pub output_dir: PathBuf,
28
29 pub template_vars: HashMap<String, String>,
31}
32
33impl Context {
34 pub fn new(config: Config, workspace_root: PathBuf, executable: PathBuf) -> Result<Self> {
36 let target_dir = workspace_root.join("target").join("image-runner");
37 let cache_dir = target_dir.join("cache");
38 let output_dir = target_dir.join("output");
39
40 std::fs::create_dir_all(&cache_dir)?;
42 std::fs::create_dir_all(&output_dir)?;
43
44 let mut ctx = Self {
45 config,
46 workspace_root: workspace_root.clone(),
47 target_dir,
48 executable: executable.clone(),
49 is_test: false,
50 cache_dir,
51 output_dir,
52 template_vars: HashMap::new(),
53 };
54
55 ctx.detect_test();
57
58 ctx.init_template_vars();
60
61 Ok(ctx)
62 }
63
64 pub fn detect_test(&mut self) {
69 if let Some(file_name) = self.executable.file_name().and_then(|n| n.to_str()) {
70 if file_name.contains('-') {
73 if let Some(suffix) = file_name.rsplit('-').next() {
74 if suffix.len() >= 8 && suffix.chars().all(|c| c.is_ascii_hexdigit()) {
76 self.is_test = true;
77 }
78 }
79 }
80 }
81 }
82
83 fn init_template_vars(&mut self) {
85 self.template_vars = self.config.variables.clone();
87
88 self.template_vars.insert(
90 "EXECUTABLE".to_string(),
91 self.executable.display().to_string(),
92 );
93
94 if let Some(exe_name) = self.executable.file_name().and_then(|n| n.to_str()) {
95 self.template_vars
96 .insert("EXECUTABLE_NAME".to_string(), exe_name.to_string());
97 }
98
99 self.template_vars.insert(
100 "WORKSPACE_ROOT".to_string(),
101 self.workspace_root.display().to_string(),
102 );
103
104 self.template_vars.insert(
105 "OUTPUT_DIR".to_string(),
106 self.output_dir.display().to_string(),
107 );
108
109 self.template_vars.insert(
110 "IS_TEST".to_string(),
111 if self.is_test { "1" } else { "0" }.to_string(),
112 );
113 }
114
115 pub fn get_extra_args(&self) -> &[String] {
117 if self.is_test {
118 &self.config.test.extra_args
119 } else {
120 &self.config.run.extra_args
121 }
122 }
123
124 pub fn test_success_exit_code(&self) -> Option<i32> {
126 self.config.test.success_exit_code
127 }
128}