|
21 | 21 | import subprocess |
22 | 22 | import sys |
23 | 23 |
|
| 24 | + |
24 | 25 | def logger_setup(): |
25 | | - # The root logger of the hierarchy. |
26 | | - logger = logging.getLogger() |
27 | | - logger.setLevel(logging.DEBUG) |
| 26 | + # The root logger of the hierarchy. |
| 27 | + logger = logging.getLogger() |
| 28 | + logger.setLevel(logging.DEBUG) |
| 29 | + |
| 30 | + # Add a StreamHandler to log to stdout. |
| 31 | + stream_handler = logging.StreamHandler(sys.stdout) |
| 32 | + stream_handler.setLevel(logging.DEBUG) |
| 33 | + formatter = logging.Formatter( |
| 34 | + "%(asctime)s - %(name)s - %(levelname)s - %(message)s") |
| 35 | + stream_handler.setFormatter(formatter) |
| 36 | + logger.addHandler(stream_handler) |
28 | 37 |
|
29 | | - # Add a StreamHandler to log to stdout. |
30 | | - stream_handler = logging.StreamHandler(sys.stdout) |
31 | | - stream_handler.setLevel(logging.DEBUG) |
32 | | - formatter = logging.Formatter( |
33 | | - "%(asctime)s - %(name)s - %(levelname)s - %(message)s") |
34 | | - stream_handler.setFormatter(formatter) |
35 | | - logger.addHandler(stream_handler) |
| 38 | + return logger |
36 | 39 |
|
37 | | - return logger |
38 | 40 |
|
39 | 41 | def log_run(dir, logger, cmd): |
40 | | - # Logs the command. |
41 | | - logger.info(cmd) |
42 | | - # Runs the command. |
43 | | - subprocess.call(cmd, cwd=dir, shell=True) |
| 42 | + # Logs the command. |
| 43 | + logger.info(cmd) |
| 44 | + # Runs the command. |
| 45 | + subprocess.call(cmd, cwd=dir, shell=True) |
| 46 | + |
44 | 47 |
|
45 | 48 | def modify_proj_file(dir): |
46 | | - f = fileinput.FileInput(files = [os.path.join(dir,"main.cpp")], inplace = True) |
47 | | - for line in f: |
48 | | - print line.replace("AppDelegate.h", "app_delegate.h"), |
| 49 | + f = fileinput.FileInput(files=[os.path.join(dir, "main.cpp")], |
| 50 | + inplace=True) |
| 51 | + for line in f: |
| 52 | + print line.replace("AppDelegate.h", "app_delegate.h"), |
| 53 | + |
49 | 54 |
|
50 | 55 | def main(): |
51 | | - """The main function.""" |
52 | | - # The setup_demo.py script directory. |
53 | | - ROOT_DIRECTORY = os.path.dirname(os.path.abspath(__file__)) |
54 | | - |
55 | | - # Sets up the logging format and handler. |
56 | | - logger = logger_setup() |
57 | | - |
58 | | - # Directory paths. |
59 | | - game_name = "tic_tac_toe_demo" |
60 | | - game_resources_dir = os.path.join(ROOT_DIRECTORY, "game_resources") |
61 | | - game_files_dir = os.path.join(ROOT_DIRECTORY, game_name); |
62 | | - windows_proj_dir = os.path.join(game_files_dir,"proj.win32") |
63 | | - mac_proj_dir = os.path.join(game_files_dir,"proj.ios_mac","mac") |
64 | | - linux_proj_dir = os.path.join(game_files_dir,"proj.linux") |
65 | | - build_dir = os.path.join(game_files_dir,"build") |
66 | | - executable_dir = os.path.join(build_dir,"bin",game_name,"Debug") |
67 | | - |
68 | | - # Creating the cocos2d-x project. |
69 | | - log_run(ROOT_DIRECTORY, logger,"cocos new {0} -p com.DemoApp.{0} -l cpp -d .".format(game_name)) |
70 | | - |
71 | | - # Removing the default cocos2d-x project files. |
72 | | - log_run(ROOT_DIRECTORY, logger,"rm -r {0}/Classes {0}/Resources {0}/CMakeLists.txt".format(game_files_dir) ) |
73 | | - |
74 | | - # Copies the google-services.json file into the correct directory to run the executable. |
75 | | - log_run(ROOT_DIRECTORY, logger,"cp google_services/google-services.json {}".format(os.path.join(game_resources_dir,"build","bin", game_name, "Debug"))) |
76 | | - |
77 | | - # Copies the tic-tac-toe game files into the cocos2d-x project files. |
78 | | - log_run(ROOT_DIRECTORY, logger, "cp {} {} -TRv".format(game_resources_dir,game_files_dir)) |
79 | | - |
80 | | - # Changes the windows project main.cpp to include the new app_delegate header. |
81 | | - modify_proj_file(windows_proj_dir) |
82 | | - |
83 | | - # Changes directory into the build directory. |
84 | | - log_run(build_dir, logger, 'cmake .. -G "Visual Studio 16 2019" -A Win32') |
85 | | - |
86 | | - # Runs cmake with the Visual Studio 2019 as the generator and windows as the target. |
87 | | - log_run(build_dir, logger,"cmake --build .") |
88 | | - |
89 | | - # Runs the tic-tac-toe executable. |
90 | | - log_run(executable_dir, logger,'{}.exe'.format(game_name)) |
| 56 | + """The main function.""" |
| 57 | + # The setup_demo.py script directory. |
| 58 | + ROOT_DIRECTORY = os.path.dirname(os.path.abspath(__file__)) |
| 59 | + |
| 60 | + # Sets up the logging format and handler. |
| 61 | + logger = logger_setup() |
| 62 | + |
| 63 | + # Directory paths. |
| 64 | + game_name = "tic_tac_toe_demo" |
| 65 | + game_resources_dir = os.path.join(ROOT_DIRECTORY, "game_resources") |
| 66 | + game_files_dir = os.path.join(ROOT_DIRECTORY, game_name) |
| 67 | + windows_proj_dir = os.path.join(game_files_dir, "proj.win32") |
| 68 | + mac_proj_dir = os.path.join(game_files_dir, "proj.ios_mac", "mac") |
| 69 | + linux_proj_dir = os.path.join(game_files_dir, "proj.linux") |
| 70 | + build_dir = os.path.join(game_files_dir, "build") |
| 71 | + executable_dir = os.path.join(build_dir, "bin", game_name, "Debug") |
| 72 | + |
| 73 | + # Checks whether the google-services.json exists in the debug directory. |
| 74 | + if not os.path.isfile( |
| 75 | + os.path.join(ROOT_DIRECTORY, "google_services", |
| 76 | + "google-services.json")): |
| 77 | + # Runs the tic-tac-toe executable. |
| 78 | + logger.error("google_services/google-services.json is missing.") |
| 79 | + exit() |
| 80 | + |
| 81 | + # Creating the cocos2d-x project. |
| 82 | + log_run(ROOT_DIRECTORY, logger, |
| 83 | + "cocos new {0} -p com.DemoApp.{0} -l cpp -d .".format(game_name)) |
| 84 | + |
| 85 | + # Removing the default cocos2d-x project files. |
| 86 | + log_run( |
| 87 | + ROOT_DIRECTORY, logger, |
| 88 | + "rm -r {0}/Classes {0}/Resources {0}/CMakeLists.txt".format( |
| 89 | + game_files_dir)) |
| 90 | + |
| 91 | + # Copies the google-services.json file into the correct directory to run the executable. |
| 92 | + log_run( |
| 93 | + ROOT_DIRECTORY, logger, |
| 94 | + "cp google_services/google-services.json {}".format( |
| 95 | + os.path.join(game_resources_dir, "build", "bin", game_name, |
| 96 | + "Debug"))) |
| 97 | + |
| 98 | + # Copies the tic-tac-toe game files into the cocos2d-x project files. |
| 99 | + log_run(ROOT_DIRECTORY, logger, |
| 100 | + "cp {} {} -TRv".format(game_resources_dir, game_files_dir)) |
| 101 | + |
| 102 | + # Changes the windows project main.cpp to include the new app_delegate header. |
| 103 | + modify_proj_file(windows_proj_dir) |
| 104 | + |
| 105 | + # Runs cmake with the Visual Studio 2019 as the generator and windows as the target. |
| 106 | + log_run(build_dir, logger, 'cmake .. -G "Visual Studio 16 2019" -A Win32') |
| 107 | + |
| 108 | + # Builds the tic_tac_toe_demo executable. |
| 109 | + log_run(build_dir, logger, "cmake --build .") |
| 110 | + |
| 111 | + logger.info("Demo setup succeeded.") |
| 112 | + |
91 | 113 |
|
92 | 114 | # Check to see if this script is being called directly. |
93 | 115 | if __name__ == "__main__": |
94 | | - exit(main()) |
| 116 | + exit(main()) |
0 commit comments