1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
use std::path::{Path, PathBuf};
use crate::component::Component;
use crate::error::Result;
use crate::server::CommandOutput;
/// Output from a extension runner script execution.
pub struct RunnerOutput {
pub exit_code: i32,
pub success: bool,
pub stdout: String,
pub stderr: String,
}
use super::ExtensionExecutionContext;
/// Orchestrates extension script execution for lint/test/build runners.
///
/// Encapsulates the shared logic for finding components, resolving extensions,
/// loading manifests, merging settings, and executing runner scripts.
pub struct ExtensionRunner {
execution_context: ExtensionExecutionContext,
settings_overrides: Vec<(String, String)>,
env_vars: Vec<(String, String)>,
script_args: Vec<String>,
path_override: Option<String>,
pre_loaded_component: Option<Component>,
/// Override the working directory for script execution.
/// When set, the script runs in this directory instead of deriving it from the extension path.
/// Used by Build to run in the component's `local_path`.
working_dir: Option<String>,
/// Override the command string instead of constructing from extension_path + script_path.
/// Used by Build when `command_template` produces a pre-resolved command.
command_override: Option<String>,
}
impl ExtensionRunner {
/// Use a pre-loaded component instead of loading by ID.
///
/// This avoids re-loading from config when the caller already has a
/// resolved component (e.g., from portable config discovery in CI).
pub fn component(mut self, comp: Component) -> Self {
self.pre_loaded_component = Some(comp);
self
}
/// Create a runner from a pre-resolved execution context.
pub(crate) fn for_context(execution_context: ExtensionExecutionContext) -> Self {
Self {
execution_context,
settings_overrides: Vec::new(),
env_vars: Vec::new(),
script_args: Vec::new(),
path_override: None,
pre_loaded_component: None,
working_dir: None,
command_override: None,
}
}
/// Override the component's `local_path` for this execution.
///
/// Use this when running against a workspace clone or temporary checkout
/// instead of the configured component path.
pub fn path_override(mut self, path: Option<String>) -> Self {
self.path_override = path;
self
}
/// Add settings overrides from key=value pairs.
pub fn settings(mut self, overrides: &[(String, String)]) -> Self {
self.settings_overrides.extend(overrides.iter().cloned());
self
}
/// Add an environment variable.
pub fn env(mut self, key: &str, value: &str) -> Self {
self.env_vars.push((key.to_string(), value.to_string()));
self
}
/// Add an environment variable if condition is true.
pub fn env_if(mut self, condition: bool, key: &str, value: &str) -> Self {
if condition {
self.env_vars.push((key.to_string(), value.to_string()));
}
self
}
/// Add an environment variable if the Option is Some.
pub(crate) fn env_opt(mut self, key: &str, value: &Option<String>) -> Self {
if let Some(v) = value {
self.env_vars.push((key.to_string(), v.clone()));
}
self
}
/// Add arguments to pass to the script.
pub fn script_args(mut self, args: &[String]) -> Self {
self.script_args.extend(args.iter().cloned());
self
}
/// Set the working directory for script execution.
///
/// By default, scripts run relative to the extension path. Use this to
/// run in a different directory (e.g., the component's `local_path` for builds).
pub(crate) fn working_dir(mut self, dir: &str) -> Self {
self.working_dir = Some(dir.to_string());
self
}
/// Override the command string instead of constructing from extension_path + script_path.
///
/// Use this when the command is pre-resolved (e.g., Build's `command_template`
/// has already been interpolated with the script path).
pub(crate) fn command_override(mut self, command: String) -> Self {
self.command_override = Some(command);
self
}
/// Execute the extension runner script.
///
/// Performs the full orchestration:
/// 1. Load component configuration
/// 2. Determine extension from component config
/// 3. Find extension path
/// 4. Validate script exists (unless command_override is set)
/// 5. Load manifest
/// 6. Merge settings (manifest defaults → component → overrides)
/// 7. Prepare environment variables
/// 8. Execute via shell
pub fn run(&self) -> Result<RunnerOutput> {
let prepared = super::execution::prepare_capability_run(
&self.execution_context,
self.pre_loaded_component.as_ref(),
self.path_override.as_deref(),
&self.settings_overrides,
self.command_override.is_some(),
)?;
let project_path = PathBuf::from(&prepared.execution.component.local_path);
let env_vars = self.prepare_env_vars(
&prepared.execution.extension_path,
&project_path,
&prepared.settings_json,
&prepared.execution.extension_id,
);
let output = self.execute_script(&prepared.execution.extension_path, &env_vars)?;
Ok(RunnerOutput {
exit_code: output.exit_code,
success: output.success,
stdout: output.stdout,
stderr: output.stderr,
})
}
fn prepare_env_vars(
&self,
extension_path: &Path,
project_path: &Path,
settings_json: &str,
extension_name: &str,
) -> Vec<(String, String)> {
super::execution::build_capability_env(
extension_name,
&self.execution_context.component.id,
extension_path,
project_path,
settings_json,
&self.env_vars,
)
}
fn execute_script(
&self,
extension_path: &Path,
env_vars: &[(String, String)],
) -> Result<CommandOutput> {
super::execution::execute_capability_script(
extension_path,
&self.execution_context.script_path,
&self.script_args,
env_vars,
self.working_dir.as_deref(),
self.command_override.as_deref(),
)
}
}