newProject.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. //-----------------------------------------------------------------------------
  3. // Copyright (c) 2012 GarageGames, LLC
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to
  7. // deal in the Software without restriction, including without limitation the
  8. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9. // sell copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. // IN THE SOFTWARE.
  22. //-----------------------------------------------------------------------------
  23. // Yes, I still need to comment most of the stuff in here. I tried to do a quick port over of the stuff from the bat and
  24. // skipped laying it out
  25. function copyDir( $source, $target, $exclude = array())
  26. {
  27. if ( is_dir( $source ) )
  28. {
  29. @mkdir( $target, 0777, true );
  30. $dirRef = dir( $source );
  31. while ( FALSE !== ( $entry = $dirRef->read() ) )
  32. {
  33. // Read through exclude list. If the current directory is on it, skip it
  34. $skip = false;
  35. foreach($exclude as $exclFile)
  36. {
  37. if($entry == $exclFile)
  38. {
  39. $skip = true;
  40. continue;
  41. }
  42. }
  43. if ( $entry == '.' || $entry == '..' || $skip )
  44. continue;
  45. $entryFullPath = $source . '/' . $entry;
  46. if ( is_dir( $entryFullPath ) )
  47. {
  48. copyDir( $entryFullPath, $target . '/' . $entry, $exclude );
  49. continue;
  50. }
  51. copy( $entryFullPath, $target . '/' . $entry );
  52. echo("Copying " . $entry . " to " . $target . "/\n");
  53. }
  54. $dirRef->close();
  55. }
  56. else
  57. copy( $source, $target );
  58. }
  59. // Function taken from PHP.net
  60. function rm_recursive($filepath)
  61. {
  62. if (is_dir($filepath) && !is_link($filepath))
  63. {
  64. if ($dirRef = opendir($filepath))
  65. {
  66. while (($curfile = readdir($dirRef)) !== false)
  67. {
  68. if ($curfile == '.' || $curfile == '..')
  69. continue;
  70. if (!rm_recursive($filepath.'/'.$curfile))
  71. throw new Exception($filepath.'/'.$curfile.' could not be deleted.');
  72. }
  73. closedir($dirRef);
  74. }
  75. return rmdir($filepath);
  76. }
  77. return unlink($filepath);
  78. }
  79. // Replaces all instances of the given text string from the given file
  80. function ReplaceTextInFile($file, $string, $replace)
  81. {
  82. $buf = "";
  83. $confg = file($file);
  84. foreach ($confg as $line)
  85. {
  86. $line = str_replace($string, $replace, $line);
  87. $buf .= $line;
  88. }
  89. $confg = fopen($file, "w");
  90. fwrite($confg, $buf);
  91. fclose($confg);
  92. }
  93. // Flag that notes whether a simple (non-fatal) error has occurred
  94. $withError = false;
  95. // Minimum number of passed arguments is 3
  96. if($argc < 4)
  97. exit("\nError! Invalid number of arguments! Correct format is Project_Name Torque_Root Project_Destination_Path (optional -O overwrite existing files)\n");
  98. // If there are 5 args and the last one isn't the override flag, it's a fatal error
  99. else if($argc == 5 && $argv[4] != "-O")
  100. exit("\nInvalid argument of $argv[4]! Should be -O (optional overwrite existing files flag)\n");
  101. // Define our main working paths
  102. $torqueDir = $argv[2];
  103. $newPath = $argv[3] . "/".$argv[1] . "/";
  104. $templPath = $torqueDir . "/Templates/Full/";
  105. // Aesthetics...php doesn't care
  106. $newPath = str_replace("//", "/", $newPath);
  107. $templPath = str_replace("//", "/", $templPath);
  108. // Check if template path exists (ie user didn't do something wrong)
  109. if(is_dir($templPath))
  110. {
  111. // If the destination path doesn't exist, make it
  112. if(!is_dir($argv[3]))
  113. mkdir($argv[3], 0777, true);
  114. // If the project folder exists, check for -O
  115. if(is_dir($newPath))
  116. {
  117. //If there's no -O, we exit out
  118. if($argv[4] != "-O")
  119. exit("\nProject folder already exists! Use -O as 4th argument is you wish to overwrite");
  120. // Otherwise we delete the other .exe and .torsion as they won't otherwise be overwritten
  121. else
  122. {
  123. if(!unlink($newPath . "game/" . $argv[1] . ".exe") ||
  124. !unlink($newPath . "game/" . $argv[1] . ".torsion"))
  125. {
  126. echo("\nError deleting old files " . $argv[1] . ".exe and " . $argv[1] . ".torsion\n");
  127. $withError = true;
  128. }
  129. }
  130. }
  131. // Read directory exclusion list from file (most likely provided by GUI prompt)
  132. $exclude = file($templPath . "exclude.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
  133. // By default exclude .svn, config, and buildFiles
  134. if($exclude == false)
  135. {
  136. $exclude = array(".svn", "Link");
  137. }
  138. else
  139. {
  140. $exclude[] = ".svn";
  141. $exclude[] = "Link";
  142. }
  143. copyDir($templPath, $newPath, $exclude);
  144. }
  145. // If the given template directory does not exist, we exit
  146. else{
  147. exit("\nTemplate directory does not exist at $templPath!");
  148. }
  149. //Replace references to template
  150. ReplaceTextInFile($newPath . "buildFiles/config/project.conf", "Full", $argv[1]);
  151. ReplaceTextInFile($newPath . "buildFiles/config/project.360.conf", "Full", $argv[1]);
  152. ReplaceTextInFile($newPath . "buildFiles/config/project.mac.conf", "Full", $argv[1]);
  153. ReplaceTextInFile($newPath . "game/Template.torsion", "Full", $argv[1]);
  154. ReplaceTextInFile($newPath . "game/main.cs", "Full", $argv[1]);
  155. ReplaceTextInFile($newPath . "source/torqueConfig.h", "Full", $argv[1]);
  156. //Rename the executable and the torsion project file
  157. if(!rename($newPath . "game/Full.exe", $newPath . "game/" . $argv[1] . ".exe"))
  158. {
  159. echo("\n\nCould not rename Full.exe! You may need to rename manually\n");
  160. $withError = true;
  161. }
  162. if(!rename($newPath . "game/Full.torsion", $newPath . "game/" . $argv[1] . ".torsion"))
  163. {
  164. echo("\n\nCould not rename Full.torsion! You may need to rename manually\n");
  165. $withError = true;
  166. }
  167. chdir($newPath);
  168. // Generate the projects and solutions using the generateProjects.bat
  169. // We don't hand in the $torqueDir if our output path is in the Torque folders
  170. // Probably should make this smarter so that it can handle directories that
  171. // are in the the Torque folder but are not at the same level as GameExamples and Projects
  172. if (stristr($newPath, $torqueDir))
  173. passthru("generateProjects.bat noPause");
  174. else
  175. passthru("generateProjects.bat noPause $torqueDir");
  176. // If there wasn't an error, print happy message
  177. if(!$withError)
  178. echo("\nProject creation complete!\nYou can find your new project at $newPath.");
  179. // If there was, print ultra mega sad message
  180. else
  181. echo("\nProject creation completed, however there were some errors. Please verify that your new project is correct.\nYou can find your new project at $newPath.");
  182. ?>