/tags/jsdoc_toolkit-1.4.0/app/run.js

http://jsdoc-toolkit.googlecode.com/ · JavaScript · 153 lines · 131 code · 19 blank · 3 comment · 44 complexity · e157b19172014f759d45018a4bd9ac74 MD5 · raw file

  1. var VERBOSE = false;
  2. try {
  3. importClass(java.lang.System);
  4. }
  5. catch (e) {
  6. throw "RuntimeException: The class java.lang.System is required to run this script.";
  7. }
  8. var __DIR__ = (System.getProperty("jsdoc.dir")||System.getProperty("user.dir"))+Packages.java.io.File.separator;
  9. /** Load required libraries. */
  10. function require(lib) {
  11. var libDirs = ['', __DIR__, __DIR__+'app/', __DIR__+'../'];
  12. var libErrors = [];
  13. for(var i = 0; i < libDirs.length; i++) {
  14. try {
  15. var file = new Packages.java.io.File(libDirs[i]+lib);
  16. if(!file.exists()) {
  17. libErrors.push('Could not find: ['+(libDirs[i]+lib)+']');
  18. }
  19. else {
  20. if (VERBOSE) print("Loading: ["+(libDirs[i]+lib)+"] ...");
  21. load(libDirs[i]+lib);
  22. return;
  23. }
  24. }
  25. catch (e) {
  26. libErrors.push('Error loading: ['+(libDirs[i]+lib)+']');
  27. }
  28. }
  29. for(var i=0; i < libErrors.length; i++) {
  30. print("ERROR: ["+libErrors[i]+"]");
  31. }
  32. quit();
  33. }
  34. require("app/JsDoc.js");
  35. require("app/Util.js");
  36. JsDoc.opt = Util.getOptions(arguments, {d:'directory', t:'template', r:'recurse', x:'ext', p:'private', a:'allfunctions', A:'Allfunctions', e:'encoding', o:'out', h:'help', 'D[]':'define'});
  37. VERBOSE = JsDoc.opt.v;
  38. require("app/JsIO.js");
  39. require("app/Symbol.js");
  40. require("app/JsToke.js");
  41. require("app/JsParse.js");
  42. require("app/DocTag.js");
  43. require("app/Doclet.js");
  44. require("app/DocFile.js");
  45. require("app/JsPlate.js");
  46. /** The main function. Called automatically. */
  47. function Main() {
  48. if (JsDoc.opt.o) LOG.out = IO.open(JsDoc.opt.o, true);
  49. if (!JsDoc.opt.e) JsDoc.opt.e = "utf-8";
  50. IO.setEncoding(JsDoc.opt.e);
  51. if (JsDoc.opt.c) {
  52. eval('conf = '+IO.readFile(JsDoc.opt.c));
  53. for (var c in conf) {
  54. if (c !== "D") {
  55. JsDoc.opt[c] = conf[c];
  56. }
  57. }
  58. }
  59. if (JsDoc.opt.h || JsDoc.opt._.length == 0 || JsDoc.opt.t === true) JsDoc.usage();
  60. var ext = ["js"];
  61. if (JsDoc.opt.x) ext = JsDoc.opt.x.split(",").map(function(x) {return x.toLowerCase()});
  62. if (typeof(JsDoc.opt.r) == "boolean") JsDoc.opt.r = 10;
  63. else if (!isNaN(parseInt(JsDoc.opt.r))) JsDoc.opt.r = parseInt(JsDoc.opt.r);
  64. else JsDoc.opt.r = 1;
  65. if (JsDoc.opt.d === true || JsDoc.opt.t === true) { // like when a user enters: -d mydir
  66. LOG.warn("Option malformed.");
  67. JsDoc.usage();
  68. }
  69. else if (!JsDoc.opt.d) {
  70. JsDoc.opt.d = "js_docs_out";
  71. }
  72. JsDoc.opt.d += (JsDoc.opt.d.indexOf(IO.FileSeparator) == JsDoc.opt.d.length-1)?
  73. "" : IO.FileSeparator;
  74. LOG.inform("Creating output directory: "+JsDoc.opt.d);
  75. IO.makeDir(JsDoc.opt.d);
  76. LOG.inform("Scanning for source files: recursion set to "+JsDoc.opt.r+" subdir"+((JsDoc.opt.r==1)?"":"s")+".");
  77. function isJs(element, index, array) {
  78. var thisExt = element.split(".").pop().toLowerCase();
  79. return (ext.indexOf(thisExt) > -1); // we're only interested in files with certain extensions
  80. }
  81. var srcFiles = [];
  82. for (var d = 0; d < JsDoc.opt._.length; d++) {
  83. if (!IO.exists(JsDoc.opt._[d])) {
  84. LOG.warn("src file doesn't exist, skipping: "+JsDoc.opt._[d]);
  85. }
  86. else {
  87. srcFiles = srcFiles.concat(
  88. IO.ls(JsDoc.opt._[d], JsDoc.opt.r).filter(isJs)
  89. );
  90. }
  91. }
  92. if (srcFiles.length == 0 ) {
  93. LOG.warn("No valid files found to parse. Nothing to do.");
  94. }
  95. else {
  96. LOG.inform(srcFiles.length+" source file"+((srcFiles ==1)?"":"s")+" found:\n\t"+srcFiles.join("\n\t"));
  97. var fileGroup = JsDoc.parse(srcFiles, JsDoc.opt);
  98. var D = {};
  99. if (JsDoc.opt.D) {
  100. for (var i = 0; i < JsDoc.opt.D.length; i++) {
  101. var defineParts = JsDoc.opt.D[i].split(":", 2);
  102. D[defineParts[0]] = defineParts[1];
  103. }
  104. JsDoc.opt.D = D;
  105. }
  106. else {
  107. JsDoc.opt.D = {};
  108. }
  109. // combine any conf file D options with the commandline D options
  110. if (typeof conf != "undefined") for (var c in conf.D) {
  111. if (typeof JsDoc.opt.D[c] == "undefined") {
  112. JsDoc.opt.D[c] = conf.D[c];
  113. }
  114. }
  115. if (JsDoc.opt.t && IO.exists(JsDoc.opt.t)) {
  116. JsDoc.opt.t += (JsDoc.opt.t.indexOf(IO.FileSeparator)==JsDoc.opt.t.length-1)?
  117. "" : IO.FileSeparator;
  118. LOG.inform("Loading template: "+JsDoc.opt.t+"publish.js");
  119. require(JsDoc.opt.t+"publish.js");
  120. LOG.inform("Publishing all files...");
  121. publish(fileGroup, JsDoc.opt);
  122. LOG.inform("Finished.");
  123. }
  124. else {
  125. LOG.warn("Use the -t option to specify a template for formatting.");
  126. LOG.warn("Dumping results to stdout.");
  127. require("app/Dumper.js");
  128. print(Dumper.dump(fileGroup));
  129. }
  130. }
  131. if (LOG.out) LOG.out.close();
  132. if (LOG.warnings.length > 0) print(LOG.warnings.length+" warnings.");
  133. }
  134. Main();