Skip to content

Commit cd9224f

Browse files
committed
adapt commandcachekey with should_cache logic
1 parent 9a68c9f commit cd9224f

File tree

2 files changed

+20
-24
lines changed

2 files changed

+20
-24
lines changed

src/bootstrap/src/utils/exec.rs

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ impl OutputMode {
5757

5858
#[derive(Clone, Debug, PartialEq, Eq, Hash, Default)]
5959
pub struct CommandCacheKey {
60-
program: String,
61-
args: Vec<String>,
62-
envs: Vec<(String, String)>,
60+
program: OsString,
61+
args: Vec<OsString>,
62+
envs: Vec<(OsString, OsString)>,
6363
cwd: Option<PathBuf>,
6464
}
6565

@@ -77,28 +77,31 @@ pub struct CommandCacheKey {
7777
/// [allow_failure]: BootstrapCommand::allow_failure
7878
/// [delay_failure]: BootstrapCommand::delay_failure
7979
pub struct BootstrapCommand {
80-
program: OsString,
81-
args: Vec<OsString>,
82-
envs: Vec<(OsString, OsString)>,
83-
cwd: Option<PathBuf>,
84-
should_cache: bool,
85-
80+
cache_key: CommandCacheKey,
8681
command: Command,
8782
pub failure_behavior: BehaviorOnFailure,
8883
// Run the command even during dry run
8984
pub run_always: bool,
9085
// This field makes sure that each command is executed (or disarmed) before it is dropped,
9186
// to avoid forgetting to execute a command.
9287
drop_bomb: DropBomb,
88+
should_cache: bool,
9389
}
9490

9591
impl<'a> BootstrapCommand {
9692
#[track_caller]
9793
pub fn new<S: AsRef<OsStr>>(program: S) -> Self {
98-
Self { should_cache: true, ..Command::new(program).into() }
94+
Self {
95+
should_cache: true,
96+
cache_key: CommandCacheKey {
97+
program: program.as_ref().to_os_string(),
98+
..CommandCacheKey::default()
99+
},
100+
..Command::new(program).into()
101+
}
99102
}
100103
pub fn arg<S: AsRef<OsStr>>(&mut self, arg: S) -> &mut Self {
101-
self.args.push(arg.as_ref().to_os_string());
104+
self.cache_key.args.push(arg.as_ref().to_os_string());
102105
self.command.arg(arg.as_ref());
103106
self
104107
}
@@ -123,7 +126,7 @@ impl<'a> BootstrapCommand {
123126
K: AsRef<OsStr>,
124127
V: AsRef<OsStr>,
125128
{
126-
self.envs.push((key.as_ref().to_os_string(), val.as_ref().to_os_string()));
129+
self.cache_key.envs.push((key.as_ref().to_os_string(), val.as_ref().to_os_string()));
127130
self.command.env(key, val);
128131
self
129132
}
@@ -142,7 +145,7 @@ impl<'a> BootstrapCommand {
142145
}
143146

144147
pub fn current_dir<P: AsRef<Path>>(&mut self, dir: P) -> &mut Self {
145-
self.cwd = Some(dir.as_ref().to_path_buf());
148+
self.cache_key.cwd = Some(dir.as_ref().to_path_buf());
146149
self.command.current_dir(dir);
147150
self
148151
}
@@ -237,12 +240,7 @@ impl<'a> BootstrapCommand {
237240
}
238241

239242
pub fn cache_key(&self) -> CommandCacheKey {
240-
CommandCacheKey {
241-
program: self.program.clone(),
242-
args: self.args.clone(),
243-
envs: self.envs.clone(),
244-
cwd: self.cwd.clone(),
245-
}
243+
self.cache_key.clone()
246244
}
247245
}
248246

@@ -259,10 +257,7 @@ impl From<Command> for BootstrapCommand {
259257
let program = command.get_program().to_owned();
260258

261259
Self {
262-
program: program.clone(),
263-
args: Vec::new(),
264-
envs: Vec::new(),
265-
cwd: None,
260+
cache_key: CommandCacheKey::default(),
266261
should_cache: false,
267262
command,
268263
failure_behavior: BehaviorOnFailure::Exit,

src/bootstrap/src/utils/execution_context.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@ impl ExecutionContext {
149149
let cache_key = command.cache_key();
150150

151151
if command.should_cache()
152-
&& let Some(cached_output) = self.command_cache.get(&cache_key) {
152+
&& let Some(cached_output) = self.command_cache.get(&cache_key)
153+
{
153154
command.mark_as_executed();
154155
if self.dry_run() && !command.run_always {
155156
return CommandOutput::default();

0 commit comments

Comments
 (0)