Skip to content

Commit

Permalink
more init script changes
Browse files Browse the repository at this point in the history
  • Loading branch information
codekansas committed Nov 2, 2023
1 parent 29ebd7c commit 95c28a1
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions initialize
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def rename_project_files(project_type: ProjectType, dry_run: bool) -> None:
def update_project_name(from_str: str, to_str: str, file_path: Path, dry_run: bool) -> None:
if file_path.exists():
if dry_run:
show_dry_run(f"Updating project name in {file_path}", grey=True)
show_dry_run(f"Updating {from_str} to {to_str} in {file_path}", grey=True)
else:
with open(file_path, "r", encoding="utf-8") as f:
file_content = f.read()
Expand All @@ -176,16 +176,27 @@ def rename_project(
if (project_root := root_dir / "project").exists():
if dry_run:
show_dry_run(f"Renaming project root to '{new_name}'")

# Moves `requirements*.txt` to `<name>/requirements*.txt`.
for file_name in ("requirements.txt", "requirements-dev.txt"):
if (requirements_txt := root_dir / file_name).exists():
show_dry_run(f"Moving {file_name} to {new_name}/{file_name}")

else:
if (new_project_root := root_dir / new_name).exists():
shutil.rmtree(new_project_root)
project_root.rename(new_project_root)

# Updates project imports.
test_root = root_dir / "tests"
for py_file in itertools.chain(new_project_root.glob("**/*.py"), test_root.glob("**/*.py")):
update_project_name("import project", f"import {new_name}", py_file, dry_run)
update_project_name("from project", f"from {new_name}", py_file, dry_run)
# Updates project imports.
test_root = root_dir / "tests"
for py_file in itertools.chain(new_project_root.glob("**/*.py"), test_root.glob("**/*.py")):
update_project_name("import project", f"import {new_name}", py_file, dry_run)
update_project_name("from project", f"from {new_name}", py_file, dry_run)

# Moves `requirements*.txt` to `<name>/requirements*.txt`.
for file_name in ("requirements.txt", "requirements-dev.txt"):
if (requirements_txt := root_dir / file_name).exists():
shutil.move(requirements_txt, new_project_root / file_name)

else:
show_warning(f"Can't find project root in {project_root}")
Expand Down Expand Up @@ -225,14 +236,6 @@ def rename_project(
update_project_name("<name>", new_name, readme_md, dry_run)
update_project_name("<description>", new_description, readme_md, dry_run)

# Moves `requirements*.txt` to `<name>/requirements*.txt`.
for file_name in ("requirements.txt", "requirements-dev.txt"):
if (requirements_txt := root_dir / file_name).exists():
if dry_run:
show_dry_run(f"Moving {file_name} to {new_name}/{file_name}")
else:
shutil.move(requirements_txt, new_project_root / file_name)


def remove_init(remove: bool, dry_run: bool) -> None:
if not remove:
Expand Down

0 comments on commit 95c28a1

Please sign in to comment.