Skip to content

Commit

Permalink
feat: add to pypi in feature (#1135)
Browse files Browse the repository at this point in the history
  • Loading branch information
ruben-arts authored Apr 8, 2024
1 parent 11e2cf1 commit 20013a7
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 14 deletions.
8 changes: 6 additions & 2 deletions src/cli/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ pub async fn execute(args: Args) -> miette::Result<()> {

add_pypi_specs_to_project(
&mut project,
&feature_name,
specs,
spec_platforms,
args.no_lockfile_update,
Expand Down Expand Up @@ -218,6 +219,7 @@ pub async fn execute(args: Args) -> miette::Result<()> {

pub async fn add_pypi_specs_to_project(
project: &mut Project,
feature_name: &FeatureName,
specs: Vec<(PyPiPackageName, PyPiRequirement)>,
specs_platforms: &[Platform],
no_update_lockfile: bool,
Expand All @@ -227,12 +229,14 @@ pub async fn add_pypi_specs_to_project(
// TODO: Get best version
// Add the dependency to the project
if specs_platforms.is_empty() {
project.manifest.add_pypi_dependency(name, spec, None)?;
project
.manifest
.add_pypi_dependency(name, spec, None, feature_name)?;
} else {
for platform in specs_platforms.iter() {
project
.manifest
.add_pypi_dependency(name, spec, Some(*platform))?;
.add_pypi_dependency(name, spec, Some(*platform), feature_name)?;
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/cli/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ pub async fn execute(args: Args) -> miette::Result<()> {
&spec.0,
&spec.1,
Some(platform.parse().into_diagnostic()?),
&FeatureName::default(),
)?;
}
}
Expand Down
17 changes: 9 additions & 8 deletions src/project/manifest/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,16 +223,10 @@ impl ManifestSource {
requirement: &PyPiRequirement,
platform: Option<Platform>,
project_root: &Path,
feature_name: &FeatureName,
) -> Result<(), Report> {
match self {
ManifestSource::PixiToml(_) => self.add_dependency_helper(
name.as_source(),
(*requirement).clone().into(),
consts::PYPI_DEPENDENCIES,
platform,
&FeatureName::Default,
),
ManifestSource::PyProjectToml(_) => {
ManifestSource::PyProjectToml(_) if feature_name.is_default() => {
let dep = requirement
.as_pep508(name.as_normalized(), project_root)
.map_err(|_| {
Expand All @@ -259,6 +253,13 @@ impl ManifestSource {
}
Ok(())
}
_ => self.add_dependency_helper(
name.as_source(),
requirement.clone().into(),
consts::PYPI_DEPENDENCIES,
platform,
feature_name,
),
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/project/manifest/feature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ impl FeatureName {
self.name().unwrap_or(consts::DEFAULT_FEATURE_NAME)
}

/// Returns true if the feature is the default feature.
pub fn is_default(&self) -> bool {
matches!(self, FeatureName::Default)
}

/// Returns a styled version of the feature name for display in the console.
pub fn fancy_display(&self) -> console::StyledObject<&str> {
console::style(self.as_str()).cyan()
Expand Down
12 changes: 9 additions & 3 deletions src/project/manifest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,17 +391,23 @@ impl Manifest {
name: &PyPiPackageName,
requirement: &PyPiRequirement,
platform: Option<Platform>,
feature_name: &FeatureName,
) -> miette::Result<()> {
// Add the pypi dependency to the TOML document
let project_root = self
.path
.parent()
.expect("Path should always have a parent");
self.document
.add_pypi_dependency(name, requirement, platform, project_root)?;
self.document.add_pypi_dependency(
name,
requirement,
platform,
project_root,
feature_name,
)?;

// Add the dependency to the manifest as well
self.target_mut(platform, None)
self.target_mut(platform, Some(feature_name))
.add_pypi_dependency(name.clone(), requirement.clone());

Ok(())
Expand Down
25 changes: 24 additions & 1 deletion src/project/manifest/pyproject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ mod tests {
let name = PyPiPackageName::from_str("numpy").unwrap();
let requirement = PyPiRequirement::RawVersion(">=3.12".parse().unwrap());
manifest
.add_pypi_dependency(&name, &requirement, None)
.add_pypi_dependency(&name, &requirement, None, &FeatureName::Default)
.unwrap();

assert!(manifest
Expand All @@ -429,6 +429,29 @@ mod tests {
.get(&name)
.is_some());

// Add numpy to feature in pyproject
let name = PyPiPackageName::from_str("pytest").unwrap();
let requirement = PyPiRequirement::RawVersion(">=3.12".parse().unwrap());
manifest
.add_pypi_dependency(
&name,
&requirement,
None,
&FeatureName::Named("test".to_string()),
)
.unwrap();
assert!(manifest
.feature(&FeatureName::Named("test".to_string()))
.unwrap()
.targets
.for_opt_target(None)
.unwrap()
.pypi_dependencies
.as_ref()
.unwrap()
.get(&name)
.is_some());

assert_snapshot!(manifest.document.to_string());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ expression: manifest.document.to_string()

[tool.pixi.tasks]
start = "python -m flask run --port=5050"

[tool.pixi.feature.test.pypi-dependencies]
pytest = ">=3.12"

0 comments on commit 20013a7

Please sign in to comment.