zed/script/move-crate.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

54 lines
1.7 KiB
Python
Raw Normal View History

2024-11-11 23:17:36 +00:00
import sys
import os
import shutil
crate_name = sys.argv[1]
src_path = f"{crate_name}/src"
dest_path = f"zed_common/{crate_name}"
# Move the crate to zed_common
if os.path.exists(src_path):
if not os.path.exists("zed_common"):
os.makedirs("zed_common")
shutil.move(src_path, dest_path)
print(f"Moved code to zed_common")
else:
print(f"Source path {src_path} does not exist")
src_mod_file = f"{dest_path}/{crate_name}.rs"
# Rename crate_name.rs to mod.rs
if os.path.exists(src_mod_file):
os.rename(src_mod_file, f"{dest_path}/mod.rs")
print("Created new mod.rs file")
else:
print(f"Source path {dest_path}.rs does not exist")
print("Fixing imports...")
# Look through all the text files, scan for use crate:: and replace with use crate::{crate_name}
for root, dirs, files in os.walk(dest_path):
for file in files:
if file.endswith(".rs"):
with open(os.path.join(root, file), "r") as f:
content = f.read()
new_content = content.replace(f"use crate::", f"use crate::{crate_name}::")
if new_content != content:
with open(os.path.join(root, file), "w") as f:
f.write(new_content)
print("Fixed imports")
# Remove crate from workspace file
with open("../Cargo.toml", "r") as f:
lines = f.readlines()
new_lines = [line for line in lines if not (f"crates/{crate_name}" in line or f"{crate_name}.workspace = true" in line)]
if len(new_lines) != len(lines):
with open("../Cargo.toml", "w") as f:
f.writelines(new_lines)
print(f"Removed {crate_name} from workspace file")
else:
print(f"Crate {crate_name} not found in workspace file")