run.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /**
  2. * @fileOverview
  3. * A bootstrap script that creates some basic required objects
  4. * for loading other scripts.
  5. * @author Michael Mathews, micmath@gmail.com
  6. * @version $Id: run.js 756 2009-01-07 21:32:58Z micmath $
  7. */
  8. /**
  9. * @namespace Keep track of any messages from the running script.
  10. */
  11. LOG = {
  12. warn: function(msg, e) {
  13. if (JSDOC.opt.q) return;
  14. if (e) msg = e.fileName+", line "+e.lineNumber+": "+msg;
  15. msg = ">> WARNING: "+msg;
  16. LOG.warnings.push(msg);
  17. if (LOG.out) LOG.out.write(msg+"\n");
  18. else print(msg);
  19. },
  20. inform: function(msg) {
  21. if (JSDOC.opt.q) return;
  22. msg = " > "+msg;
  23. if (LOG.out) LOG.out.write(msg+"\n");
  24. else if (typeof LOG.verbose != "undefined" && LOG.verbose) print(msg);
  25. }
  26. };
  27. LOG.warnings = [];
  28. LOG.verbose = false
  29. LOG.out = undefined;
  30. /**
  31. * @class Manipulate a filepath.
  32. */
  33. function FilePath(absPath, separator) {
  34. this.slash = separator || "/";
  35. this.root = this.slash;
  36. this.path = [];
  37. this.file = "";
  38. var parts = absPath.split(/[\\\/]/);
  39. if (parts) {
  40. if (parts.length) this.root = parts.shift() + this.slash;
  41. if (parts.length) this.file = parts.pop()
  42. if (parts.length) this.path = parts;
  43. }
  44. this.path = this.resolvePath();
  45. }
  46. /** Collapse any dot-dot or dot items in a filepath. */
  47. FilePath.prototype.resolvePath = function() {
  48. var resolvedPath = [];
  49. for (var i = 0; i < this.path.length; i++) {
  50. if (this.path[i] == "..") resolvedPath.pop();
  51. else if (this.path[i] != ".") resolvedPath.push(this.path[i]);
  52. }
  53. return resolvedPath;
  54. }
  55. /** Trim off the filename. */
  56. FilePath.prototype.toDir = function() {
  57. if (this.file) this.file = "";
  58. return this;
  59. }
  60. /** Go up a directory. */
  61. FilePath.prototype.upDir = function() {
  62. this.toDir();
  63. if (this.path.length) this.path.pop();
  64. return this;
  65. }
  66. FilePath.prototype.toString = function() {
  67. return this.root
  68. + this.path.join(this.slash)
  69. + ((this.path.length > 0)? this.slash : "")
  70. + this.file;
  71. }
  72. /**
  73. * Turn a path into just the name of the file.
  74. */
  75. FilePath.fileName = function(path) {
  76. var nameStart = Math.max(path.lastIndexOf("/")+1, path.lastIndexOf("\\")+1, 0);
  77. return path.substring(nameStart);
  78. }
  79. /**
  80. * Get the extension of a filename
  81. */
  82. FilePath.fileExtension = function(filename) {
  83. return filename.split(".").pop().toLowerCase();
  84. };
  85. /**
  86. * Turn a path into just the directory part.
  87. */
  88. FilePath.dir = function(path) {
  89. var nameStart = Math.max(path.lastIndexOf("/")+1, path.lastIndexOf("\\")+1, 0);
  90. return path.substring(0, nameStart-1);
  91. }
  92. importClass(java.lang.System);
  93. /**
  94. * @namespace A collection of information about your system.
  95. */
  96. SYS = {
  97. /**
  98. * Information about your operating system: arch, name, version.
  99. * @type string
  100. */
  101. os: [
  102. new String(System.getProperty("os.arch")),
  103. new String(System.getProperty("os.name")),
  104. new String(System.getProperty("os.version"))
  105. ].join(", "),
  106. /**
  107. * Which way does your slash lean.
  108. * @type string
  109. */
  110. slash: System.getProperty("file.separator")||"/",
  111. /**
  112. * The path to the working directory where you ran java.
  113. * @type string
  114. */
  115. userDir: new String(System.getProperty("user.dir")),
  116. /**
  117. * Where is Java's home folder.
  118. * @type string
  119. */
  120. javaHome: new String(System.getProperty("java.home")),
  121. /**
  122. * The absolute path to the directory containing this script.
  123. * @type string
  124. */
  125. pwd: undefined
  126. };
  127. // jsrun appends an argument, with the path to here.
  128. if (arguments[arguments.length-1].match(/^-j=(.+)/)) {
  129. if (RegExp.$1.charAt(0) == SYS.slash || RegExp.$1.charAt(1) == ":") { // absolute path to here
  130. SYS.pwd = new FilePath(RegExp.$1).toDir().toString();
  131. }
  132. else { // relative path to here
  133. SYS.pwd = new FilePath(SYS.userDir + SYS.slash + RegExp.$1).toDir().toString();
  134. }
  135. arguments.pop();
  136. }
  137. else {
  138. print("The run.js script requires you use jsrun.jar.");
  139. quit();
  140. }
  141. // shortcut
  142. var File = Packages.java.io.File;
  143. /**
  144. * @namespace A collection of functions that deal with reading a writing to disk.
  145. */
  146. IO = {
  147. /**
  148. * Create a new file in the given directory, with the given name and contents.
  149. */
  150. saveFile: function(/**string*/ outDir, /**string*/ fileName, /**string*/ content) {
  151. var out = new Packages.java.io.PrintWriter(
  152. new Packages.java.io.OutputStreamWriter(
  153. new Packages.java.io.FileOutputStream(outDir+SYS.slash+fileName),
  154. IO.encoding
  155. )
  156. );
  157. out.write(content);
  158. out.flush();
  159. out.close();
  160. },
  161. /**
  162. * @type string
  163. */
  164. readFile: function(/**string*/ path) {
  165. if (!IO.exists(path)) {
  166. throw "File doesn't exist there: "+path;
  167. }
  168. return readFile(path, IO.encoding);
  169. },
  170. /**
  171. * @param inFile
  172. * @param outDir
  173. * @param [fileName=The original filename]
  174. */
  175. copyFile: function(/**string*/ inFile, /**string*/ outDir, /**string*/ fileName) {
  176. if (fileName == null) fileName = FilePath.fileName(inFile);
  177. var inFile = new File(inFile);
  178. var outFile = new File(outDir+SYS.slash+fileName);
  179. var bis = new Packages.java.io.BufferedInputStream(new Packages.java.io.FileInputStream(inFile), 4096);
  180. var bos = new Packages.java.io.BufferedOutputStream(new Packages.java.io.FileOutputStream(outFile), 4096);
  181. var theChar;
  182. while ((theChar = bis.read()) != -1) {
  183. bos.write(theChar);
  184. }
  185. bos.close();
  186. bis.close();
  187. },
  188. /**
  189. * Creates a series of nested directories.
  190. */
  191. mkPath: function(/**Array*/ path) {
  192. if (path.constructor != Array) path = path.split(/[\\\/]/);
  193. var make = "";
  194. for (var i = 0, l = path.length; i < l; i++) {
  195. make += path[i] + SYS.slash;
  196. if (! IO.exists(make)) {
  197. IO.makeDir(make);
  198. }
  199. }
  200. },
  201. /**
  202. * Creates a directory at the given path.
  203. */
  204. makeDir: function(/**string*/ path) {
  205. (new File(path)).mkdir();
  206. },
  207. /**
  208. * @type string[]
  209. * @param dir The starting directory to look in.
  210. * @param [recurse=1] How many levels deep to scan.
  211. * @returns An array of all the paths to files in the given dir.
  212. */
  213. ls: function(/**string*/ dir, /**number*/ recurse, _allFiles, _path) {
  214. if (_path === undefined) { // initially
  215. var _allFiles = [];
  216. var _path = [dir];
  217. }
  218. if (_path.length == 0) return _allFiles;
  219. if (recurse === undefined) recurse = 1;
  220. dir = new File(dir);
  221. if (!dir.directory) return [String(dir)];
  222. var files = dir.list();
  223. for (var f = 0; f < files.length; f++) {
  224. var file = String(files[f]);
  225. if (file.match(/^\.[^\.\/\\]/)) continue; // skip dot files
  226. if ((new File(_path.join(SYS.slash)+SYS.slash+file)).list()) { // it's a directory
  227. _path.push(file);
  228. if (_path.length-1 < recurse) IO.ls(_path.join(SYS.slash), recurse, _allFiles, _path);
  229. _path.pop();
  230. }
  231. else {
  232. _allFiles.push((_path.join(SYS.slash)+SYS.slash+file).replace(SYS.slash+SYS.slash, SYS.slash));
  233. }
  234. }
  235. return _allFiles;
  236. },
  237. /**
  238. * @type boolean
  239. */
  240. exists: function(/**string*/ path) {
  241. file = new File(path);
  242. if (file.isDirectory()){
  243. return true;
  244. }
  245. if (!file.exists()){
  246. return false;
  247. }
  248. if (!file.canRead()){
  249. return false;
  250. }
  251. return true;
  252. },
  253. /**
  254. *
  255. */
  256. open: function(/**string*/ path, /**string*/ append) {
  257. var append = true;
  258. var outFile = new File(path);
  259. var out = new Packages.java.io.PrintWriter(
  260. new Packages.java.io.OutputStreamWriter(
  261. new Packages.java.io.FileOutputStream(outFile, append),
  262. IO.encoding
  263. )
  264. );
  265. return out;
  266. },
  267. /**
  268. * Sets {@link IO.encoding}.
  269. * Encoding is used when reading and writing text to files,
  270. * and in the meta tags of HTML output.
  271. */
  272. setEncoding: function(/**string*/ encoding) {
  273. if (/ISO-8859-([0-9]+)/i.test(encoding)) {
  274. IO.encoding = "ISO8859_"+RegExp.$1;
  275. }
  276. else {
  277. IO.encoding = encoding;
  278. }
  279. },
  280. /**
  281. * @default "utf-8"
  282. * @private
  283. */
  284. encoding: "utf-8",
  285. /**
  286. * Load the given script.
  287. */
  288. include: function(relativePath) {
  289. load(SYS.pwd+relativePath);
  290. },
  291. /**
  292. * Loads all scripts from the given directory path.
  293. */
  294. includeDir: function(path) {
  295. if (!path) return;
  296. for (var lib = IO.ls(SYS.pwd+path), i = 0; i < lib.length; i++)
  297. if (/\.js$/i.test(lib[i])) load(lib[i]);
  298. }
  299. }
  300. // now run the application
  301. IO.include("frame.js");
  302. IO.include("main.js");
  303. main();