-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathrenamePackage.py
executable file
·82 lines (69 loc) · 2.51 KB
/
renamePackage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/python
# Usage renamePackage.py package
# Ex: renamePackage.py com.demo.mobile
import os, sys
import shutil
import platform
from functools import reduce
stuffToRemove = [".gradle", ".git", ".idea", "build", "app/build", ".iml", "local.properties"]
dirChar = os.sep
args = sys.argv
if (len(args) != 2):
print("please enter a new package name")
exit()
new_package = args[1]
original_package = "com.atomicrobot.carbon"
new_package_directory = dirChar + new_package.lower().replace('.', dirChar) + dirChar
original_package_directory = dirChar + original_package.lower().replace('.', dirChar) + dirChar
#deletes files and folders
def nuke(folders):
for f in folders:
f = f.replace('/', dirChar)#Make sure it has the correct dir separator
print('Removing ' + f)
try:
if (platform.system() == 'Windows'):
os.system('rmdir /s /q ' + f)
else:
os.system('rm -rf ' + f)
except:
None
return
def refactorPackagenameInFile(file,oldPackageName, newPackageName):
#only refactor these files
if (file.endswith(".java") or file.endswith(".kt") or file.endswith(".xml") or file.endswith(".properties") or file.endswith(".txt") or file.endswith(".gradle")):
f = open(file, 'r')
contents = f.read()
f.close()
refactored = contents.replace(oldPackageName, newPackageName)
f = open(file, 'w')
f.write(refactored)
f.close()
return
def refactorAllFolders():
for root, dir, files in os.walk('app'):
for f in files:
fpath = os.path.join(root, f)
if original_package_directory in fpath:
oldPath = fpath
newPath = fpath.replace(original_package_directory,new_package_directory)
try:#attempt to make the new package directory incase it doesn't exist
os.makedirs((root+dirChar).replace(original_package_directory, new_package_directory))
except:
None
shutil.copy(oldPath, newPath)#copy the file to the new path
refactorPackagenameInFile(newPath, original_package, new_package)
else:
refactorPackagenameInFile(fpath, original_package, new_package)
for root, dir, files in os.walk('app/src'):
#only use the first iteration, we just want the immidate children of this folder
for folder in dir:
folderpath = 'app' + dirChar + 'src' + dirChar + folder + dirChar + 'java' + dirChar + 'com' + dirChar + 'atomicrobot'
shutil.rmtree(folderpath)
break
nuke(stuffToRemove)
refactorAllFolders()
os.system('git init')
os.system('git add .')
os.system('git commit -q -m "Initial import from github.com/atomicrobot/Carbon-Android"')
print('all done :)')
os.system('git log --oneline')