import re import sys def parse_toml_deps(content): """Extract dependency versions from TOML content.""" deps = {} in_deps = False for line in content.split("\n"): if line.strip() == "[dependencies]": in_deps = True continue if in_deps and line.strip().startswith("["): break if in_deps and "=" in line and not line.strip().startswith("#"): # Parse: package = "version" or package = { version = "version", ... } match = re.match(r'^\s*(\S+)\s*=\s*["{].*version\s*=\s*"([^"]+)"', line) if match: deps[match.group(1)] = match.group(2) else: match = re.match(r'^\s*(\S+)\s*=\s*"([^"]+)"', line) if match: deps[match.group(1)] = match.group(2) return deps def update_template_versions(template_content, new_versions): """Update versions in template while preserving Liquid syntax.""" lines = template_content.split("\n") result = [] for line in lines: updated_line = line # Skip Liquid control flow lines if re.match(r"^\s*{%", line): result.append(line) continue # Check if this line has a dependency with version for dep_name, new_version in new_versions.items(): # Match: package = "version" pattern1 = rf'^(\s*{re.escape(dep_name)}\s*=\s*")([^"]+)(".*)' match = re.match(pattern1, updated_line) if match: updated_line = f"{match.group(1)}{new_version}{match.group(3)}" break # Match: package = { version = "version", ... } pattern2 = rf'^(\s*{re.escape(dep_name)}\s*=\s*{{[^}}]*version\s*=\s*")([^"]+)(".*)' match = re.match(pattern2, updated_line) if match: updated_line = f"{match.group(1)}{new_version}{match.group(3)}" break result.append(updated_line) return "\n".join(result) if __name__ == "__main__": # Read files with open(sys.argv[1], "r") as f: upgraded_content = f.read() with open(sys.argv[2], "r") as f: template_content = f.read() # Extract new versions from upgraded Cargo.toml new_versions = parse_toml_deps(upgraded_content) print(f"Found {len(new_versions)} dependencies:") for dep, version in sorted(new_versions.items()): print(f" {dep} -> {version}") # Update template while preserving Liquid syntax updated_template = update_template_versions(template_content, new_versions) # Write updated template with open(sys.argv[2], "w") as f: f.write(updated_template) print("\n✅ Template updated successfully!")