PageRenderTime 34ms CodeModel.GetById 18ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 1ms

/tags/jsdoc_toolkit-2.0.1/jsdoc-toolkit/app/run.js

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