-
Notifications
You must be signed in to change notification settings - Fork 331
Expand file tree
/
Copy pathmod.rs
More file actions
339 lines (286 loc) · 10.8 KB
/
mod.rs
File metadata and controls
339 lines (286 loc) · 10.8 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
use std::fmt::{self, Display};
use std::fs::create_dir_all;
use std::path::{Path, PathBuf};
use std::process::Command;
use super::Tool;
use crate::error::{Context, ErrorKind, Fallible};
use crate::fs::{remove_dir_if_exists, rename, symlink_dir};
use crate::layout::volta_home;
use crate::platform::{Image, PlatformSpec};
use crate::session::Session;
use crate::style::{success_prefix, tool_version};
use crate::sync::VoltaLock;
use crate::version::VersionSpec;
use fs_utils::ensure_containing_dir_exists;
use log::info;
use tempfile::{tempdir_in, TempDir};
mod configure;
mod install;
mod manager;
mod metadata;
mod uninstall;
pub use manager::PackageManager;
pub use metadata::{BinConfig, PackageConfig, PackageManifest};
pub use uninstall::uninstall;
/// The Tool implementation for installing 3rd-party global packages
pub struct Package {
name: String,
version: VersionSpec,
staging: TempDir,
}
impl Package {
pub fn new(name: String, version: VersionSpec) -> Fallible<Self> {
let staging = setup_staging_directory(PackageManager::Npm, NeedsScope::No)?;
Ok(Package {
name,
version,
staging,
})
}
pub fn run_install(&self, platform_image: &Image) -> Fallible<()> {
install::run_global_install(
self.to_string(),
self.staging.path().to_owned(),
platform_image,
)
}
pub fn complete_install(self, image: &Image) -> Fallible<PackageManifest> {
let manager = PackageManager::Npm;
let manifest =
configure::parse_manifest(&self.name, self.staging.path().to_owned(), manager)?;
persist_install(&self.name, &self.version, self.staging.path())?;
link_package_to_shared_dir(&self.name, manager)?;
configure::write_config_and_shims(&self.name, &manifest, image, manager)?;
Ok(manifest)
}
}
impl Tool for Package {
fn fetch(self: Box<Self>, _session: &mut Session) -> Fallible<()> {
Err(ErrorKind::CannotFetchPackage {
package: self.to_string(),
}
.into())
}
fn install(self: Box<Self>, session: &mut Session) -> Fallible<()> {
let _lock = VoltaLock::acquire();
let default_image = session
.default_platform()?
.map(PlatformSpec::as_default)
.ok_or(ErrorKind::NoPlatform)?
.checkout(session)?;
self.run_install(&default_image)?;
let manifest = self.complete_install(&default_image)?;
let bins = manifest.bin.join(", ");
if bins.is_empty() {
info!(
"{} installed {}",
success_prefix(),
tool_version(manifest.name, manifest.version)
);
} else {
info!(
"{} installed {} with executables: {}",
success_prefix(),
tool_version(manifest.name, manifest.version),
bins
);
}
Ok(())
}
fn pin(self: Box<Self>, _session: &mut Session) -> Fallible<()> {
Err(ErrorKind::CannotPinPackage { package: self.name }.into())
}
fn uninstall(self: Box<Self>, _session: &mut Session) -> Fallible<()> {
// For packages, specifically report that we do not support uninstalling
// specific versions. For package managers, we currently
// *intentionally* let this fall through to inform the user that we do
// not support uninstalling those *at all*.
let VersionSpec::None = &self.version else {
return Err(ErrorKind::Unimplemented {
feature: "uninstalling specific versions of tools".into(),
}
.into());
};
uninstall(&self.name)
}
}
impl Display for Package {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.version {
VersionSpec::None => f.write_str(&self.name),
_ => f.write_str(&tool_version(&self.name, &self.version)),
}
}
}
/// Helper struct for direct installs through `npm i -g` or `yarn global add`
///
/// Provides methods to simplify installing into a staging directory and then moving that install
/// into the proper location after it is complete.
///
/// Note: We don't always know the name of the package up-front, as the install could be from a
/// tarball or a git coordinate. If we do know ahead of time, then we can skip looking it up
pub struct DirectInstall {
staging: TempDir,
manager: PackageManager,
name: Option<String>,
}
impl DirectInstall {
pub fn new(manager: PackageManager) -> Fallible<Self> {
let staging = setup_staging_directory(manager, NeedsScope::No)?;
Ok(DirectInstall {
staging,
manager,
name: None,
})
}
pub fn with_name(manager: PackageManager, name: String) -> Fallible<Self> {
let staging = setup_staging_directory(manager, name.contains('/').into())?;
Ok(DirectInstall {
staging,
manager,
name: Some(name),
})
}
pub fn setup_command(&self, command: &mut Command) {
self.manager
.setup_global_command(command, self.staging.path().to_owned());
}
pub fn complete_install(self, image: &Image) -> Fallible<()> {
let DirectInstall {
staging,
name,
manager,
} = self;
let name = name
.or_else(|| manager.get_installed_package(staging.path().to_owned()))
.ok_or(ErrorKind::InstalledPackageNameError)?;
let manifest = configure::parse_manifest(&name, staging.path().to_owned(), manager)?;
persist_install(&name, &manifest.version, staging.path())?;
link_package_to_shared_dir(&name, manager)?;
configure::write_config_and_shims(&name, &manifest, image, manager)
}
}
/// Helper struct for direct in-place upgrades using `npm update -g` or `yarn global upgrade`
///
/// Upgrades the requested package directly in the image directory
pub struct InPlaceUpgrade {
package: String,
directory: PathBuf,
manager: PackageManager,
}
impl InPlaceUpgrade {
pub fn new(package: String, manager: PackageManager) -> Fallible<Self> {
let directory = volta_home()?.package_image_dir(&package);
Ok(Self {
package,
directory,
manager,
})
}
/// Check for possible failure cases with the package to be upgraded
/// - The package is not installed as a global
/// - The package exists, but was installed with a different package manager
pub fn check_upgraded_package(&self) -> Fallible<()> {
let config =
PackageConfig::from_file(volta_home()?.default_package_config_file(&self.package))
.with_context(|| ErrorKind::UpgradePackageNotFound {
package: self.package.clone(),
manager: self.manager,
})?;
if config.manager != self.manager {
Err(ErrorKind::UpgradePackageWrongManager {
package: self.package.clone(),
manager: config.manager,
}
.into())
} else {
Ok(())
}
}
pub fn setup_command(&self, command: &mut Command) {
self.manager
.setup_global_command(command, self.directory.clone());
}
pub fn complete_upgrade(self, image: &Image) -> Fallible<()> {
let manifest = configure::parse_manifest(&self.package, self.directory, self.manager)?;
link_package_to_shared_dir(&self.package, self.manager)?;
configure::write_config_and_shims(&self.package, &manifest, image, self.manager)
}
}
#[derive(Clone, Copy, PartialEq)]
enum NeedsScope {
Yes,
No,
}
impl From<bool> for NeedsScope {
fn from(value: bool) -> Self {
if value {
NeedsScope::Yes
} else {
NeedsScope::No
}
}
}
/// Create the temporary staging directory we will use to install and ensure expected
/// subdirectories exist within it
fn setup_staging_directory(manager: PackageManager, needs_scope: NeedsScope) -> Fallible<TempDir> {
// Workaround to ensure relative symlinks continue to work.
// The final installed location of packages is:
// $VOLTA_HOME/tools/image/packages/{name}/
// To ensure that the temp directory has the same amount of nesting, we use:
// $VOLTA_HOME/tmp/image/packages/{tempdir}/
// This way any relative symlinks will have the same amount of nesting and will remain valid
// even when the directory is persisted.
// We also need to handle the case when the linked package has a scope, which requires another
// level of nesting
let mut staging_root = volta_home()?.tmp_dir().to_owned();
staging_root.push("image");
staging_root.push("packages");
if needs_scope == NeedsScope::Yes {
staging_root.push("scope");
}
create_dir_all(&staging_root).with_context(|| ErrorKind::ContainingDirError {
path: staging_root.clone(),
})?;
let staging = tempdir_in(&staging_root).with_context(|| ErrorKind::CreateTempDirError {
in_dir: staging_root,
})?;
let source_dir = manager.source_dir(staging.path().to_owned());
ensure_containing_dir_exists(&source_dir)
.with_context(|| ErrorKind::ContainingDirError { path: source_dir })?;
let binary_dir = manager.binary_dir(staging.path().to_owned());
ensure_containing_dir_exists(&binary_dir)
.with_context(|| ErrorKind::ContainingDirError { path: binary_dir })?;
Ok(staging)
}
fn persist_install<V>(package_name: &str, package_version: V, staging_dir: &Path) -> Fallible<()>
where
V: Display,
{
let package_dir = volta_home()?.package_image_dir(package_name);
remove_dir_if_exists(&package_dir)?;
// Handle scoped packages (@vue/cli), which have an extra directory for the scope
ensure_containing_dir_exists(&package_dir).with_context(|| ErrorKind::ContainingDirError {
path: package_dir.to_owned(),
})?;
rename(staging_dir, &package_dir).with_context(|| ErrorKind::SetupToolImageError {
tool: package_name.into(),
version: package_version.to_string(),
dir: package_dir,
})?;
Ok(())
}
fn link_package_to_shared_dir(package_name: &str, manager: PackageManager) -> Fallible<()> {
let home = volta_home()?;
let mut source = manager.source_dir(home.package_image_dir(package_name));
source.push(package_name);
let target = home.shared_lib_dir(package_name);
remove_dir_if_exists(&target)?;
// Handle scoped packages (@vue/cli), which have an extra directory for the scope
ensure_containing_dir_exists(&target).with_context(|| ErrorKind::ContainingDirError {
path: target.clone(),
})?;
symlink_dir(source, target).with_context(|| ErrorKind::CreateSharedLinkError {
name: package_name.into(),
})
}