projectcreator.py 5.46 KB
Newer Older
Milan Irigoyen's avatar
Init  
Milan Irigoyen committed
1
2
3
4
5
#!/usr/bin/env python3

"""MMG project init.

Usage:
Milan Irigoyen's avatar
Milan Irigoyen committed
6
7
  projectcreator.py --project-name=<name> --namespace=<namespace> [--replace-only]
  projectcreator.py (-h | --help)
Milan Irigoyen's avatar
Init  
Milan Irigoyen committed
8
9
10
11
12

Options:
  -h --help     Show this screen.
  -p --project-name=<name>    Full name of the project.
  -n --namespace=<namespace>  Namespace to use for the project.
13
  --replace-only              Only replace names in files
Milan Irigoyen's avatar
Init  
Milan Irigoyen committed
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
"""


from contextlib import contextmanager
from docopt import docopt
import os
import shutil
import sys
import zipfile


# -------------------------
# CONTEXT MANAGER
# -------------------------
@contextmanager
def cwd(path):
    oldpwd=os.getcwd()
    os.chdir(path)
    try:
        yield
    finally:
        os.chdir(oldpwd)

# -------------------------
# Setup script
# -------------------------

def convertFileName(filename, templateNames, projectNames):
	newFileName = filename
	for i in range(len(templateNames)):
		newFileName = newFileName.replace(templateNames[i], projectNames[i])
	return newFileName

# Replaces project name (lower case, uppercase, and mixed) in the CMakeLists.txt file (if it exists)
# in the src directory (if it exists), and in the readme.md file (if it exists)
def replaceNamesInFile(filename, templateNames, projectNames):
	# Read the file, looking for stuff to replace
	with open(filename) as f:
		s = f.read()
		shouldReplace = False
		for name in templateNames:
			if name in s:
				shouldReplace = True
				break
		# If there is nothing to replace, return
		if not shouldReplace:
			return

	# Safely write the changed content, if found in the file
	with open(filename, 'w') as f:
		for i in range(len(templateNames)):
			s = s.replace(templateNames[i], projectNames[i])
		f.write(s)

Milan Irigoyen's avatar
Milan Irigoyen committed
68

Milan Irigoyen's avatar
Init  
Milan Irigoyen committed
69
# Traverse all sources and rename files
70
def walkSources(root, templateNames, projectNames):
Milan Irigoyen's avatar
Milan Irigoyen committed
71
	# print ("Walking " + root)
Milan Irigoyen's avatar
Init  
Milan Irigoyen committed
72
	for dir, subdirs, files in os.walk(root):
Milan Irigoyen's avatar
Milan Irigoyen committed
73

Milan Irigoyen's avatar
Init  
Milan Irigoyen committed
74
75
76
		# Rename directory if needed
		dirNew = convertFileName(dir, templateNames, projectNames)
		if (dir != dirNew):
Milan Irigoyen's avatar
Milan Irigoyen committed
77
			# print ("Renaming " + dir + " in " + dirNew)
Milan Irigoyen's avatar
Init  
Milan Irigoyen committed
78
			os.rename(dir, dirNew)
Milan Irigoyen's avatar
Milan Irigoyen committed
79
80
81
82


		# print ("Processing " + dir + " with subdirs " + str(subdirs) + " and files " + str(files))

Milan Irigoyen's avatar
Init  
Milan Irigoyen committed
83
84
85
86
87
88
89
90
		for f in files:
			# Rename file if needed
			f_new = convertFileName(f, templateNames, projectNames)
			f_new_path = os.path.join(dirNew, f_new)
			if (f != f_new):
				os.rename(os.path.join(dirNew, f), f_new_path)
			# Replace file contents
			replaceNamesInFile(f_new_path, templateNames, projectNames)
Milan Irigoyen's avatar
Milan Irigoyen committed
91

92
		for sub in subdirs:
93
			subPath = os.path.join(dirNew, sub)
Milan Irigoyen's avatar
Milan Irigoyen committed
94
95
			# print ("New dir path : " + dirNew)
			# print ("Sub path " + subPath)
96
			walkSources(subPath, templateNames, projectNames)
Milan Irigoyen's avatar
Milan Irigoyen committed
97

98
99
100
101
102
103
104
def setupGit():
	print("")
	print("--- GIT SETUP ---")
	print("--- Adding submodules ---")
	print("")
	# Setup git
	os.system("git init")
Milan Irigoyen's avatar
Milan Irigoyen committed
105
106
107
108
	#Create git ignore
	f = open(".gitignore", "w")
	f.write("build/")
	f.close()
Milan Irigoyen's avatar
Milan Irigoyen committed
109

110
	# Shared repositories
Milan Irigoyen's avatar
Milan Irigoyen committed
111
112
113
114
	os.system("git submodule add git@git.edrevan.com:mmg/mme/mmecmake.git")
	os.system("git submodule add git@git.edrevan.com:mmg/mme/mme.git shared/mme")
	os.system("git submodule add git@git.edrevan.com:mmg/mme/mmecpp.git shared/mmecpp")
	os.system("git submodule add git@git.edrevan.com:mmg/mme/sfmlutils.git shared/sfmlutils")
115
	# Third parties
Milan Irigoyen's avatar
Milan Irigoyen committed
116
117
	os.system("git submodule add git@git.edrevan.com:mmg/mme/catch.git thirdparties/catch")
	os.system("git submodule add https://github.com/jbeder/yaml-cpp.git thirdparties/yamlcpp")
118
119
120
121
122
123
124
	os.system("git submodule init")
	print("")
	print("--- Updating submodules ---")
	print("")
	# Update submodules to latest
	os.chdir("shared/mme")
	os.system("git checkout develop")
Milan Irigoyen's avatar
Milan Irigoyen committed
125
	os.chdir("../mmecpp")
126
127
128
129
130
	os.system("git checkout develop")
	os.chdir("../sfmlutils")
	os.system("git checkout develop")
	os.chdir("../../")
	os.system("python thirdparties/catch/update.py")
Milan Irigoyen's avatar
Init  
Milan Irigoyen committed
131

Milan Irigoyen's avatar
Milan Irigoyen committed
132

Milan Irigoyen's avatar
Init  
Milan Irigoyen committed
133
134
135
136
137
138
139
140
# Print script help
def printHelp():
	print("mmgprojectinit.py -p <projectname> -n <namespace>")
	print("Initializes a new project in the repository of this file. This includes :")
	print(" - Adding an configuring necessary mmg submodules")
	print(" - Downloading usual third parties")
	print(" - Downloading the game template from TODO ADRESS, and replacing template name with name given in parameter")
	print("The project name is the full project name. The namespace is the namespace to use for code files.")
Milan Irigoyen's avatar
Milan Irigoyen committed
141

Milan Irigoyen's avatar
Init  
Milan Irigoyen committed
142
143
144
def main(argv) :
	# Parsing command line arguments
	arguments = docopt(__doc__)
145

Milan Irigoyen's avatar
Init  
Milan Irigoyen committed
146
147
148
149
150
	projectName = arguments["--project-name"]
	namespace = arguments["--namespace"]
	projectNames = [projectName, projectName.lower(), projectName.upper(), namespace, namespace.capitalize()]
	templateName = "MmgProjectTemplate"
	templateNames = [templateName, templateName.lower(), templateName.upper(), "mmgtmpl", "Mmgtmpl"]
Milan Irigoyen's avatar
Milan Irigoyen committed
151

152
	# If replace only command, only do the replacing part
153
	if arguments["--replace-only"]:
154
155
156
		walkSources("src", templateNames, projectNames)
		return

Milan Irigoyen's avatar
Init  
Milan Irigoyen committed
157
158
159
160
	with cwd(os.path.dirname(os.path.realpath(__file__))) :
		print("----------------------------------")
		print(" Setuping project " + projectName)
		print("----------------------------------")
Milan Irigoyen's avatar
Milan Irigoyen committed
161

Milan Irigoyen's avatar
Milan Irigoyen committed
162
		setupGit()
Milan Irigoyen's avatar
Milan Irigoyen committed
163

Milan Irigoyen's avatar
Init  
Milan Irigoyen committed
164
165
166
167
168
169
		print("")
		print("--- TEMPLATE PROJECT SETUP ---")
		print("")
		# Replace template names in all project files
		replaceNamesInFile("CMakeLists.txt", templateNames, projectNames)
		replaceNamesInFile("readme.md", templateNames, projectNames)
Milan Irigoyen's avatar
Milan Irigoyen committed
170
		walkSources("code", templateNames, projectNames)
Milan Irigoyen's avatar
Milan Irigoyen committed
171

Milan Irigoyen's avatar
Init  
Milan Irigoyen committed
172
173
174
175
176
177
178
179
		print("")
		print("--- SETUP COMPLETE ---")
		print("You can now run cmake.")
		print("This script will now self destruct. Goodbye ^_^")
		os.remove(sys.argv[0])

if __name__ == "__main__":
	main(sys.argv[1:])