Skip to content

Commit

Permalink
Cleanup python script to rename project
Browse files Browse the repository at this point in the history
  • Loading branch information
Techwolf12 committed Mar 31, 2023
1 parent 8aa35a8 commit 7155ce5
Showing 1 changed file with 32 additions and 13 deletions.
45 changes: 32 additions & 13 deletions scripts/rename-project.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Renames the project and puts the project name in all package folders and references =========
"""
This script renames the project, it's identifiers and references.
Then moves files to the correct locations.
"""

import os
import sys
import re
from pathlib import Path

Expand All @@ -10,36 +12,50 @@
oldPackageName = "template"
oldProjectName = "TemplateAndroid"

print('Enter new project name:')
print("Enter new project name:")
newProjectName = input()

print('Enter main package name without a prefix, nl.q42.<your-package-name>:')
print("Enter main package name without a prefix, nl.q42.<your-package-name>:")
newPackageName = input()

# ========= Rename package files: =========
# ========= Rename package files:

print('\nRenaming all package folders named "' + oldPackageName + '" to "' + newProjectName + '".\n')
print(
"\nRenaming all package folders named '%s' to '%s'.\n"
% (oldPackageName, newProjectName)
)

for root, dirs, files in os.walk(folder, topdown=False):
for subDir in dirs:
if subDir.endswith(oldPackageName):
print("Renaming folder: " + os.path.join(root, subDir))
os.rename(os.path.join(root, subDir), os.path.join(root, subDir.replace(oldPackageName, newPackageName)))
os.rename(
os.path.join(root, subDir),
os.path.join(root, subDir.replace(oldPackageName, newPackageName)),
)

# ========= Rename usages in kotlin, resource and gradle files: =========

print("\nReplacing all package references in kotlin, xml and gradle files to: '" + newProjectName + "'.\n")
print(
"\nReplacing all package references in kotlin, xml and gradle files to: '%s'.\n"
% (newProjectName)
)


def replace_package_name_occurences_in_file(filename):
with open(filename, 'r') as file:
with open(filename, "r") as file:
filedata = file.read()

filedata = re.sub(re.escape("." + oldPackageName), "." + newPackageName, filedata)
filedata = re.sub(re.escape(oldProjectName), newProjectName.capitalize(), filedata)
filedata = re.sub(
re.escape("." + oldPackageName), "." + newPackageName, filedata
)
filedata = re.sub(
re.escape(oldProjectName), newProjectName.capitalize(), filedata
)

print("Updating file: " + filename)

with open(filename, 'w') as file:
with open(filename, "w") as file:
file.write(filedata)
file.close()

Expand All @@ -50,4 +66,7 @@ def replace_package_name_occurences_in_file(filename):
file_name = os.path.join(root, name)
replace_package_name_occurences_in_file(file_name)

print("\nDone renaming project to: '" + newProjectName + "' and package to 'nl.q42."+ newPackageName +"'.\n")
print(
"\nDone renaming project to: '%s' and package to 'nl.q42.%s.\n"
% (newProjectName, newPackageName)
)

0 comments on commit 7155ce5

Please sign in to comment.