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