|
| 1 | +//! Cargo.toml transformation for split/sync operations |
| 2 | +//! |
| 3 | +//! This module provides simple Cargo.toml transformations needed by split and sync: |
| 4 | +//! - Transform workspace dependencies to standalone format (for splits) |
| 5 | +//! - Transform standalone dependencies back to workspace format (for syncs) |
| 6 | +
|
| 7 | +use crate::cargo::manifest_ops; |
| 8 | +use crate::error::{RailError, RailResult}; |
| 9 | +use cargo_metadata::Metadata; |
| 10 | +use std::path::PathBuf; |
| 11 | +use toml_edit::{DocumentMut, Item}; |
| 12 | + |
| 13 | +/// Context for Cargo.toml transformations |
| 14 | +pub struct TransformContext { |
| 15 | + /// Name of the crate being transformed |
| 16 | + pub crate_name: String, |
| 17 | + /// Workspace root path |
| 18 | + pub workspace_root: PathBuf, |
| 19 | +} |
| 20 | + |
| 21 | +/// Cargo.toml transformer for split/sync operations |
| 22 | +pub struct CargoTransform { |
| 23 | + metadata: Metadata, |
| 24 | +} |
| 25 | + |
| 26 | +impl CargoTransform { |
| 27 | + /// Create a new transformer with workspace metadata |
| 28 | + pub fn new(metadata: Metadata) -> Self { |
| 29 | + Self { metadata } |
| 30 | + } |
| 31 | + |
| 32 | + /// Transform a Cargo.toml from workspace format to split (standalone) format |
| 33 | + /// |
| 34 | + /// This replaces workspace dependency references with concrete version requirements. |
| 35 | + pub fn transform_to_split(&self, content: &str, context: &TransformContext) -> RailResult<String> { |
| 36 | + let mut doc: DocumentMut = content |
| 37 | + .parse() |
| 38 | + .map_err(|e| RailError::message(format!("Failed to parse Cargo.toml: {}", e)))?; |
| 39 | + |
| 40 | + // Remove workspace inheritance markers and resolve to actual values |
| 41 | + self.resolve_workspace_inheritance(&mut doc, &context.workspace_root)?; |
| 42 | + |
| 43 | + // Transform workspace dependencies to standalone format |
| 44 | + self.transform_dependencies_to_standalone(&mut doc)?; |
| 45 | + |
| 46 | + Ok(doc.to_string()) |
| 47 | + } |
| 48 | + |
| 49 | + /// Transform a Cargo.toml from split (standalone) format back to workspace format |
| 50 | + /// |
| 51 | + /// This is currently a no-op since syncing from remote to mono doesn't need transformation. |
| 52 | + /// The crate in the monorepo already uses workspace format. |
| 53 | + pub fn transform_to_mono(&self, content: &str, _context: &TransformContext) -> RailResult<String> { |
| 54 | + // For now, pass through unchanged. If we need to restore workspace.dependencies |
| 55 | + // references, we can implement that here. |
| 56 | + Ok(content.to_string()) |
| 57 | + } |
| 58 | + |
| 59 | + /// Resolve workspace inheritance (workspace = true fields) to actual values |
| 60 | + fn resolve_workspace_inheritance(&self, doc: &mut DocumentMut, workspace_root: &std::path::Path) -> RailResult<()> { |
| 61 | + // Load workspace Cargo.toml to get [workspace.package] values |
| 62 | + let workspace_toml_path = workspace_root.join("Cargo.toml"); |
| 63 | + let workspace_doc = manifest_ops::read_toml_file(&workspace_toml_path)?; |
| 64 | + |
| 65 | + // Get workspace.package table if it exists |
| 66 | + let workspace_package = workspace_doc |
| 67 | + .get("workspace") |
| 68 | + .and_then(|w| w.as_table()) |
| 69 | + .and_then(|w| w.get("package")) |
| 70 | + .and_then(|p| p.as_table()); |
| 71 | + |
| 72 | + // Use manifest_ops to resolve package inheritance |
| 73 | + if let Some(workspace_pkg) = workspace_package { |
| 74 | + manifest_ops::resolve_package_workspace_inheritance(doc, workspace_pkg)?; |
| 75 | + } |
| 76 | + |
| 77 | + Ok(()) |
| 78 | + } |
| 79 | + |
| 80 | + /// Transform workspace dependencies to standalone format |
| 81 | + fn transform_dependencies_to_standalone(&self, doc: &mut DocumentMut) -> RailResult<()> { |
| 82 | + // Transform each dependency section using manifest_ops |
| 83 | + manifest_ops::transform_dependencies_in_section(doc, "dependencies", |name, item| { |
| 84 | + self.transform_and_resolve_dep(item, name) |
| 85 | + })?; |
| 86 | + |
| 87 | + manifest_ops::transform_dependencies_in_section(doc, "dev-dependencies", |name, item| { |
| 88 | + self.transform_and_resolve_dep(item, name) |
| 89 | + })?; |
| 90 | + |
| 91 | + manifest_ops::transform_dependencies_in_section(doc, "build-dependencies", |name, item| { |
| 92 | + self.transform_and_resolve_dep(item, name) |
| 93 | + })?; |
| 94 | + |
| 95 | + Ok(()) |
| 96 | + } |
| 97 | + |
| 98 | + /// Helper: transform and resolve a single dependency |
| 99 | + fn transform_and_resolve_dep(&self, dep_item: &mut Item, dep_name: &str) -> RailResult<()> { |
| 100 | + // Check if this is a workspace dependency using manifest_ops |
| 101 | + if manifest_ops::is_workspace_dep(dep_item) { |
| 102 | + // Find the dependency version in workspace metadata |
| 103 | + if let Some(pkg) = self.metadata.packages.iter().find(|p| p.name == dep_name) { |
| 104 | + let version = pkg.version.to_string(); |
| 105 | + |
| 106 | + // Remove workspace marker and set version using manifest_ops |
| 107 | + manifest_ops::extract_workspace_marker(dep_item); |
| 108 | + manifest_ops::set_version(dep_item, &version)?; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + // Remove path dependencies (they won't be valid in split repo) |
| 113 | + // This applies to both workspace and non-workspace dependencies |
| 114 | + manifest_ops::remove_path(dep_item); |
| 115 | + |
| 116 | + Ok(()) |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +#[cfg(test)] |
| 121 | +mod tests { |
| 122 | + use super::*; |
| 123 | + |
| 124 | + #[test] |
| 125 | + fn test_transform_workspace_dep() { |
| 126 | + let input = r#" |
| 127 | +[package] |
| 128 | +name = "test-crate" |
| 129 | +version = "0.1.0" |
| 130 | +
|
| 131 | +[dependencies] |
| 132 | +other-crate = { workspace = true } |
| 133 | +serde = "1.0" |
| 134 | +"#; |
| 135 | + |
| 136 | + // For testing, we'll just verify it parses and doesn't crash |
| 137 | + let doc: DocumentMut = input.parse().unwrap(); |
| 138 | + assert!(doc.get("dependencies").is_some()); |
| 139 | + } |
| 140 | + |
| 141 | + #[test] |
| 142 | + fn test_transform_to_mono_passthrough() { |
| 143 | + let input = r#" |
| 144 | +[package] |
| 145 | +name = "test-crate" |
| 146 | +version = "0.1.0" |
| 147 | +
|
| 148 | +[dependencies] |
| 149 | +serde = "1.0" |
| 150 | +"#; |
| 151 | + |
| 152 | + // Create a minimal metadata for testing |
| 153 | + let metadata_json = serde_json::json!({ |
| 154 | + "packages": [], |
| 155 | + "workspace_members": [], |
| 156 | + "resolve": null, |
| 157 | + "target_directory": "/tmp", |
| 158 | + "version": 1, |
| 159 | + "workspace_root": "/tmp", |
| 160 | + "metadata": null |
| 161 | + }); |
| 162 | + |
| 163 | + let metadata: Metadata = serde_json::from_value(metadata_json).unwrap(); |
| 164 | + let transformer = CargoTransform::new(metadata); |
| 165 | + let context = TransformContext { |
| 166 | + crate_name: "test-crate".to_string(), |
| 167 | + workspace_root: PathBuf::from("/tmp"), |
| 168 | + }; |
| 169 | + |
| 170 | + let result = transformer.transform_to_mono(input, &context).unwrap(); |
| 171 | + assert_eq!(result, input); |
| 172 | + } |
| 173 | +} |
0 commit comments