Skip to content

Commit

Permalink
Use array of strings for toml dependencies (#195)
Browse files Browse the repository at this point in the history
  • Loading branch information
cnpryer authored Sep 23, 2022
1 parent cabfc7f commit dc44e7e
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 61 deletions.
7 changes: 1 addition & 6 deletions resources/mock-project/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,12 @@
name = "mock_project"
version = "0.0.1"
description = ""
dependencies = ["click==8.1.3", "black==22.8.0"]

[[project.authors]]
name = "Chris Pryer"
email = "[email protected]"

[project.dependencies]
click = "8.1.3"

[project.dev-dependencies]
black = "22.8.0"

[build-system]
requires = ["huak-core>=1.0.0"]
build-backend = "huak.core.build.api"
19 changes: 2 additions & 17 deletions src/huak/config/pyproject/project/mod.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,4 @@
use serde_derive::{Deserialize, Serialize};
use toml::value::{Map, Value};

/// Struct containing dependency information.
/// ```toml
/// name = version
/// ```
#[allow(dead_code)]
#[derive(Clone, Deserialize, Debug)]
pub(crate) struct Dependency {
pub(crate) name: String,
pub(crate) version: String,
}

/// Struct containing Author information.
/// ```toml
Expand All @@ -37,10 +25,8 @@ pub(crate) struct Project {
pub(crate) name: String,
pub(crate) version: String,
pub(crate) description: String,
pub(crate) dependencies: Vec<String>,
pub(crate) authors: Vec<Author>,
pub(crate) dependencies: Map<String, Value>,
#[serde(rename = "dev-dependencies")]
pub(crate) dev_dependencies: Map<String, Value>,
}

impl Default for Project {
Expand All @@ -50,8 +36,7 @@ impl Default for Project {
version: "0.0.1".to_string(),
description: "".to_string(),
authors: vec![],
dependencies: Map::new(),
dev_dependencies: Map::new(),
dependencies: vec![],
}
}
}
10 changes: 2 additions & 8 deletions src/huak/config/pyproject/toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,12 @@ mod tests {
name = "Test"
version = "0.1.0"
description = ""
dependencies = ["click==8.1.3", "black==22.8.0"]
[[project.authors]]
name = "Chris Pryer"
email = "[email protected]"
[project.dependencies]
[project.dev-dependencies]
[build-system]
requires = ["huak-core>=1.0.0"]
build-backend = "huak.core.build.api"
Expand All @@ -87,15 +84,12 @@ build-backend = "huak.core.build.api"
name = "Test"
version = "0.1.0"
description = ""
dependencies = ["click==8.1.3", "black==22.8.0"]
[[project.authors]]
name = "Chris Pryer"
email = "[email protected]"
[project.dependencies]
[project.dev-dependencies]
[build-system]
requires = ["huak-core>=1.0.0"]
build-backend = "huak.core.build.api"
Expand Down
7 changes: 2 additions & 5 deletions src/huak/env/venv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,8 @@ impl PythonEnvironment for Venv {
dependency: &PythonPackage,
) -> Result<(), CliError> {
let cwd = env::current_dir()?;
let module_str = match dependency.version.is_empty() {
true => dependency.name.to_string(),
false => format!("{}=={}", dependency.name, dependency.version),
};
let args = ["install", &module_str];
let module_str = &dependency.string();
let args = ["install", module_str];
let module = "pip";

self.exec_module(module, &args, cwd.as_path())?;
Expand Down
6 changes: 1 addition & 5 deletions src/huak/ops/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@ pub fn install_project_dependencies(project: &Project) -> CliResult {
));
}

for dependency in &project.config().dependency_list("main") {
project.venv().install_package(dependency)?;
}

for dependency in &project.config().dependency_list("dev") {
for dependency in &project.config().dependency_list() {
project.venv().install_package(dependency)?;
}

Expand Down
23 changes: 17 additions & 6 deletions src/huak/ops/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ pub fn remove_project_dependency(
};

let mut toml = Toml::open(&project.root.join("pyproject.toml"))?;
toml.project.dependencies.remove(dependency);
toml.project.dev_dependencies.remove(dependency);
toml.project
.dependencies
.retain(|s| !s.starts_with(dependency));

// Serialize pyproject.toml.
fs::write(&project.root.join("pyproject.toml"), toml.to_string()?)?;
Expand Down Expand Up @@ -50,14 +51,24 @@ mod tests {
create_mock_project(directory.join("mock-project")).unwrap();
let toml_path = project.root.join("pyproject.toml");
let toml = Toml::open(&toml_path).unwrap();
let had_path = toml.project.dependencies.contains_key("click");
let prev = toml
.project
.dependencies
.into_iter()
.filter(|s| s.starts_with("click"))
.collect::<Vec<String>>();

remove_project_dependency(&project, "click").unwrap();

let toml = Toml::open(&toml_path).unwrap();
let has_path = toml.project.dependencies.contains_key("click");
let curr = toml
.project
.dependencies
.into_iter()
.filter(|s| s.starts_with("click"))
.collect::<Vec<String>>();

assert!(had_path);
assert!(!has_path);
assert!(!prev.is_empty());
assert!(curr.is_empty());
}
}
10 changes: 8 additions & 2 deletions src/huak/package/python.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
/// A Python package struct.
#[derive(Clone, Eq, PartialEq, Debug)]
pub struct PythonPackage {
string: String,
pub name: String,
pub version: String,
}

impl PythonPackage {
pub fn new(name: String) -> PythonPackage {
pub fn new(string: String) -> PythonPackage {
PythonPackage {
name,
string,
name: "".to_string(),
version: "".to_string(),
}
}

pub fn string(&self) -> &String {
&self.string
}
}
16 changes: 4 additions & 12 deletions src/huak/project/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const DEFAULT_SEARCH_STEPS: usize = 5;

/// Traits for Python-specific configuration.
pub trait PythonConfig {
fn dependency_list(&self, kind: &str) -> Vec<PythonPackage>;
fn dependency_list(&self) -> Vec<PythonPackage>;
}

/// `Manifest` data the configuration uses to manage standard configuration
Expand Down Expand Up @@ -97,22 +97,14 @@ impl Config {
impl PythonConfig for Config {
// Get vec of dependencies from the manifest.
// TODO: More than toml.
fn dependency_list(&self, kind: &str) -> Vec<PythonPackage> {
fn dependency_list(&self) -> Vec<PythonPackage> {
// Get huak's spanned table found in the Toml.
let table = &self.manifest.toml.project;

// Dependencies to list from.
let from = match kind {
"dev" => &table.dev_dependencies,
_ => &table.dependencies,
};
let from = &table.dependencies;

// Collect into vector of owned `PythonPackage` data.
from.into_iter()
.map(|d| PythonPackage {
name: d.0.to_string(),
version: d.1.as_str().unwrap().to_string(),
})
.collect()
from.iter().map(|d| PythonPackage::new(d.clone())).collect()
}
}

0 comments on commit dc44e7e

Please sign in to comment.