diff --git a/crates/cli/src/execve.rs b/crates/cli/src/execve.rs index fa02bbd71..7d2e74ff0 100644 --- a/crates/cli/src/execve.rs +++ b/crates/cli/src/execve.rs @@ -3,7 +3,7 @@ use nix::unistd::execve as nix_execve; #[cfg(unix)] use std::ffi::CString; -use libpkgx::env::PlatformCaseAwareEnvKey; +use libpkgx::platform_case_aware_env_key::PlatformCaseAwareEnvKey; use std::{collections::HashMap, error::Error}; #[cfg(unix)] diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index 9d519b39b..db46ce3dc 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -11,13 +11,9 @@ use std::{collections::HashMap, error::Error, fmt::Write, sync::Arc, time::Durat use execve::execve; use indicatif::{ProgressBar, ProgressState, ProgressStyle}; use libpkgx::{ - env::{self, construct_platform_case_aware_env_key}, - hydrate::hydrate, - install_multi, pantry_db, - resolve::resolve, - sync, - types::PackageReq, - utils, + env, hydrate::hydrate, install_multi, pantry_db, + platform_case_aware_env_key::construct_platform_case_aware_env_key, resolve::resolve, sync, + types::PackageReq, utils, }; use regex::Regex; use rusqlite::Connection; @@ -135,7 +131,7 @@ async fn main() -> Result<(), Box> { }) .await?; - let resolution = resolve(graph, &config).await?; + let resolution = resolve(&graph, &config).await?; let spinner_clone = spinner.clone(); let clear_progress_bar = move || { @@ -227,6 +223,16 @@ async fn main() -> Result<(), Box> { pkgx_lvl.to_string(), ); + // TODO should be output by +syntax too + env.insert( + construct_platform_case_aware_env_key("PKGX_ENV".to_string()), + graph + .iter() + .map(|pkg| format!("{}", pkg)) + .collect::>() + .join(env::SEP), + ); + clear_progress_bar(); if flags.shebang { diff --git a/crates/lib/src/env.rs b/crates/lib/src/env.rs index fc36eac21..36d0abf8e 100644 --- a/crates/lib/src/env.rs +++ b/crates/lib/src/env.rs @@ -7,68 +7,15 @@ use std::{ #[cfg(unix)] use std::str::FromStr; -#[cfg(windows)] -use std::{ - fmt, - hash::{Hash, Hasher}, +use crate::{ + platform_case_aware_env_key::{construct_platform_case_aware_env_key, PlatformCaseAwareEnvKey}, + types::Installation, }; -#[cfg(windows)] -#[derive(Clone)] -pub struct CaseInsensitiveKey(pub String); - -#[cfg(windows)] -impl PartialEq for CaseInsensitiveKey { - fn eq(&self, other: &Self) -> bool { - self.0.eq_ignore_ascii_case(&other.0) - } -} - -#[cfg(windows)] -impl fmt::Display for CaseInsensitiveKey { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{}", self.0) - } -} - -#[cfg(windows)] -impl Eq for CaseInsensitiveKey {} - -#[cfg(windows)] -impl Hash for CaseInsensitiveKey { - fn hash(&self, state: &mut H) { - self.0.to_lowercase().hash(state); - } -} - -#[cfg(windows)] -impl fmt::Debug for CaseInsensitiveKey { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{:?}", self.0) - } -} - -#[cfg(windows)] -pub type PlatformCaseAwareEnvKey = CaseInsensitiveKey; -#[cfg(not(windows))] -pub type PlatformCaseAwareEnvKey = String; - -#[cfg(windows)] -pub fn construct_platform_case_aware_env_key(key: String) -> PlatformCaseAwareEnvKey { - CaseInsensitiveKey(key) -} - -#[cfg(not(windows))] -pub fn construct_platform_case_aware_env_key(key: String) -> PlatformCaseAwareEnvKey { - key -} - -use crate::types::Installation; - #[cfg(unix)] -const SEP: &str = ":"; +pub const SEP: &str = ":"; #[cfg(windows)] -const SEP: &str = ";"; +pub const SEP: &str = ";"; pub fn map(installations: &Vec) -> HashMap> { let mut vars: HashMap> = HashMap::new(); diff --git a/crates/lib/src/lib.rs b/crates/lib/src/lib.rs index ad96a7bd2..2ad2c2ae4 100644 --- a/crates/lib/src/lib.rs +++ b/crates/lib/src/lib.rs @@ -8,6 +8,7 @@ pub mod install_multi; mod inventory; mod pantry; pub mod pantry_db; +pub mod platform_case_aware_env_key; pub mod resolve; pub mod sync; pub mod types; diff --git a/crates/lib/src/platform_case_aware_env_key.rs b/crates/lib/src/platform_case_aware_env_key.rs new file mode 100644 index 000000000..320b41a44 --- /dev/null +++ b/crates/lib/src/platform_case_aware_env_key.rs @@ -0,0 +1,55 @@ +#[cfg(windows)] +use std::{ + fmt, + hash::{Hash, Hasher}, +}; + +#[cfg(windows)] +#[derive(Clone)] +pub struct CaseInsensitiveKey(pub String); + +#[cfg(windows)] +impl PartialEq for CaseInsensitiveKey { + fn eq(&self, other: &Self) -> bool { + self.0.eq_ignore_ascii_case(&other.0) + } +} + +#[cfg(windows)] +impl fmt::Display for CaseInsensitiveKey { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", self.0) + } +} + +#[cfg(windows)] +impl Eq for CaseInsensitiveKey {} + +#[cfg(windows)] +impl Hash for CaseInsensitiveKey { + fn hash(&self, state: &mut H) { + self.0.to_lowercase().hash(state); + } +} + +#[cfg(windows)] +impl fmt::Debug for CaseInsensitiveKey { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{:?}", self.0) + } +} + +#[cfg(windows)] +pub type PlatformCaseAwareEnvKey = CaseInsensitiveKey; +#[cfg(not(windows))] +pub type PlatformCaseAwareEnvKey = String; + +#[cfg(windows)] +pub fn construct_platform_case_aware_env_key(key: String) -> PlatformCaseAwareEnvKey { + CaseInsensitiveKey(key) +} + +#[cfg(not(windows))] +pub fn construct_platform_case_aware_env_key(key: String) -> PlatformCaseAwareEnvKey { + key +} diff --git a/crates/lib/src/resolve.rs b/crates/lib/src/resolve.rs index 8351649b4..a77f79512 100644 --- a/crates/lib/src/resolve.rs +++ b/crates/lib/src/resolve.rs @@ -18,7 +18,10 @@ pub struct Resolution { //TODO no need to take array since it doesn’t consider anything use futures::stream::{FuturesUnordered, StreamExt}; -pub async fn resolve(reqs: Vec, config: &Config) -> Result> { +pub async fn resolve( + reqs: &Vec, + config: &Config, +) -> Result> { let mut rv = Resolution::default(); // Create a FuturesUnordered to run the tasks concurrently @@ -26,19 +29,19 @@ pub async fn resolve(reqs: Vec, config: &Config) -> Result>(( Some((installation.clone(), installation.pkg.clone())), None, )) - } else if let Some(version) = inventory::select(&req, config).await? { + } else if let Some(version) = inventory::select(req, config).await? { let pkg = Package { project: req.project.clone(), version, }; Ok::<_, Box>((None, Some(pkg))) } else { - Err(Box::new(ResolveError { pkg: req }) as Box) + Err(Box::new(ResolveError { pkg: req.clone() }) as Box) } }); }