Skip to content

Commit

Permalink
feat(poetry-migrator): added ability to pull in the description from …
Browse files Browse the repository at this point in the history
…poetry packages
  • Loading branch information
stvnksslr committed Nov 11, 2024
1 parent 01342be commit c232904
Show file tree
Hide file tree
Showing 5 changed files with 270 additions and 32 deletions.
9 changes: 5 additions & 4 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 @@ -20,6 +20,7 @@ serde_json = "1.0.132"
toml = "0.8.19"
which = "7.0.0"
openssl = { version = "0.10", features = ["vendored"] }
regex = "1.11.1"

[dev-dependencies]
tempfile = "3.14.0"
2 changes: 1 addition & 1 deletion src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ pub struct Poetry {
#[derive(Deserialize, Debug)]
pub struct Group {
pub dependencies: HashMap<String, toml::Value>,
}
}
101 changes: 89 additions & 12 deletions src/utils/pyproject.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,104 @@
use log::{info, warn};
use std::path::{Path};
use log::{info};
use std::path::Path;
use std::io::Write;

pub fn update_pyproject_toml(project_dir: &Path, extra_urls: &[String]) -> Result<(), String> {
// Read the old pyproject.toml to get Poetry metadata
let old_pyproject_path = project_dir.join("old.pyproject.toml");
if !old_pyproject_path.exists() {
return Ok(());
}

let old_content = std::fs::read_to_string(&old_pyproject_path)
.map_err(|e| format!("Failed to read old.pyproject.toml: {}", e))?;

// Parse the Poetry section manually
let mut poetry_version = None;
let mut poetry_description = None;
let mut in_poetry_section = false;

for line in old_content.lines() {
let trimmed = line.trim();

if trimmed == "[tool.poetry]" {
in_poetry_section = true;
continue;
} else if trimmed.starts_with('[') && trimmed != "[tool.poetry]" {
in_poetry_section = false;
continue;
}

if !in_poetry_section {
continue;
}

// Extract version and description from Poetry section
if trimmed.starts_with("version = ") {
poetry_version = Some(trimmed.split_once('=').unwrap().1.trim().trim_matches('"'));
} else if trimmed.starts_with("description = ") {
poetry_description = Some(trimmed.split_once('=').unwrap().1.trim().trim_matches('"'));
}
}

// Now update the new pyproject.toml
let pyproject_path = project_dir.join("pyproject.toml");
let mut content = std::fs::read_to_string(&pyproject_path)
let content = std::fs::read_to_string(&pyproject_path)
.map_err(|e| format!("Failed to read pyproject.toml: {}", e))?;

let mut lines: Vec<String> = Vec::new();
let mut in_project_section = false;

for line in content.lines() {
let trimmed = line.trim();

if trimmed == "[project]" {
in_project_section = true;
lines.push(line.to_string());
continue;
} else if trimmed.starts_with('[') {
in_project_section = false;
}

if !in_project_section {
lines.push(line.to_string());
continue;
}

// Update version and description in the project section
if trimmed.starts_with("version = ") && poetry_version.is_some() {
lines.push(format!("version = \"{}\"", poetry_version.unwrap()));
} else if trimmed.starts_with("description = ") && poetry_description.is_some() {
lines.push(format!("description = \"{}\"", poetry_description.unwrap()));
} else {
lines.push(line.to_string());
}
}

// Write the updated content back
let mut file = std::fs::OpenOptions::new()
.write(true)
.truncate(true)
.open(&pyproject_path)
.map_err(|e| format!("Failed to open pyproject.toml for writing: {}", e))?;

for line in lines {
writeln!(file, "{}", line)
.map_err(|e| format!("Failed to write line to pyproject.toml: {}", e))?;
}

// Handle extra URLs in a new section if needed
if !extra_urls.is_empty() {
let uv_section = format!(
"\n[tool.uv]\nextra-index-url = {}\n",
serde_json::to_string(extra_urls).map_err(|e| format!("Failed to serialize extra URLs: {}", e))?
);

if content.contains("[tool.uv]") {
warn!("[tool.uv] section already exists in pyproject.toml. Extra URLs might need manual merging.");
} else {
content.push_str(&uv_section);
}

std::fs::write(&pyproject_path, content)
.map_err(|e| format!("Failed to write updated pyproject.toml: {}", e))?;
file.write_all(uv_section.as_bytes())
.map_err(|e| format!("Failed to write uv section: {}", e))?;
}

info!("Updated pyproject.toml with extra index URLs");
if poetry_version.is_some() || poetry_description.is_some() {
info!("Successfully updated project metadata from Poetry configuration");
}

Ok(())
Expand Down
Loading

0 comments on commit c232904

Please sign in to comment.