Skip to content

Commit 41058fa

Browse files
committed
Move Toolchain.install to InstallMethod
This is preparation for removing is_valid_install_method.
1 parent 5e7f650 commit 41058fa

File tree

2 files changed

+56
-44
lines changed

2 files changed

+56
-44
lines changed

src/install.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ use crate::dist::prefix::InstallPrefix;
88
use crate::dist::temp;
99
use crate::dist::Notification;
1010
use crate::errors::Result;
11+
use crate::notifications::Notification as RootNotification;
12+
use crate::toolchain::{Toolchain, UpdateStatus};
1113
use crate::utils::utils;
1214
use std::path::Path;
1315

@@ -38,6 +40,48 @@ pub enum InstallMethod<'a> {
3840
}
3941

4042
impl<'a> InstallMethod<'a> {
43+
// Install a toolchain
44+
pub fn install(&self, toolchain: &Toolchain<'a>) -> Result<UpdateStatus> {
45+
assert!(toolchain.is_valid_install_method(*self));
46+
let previous_version = if toolchain.exists() {
47+
Some(toolchain.rustc_version())
48+
} else {
49+
None
50+
};
51+
if previous_version.is_some() {
52+
(toolchain.cfg().notify_handler)(RootNotification::UpdatingToolchain(
53+
&toolchain.name(),
54+
));
55+
} else {
56+
(toolchain.cfg().notify_handler)(RootNotification::InstallingToolchain(
57+
&toolchain.name(),
58+
));
59+
}
60+
(toolchain.cfg().notify_handler)(RootNotification::ToolchainDirectory(
61+
&toolchain.path(),
62+
&toolchain.name(),
63+
));
64+
let updated = self.run(&toolchain.path(), &|n| {
65+
(toolchain.cfg().notify_handler)(n.into())
66+
})?;
67+
68+
if !updated {
69+
(toolchain.cfg().notify_handler)(RootNotification::UpdateHashMatches);
70+
} else {
71+
(toolchain.cfg().notify_handler)(RootNotification::InstalledToolchain(
72+
&toolchain.name(),
73+
));
74+
}
75+
76+
let status = match (updated, previous_version) {
77+
(true, None) => UpdateStatus::Installed,
78+
(true, Some(v)) => UpdateStatus::Updated(v),
79+
(false, _) => UpdateStatus::Unchanged,
80+
};
81+
82+
Ok(status)
83+
}
84+
4185
pub fn run(self, path: &Path, notify_handler: &dyn Fn(Notification<'_>)) -> Result<bool> {
4286
if path.exists() {
4387
// Don't uninstall first for Dist method

src/toolchain.rs

Lines changed: 12 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ impl<'a> Toolchain<'a> {
8181
Ok(Box::new(toolchain) as Box<dyn InstalledToolchain<'a>>)
8282
}
8383
}
84-
84+
pub fn cfg(&self) -> &Cfg {
85+
&self.cfg
86+
}
8587
pub fn name(&self) -> &str {
8688
&self.name
8789
}
@@ -136,51 +138,22 @@ impl<'a> Toolchain<'a> {
136138
}
137139
Ok(())
138140
}
139-
// Custom and Distributable. Installed and not installed.
140-
fn install(&self, install_method: InstallMethod<'_>) -> Result<UpdateStatus> {
141-
assert!(self.is_valid_install_method(install_method));
142-
let previous_version = if self.exists() {
143-
Some(self.rustc_version())
144-
} else {
145-
None
146-
};
147-
if previous_version.is_some() {
148-
(self.cfg.notify_handler)(Notification::UpdatingToolchain(&self.name));
149-
} else {
150-
(self.cfg.notify_handler)(Notification::InstallingToolchain(&self.name));
151-
}
152-
(self.cfg.notify_handler)(Notification::ToolchainDirectory(&self.path, &self.name));
153-
let updated = install_method.run(&self.path, &|n| (self.cfg.notify_handler)(n.into()))?;
154141

155-
if !updated {
156-
(self.cfg.notify_handler)(Notification::UpdateHashMatches);
157-
} else {
158-
(self.cfg.notify_handler)(Notification::InstalledToolchain(&self.name));
159-
}
160-
161-
let status = match (updated, previous_version) {
162-
(true, None) => UpdateStatus::Installed,
163-
(true, Some(v)) => UpdateStatus::Updated(v),
164-
(false, _) => UpdateStatus::Unchanged,
165-
};
166-
167-
Ok(status)
168-
}
169142
// Custom and Distributable, Installed and not installed.
170143
// Perhaps make into a helper?
171144
fn install_if_not_installed(&self, install_method: InstallMethod<'_>) -> Result<UpdateStatus> {
172145
assert!(self.is_valid_install_method(install_method));
173146
(self.cfg.notify_handler)(Notification::LookingForToolchain(&self.name));
174147
if !self.exists() {
175-
Ok(self.install(install_method)?)
148+
Ok(install_method.install(&self)?)
176149
} else {
177150
(self.cfg.notify_handler)(Notification::UsingExistingToolchain(&self.name));
178151
Ok(UpdateStatus::Unchanged)
179152
}
180153
}
181154
// Custom and Distributable. Installed and not installed (because of install_from_dist_if_not_installed) Goes away?
182155
// Or perhaps a trait.
183-
fn is_valid_install_method(&self, install_method: InstallMethod<'_>) -> bool {
156+
pub fn is_valid_install_method(&self, install_method: InstallMethod<'_>) -> bool {
184157
match install_method {
185158
InstallMethod::Copy(_) | InstallMethod::Link(_) | InstallMethod::Installer(..) => {
186159
self.is_custom()
@@ -253,21 +226,15 @@ impl<'a> Toolchain<'a> {
253226
utils::download_file(&url, &local_installer, None, &|n| {
254227
(self.cfg.notify_handler)(n.into())
255228
})?;
256-
self.install(InstallMethod::Installer(
257-
&local_installer,
258-
&self.cfg.temp_cfg,
259-
))?;
229+
InstallMethod::Installer(&local_installer, &self.cfg.temp_cfg).install(&self)?;
260230
} else {
261231
// If installer is a filename
262232

263233
// No need to download
264234
let local_installer = Path::new(installer);
265235

266236
// Install from file
267-
self.install(InstallMethod::Installer(
268-
&local_installer,
269-
&self.cfg.temp_cfg,
270-
))?;
237+
InstallMethod::Installer(&local_installer, &self.cfg.temp_cfg).install(&self)?;
271238
}
272239
}
273240

@@ -289,9 +256,9 @@ impl<'a> Toolchain<'a> {
289256
utils::assert_is_file(&pathbuf)?;
290257

291258
if link {
292-
self.install(InstallMethod::Link(&utils::to_absolute(src)?))?;
259+
InstallMethod::Link(&utils::to_absolute(src)?).install(&self)?;
293260
} else {
294-
self.install(InstallMethod::Copy(src))?;
261+
InstallMethod::Copy(src).install(&self)?;
295262
}
296263

297264
Ok(())
@@ -765,7 +732,7 @@ impl<'a> DistributableToolchain<'a> {
765732
) -> Result<UpdateStatus> {
766733
let update_hash = self.update_hash()?;
767734
let old_date = self.get_manifest().ok().and_then(|m| m.map(|m| m.date));
768-
self.0.install(InstallMethod::Dist(
735+
InstallMethod::Dist(
769736
&self.desc()?,
770737
self.0.cfg.get_profile()?,
771738
Some(&update_hash),
@@ -776,7 +743,8 @@ impl<'a> DistributableToolchain<'a> {
776743
old_date.as_ref().map(|s| &**s),
777744
components,
778745
targets,
779-
))
746+
)
747+
.install(&self.0)
780748
}
781749

782750
// Installed or not installed.

0 commit comments

Comments
 (0)