Skip to content

Commit 05e1ae7

Browse files
committed
move execution context out of deferred command, add as_ref implementation and update wait_for_output usage
1 parent dca9fe0 commit 05e1ae7

File tree

2 files changed

+20
-26
lines changed

2 files changed

+20
-26
lines changed

src/bootstrap/src/utils/channel.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ impl GitInfo {
7979
.start_capture_stdout(&exec_ctx);
8080

8181
GitInfo::Present(Some(Info {
82-
commit_date: ver_date.wait_for_output().stdout().trim().to_string(),
83-
sha: ver_hash.wait_for_output().stdout().trim().to_string(),
84-
short_sha: short_ver_hash.wait_for_output().stdout().trim().to_string(),
82+
commit_date: ver_date.wait_for_output(&exec_ctx).stdout().trim().to_string(),
83+
sha: ver_hash.wait_for_output(&exec_ctx).stdout().trim().to_string(),
84+
short_sha: short_ver_hash.wait_for_output(&exec_ctx).stdout().trim().to_string(),
8585
}))
8686
}
8787

src/bootstrap/src/utils/execution_context.rs

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,7 @@ impl ExecutionContext {
9494
let executed_at = std::panic::Location::caller();
9595

9696
if self.dry_run() && !command.run_always {
97-
return DeferredCommand {
98-
process: None,
99-
stdout,
100-
stderr,
101-
command,
102-
exec_ctx: Arc::new(self.clone()),
103-
created_at,
104-
};
97+
return DeferredCommand { process: None, stdout, stderr, command, created_at };
10598
}
10699

107100
#[cfg(feature = "tracing")]
@@ -117,14 +110,7 @@ impl ExecutionContext {
117110

118111
let child = cmd.spawn().unwrap();
119112

120-
DeferredCommand {
121-
process: Some(child),
122-
stdout,
123-
stderr,
124-
command,
125-
exec_ctx: Arc::new(self.clone()),
126-
created_at,
127-
}
113+
DeferredCommand { process: Some(child), stdout, stderr, command, created_at }
128114
}
129115

130116
/// Execute a command and return its output.
@@ -137,7 +123,7 @@ impl ExecutionContext {
137123
stdout: OutputMode,
138124
stderr: OutputMode,
139125
) -> CommandOutput {
140-
self.start(command, stdout, stderr).wait_for_output()
126+
self.start(command, stdout, stderr).wait_for_output(self)
141127
}
142128

143129
fn fail(&self, message: &str, output: CommandOutput) -> ! {
@@ -164,20 +150,28 @@ impl ExecutionContext {
164150
}
165151
}
166152

153+
impl AsRef<ExecutionContext> for ExecutionContext {
154+
fn as_ref(&self) -> &ExecutionContext {
155+
self
156+
}
157+
}
158+
167159
pub struct DeferredCommand<'a> {
168160
process: Option<Child>,
169161
command: &'a mut BootstrapCommand,
170162
stdout: OutputMode,
171163
stderr: OutputMode,
172-
exec_ctx: Arc<ExecutionContext>,
173164
created_at: Location<'a>,
174165
}
175166

176167
impl<'a> DeferredCommand<'a> {
177-
pub fn wait_for_output(mut self) -> CommandOutput {
168+
pub fn wait_for_output(mut self, exec_ctx: impl AsRef<ExecutionContext>) -> CommandOutput {
178169
if self.process.is_none() {
179170
return CommandOutput::default();
180171
}
172+
173+
let exec_ctx = exec_ctx.as_ref();
174+
181175
let output = self.process.take().unwrap().wait_with_output();
182176

183177
let created_at = self.created_at;
@@ -234,14 +228,14 @@ Executed at: {executed_at}"#,
234228
if !output.is_success() {
235229
match self.command.failure_behavior {
236230
BehaviorOnFailure::DelayFail => {
237-
if self.exec_ctx.fail_fast {
238-
self.exec_ctx.fail(&message, output);
231+
if exec_ctx.fail_fast {
232+
exec_ctx.fail(&message, output);
239233
}
240234

241-
self.exec_ctx.add_to_delay_failure(message);
235+
exec_ctx.add_to_delay_failure(message);
242236
}
243237
BehaviorOnFailure::Exit => {
244-
self.exec_ctx.fail(&message, output);
238+
exec_ctx.fail(&message, output);
245239
}
246240
BehaviorOnFailure::Ignore => {
247241
// If failures are allowed, either the error has been printed already

0 commit comments

Comments
 (0)