Skip to content

Commit 069c9e8

Browse files
committed
add streaming command struct for (spawn + piping scenario)
1 parent 25face9 commit 069c9e8

File tree

1 file changed

+50
-1
lines changed

1 file changed

+50
-1
lines changed

src/bootstrap/src/utils/exec.rs

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ use std::fmt::{Debug, Formatter};
1313
use std::hash::Hash;
1414
use std::panic::Location;
1515
use std::path::Path;
16-
use std::process::{Child, Command, CommandArgs, CommandEnvs, ExitStatus, Output, Stdio};
16+
use std::process::{
17+
Child, ChildStderr, ChildStdout, Command, CommandArgs, CommandEnvs, ExitStatus, Output, Stdio,
18+
};
1719
use std::sync::{Arc, Mutex};
1820

1921
use build_helper::ci::CiEnv;
@@ -449,6 +451,12 @@ enum CommandState<'a> {
449451
},
450452
}
451453

454+
pub struct CommandStreaming {
455+
child: Child,
456+
pub stdout: Option<ChildStdout>,
457+
pub stderr: Option<ChildStderr>,
458+
}
459+
452460
#[must_use]
453461
pub struct DeferredCommand<'a> {
454462
state: CommandState<'a>,
@@ -625,7 +633,48 @@ impl AsRef<ExecutionContext> for ExecutionContext {
625633
}
626634
}
627635

636+
impl CommandStreaming {
637+
pub fn wait(mut self) -> Result<ExitStatus, std::io::Error> {
638+
self.child.wait()
639+
}
640+
641+
pub fn wait_with_output(self) -> Result<Output, std::io::Error> {
642+
self.child.wait_with_output()
643+
}
644+
}
645+
628646
impl<'a> DeferredCommand<'a> {
647+
pub fn stream(self) -> Option<CommandStreaming> {
648+
if let CommandState::Deferred {
649+
process,
650+
command,
651+
stdout: _,
652+
stderr: _,
653+
executed_at: _,
654+
cache_key: _,
655+
} = self.state
656+
{
657+
command.mark_as_executed();
658+
659+
let child = match process {
660+
Some(child) => child,
661+
None => {
662+
return None;
663+
}
664+
};
665+
666+
let mut child = match child {
667+
Ok(child) => child,
668+
Err(e) => panic!("failed to execute command: {:?}\nERROR: {e}", command),
669+
};
670+
671+
let stdout = child.stdout.take();
672+
let stderr = child.stderr.take();
673+
return Some(CommandStreaming { child, stdout, stderr });
674+
}
675+
None
676+
}
677+
629678
pub fn wait_for_output(self, exec_ctx: impl AsRef<ExecutionContext>) -> CommandOutput {
630679
match self.state {
631680
CommandState::Cached(output) => output,

0 commit comments

Comments
 (0)