Skip to content

Commit 236cf97

Browse files
Merge pull request #662 from charlespierce/volta_setup_shims
Regenerate shims on 'volta setup'
2 parents fe34353 + c910646 commit 236cf97

5 files changed

Lines changed: 59 additions & 59 deletions

File tree

crates/volta-core/src/shim.rs

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,59 @@
11
//! Provides utilities for modifying shims for 3rd-party executables
22
3-
use std::{fs, io};
3+
use std::collections::HashSet;
4+
use std::fs::{self, DirEntry, Metadata};
5+
use std::io;
6+
use std::path::Path;
47

58
use crate::error::ErrorDetails;
6-
use crate::fs::symlink_file;
9+
use crate::fs::{read_dir_eager, symlink_file};
710
use crate::layout::{volta_home, volta_install};
8-
use volta_fail::{throw, FailExt, Fallible};
11+
use log::debug;
12+
use volta_fail::{throw, FailExt, Fallible, ResultExt};
13+
14+
pub fn regenerate_shims_for_dir(dir: &Path) -> Fallible<()> {
15+
debug!("Rebuilding shims for directory: {}", dir.display());
16+
for shim_name in get_shim_list_deduped(dir)?.iter() {
17+
delete(shim_name)?;
18+
create(shim_name)?;
19+
}
20+
21+
Ok(())
22+
}
23+
24+
fn get_shim_list_deduped(dir: &Path) -> Fallible<HashSet<String>> {
25+
let contents = read_dir_eager(dir).with_context(|_| ErrorDetails::ReadDirError {
26+
dir: dir.to_owned(),
27+
})?;
28+
29+
#[cfg(unix)]
30+
{
31+
let mut shims: HashSet<String> = contents.filter_map(entry_to_shim_name).collect();
32+
shims.insert("node".into());
33+
shims.insert("npm".into());
34+
shims.insert("npx".into());
35+
shims.insert("yarn".into());
36+
Ok(shims)
37+
}
38+
39+
#[cfg(windows)]
40+
{
41+
// On Windows, the default shims are installed in Program Files, so we don't need to generate them here
42+
Ok(contents.filter_map(entry_to_shim_name).collect())
43+
}
44+
}
45+
46+
fn entry_to_shim_name((entry, metadata): (DirEntry, Metadata)) -> Option<String> {
47+
if metadata.file_type().is_symlink() {
48+
entry
49+
.path()
50+
.file_stem()
51+
.and_then(|stem| stem.to_str())
52+
.map(|stem| stem.to_string())
53+
} else {
54+
None
55+
}
56+
}
957

1058
#[derive(PartialEq)]
1159
pub enum ShimResult {

crates/volta-migrate/src/lib.rs

Lines changed: 2 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@
88
//! need to be aware that they may be partially applied (if something fails in the process) and should be
99
//! able to re-start gracefully from an interrupted migration
1010
11-
use std::collections::HashSet;
1211
use std::convert::TryInto;
13-
use std::fs::{DirEntry, Metadata};
14-
use std::path::Path;
1512

1613
mod empty;
1714
mod v0;
@@ -20,14 +17,10 @@ mod v1;
2017
use v0::V0;
2118
use v1::V1;
2219

23-
use log::debug;
24-
use volta_core::error::ErrorDetails;
25-
use volta_core::fs::read_dir_eager;
2620
use volta_core::layout::volta_home;
2721
#[cfg(unix)]
2822
use volta_core::layout::volta_install;
29-
use volta_core::shim;
30-
use volta_fail::{Fallible, ResultExt};
23+
use volta_fail::Fallible;
3124
use volta_layout::v1::VoltaHome;
3225

3326
/// Represents the state of the Volta directory at every point in the migration process
@@ -115,56 +108,11 @@ pub fn run_migration() -> Fallible<()> {
115108
state = match state {
116109
MigrationState::Empty(e) => MigrationState::V1(Box::new(e.try_into()?)),
117110
MigrationState::V0(zero) => MigrationState::V1(Box::new((*zero).try_into()?)),
118-
MigrationState::V1(one) => {
119-
regenerate_shims_for_dir(one.home.shim_dir())?;
111+
MigrationState::V1(_) => {
120112
break;
121113
}
122114
};
123115
}
124116

125117
Ok(())
126118
}
127-
128-
fn regenerate_shims_for_dir(dir: &Path) -> Fallible<()> {
129-
debug!("Rebuilding shims");
130-
for shim_name in get_shim_list_deduped(dir)?.iter() {
131-
shim::delete(shim_name)?;
132-
shim::create(shim_name)?;
133-
}
134-
135-
Ok(())
136-
}
137-
138-
fn get_shim_list_deduped(dir: &Path) -> Fallible<HashSet<String>> {
139-
let contents = read_dir_eager(dir).with_context(|_| ErrorDetails::ReadDirError {
140-
dir: dir.to_owned(),
141-
})?;
142-
143-
#[cfg(unix)]
144-
{
145-
let mut shims: HashSet<String> = contents.filter_map(entry_to_shim_name).collect();
146-
shims.insert("node".into());
147-
shims.insert("npm".into());
148-
shims.insert("npx".into());
149-
shims.insert("yarn".into());
150-
Ok(shims)
151-
}
152-
153-
#[cfg(windows)]
154-
{
155-
// On Windows, the default shims are installed in Program Files, so we don't need to generate them here
156-
Ok(contents.filter_map(entry_to_shim_name).collect())
157-
}
158-
}
159-
160-
fn entry_to_shim_name((entry, metadata): (DirEntry, Metadata)) -> Option<String> {
161-
if metadata.file_type().is_symlink() {
162-
entry
163-
.path()
164-
.file_stem()
165-
.and_then(|stem| stem.to_str())
166-
.map(|stem| stem.to_string())
167-
} else {
168-
None
169-
}
170-
}

src/command/setup.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use log::info;
22
use structopt::StructOpt;
3+
use volta_core::layout::volta_home;
34
use volta_core::session::{ActivityKind, Session};
5+
use volta_core::shim::regenerate_shims_for_dir;
46
use volta_core::style::success_prefix;
57
use volta_fail::{ExitCode, Fallible};
68

@@ -14,6 +16,7 @@ impl Command for Setup {
1416
session.add_event_start(ActivityKind::Setup);
1517

1618
os::setup_environment()?;
19+
regenerate_shims_for_dir(volta_home()?.shim_dir())?;
1720

1821
info!(
1922
"{} Setup complete. Open a new terminal to start using Volta!",

src/common.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::process::{Command, ExitStatus};
22

33
use volta_core::error::ErrorDetails;
44
use volta_core::layout::{volta_home, volta_install};
5+
use volta_core::shim::regenerate_shims_for_dir;
56
use volta_fail::{ResultExt, VoltaError};
67

78
pub enum Error {
@@ -19,6 +20,7 @@ pub fn ensure_layout() -> Result<(), Error> {
1920
.status()
2021
.with_context(|_| ErrorDetails::CouldNotStartMigration)
2122
.into_result()?;
23+
regenerate_shims_for_dir(home.shim_dir()).map_err(Error::Volta)?;
2224
}
2325

2426
Ok(())

tests/acceptance/migrations.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@ fn legacy_v0_volta_home_is_upgraded() {
7474
assert!(Sandbox::path_exists(".volta/layout.v1"));
7575

7676
// shims should all be created
77-
// NOTE: this doesn't work in Windows, because the shim directory
78-
// is stored in the Registry, and not accessible
77+
// NOTE: this doesn't work in Windows, because the default shims are stored separately
7978
#[cfg(unix)]
8079
{
8180
assert!(Sandbox::shim_exists("node"));

0 commit comments

Comments
 (0)