Skip to content

Commit e0eb62a

Browse files
committed
Use cargo to install pyo3
1 parent 58813b7 commit e0eb62a

File tree

4 files changed

+23
-78
lines changed

4 files changed

+23
-78
lines changed

src/main.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ use crate::{
2727
config::Config,
2828
dev_dependency_installer::install_dev_dependencies,
2929
project_generator::generate_project,
30-
project_info::{get_project_info, ProjectInfo},
30+
project_info::{get_project_info, ProjectInfo, ProjectManager},
31+
rust_files::cargo_add_pyo3,
3132
};
3233

3334
#[cfg(feature = "fastapi")]
@@ -42,6 +43,10 @@ fn create(project_info: &ProjectInfo) -> Result<()> {
4243
.output()
4344
.expect("Failed to initialize git");
4445

46+
if matches!(project_info.project_manager, ProjectManager::Maturin) {
47+
cargo_add_pyo3(project_info)?;
48+
}
49+
4550
install_dev_dependencies(project_info)?;
4651

4752
#[cfg(feature = "fastapi")]

src/package_version.rs

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -115,33 +115,6 @@ impl PreCommitHookVersion {
115115
}
116116
}
117117

118-
#[derive(Debug)]
119-
pub struct RustPackageVersion {
120-
pub name: String,
121-
pub version: String,
122-
pub features: Option<Vec<String>>,
123-
}
124-
125-
impl LatestVersion for RustPackageVersion {
126-
fn get_latest_version(&mut self) -> Result<()> {
127-
let url = format!("https://crates.io/api/v1/crates/{}", self.name);
128-
let client = reqwest::blocking::Client::new();
129-
let response = client
130-
.get(url)
131-
.header(reqwest::header::USER_AGENT, "python-project-generator")
132-
.timeout(Duration::new(5, 0))
133-
.send()?
134-
.text()?;
135-
let info: serde_json::Value = serde_json::from_str(&response)?;
136-
self.name = info["crate"]["id"].to_string().replace('"', "");
137-
self.version = info["crate"]["max_stable_version"]
138-
.to_string()
139-
.replace('"', "");
140-
141-
Ok(())
142-
}
143-
}
144-
145118
pub fn default_pre_commit_rev(hook: &PreCommitHook) -> String {
146119
match hook {
147120
PreCommitHook::MyPy => "v1.18.2".to_string(),

src/rust_files.rs

Lines changed: 16 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,17 @@
11
use anyhow::Result;
2-
use colored::*;
3-
use rayon::prelude::*;
42

53
use crate::{
64
file_manager::save_file_with_content,
75
licenses::license_str,
8-
package_version::{LatestVersion, RustPackageVersion},
96
project_info::{LicenseType, ProjectInfo},
107
};
118

12-
fn build_latest_dependencies() -> String {
13-
let mut version_string = String::new();
14-
let mut packages = vec![RustPackageVersion {
15-
name: "pyo3".to_string(),
16-
version: "0.27.1".to_string(),
17-
features: Some(vec!["extension-module".to_string()]),
18-
}];
19-
20-
packages.par_iter_mut().for_each(|package| {
21-
if package.get_latest_version().is_err() {
22-
let error_message = format!(
23-
"Error retrieving latest crate version for {}. Using default.",
24-
package.name
25-
);
26-
println!("\n{}", error_message.yellow());
27-
}
28-
});
29-
30-
for package in packages {
31-
if let Some(features) = &package.features {
32-
let mut feature_str = "[".to_string();
33-
for feature in features {
34-
feature_str.push_str(&format!(r#""{feature}", "#));
35-
}
36-
37-
feature_str.truncate(feature_str.len() - 2);
38-
feature_str.push(']');
39-
40-
version_string.push_str(&format!(
41-
"{} = {{ version = \"{}\", features = {} }}\n",
42-
package.name, package.version, feature_str
43-
));
44-
} else {
45-
version_string.push_str(&format!(
46-
"{} = {{ version = \"{}\" }}\n",
47-
package.name, package.version
48-
));
49-
}
50-
}
51-
52-
version_string.trim().to_string()
53-
}
54-
559
fn create_cargo_toml_file(
5610
project_slug: &str,
5711
project_description: &str,
5812
source_dir: &str,
5913
license_type: &LicenseType,
6014
) -> String {
61-
let versions = build_latest_dependencies();
6215
let license = license_str(license_type);
6316
let name = source_dir.replace([' ', '-'], "_");
6417

@@ -76,7 +29,6 @@ name = "_{name}"
7629
crate-type = ["cdylib"]
7730
7831
[dependencies]
79-
{versions}
8032
"#
8133
)
8234
}
@@ -95,6 +47,22 @@ pub fn save_cargo_toml_file(project_info: &ProjectInfo) -> Result<()> {
9547
Ok(())
9648
}
9749

50+
pub fn cargo_add_pyo3(project_info: &ProjectInfo) -> Result<()> {
51+
use anyhow::bail;
52+
53+
let output = std::process::Command::new("cargo")
54+
.args(["add", "pyo3", "--features", "extension-module"])
55+
.current_dir(project_info.base_dir())
56+
.output()?;
57+
58+
if !output.status.success() {
59+
let stderr = String::from_utf8_lossy(&output.stderr);
60+
bail!("Failed to add pyo3 dependency: {stderr}");
61+
}
62+
63+
Ok(())
64+
}
65+
9866
fn create_lib_file(project_info: &ProjectInfo) -> String {
9967
let module = project_info.module_name();
10068
format!(
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
---
22
source: src/rust_files.rs
33
expression: content
4-
snapshot_kind: text
54
---
6-
"[package]\nname = \"my-project\"\nversion = \"1.0.0\"\ndescription = \"This is a test\"\nedition = \"2021\"\nlicense = \"MIT\"\nreadme = \"README.md\"\n\n[lib]\nname = \"_my_project\"\ncrate-type = [\"cdylib\"]\n\n[dependencies]\npyo3 = { version = \"1.0.0\", features = [\"extension-module\"] }\n"
5+
"[package]\nname = \"my-project\"\nversion = \"1.0.0\"\ndescription = \"This is a test\"\nedition = \"2021\"\nlicense = \"MIT\"\nreadme = \"README.md\"\n\n[lib]\nname = \"_my_project\"\ncrate-type = [\"cdylib\"]\n\n[dependencies]\n"

0 commit comments

Comments
 (0)