Skip to content
Commit 7b2b9317 authored by Peter Goodspeed-Niklaus's avatar Peter Goodspeed-Niklaus
Browse files

Apply rename rules: polkadot-* -> pdot-* && *-runtime -> runtime-*

This would have been impossibly tedious by hand, so used this script:

```python3
from pathlib import Path
from typing import Optional
import toml
import subprocess

def polkadot_crates(polkadot_path: Path):
    """
    yield a sequence of polkadot crate names
    """
    for cargo_toml in polkadot_path.glob("**/Cargo.toml"):
        with cargo_toml.open() as cargo_toml_file:
            data = toml.load(cargo_toml_file)

        yield (cargo_toml.parent, data["package"]["name"])

def underscored(s: str) -> str:
    return s.replace("-", "_")

def new_name_for(name: str) -> Optional[str]:
    if name.endswith("-runtime"):
        return f"runtime-{name[:-8]}"
    if name.startswith("polkadot-"):
        return f"pdot-{name[9:]}"
    return None

def rename(polkadot_path: Path, crate: str, crate_name: str) -> None:
    new_name = new_name_for(crate_name)
    if new_name is None:
        return

    print(f'replacing {crate_name} with {new_name}')

    replacements = [
        (crate_name, new_name),
        (underscored(crate_name), underscored(new_name)),
    ]

    for (old, new) in replacements:
        if "/" in old or "/" in new:
            print(f"patterns cannot contain slashes")
            return

        output = subprocess.run(
            ["rg", "--files-with-matches", old, polkadot_path],
            stdout=subprocess.PIPE,
            text=True,
        )
        if output.stdout != "":
            # generally, we want to check the return code.
            # however, rg always exits 1 when no matches are discovered, and
            # there appears to be no way to change that behavior via flags.
            # therefore, only check it if we have some lines of output.
            output.check_returncode()

        for file in output.stdout.splitlines():
            file = file.strip()

            if file == "":
                continue

            output = subprocess.run(["sed", "-i", "-E", "-e", f"s/{old}/{new}/g", file], text=True)
            output.check_returncode()

def main():
    import argparse

    parser = argparse.ArgumentParser(
        description="interactively rename polkadot node crates"
    )
    parser.add_argument("PATH", type=Path, help="path to the polkadot repo")

    args = parser.parse_args()

    for (crate, crate_name) in sorted(polkadot_crates(args.PATH)):
        rename(args.PATH, crate, crate_name)

if __name__ == "__main__":
    main()
```
parent 4cdb3ca6
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment