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 $
10 * @namespace Keep track of any messages from the running script.
13 warn
: function(msg
, e
) {
14 if (JSDOC
.opt
.q
) return;
15 if (e
) msg
= e
.fileName
+", line "+e
.lineNumber
+": "+msg
;
17 msg
= ">> WARNING: "+msg
;
18 LOG
.warnings
.push(msg
);
19 if (LOG
.out
) LOG
.out
.write(msg
+"\n");
23 inform
: function(msg
) {
24 if (JSDOC
.opt
.q
) return;
26 if (LOG
.out
) LOG
.out
.write(msg
+"\n");
27 else if (typeof LOG
.verbose
!= "undefined" && LOG
.verbose
) print(msg
);
35 * @class Manipulate a filepath.
37 function FilePath(absPath
, separator
) {
38 this.slash
= separator
|| "/";
39 this.root
= this.slash
;
43 var parts
= absPath
.split(/[\\\/]/);
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
;
50 this.path
= this.resolvePath();
53 /** Collapse any dot-dot or dot items in a filepath. */
54 FilePath
.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
]);
63 /** Trim off the filename. */
64 FilePath
.prototype.toDir
= function() {
65 if (this.file
) this.file
= "";
69 /** Go up a directory. */
70 FilePath
.prototype.upDir
= function() {
72 if (this.path
.length
) this.path
.pop();
76 FilePath
.prototype.toString
= function() {
78 + this.path
.join(this.slash
)
79 + ((this.path
.length
> 0)? this.slash
: "")
84 * Turn a path into just the name of the file.
86 FilePath
.fileName
= function(path
) {
87 var nameStart
= Math
.max(path
.lastIndexOf("/")+1, path
.lastIndexOf("\\")+1, 0);
88 return path
.substring(nameStart
);
92 * Get the extension of a filename
94 FilePath
.fileExtension
= function(filename
) {
95 return filename
.split(".").pop().toLowerCase();
99 * Turn a path into just the directory part.
101 FilePath
.dir
= function(path
) {
102 var nameStart
= Math
.max(path
.lastIndexOf("/")+1, path
.lastIndexOf("\\")+1, 0);
103 return path
.substring(0, nameStart
-1);
107 importClass(java
.lang
.System
);
110 * @namespace A collection of information about your system.
114 * Information about your operating system: arch, name, version.
118 new String(System
.getProperty("os.arch")),
119 new String(System
.getProperty("os.name")),
120 new String(System
.getProperty("os.version"))
124 * Which way does your slash lean.
127 slash
: System
.getProperty("file.separator")||"/",
130 * The path to the working directory where you ran java.
133 userDir
: new String(System
.getProperty("user.dir")),
136 * Where is Java's home folder.
139 javaHome
: new String(System
.getProperty("java.home")),
142 * The absolute path to the directory containing this script.
148 // jsrun appends an argument, with the path to here.
149 if (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();
153 else { // relative path to here
154 SYS
.pwd
= new FilePath(SYS
.userDir
+ SYS
.slash
+ RegExp
.$1).toDir().toString();
159 print("The run.js script requires you use jsrun.jar.");
164 var File
= Packages
.java
.io
.File
;
167 * @namespace A collection of functions that deal with reading a writing to disk.
172 * Create a new file in the given directory, with the given name and contents.
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
),
189 readFile
: function(/**string*/ path
) {
190 if (!IO
.exists(path
)) {
191 throw "File doesn't exist there: "+path
;
193 return readFile(path
, IO
.encoding
);
199 * @param [fileName=The original filename]
201 copyFile
: function(/**string*/ inFile
, /**string*/ outDir
, /**string*/ fileName
) {
202 if (fileName
== null) fileName
= FilePath
.fileName(inFile
);
204 var inFile
= new File(inFile
);
205 var outFile
= new File(outDir
+SYS
.slash
+fileName
);
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);
210 while ((theChar
= bis
.read()) != -1) {
218 * Creates a series of nested directories.
220 mkPath
: function(/**Array*/ path
) {
221 if (path
.constructor
!= Array
) path
= path
.split(/[\\\/]/);
223 for (var i
= 0, l
= path
.length
; i
< l
; i
++) {
224 make
+= path
[i
] + SYS
.slash
;
225 if (! IO
.exists(make
)) {
232 * Creates a directory at the given path.
234 makeDir
: function(/**string*/ path
) {
235 (new File(path
)).mkdir();
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.
244 ls
: function(/**string*/ dir
, /**number*/ recurse
, _allFiles
, _path
) {
245 if (_path
=== undefined
) { // initially
249 if (_path
.length
== 0) return _allFiles
;
250 if (recurse
=== undefined
) recurse
= 1;
253 if (!dir
.directory
) return [String(dir
)];
254 var files
= dir
.list();
256 for (var f
= 0; f
< files
.length
; f
++) {
257 var file
= String(files
[f
]);
258 if (file
.match(/^\.[^\.\/\\]/)) continue; // skip dot files
260 if ((new File(_path
.join(SYS
.slash
)+SYS
.slash
+file
)).list()) { // it's a directory
262 if (_path
.length
-1 < recurse
) IO
.ls(_path
.join(SYS
.slash
), recurse
, _allFiles
, _path
);
266 _allFiles
.push((_path
.join(SYS
.slash
)+SYS
.slash
+file
).replace(SYS
.slash
+SYS
.slash
, SYS
.slash
));
276 exists
: function(/**string*/ path
) {
277 file
= new File(path
);
279 if (file
.isDirectory()){
285 if (!file
.canRead()){
294 open
: function(/**string*/ path
, /**string*/ append
) {
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
),
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.
311 setEncoding
: function(/**string*/ encoding
) {
312 if (/ISO-8859-([0-9]+)/i.test(encoding
)) {
313 IO
.encoding
= "ISO8859_"+RegExp
.$1;
316 IO
.encoding
= encoding
;
327 * Load the given script.
329 include
: function(relativePath
) {
330 load(SYS
.pwd
+relativePath
);
334 * Loads all scripts from the given directory path.
336 includeDir
: function(path
) {
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
]);
344 // now run the application
345 IO
.include("frame.js");
346 IO
.include("main.js");