-
Notifications
You must be signed in to change notification settings - Fork 331
Expand file tree
/
Copy pathmod.rs
More file actions
115 lines (96 loc) · 3.1 KB
/
mod.rs
File metadata and controls
115 lines (96 loc) · 3.1 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
use std::fmt::{self, Display};
use super::{
check_fetched, debug_already_fetched, info_fetched, info_installed, info_pinned,
info_project_version, FetchStatus, Tool,
};
use crate::error::{ErrorKind, Fallible};
use crate::inventory::yarn_available;
use crate::session::Session;
use crate::style::tool_version;
use crate::sync::VoltaLock;
use node_semver::Version;
mod fetch;
mod metadata;
mod resolve;
pub use resolve::resolve;
/// The Tool implementation for fetching and installing Yarn
pub struct Yarn {
pub(super) version: Version,
}
impl Yarn {
pub fn new(version: Version) -> Self {
Yarn { version }
}
pub fn archive_basename(version: &str) -> String {
format!("yarn-v{}", version)
}
pub fn archive_filename(version: &str) -> String {
format!("{}.tar.gz", Yarn::archive_basename(version))
}
pub(crate) fn ensure_fetched(&self, session: &mut Session) -> Fallible<()> {
match check_fetched(|| yarn_available(&self.version))? {
FetchStatus::AlreadyFetched => {
debug_already_fetched(self);
Ok(())
}
FetchStatus::FetchNeeded(_lock) => fetch::fetch(&self.version, session.hooks()?.yarn()),
}
}
}
impl Tool for Yarn {
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_yarn(Some(self.version.clone()))?;
info_installed(self);
if let Ok(Some(project)) = session.project_platform() {
if let Some(yarn) = &project.yarn {
info_project_version(tool_version("yarn", yarn));
}
}
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_yarn(Some(self.version.clone()))?;
info_pinned(self);
Ok(())
} else {
Err(ErrorKind::NotInPackage.into())
}
}
fn uninstall(self: Box<Self>, _session: &mut Session) -> Fallible<()> {
Err(ErrorKind::Unimplemented {
feature: "Uninstalling yarn".into(),
}
.into())
}
}
impl Display for Yarn {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&tool_version("yarn", &self.version))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_yarn_archive_basename() {
assert_eq!(Yarn::archive_basename("1.2.3"), "yarn-v1.2.3");
}
#[test]
fn test_yarn_archive_filename() {
assert_eq!(Yarn::archive_filename("1.2.3"), "yarn-v1.2.3.tar.gz");
}
}