Skip to content

Commit

Permalink
fix: remove todo about cygwin path and add remaining windows env vars (
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfv authored Apr 17, 2024
1 parent ce84a2a commit b663312
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 12 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ tui-input = { version = "0.8.0", optional = true }
reflink-copy = "0.1.15"
rayon = "1.10.0"
patch = "0.7.0"
regex = "1.10.4"

[dev-dependencies]
insta = { version = "1.38.0", features = ["yaml"] }
Expand Down
70 changes: 58 additions & 12 deletions src/windows/env.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,43 @@
use std::{collections::HashMap, path::Path};
use std::{
collections::HashMap,
path::{Component::*, Path, Prefix::Disk},
};

use rattler_conda_types::Platform;
use regex::Regex;

fn get_drive_letter(path: &Path) -> Option<char> {
path.components().find_map(|component| match component {
Prefix(prefix_component) => match prefix_component.kind() {
Disk(letter) => Some(letter as char),
_ => None,
},
_ => None,
})
}

fn to_cygdrive(path: &Path) -> String {
if let Some(drive_letter) = get_drive_letter(path) {
// skip first component, which is the drive letter and the `\` after it
let rest = path.iter().skip(2);
format!(
"/cygdrive/{}/{}",
drive_letter.to_lowercase(),
rest.map(|c| c.to_string_lossy())
.collect::<Vec<_>>()
.join("/")
)
} else {
// fallback to `c` if no drive letter is found
format!(
"/cygdrive/c/{}",
path.iter()
.map(|c| c.to_string_lossy())
.collect::<Vec<_>>()
.join("/")
)
}
}

pub fn default_env_vars(prefix: &Path, target_platform: &Platform) -> HashMap<String, String> {
let win_arch = match target_platform {
Expand Down Expand Up @@ -39,8 +76,6 @@ pub fn default_env_vars(prefix: &Path, target_platform: &Platform) -> HashMap<St
library_prefix.join("lib").to_string_lossy().to_string(),
);

// vars.insert("CYGWIN_PREFIX", "".join(("/cygdrive/", drive.lower(), tail.replace("\\", "/"))));

let default_vars = vec![
"ALLUSERSPROFILE",
"APPDATA",
Expand Down Expand Up @@ -87,16 +122,27 @@ pub fn default_env_vars(prefix: &Path, target_platform: &Platform) -> HashMap<St
std::env::var("BUILD").unwrap_or_else(|_| format!("{}-pc-windows-{}", win_arch, win_msvc)),
);

// TODO
// get_default(
// "CYGWIN_PREFIX", "".join(("/cygdrive/", drive.lower(), tail.replace("\\", "/")))
// )
vars.insert("CYGWIN_PREFIX".to_string(), to_cygdrive(prefix));

// for k in os.environ.keys():
// if re.match("VS[0-9]{2,3}COMNTOOLS", k):
// get_default(k)
// elif re.match("VS[0-9]{4}INSTALLDIR", k):
// get_default(k)
let re_vs_comntools = Regex::new(r"^VS[0-9]{2,3}COMNTOOLS$").unwrap();
let re_vs_installdir = Regex::new(r"^VS[0-9]{4}INSTALLDIR$").unwrap();

for (key, val) in std::env::vars() {
if re_vs_comntools.is_match(&key) || re_vs_installdir.is_match(&key) {
vars.insert(key, val);
}
}

vars
}

#[cfg(test)]
mod test {
#[cfg(target_os = "windows")]
#[test]
fn test_cygdrive() {
let path = std::path::Path::new("C:\\Users\\user\\Documents");
let cygdrive = super::to_cygdrive(path);
assert_eq!(cygdrive, "/cygdrive/c/Users/user/Documents");
}
}

0 comments on commit b663312

Please sign in to comment.