Skip to content

Commit 138d33e

Browse files
authored
Merge pull request #2393 from rbtcollins/fix-windows
Fix homedir detection on Windows
2 parents c19b43c + 0b8f815 commit 138d33e

File tree

5 files changed

+65
-10
lines changed

5 files changed

+65
-10
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ error-chain = "0.12"
2828
effective-limits = "0.5.1"
2929
flate2 = "1"
3030
git-testament = "0.1.4"
31-
home = { git = "https://github.com/rbtcollins/home", rev="81c8dfcd815765daf384319a04e237feb9d5e72d" }
31+
home = { git = "https://github.com/rbtcollins/home", rev="a243ee2fbee6022c57d56f5aa79aefe194eabe53" }
3232
lazy_static = "1"
3333
libc = "0.2"
3434
num_cpus = "1.13"

src/currentprocess.rs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,30 @@ use varsource::*;
5252
/// needing to thread trait parameters across the entire code base: none of the
5353
/// methods are in performance critical loops (except perhaps progress bars -
5454
/// and even there we should be doing debouncing and managing update rates).
55+
/// The real trait is CurrentProcess; HomeProcess is a single trait because
56+
/// Box<T> only allows autotraits to be added to it; so we use a subtrait to add
57+
/// home::Env in.
58+
pub trait HomeProcess: CurrentProcess + home::Env {
59+
fn clone_boxed(&self) -> Box<dyn HomeProcess>;
60+
}
61+
62+
// Machinery for Cloning boxes
63+
64+
impl<T> HomeProcess for T
65+
where
66+
T: 'static + CurrentProcess + home::Env + Clone,
67+
{
68+
fn clone_boxed(&self) -> Box<dyn HomeProcess + 'static> {
69+
Box::new(T::clone(self))
70+
}
71+
}
72+
73+
impl Clone for Box<dyn HomeProcess + 'static> {
74+
fn clone(&self) -> Self {
75+
HomeProcess::clone_boxed(self.as_ref())
76+
}
77+
}
78+
5579
pub trait CurrentProcess:
5680
ArgSource
5781
+ CurrentDirSource
@@ -93,6 +117,11 @@ impl Clone for Box<dyn CurrentProcess + 'static> {
93117

94118
/// Obtain the current instance of CurrentProcess
95119
pub fn process() -> Box<dyn CurrentProcess> {
120+
CurrentProcess::clone_boxed(&*home_process())
121+
}
122+
123+
/// Obtain the current instance of HomeProcess
124+
pub fn home_process() -> Box<dyn HomeProcess> {
96125
match PROCESS.with(|p| p.borrow().clone()) {
97126
None => panic!("No process instance"),
98127
Some(p) => p,
@@ -105,7 +134,7 @@ static HOOK_INSTALLED: Once = Once::new();
105134
///
106135
/// If the function panics, the process definition *in that thread* is cleared
107136
/// by an implicitly installed global panic hook.
108-
pub fn with<F, R>(process: Box<dyn CurrentProcess>, f: F) -> R
137+
pub fn with<F, R>(process: Box<dyn HomeProcess>, f: F) -> R
109138
where
110139
F: FnOnce() -> R,
111140
{
@@ -134,7 +163,7 @@ fn clear_process() {
134163
}
135164

136165
thread_local! {
137-
pub static PROCESS:RefCell<Option<Box<dyn CurrentProcess>>> = RefCell::new(None);
166+
pub static PROCESS:RefCell<Option<Box<dyn HomeProcess>>> = RefCell::new(None);
138167
}
139168

140169
// PID related things

src/currentprocess/homethunk.rs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,24 @@ use std::ops::Deref;
55
use std::path::PathBuf;
66

77
use super::CurrentDirSource;
8-
use super::CurrentProcess;
8+
use super::HomeProcess;
9+
use super::OSProcess;
10+
use super::TestProcess;
911
use super::VarSource;
1012

11-
impl home::Env for Box<dyn CurrentProcess + 'static> {
13+
impl home::Env for Box<dyn HomeProcess + 'static> {
14+
fn home_dir(&self) -> Option<PathBuf> {
15+
(**self).home_dir()
16+
}
17+
fn current_dir(&self) -> Result<PathBuf, io::Error> {
18+
home::Env::current_dir(&(**self))
19+
}
20+
fn var_os(&self, key: &str) -> Option<OsString> {
21+
home::Env::var_os(&(**self), key)
22+
}
23+
}
24+
25+
impl home::Env for TestProcess {
1226
fn home_dir(&self) -> Option<PathBuf> {
1327
self.var("HOME").ok().map(|v| v.into())
1428
}
@@ -19,3 +33,15 @@ impl home::Env for Box<dyn CurrentProcess + 'static> {
1933
VarSource::var_os(self.deref(), key)
2034
}
2135
}
36+
37+
impl home::Env for OSProcess {
38+
fn home_dir(&self) -> Option<PathBuf> {
39+
home::OS_ENV.home_dir()
40+
}
41+
fn current_dir(&self) -> Result<PathBuf, io::Error> {
42+
home::OS_ENV.current_dir()
43+
}
44+
fn var_os(&self, key: &str) -> Option<OsString> {
45+
home::OS_ENV.var_os(key)
46+
}
47+
}

src/utils/utils.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ use url::Url;
1313

1414
// use crate::currentprocess::cwdsource::CurrentDirSource;
1515
use crate::errors::*;
16-
use crate::process;
1716
use crate::utils::notifications::Notification;
1817
use crate::utils::raw;
18+
use crate::{home_process, process};
1919

2020
pub use crate::utils::utils::raw::{
2121
find_cmd, has_cmd, if_not_empty, is_directory, is_file, path_exists, prefix_arg, random_string,
@@ -481,11 +481,11 @@ pub fn to_absolute<P: AsRef<Path>>(path: P) -> Result<PathBuf> {
481481
}
482482

483483
pub fn home_dir() -> Option<PathBuf> {
484-
home::home_dir_from(&process())
484+
home::home_dir_from(&home_process())
485485
}
486486

487487
pub fn cargo_home() -> Result<PathBuf> {
488-
home::cargo_home_from(&process()).map_err(|e| Error::from_kind(ErrorKind::Io(e)))
488+
home::cargo_home_from(&home_process()).map_err(|e| Error::from_kind(ErrorKind::Io(e)))
489489
}
490490

491491
// Creates a ~/.rustup folder
@@ -511,7 +511,7 @@ pub fn rustup_home_in_user_dir() -> Result<PathBuf> {
511511
}
512512

513513
pub fn rustup_home() -> Result<PathBuf> {
514-
home::rustup_home_from(&process()).map_err(|e| Error::from_kind(ErrorKind::Io(e)))
514+
home::rustup_home_from(&home_process()).map_err(|e| Error::from_kind(ErrorKind::Io(e)))
515515
}
516516

517517
pub fn format_path_for_display(path: &str) -> String {

0 commit comments

Comments
 (0)