Skip to content

Rewrite your app in Rust, piece by piece, checked against the original at every step. Works with TypeScript, JavaScript, Python, Go, Ruby, Java, C, PHP, C#, Kotlin, PowerShell and Swift.

1.5 KB
Raw Blame
#!/usr/bin/env python3
"""Renames the tool: `python3 scripts/rename.py <new-name>`.

Replaces the working name in every tracked file (crate name, binary, schema ids,
`RUST_ANYTHING_*` environment variables, docs) and in tracked paths. Review with `git diff`.
"""
import os
import re
import subprocess
import sys

OLD = "rust-anything"

def main() -> None:
    if len(sys.argv) != 2 or not re.fullmatch(r"[a-z][a-z0-9-]*", sys.argv[1]):
        sys.exit("usage: rename.py <new-name>  (lowercase letters, digits, dashes)")
    new = sys.argv[1]
    files = [os.fsdecode(p) for p in subprocess.check_output(["git", "ls-files", "-z"]).split(b"\0") if p]
    for path in files:
        try:
            with open(path, encoding="utf-8") as f:
                text = f.read()
        except (UnicodeDecodeError, FileNotFoundError):
            continue
        updated = (text.replace(OLD, new)
                       .replace(OLD.upper(), new.upper().replace("-", "_"))
                       .replace(OLD.capitalize(), new.capitalize()))
        if updated != text:
            with open(path, "w", encoding="utf-8") as f:
                f.write(updated)
    for path in files:
        if OLD in path and os.path.exists(path):
            target = path.replace(OLD, new)
            os.makedirs(os.path.dirname(target) or ".", exist_ok=True)
            subprocess.check_call(["git", "mv", path, target])
    print(f"Renamed {OLD} to {new}. Review with `git diff`, then `cargo test`.")

if __name__ == "__main__":
    main()