Skip to content

Commit

Permalink
--no-edit
Browse files Browse the repository at this point in the history
  • Loading branch information
hameerabbasi committed Oct 31, 2024
1 parent 4424b37 commit d8b53f0
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 23 deletions.
7 changes: 7 additions & 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 @@ -279,6 +279,7 @@ uv-resolver = { workspace = true }
uv-types = { workspace = true }
xxhash-rust = { workspace = true }
zip = { workspace = true, features = ["deflate", "time"] }
cargo-lint = "0.1.0"


[target.'cfg(unix)'.dependencies]
Expand Down
18 changes: 14 additions & 4 deletions crates/pixi_manifest/src/feature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,25 +257,35 @@ impl Feature {
///
/// Returns `None` if this feature does not define any target with an
/// activation.
pub fn activation_scripts(&self, platform: Option<Platform>) -> Option<&Vec<String>> {
pub fn activation_scripts(&self, platform: Option<Platform>) -> Vec<String> {
self.targets
.resolve(platform)
.filter_map(|t| t.activation.as_ref())
.filter_map(|a| a.scripts.as_ref())
.next()
.fold(Vec::new(), |mut acc, x| {
acc.extend(x.iter().cloned());
acc
})
}

/// Returns the activation environment for the most specific target that
/// matches the given `platform`.
///
/// Returns `None` if this feature does not define any target with an
/// activation.
pub fn activation_env(&self, platform: Option<Platform>) -> Option<&IndexMap<String, String>> {
pub fn activation_env(&self, platform: Option<Platform>) -> IndexMap<String, String> {
self.targets
.resolve(platform)
.filter_map(|t| t.activation.as_ref())
.filter_map(|a| a.env.as_ref())
.next()
.fold(IndexMap::new(), |mut acc, x| {
for (k, v) in x {
if !acc.contains_key(k) {
acc.insert(k.clone(), v.clone());
}
}
acc
})
}

/// Returns true if the feature contains any reference to a pypi
Expand Down
82 changes: 64 additions & 18 deletions src/project/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,7 @@ impl<'p> Environment<'p> {
/// are defined for the environment.
pub(crate) fn activation_scripts(&self, platform: Option<Platform>) -> Vec<String> {
self.features()
.filter_map(|f| f.activation_scripts(platform))
.flatten()
.cloned()
.flat_map(|f| f.activation_scripts(platform))
.collect()
}

Expand All @@ -242,7 +240,7 @@ impl<'p> Environment<'p> {
/// are defined for the environment.
pub(crate) fn activation_env(&self, platform: Option<Platform>) -> IndexMap<String, String> {
self.features()
.filter_map(|f| f.activation_env(platform))
.map(|f| f.activation_env(platform))
.fold(IndexMap::new(), |mut acc, env| {
acc.extend(env.iter().map(|(k, v)| (k.clone(), v.clone())));
acc
Expand Down Expand Up @@ -310,6 +308,7 @@ impl<'p> Hash for Environment<'p> {
mod tests {
use std::{collections::HashSet, path::Path};

use indexmap::indexmap;
use insta::assert_snapshot;
use itertools::Itertools;
use pixi_manifest::CondaDependencies;
Expand Down Expand Up @@ -474,23 +473,23 @@ mod tests {
let manifest = Project::from_str(
Path::new("pixi.toml"),
r#"
[project]
name = "foobar"
channels = []
platforms = ["linux-64", "osx-64"]
[project]
name = "foobar"
channels = []
platforms = ["linux-64", "osx-64"]
[activation]
scripts = ["default.bat"]
[activation]
scripts = ["default.bat"]
[target.linux-64.activation]
scripts = ["linux.bat"]
[target.linux-64.activation]
scripts = ["linux.bat"]
[feature.foo.activation]
scripts = ["foo.bat"]
[feature.foo.activation]
scripts = ["foo.bat"]
[environments]
foo = ["foo"]
"#,
[environments]
foo = ["foo"]
"#,
)
.unwrap();

Expand All @@ -501,7 +500,54 @@ mod tests {
);
assert_eq!(
foo_env.activation_scripts(Some(Platform::Linux64)),
vec!["foo.bat".to_string(), "linux.bat".to_string()]
vec![
"foo.bat".to_string(),
"linux.bat".to_string(),
"default.bat".to_string()
]
);
}

#[test]
fn test_activation_env() {
let manifest = Project::from_str(
Path::new("pixi.toml"),
r#"
[project]
name = "foobar"
channels = []
platforms = ["linux-64", "osx-64"]
[activation.env]
DEFAULT_VAR = "1"
[target.linux-64.activation.env]
LINUX_VAR = "1"
[feature.foo.activation.env]
FOO_VAR = "1"
[environments]
foo = ["foo"]
"#,
)
.unwrap();

let default_env = manifest.default_environment();
let foo_env = manifest.environment("foo").unwrap();
assert_eq!(
foo_env.activation_env(None),
indexmap! {
"FOO_VAR".to_string() => "1".to_string(),
"DEFAULT_VAR".to_string() => "1".to_string(),
}
);
assert_eq!(
default_env.activation_env(Some(Platform::Linux64)),
indexmap! {
"LINUX_VAR".to_string() => "1".to_string(),
"DEFAULT_VAR".to_string() => "1".to_string(),
}
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
---
source: src/project/mod.rs
expression: "format!(\"= Linux64\\n{}\\n\\n= Win64\\n{}\\n\\n= OsxArm64\\n{}\",\n fmt_activation_scripts(project.activation_scripts(Platform ::\n Linux64).unwrap()),\n fmt_activation_scripts(project.activation_scripts(Platform ::\n Win64).unwrap()),\n fmt_activation_scripts(project.activation_scripts(Platform ::\n OsxArm64).unwrap()))"
expression: "format!(\"= Linux64\\n{}\\n\\n= Win64\\n{}\\n\\n= OsxArm64\\n{}\",\nfmt_activation_scripts(project.default_environment().activation_scripts(Some(Platform::Linux64))),\nfmt_activation_scripts(project.default_environment().activation_scripts(Some(Platform::Win64))),\nfmt_activation_scripts(project.default_environment().activation_scripts(Some(Platform::OsxArm64))))"
snapshot_kind: text
---
= Linux64
Cargo.toml
pixi.toml
pixi.lock

= Win64
Cargo.lock
pixi.toml
pixi.lock

= OsxArm64
pixi.toml
Expand Down

0 comments on commit d8b53f0

Please sign in to comment.