1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#[derive(thiserror::Error, Debug)]
#[non_exhaustive]
pub enum Error {
#[error(transparent)]
IoError(#[from] std::io::Error),
#[error("The lock used to work around https://github.com/rust-lang/rustup/issues/988 has been poisoned")]
StdSyncPoisonError,
#[error("`rustup toolchain install ...` failed for some reason")]
RustupToolchainInstallError,
}
pub type Result<T> = std::result::Result<T, Error>;
static RUSTUP_MUTEX: std::sync::Mutex<()> = std::sync::Mutex::new(());
pub fn install(toolchain: impl AsRef<str>) -> Result<()> {
#[allow(deprecated)]
if !is_installed(toolchain.as_ref())? {
run_rustup_install(toolchain)?;
}
Ok(())
}
#[allow(clippy::missing_errors_doc)]
#[deprecated(since = "0.1.4", note = "Renamed to `install()` for brevity.")]
pub fn ensure_installed(toolchain: &str) -> Result<()> {
install(toolchain)
}
#[allow(clippy::missing_errors_doc)]
#[deprecated(
since = "0.1.4",
note = "Not needed, because `install()` already checks if the toolchain is installed already."
)]
pub fn is_installed(toolchain: &str) -> Result<bool> {
let _guard = RUSTUP_MUTEX.lock().map_err(|_| Error::StdSyncPoisonError)?;
Ok(std::process::Command::new("rustup")
.arg("run")
.arg(toolchain)
.arg("cargo")
.arg("--version")
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.status()?
.success())
}
fn run_rustup_install(toolchain: impl AsRef<str>) -> Result<()> {
let _guard = RUSTUP_MUTEX.lock().map_err(|_| Error::StdSyncPoisonError)?;
let status = std::process::Command::new("rustup")
.arg("toolchain")
.arg("install")
.arg("--no-self-update")
.arg("--profile")
.arg("minimal")
.arg(toolchain.as_ref())
.status()?;
if status.success() {
Ok(())
} else {
Err(Error::RustupToolchainInstallError)
}
}