BuildCommon.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. var fs = require('fs-extra');
  2. var os = require('os');
  3. var path = require("path");
  4. var glob = require('glob');
  5. var Tslint = require("tslint");
  6. var fso = require('fs');
  7. var spawnSync = require('child_process').spawnSync;
  8. var host = require("./Host");
  9. var config = require('./BuildConfig');
  10. var atomicRoot = config.atomicRoot;
  11. var jsDocFolder = config.artifactsRoot + "Build/JSDoc/";
  12. namespace('build', function() {
  13. // Linting task
  14. task('lint_typescript', {
  15. async: true
  16. }, function(fileMask, failOnError) {
  17. console.log("TSLINT: Linting files in " + fileMask);
  18. var lintConfig = JSON.parse(fs.readFileSync("./Script/tslint.json"));
  19. var tslintConfig = {
  20. configuration: lintConfig,
  21. formatter: "prose"
  22. };
  23. // lint
  24. // Since TSLint does not yet support recursively searching for files, then we need to
  25. // create a command per file. The main issue with this is that it will abort on the first error instead
  26. // of listing out all lint errors
  27. glob(fileMask, function(err, results) {
  28. var lintErrors = [];
  29. results.forEach(function(filename) {
  30. var contents = fs.readFileSync(filename, "utf8");
  31. var ll = new Tslint(filename, contents, tslintConfig);
  32. var result = ll.lint();
  33. if (result.failureCount > 0) {
  34. lintErrors.push(result.output);
  35. }
  36. });
  37. if (lintErrors.length > 0) {
  38. console.warn("TSLINT: WARNING - Lint errors detected");
  39. console.warn(lintErrors.join(''));
  40. if (failOnError) {
  41. fail("TSLint errors detected");
  42. }
  43. }
  44. complete();
  45. });
  46. });
  47. // precreate script bindgs so they can be picked up by CMake
  48. task('precreateScriptBindings', {
  49. async: true
  50. }, function(clean) {
  51. if (clean === undefined) {
  52. clean = true;
  53. }
  54. console.log("Precreating script bindings");
  55. if (clean) {
  56. common.cleanCreateDir(common.getGenScriptRootDir())
  57. }
  58. common.createGenScriptFiles();
  59. complete();
  60. });
  61. function fileExists(filePath)
  62. {
  63. try
  64. {
  65. return fs.statSync(filePath).isFile();
  66. }
  67. catch (err)
  68. {
  69. return false;
  70. }
  71. }
  72. task('genAtomicNET', {
  73. async:true
  74. }, function(platform, configuration) {
  75. if (configuration != "Debug" && configuration != "Release")
  76. configuration = "Release";
  77. // Compile AtomicNET assemblies
  78. var cmds = [];
  79. cmds.push(host.atomicTool + " net compile " + atomicRoot + "Script/AtomicNET/AtomicNETProject.json -platform " + platform + " -config " + configuration);
  80. jake.exec(cmds, function() {
  81. complete();
  82. }, {
  83. printStdout: true
  84. });
  85. })
  86. task('genscripts', {
  87. async: true
  88. }, function(force) {
  89. // default to true
  90. if (force != "true" && force != "false") {
  91. force = "true";
  92. }
  93. var anyZero = false;
  94. if (force != "true") {
  95. var filenames = common.getGenScriptFilenames();
  96. for (var i in filenames) {
  97. if (!fileExists(filenames[i]))
  98. {
  99. console.log("genscripts: file missing, regenerating script bindings: " + filenames[i]);
  100. anyZero = true;
  101. break;
  102. }
  103. var stats = fs.statSync(filenames[i]);
  104. if (stats["size"] == 0) {
  105. console.log("genscripts: file zero size, regenerating script bindings: " + filenames[i]);
  106. anyZero = true;
  107. break;
  108. }
  109. }
  110. if (!anyZero)
  111. return;
  112. }
  113. process.chdir(atomicRoot);
  114. var modules = host.getScriptModules();
  115. var bindCmd = host.atomicTool + " bind \"" + atomicRoot + "\" ";
  116. var node = host.node;
  117. var tsc = "./Build/node_modules/typescript/lib/tsc";
  118. var tslint = "./Build/node_modules/tslint/lib/tslint-cli";
  119. var dtsGenerator = "./Build/node_modules/dts-generator/bin/dts-generator";
  120. var cmds = [];
  121. for (var pkgName in modules) {
  122. cmds.push(bindCmd + "Script/Packages/" + pkgName + "/");
  123. }
  124. if (node) {
  125. // compile
  126. cmds.push(node + " " + tsc + " -p ./Script");
  127. cmds.push(node + " " + tsc + " -p ./Script/AtomicWebViewEditor");
  128. // generate combined atomic.d.ts
  129. cmds.push(node + " " + dtsGenerator + " --name Atomic --project ./Script/TypeScript --out ./Script/TypeScript/dist/Atomic.d.ts");
  130. var lintTask = jake.Task['build:lint_typescript'];
  131. lintTask.addListener('complete', function () {
  132. console.log("\n\nLint: Typescript linting complete.\n\n");
  133. jake.exec(cmds, function() {
  134. // copy some external dependencies into the editor modules directory
  135. var editorModulesDir = "./Artifacts/Build/Resources/EditorData/AtomicEditor/EditorScripts/AtomicEditor/modules";
  136. var webeditorModulesDir = "./Data/AtomicEditor/CodeEditor/source/editorCore/modules";
  137. var nodeModulesDir = "./Build/node_modules";
  138. fs.mkdirsSync(editorModulesDir);
  139. // TypeScript
  140. fs.copySync(nodeModulesDir + "/typescript/lib/typescript.js", webeditorModulesDir + "/typescript.js")
  141. // copy lib.core.d.ts into the tool data directory
  142. fs.mkdirsSync("./Artifacts/Build/Resources/EditorData/AtomicEditor/EditorScripts/AtomicEditor/TypeScriptSupport");
  143. fs.copySync("./Build/node_modules/typescript/lib/lib.es5.d.ts","./Data/AtomicEditor/TypeScriptSupport/lib.es5.d.ts");
  144. // copy the combined Atomic.d.ts to the tool data directory
  145. fs.copySync("./Script/TypeScript/dist/Atomic.d.ts","./Data/AtomicEditor/TypeScriptSupport/Atomic.d.ts")
  146. complete();
  147. }, {
  148. printStdout: true
  149. });
  150. });
  151. lintTask.invoke("{./Script/AtomicEditor/**/*.ts,./Script/AtomicWebViewEditor/**/*.ts}", false);
  152. } else {
  153. throw new Error("Node not configured for this platform: " + os.platform());
  154. }
  155. });
  156. task('gendocs', {
  157. async: true
  158. }, function() {
  159. console.log( "Generating Docs..." );
  160. fs.copySync(atomicRoot + "Build/Docs/Readme.md", jsDocFolder + "Readme.md");
  161. fs.copySync(atomicRoot + "Build/Docs/atomic-theme", jsDocFolder + "atomic-theme");
  162. var typeDoc;
  163. if (os.platform() == "win32") {
  164. // uses system node for typedoc, which should have as require npm
  165. typeDoc = "node_modules\\.bin\\typedoc.cmd";
  166. }
  167. else
  168. typeDoc = host.node + " ./node_modules/.bin/typedoc";
  169. // tsdoc is having problems when name has spaces on Windows and Linux, tried quoting/escaping
  170. // what should happen here is instead of command line use a json config file (or maybe new version of tsdoc fixes this)
  171. var name = "Atomic-Game-Engine";
  172. cmds = [
  173. "cd " + jsDocFolder + " && echo {} > package.json", // newer versions of npm require package.json to be in the folder or else it searches up the heirarchy
  174. "cd " + jsDocFolder + " && npm install typedoc",
  175. "cd " + jsDocFolder + " && " + typeDoc + " --out out " + config.atomicRoot +
  176. "Script/TypeScript/dist/Atomic.d.ts --module commonjs --includeDeclarations --mode file --theme atomic-theme --name " +
  177. name + " --readme ./Readme.md"
  178. ];
  179. jake.exec(cmds, function() {
  180. common.cleanCreateDir( config.toolDataFolder + "Docs");
  181. fs.copySync(jsDocFolder + "out", config.toolDataFolder + "Docs/JSDocs");
  182. complete();
  183. console.log( "completed installing API documentation" );
  184. }, {
  185. printStdout: true
  186. });
  187. });
  188. task('genexamples', {
  189. async: true
  190. }, function() {
  191. console.log( "Generating Examples..." );
  192. common.testRemoveDir( exampleDst);
  193. // TODO: support pulling examples from a specific branch/commit/etc
  194. var exampleSrc = atomicRoot + "Submodules/AtomicExamples/";
  195. var exampleDst = config.toolDataFolder + "AtomicExamples/";
  196. fs.copySync(exampleSrc, exampleDst);
  197. // remove the .git folder and save some space
  198. fs.removeSync( exampleDst + ".git" );
  199. complete();
  200. });
  201. }); // end of build namespace