Skip to content

Commit 3beb322

Browse files
committed
Refactor install_one into two parts
1 parent 531958b commit 3beb322

File tree

1 file changed

+48
-19
lines changed

1 file changed

+48
-19
lines changed

src/cargo/ops/cargo_install.rs

Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ pub fn install(
4949
let map = SourceConfigMap::new(config)?;
5050

5151
let (installed_anything, scheduled_error) = if krates.len() <= 1 {
52-
install_one(
52+
let pkg = determine_package(
5353
config,
5454
&root,
5555
&map,
@@ -59,9 +59,13 @@ pub fn install(
5959
vers,
6060
opts,
6161
force,
62-
no_track,
6362
true,
6463
)?;
64+
if let Some(pkg) = pkg {
65+
install_one(
66+
config, &root, source_id, from_cwd, vers, opts, force, no_track, pkg,
67+
)?;
68+
}
6569
(true, false)
6670
} else {
6771
let mut succeeded = vec![];
@@ -72,7 +76,7 @@ pub fn install(
7276
for krate in krates {
7377
let root = root.clone();
7478
let map = map.clone();
75-
match install_one(
79+
let pkg = match determine_package(
7680
config,
7781
&root,
7882
&map,
@@ -82,18 +86,34 @@ pub fn install(
8286
vers,
8387
opts,
8488
force,
85-
no_track,
8689
!did_update,
8790
) {
88-
Ok(still_needs_update) => {
91+
Ok(Some(pkg)) => {
92+
did_update = true;
93+
pkg
94+
}
95+
Ok(None) => {
96+
// Already installed
8997
succeeded.push(krate);
90-
did_update |= !still_needs_update;
98+
continue;
9199
}
92100
Err(e) => {
93101
crate::display_error(&e, &mut config.shell());
94102
failed.push(krate);
95103
// We assume an update was performed if we got an error.
96104
did_update = true;
105+
continue;
106+
}
107+
};
108+
match install_one(
109+
config, &root, source_id, from_cwd, vers, opts, force, no_track, pkg,
110+
) {
111+
Ok(()) => {
112+
succeeded.push(krate);
113+
}
114+
Err(e) => {
115+
crate::display_error(&e, &mut config.shell());
116+
failed.push(krate);
97117
}
98118
}
99119
}
@@ -138,12 +158,8 @@ pub fn install(
138158
Ok(())
139159
}
140160

141-
// Returns whether a subsequent call should attempt to update again.
142-
// The `needs_update_if_source_is_index` parameter indicates whether or not the source index should
143-
// be updated. This is used ensure it is only updated once when installing multiple crates.
144-
// The return value here is used so that the caller knows what to pass to the
145-
// `needs_update_if_source_is_index` parameter when `install_one` is called again.
146-
fn install_one(
161+
// Returns pkg to install. None if pkg is already installed
162+
fn determine_package(
147163
config: &Config,
148164
root: &Filesystem,
149165
map: &SourceConfigMap<'_>,
@@ -153,9 +169,8 @@ fn install_one(
153169
vers: Option<&str>,
154170
opts: &ops::CompileOptions,
155171
force: bool,
156-
no_track: bool,
157172
needs_update_if_source_is_index: bool,
158-
) -> CargoResult<bool> {
173+
) -> CargoResult<Option<Package>> {
159174
if let Some(name) = krate {
160175
if name == "." {
161176
bail!(
@@ -167,7 +182,6 @@ fn install_one(
167182
}
168183

169184
let dst = root.join("bin").into_path_unlocked();
170-
171185
let pkg = {
172186
let dep = {
173187
if let Some(krate) = krate {
@@ -241,7 +255,7 @@ fn install_one(
241255
pkg
242256
);
243257
config.shell().status("Ignored", &msg)?;
244-
return Ok(true);
258+
return Ok(None);
245259
}
246260
select_dep_pkg(&mut source, dep, config, needs_update_if_source_is_index)?
247261
} else {
@@ -252,6 +266,21 @@ fn install_one(
252266
)
253267
}
254268
};
269+
Ok(Some(pkg))
270+
}
271+
272+
fn install_one(
273+
config: &Config,
274+
root: &Filesystem,
275+
source_id: SourceId,
276+
from_cwd: bool,
277+
vers: Option<&str>,
278+
opts: &ops::CompileOptions,
279+
force: bool,
280+
no_track: bool,
281+
pkg: Package,
282+
) -> CargoResult<()> {
283+
let dst = root.join("bin").into_path_unlocked();
255284

256285
let (mut ws, rustc, target) = make_ws_rustc_target(config, opts, &source_id, pkg.clone())?;
257286
// If we're installing in --locked mode and there's no `Cargo.lock` published
@@ -345,7 +374,7 @@ fn install_one(
345374
pkg
346375
);
347376
config.shell().status("Ignored", &msg)?;
348-
return Ok(false);
377+
return Ok(());
349378
}
350379

351380
config.shell().status("Installing", &pkg)?;
@@ -500,7 +529,7 @@ fn install_one(
500529
"Installed",
501530
format!("package `{}` {}", pkg, executables(successful_bins.iter())),
502531
)?;
503-
Ok(false)
532+
Ok(())
504533
} else {
505534
if !to_install.is_empty() {
506535
config.shell().status(
@@ -525,7 +554,7 @@ fn install_one(
525554
),
526555
)?;
527556
}
528-
Ok(false)
557+
Ok(())
529558
}
530559
}
531560

0 commit comments

Comments
 (0)