-
Notifications
You must be signed in to change notification settings - Fork 331
Expand file tree
/
Copy pathmod.rs
More file actions
125 lines (102 loc) · 3.28 KB
/
mod.rs
File metadata and controls
125 lines (102 loc) · 3.28 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
use node_semver::Version;
use std::env;
use std::fmt::{self, Display};
use crate::error::{ErrorKind, Fallible};
use crate::inventory::pnpm_available;
use crate::session::Session;
use crate::style::tool_version;
use crate::sync::VoltaLock;
use crate::VOLTA_FEATURE_PNPM;
use super::{
check_fetched, debug_already_fetched, info_fetched, info_installed, info_pinned,
info_project_version, FetchStatus, Tool,
};
mod fetch;
mod resolve;
use super::package::uninstall;
pub use resolve::resolve;
/// The Tool implementation for fetching and installing pnpm
pub struct Pnpm {
pub(super) version: Version,
}
impl Pnpm {
pub fn new(version: Version) -> Self {
Pnpm { version }
}
pub fn archive_basename(version: &str) -> String {
format!("pnpm-{}", version)
}
pub fn archive_filename(version: &str) -> String {
format!("{}.tgz", Pnpm::archive_basename(version))
}
pub(crate) fn ensure_fetched(&self, session: &mut Session) -> Fallible<()> {
match check_fetched(|| pnpm_available(&self.version))? {
FetchStatus::AlreadyFetched => {
debug_already_fetched(self);
Ok(())
}
FetchStatus::FetchNeeded(_lock) => fetch::fetch(&self.version, session.hooks()?.pnpm()),
}
}
}
impl Tool for Pnpm {
fn fetch(self: Box<Self>, session: &mut Session) -> Fallible<()> {
self.ensure_fetched(session)?;
info_fetched(self);
Ok(())
}
fn install(self: Box<Self>, session: &mut Session) -> Fallible<()> {
// Acquire a lock on the Volta directory, if possible, to prevent concurrent changes
let _lock = VoltaLock::acquire();
self.ensure_fetched(session)?;
session
.toolchain_mut()?
.set_active_pnpm(Some(self.version.clone()))?;
info_installed(self);
if let Ok(Some(project)) = session.project_platform() {
if let Some(pnpm) = &project.pnpm {
info_project_version(tool_version("pnpm", pnpm));
}
}
Ok(())
}
fn pin(self: Box<Self>, session: &mut Session) -> Fallible<()> {
if session.project()?.is_some() {
self.ensure_fetched(session)?;
// Note: We know this will succeed, since we checked above
let project = session.project_mut()?.unwrap();
project.pin_pnpm(Some(self.version.clone()))?;
info_pinned(self);
Ok(())
} else {
Err(ErrorKind::NotInPackage.into())
}
}
fn uninstall(self: Box<Self>, _session: &mut Session) -> Fallible<()> {
if env::var_os(VOLTA_FEATURE_PNPM).is_some() {
Err(ErrorKind::Unimplemented {
feature: "Uninstalling pnpm".into(),
}
.into())
} else {
uninstall("pnpm")
}
}
}
impl Display for Pnpm {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&tool_version("pnpm", &self.version))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_pnpm_archive_basename() {
assert_eq!(Pnpm::archive_basename("1.2.3"), "pnpm-1.2.3");
}
#[test]
fn test_pnpm_archive_filename() {
assert_eq!(Pnpm::archive_filename("1.2.3"), "pnpm-1.2.3.tgz");
}
}