Merge pull request #36 from tschaub/require

Use Rhino's require implementation.
This commit is contained in:
Michael Mathews 2011-09-22 23:20:23 -07:00
commit a9574d413b
22 changed files with 2301 additions and 2555 deletions

2
jsdoc
View File

@ -3,6 +3,6 @@
# rhino discards the path to the current script file, so we must add it back
PWD=`pwd`
BASEDIR=`dirname $0`
java -classpath ${BASEDIR}/lib/js.jar org.mozilla.javascript.tools.shell.Main ${BASEDIR}/jsdoc.js --dirname=${PWD}/${BASEDIR} $@
java -classpath ${BASEDIR}/lib/js.jar org.mozilla.javascript.tools.shell.Main -modules ${BASEDIR}/node_modules -modules ${BASEDIR}/rhino_modules ${BASEDIR}/jsdoc.js --dirname=${PWD}/${BASEDIR} $@
#java -classpath ${BASEDIR}/lib/js.jar org.mozilla.javascript.tools.debugger.Main -debug ${BASEDIR}/jsdoc.js --dirname=${PWD}/${BASEDIR} $@

View File

@ -30,7 +30,6 @@ for (var i = 0; i < arguments.length; i++) {
}
}
load(__dirname + '/lib/require.js');
load(__dirname + '/lib/rhino-shim.js');
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//

View File

@ -1,222 +0,0 @@
/*
Rhino-Require is Public Domain
<http://en.wikipedia.org/wiki/Public_Domain>
The author or authors of this code dedicate any and all copyright interest
in this code to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and successors. We
intend this dedication to be an overt act of relinquishment in perpetuity of
all present and future rights to this code under copyright law.
*/
(function(global) {
var require = global.require = function(id) {
if (typeof arguments[0] !== 'string') throw 'USAGE: require(moduleId)';
var moduleContent = '',
moduleUrl;
moduleUrl = require.resolve(id);
moduleContent = '';
var file = new java.io.File(moduleUrl);
try {
var scanner = new java.util.Scanner(file).useDelimiter("\\Z");
moduleContent = String( scanner.next() );
}
catch(e) {
throw 'Unable to read file at: '+moduleUrl+', '+e;
}
if (moduleContent) {
try {
var f = new Function('require', 'exports', 'module', '__dirname', moduleContent),
exports = require.cache[moduleUrl] || {},
module = { id: id, uri: moduleUrl, exports: exports };
require._root.unshift(toDir(moduleUrl));
(function(__dirname) {
/*debug*///var lineno=1;print('\n== '+moduleUrl+' ===============\n1'+moduleContent.replace(/(\n)/g, function(m, i){return '\n'+(++lineno);}));
f.call({}, require, exports, module, __dirname);
})(require._root[0]);
require._root.shift();
}
catch(e) {
throw 'Unable to require source code from "' + moduleUrl + '": ' + e.toSource();
}
exports = module.exports || exports;
require.cache[id] = exports;
}
else {
throw 'The requested module cannot be returned: no content for id: "' + id + '" in paths: ' + require.paths.join(', ');
}
return exports;
}
require._root = [__dirname]; // the dir of the script that is calling require()
require.paths = [];
require.cache = {}; // cache module exports. Like: {id: exported}
var SLASH = Packages.java.io.File.separator;
/** Given a module id, try to find the path to the associated module.
*/
require.resolve = function(id) {
var parts = id.match(/^(\.\/|\/)?(.+)$/),
isRelative = false,
isAbsolute = false,
isInModule = false,
basename = id,
url = '';
if (parts) {
isRelative = parts[1] === './';
isAbsolute = parts[1] === '/';
isInModule = !(isRelative || isAbsolute);
basename = parts[2];
}
if (typeof basename === 'undefined') {
throw new Error('Malformed module identifier: '+id);
}
if (isAbsolute) {
rootedId = id;
}
else if (isRelative) {
var root = require._root[0],
rootedId = root + '/' + basename;
}
if (rootedId) {
if ( url = loadAsFile(rootedId) ) { return url; }
else if ( url = loadAsDir(rootedId) ) { return url; }
}
else if (isInModule) {
var url,
paths = require.paths;
for (var i = 0, len = paths.length; i < len; i++) {
rootedId = paths[i] + '/' + basename;
if ( url = loadAsFile(rootedId) ) { return url; }
else if ( url = loadAsDir(rootedId) ) { return url; }
}
if (url = findInNodemodules(require._root[0], basename, 'rhino_modules')) { return url; }
if (url = findInNodemodules(require._root[0], basename, 'node_modules')) { return url; }
}
throw new Error('Module not found: '+id);
}
function loadAsFile(id) {
if ( isFile(id) ) { return id; }
if ( isFile(id + '.js') ) { return id + '.js'; }
}
function loadAsDir(id) {
// look for the "main" property of the package.json file
if ( isFile(id + '/' + 'package.json') ) {
var packageJson = readFileSync(id + '/' + 'package.json', 'utf-8');
eval( 'packageJson = '+ packageJson);
if (packageJson.hasOwnProperty('main')) {
var main = deDotPath(id + '/' + packageJson.main);
return require.resolve(main);
}
}
if ( isFile(id + '/' + 'index.js') ) {
return id + '/' + 'index.js';
}
}
function findInNodemodules(root, id, moduleFolderName) {
var dirs = root.split('/'),
dir = '',
rootedId;
while (dirs.length) {
dir = dirs.join('/');
rootedId = dir + '/' + moduleFolderName + '/' + id;
if ( url = loadAsFile(rootedId) ) { return url; }
else if ( url = loadAsDir(rootedId) ) { return url; }
dirs.pop();
}
}
/** Given a path, return the base directory of that path.
@example toDir('/foo/bar/somefile.js'); => '/foo/bar'
*/
function toDir(path) {
var file = new java.io.File(path);
if (file.isDirectory()) {
return path;
}
var parts = path.split('/');
parts.pop();
return parts.join('/');
}
/** Returns true if the given path exists and is a file.
*/
function isFile(path) {
var file = new java.io.File(path);
if (file.isFile()) {
return true;
}
return false;
}
/** Returns true if the given path exists and is a directory.
*/
function isDir(path) {
var file = new java.io.File(path);
if (file.isDirectory()) {
return true;
}
return false;
}
/**
Resolve dots in filepaths.
*/
function deDotPath(path) {
return String(path)
.replace(/(\/|\\)[^\/\\]+\/\.\.(\/|\\)/g, '/')
.replace(/(\/|\\)\.(\/|\\|$)/g, '/');
}
function readFileSync(filename, encoding, callback) {
if (typeof arguments[1] === 'function') {
encoding = null;
callback = arguments[1];
}
encoding = encoding || java.lang.System.getProperty('file.encoding');
try {
var content = new java.util.Scanner(
new java.io.File(filename),
encoding
).useDelimiter("\\Z");
return String( content.next() );
}
catch (e) {
return '';
}
}
})(this);

View File

@ -1,10 +1,10 @@
function readFileSync(filename, encoding) {
exports.readFileSync = function(filename, encoding) {
encoding = encoding || 'utf-8';
return readFile(filename, encoding);
}
};
function readdirSync(path) {
var readdirSync = exports.readdirSync = function(path) {
var dir,
files;
@ -14,9 +14,9 @@ function readdirSync(path) {
files = dir.list();
return files;
}
};
function ls(dir, recurse, _allFiles, _path) {
var ls = exports.ls = function(dir, recurse, _allFiles, _path) {
var files,
file;
@ -56,9 +56,9 @@ function ls(dir, recurse, _allFiles, _path) {
}
return _allFiles;
}
};
function stat(path, encoding) {
var stat = exports.stat = function(path, encoding) {
var f = new java.io.File(path)
return {
isFile: function() {
@ -69,9 +69,9 @@ function stat(path, encoding) {
}
}
}
};
function mkPath(/**Array*/ path) {
exports.mkPath = function(/**Array*/ path) {
if (path.constructor != Array) path = path.split(/[\\\/]/);
var make = "";
for (var i = 0, l = path.length; i < l; i++) {
@ -80,7 +80,7 @@ function mkPath(/**Array*/ path) {
makeDir(make);
}
}
}
};
function makeDir(/**string*/ path) {
var dirPath = toDir(path);
@ -102,7 +102,7 @@ function exists(path) {
return true;
}
function toDir(path) {
var toDir = exports.toDir = function(path) {
var f = new java.io.File(path);
if (f.isDirectory()){
@ -113,9 +113,9 @@ function toDir(path) {
parts.pop();
return parts.join('/');
}
};
function copyFile(inFile, outDir, fileName) {
exports.copyFile = function(inFile, outDir, fileName) {
if (fileName == null) fileName = toFile(inFile);
outDir = toDir(outDir);
@ -131,14 +131,14 @@ function copyFile(inFile, outDir, fileName) {
}
bos.close();
bis.close();
}
};
function toFile(path) {
var parts = path.split(/[\\\/]/);
return parts.pop();
}
function writeFileSync(filename, data, encoding) {
exports.writeFileSync = function(filename, data, encoding) {
encoding = encoding || 'utf-8';
var out = new Packages.java.io.PrintWriter(
@ -155,16 +155,4 @@ function writeFileSync(filename, data, encoding) {
out.flush();
out.close();
}
}
module.exports = {
readFileSync: readFileSync,
writeFileSync: writeFileSync,
readdirSync: readdirSync,
stat: stat,
ls: ls,
mkPath: mkPath,
toDir: toDir,
copyFile: copyFile
};

View File

@ -4,7 +4,6 @@
@author Michael Mathews <micmath@gmail.com>
@license Apache License 2.0 - See file 'LICENSE.md' in this project.
*/
(function() {
// requires docs to have been indexed: docs.index must be defined here
/**
@ -66,5 +65,3 @@
}
return o;
};
})();

View File

@ -10,7 +10,7 @@
@requires jsdoc/name
@requires jsdoc/tag/dictionary
*/
(function() {
var jsdoc = {
tag: {
Tag: require('jsdoc/tag').Tag,
@ -303,5 +303,3 @@
return tagSrcs;
}
})();

View File

@ -5,7 +5,7 @@
@author Michael Mathews <micmath@gmail.com>
@license Apache License 2.0 - See file 'LICENSE.md' in this project.
*/
(function() {
var jsdoc = {
tagDictionary: require('jsdoc/tag/dictionary')
};
@ -179,4 +179,3 @@
return {longname: longname, memberof: memberof, scope: scope, name: name, variation: variation};
}
})();

View File

@ -4,7 +4,7 @@
@author Michael Mathews <micmath@gmail.com>
@license Apache License 2.0 - See file 'LICENSE.md' in this project.
*/
(function() {
var common = {
args: require('common/args')
};
@ -70,4 +70,3 @@
return ourOptions[name];
}
}
})();

View File

@ -8,7 +8,7 @@
@module jsdoc/package
@see http://wiki.commonjs.org/wiki/Packages/1.0
*/
(function() {
/**
@class
@classdesc Represents a JavaScript package.
@ -66,4 +66,3 @@
this.licenses = json.licenses;
}
})();

View File

@ -2,7 +2,7 @@
@module jsdoc/src/handlers
*/
(function() {
var currentModule = null;
/**
@ -149,4 +149,4 @@
return false;
}
}
})();

View File

@ -5,7 +5,6 @@
* @requires common/events
*/
(function() {
var Token = Packages.org.mozilla.javascript.Token,
currentParser = null,
currentSourceName = '';
@ -496,8 +495,6 @@
return commentSrc.indexOf('/***') !== 0; /*** ignore comments that start with many stars ***/
}
})();
/**
Fired whenever the parser encounters a JSDoc comment in the current source code.
@event jsdocCommentFound

View File

@ -6,7 +6,7 @@
@license Apache License 2.0 - See file 'LICENSE.md' in this project.
*/
(function() {
var common = {
mixin: require('common/util').mixin,
events: require('common/events')
@ -66,4 +66,3 @@
return filePaths;
}
})();

View File

@ -11,7 +11,7 @@
@requires jsdoc/tag/validator
@requires jsdoc/tag/type
*/
(function() {
var jsdoc = {
tag: {
@ -132,5 +132,3 @@
}
return [pname, pdesc, poptional, pdefault];
}
})();

View File

@ -3,7 +3,7 @@
@author Michael Mathews <micmath@gmail.com>
@license Apache License 2.0 - See file 'LICENSE.md' in this project.
*/
(function() {
var _synonyms = {},
_definitions = {},
_namespaces = [];
@ -68,6 +68,9 @@
require('jsdoc/tag/dictionary/definitions').defineTags(dictionary);
module.exports = dictionary;
for (var prop in dictionary) {
if (dictionary.hasOwnProperty(prop)) {
exports[prop] = dictionary[prop];
}
}
})();

View File

@ -5,7 +5,7 @@
@author Michael Mathews <micmath@gmail.com>
@license Apache License 2.0 - See file 'LICENSE.md' in this project.
*/
(function() {
/** Populate the given dictionary with all known JSDoc tag definitions.
@param {module:jsdoc/tag/dictionary} dictionary
*/
@ -577,4 +577,3 @@
if (m) { return m[1]; }
else { return ''; }
}
})();

View File

@ -5,7 +5,6 @@
@license Apache License 2.0 - See file 'LICENSE.md' in this project.
*/
(function() {
/**
@param {string} tagValue
@ -110,4 +109,3 @@
function trim(text) {
return text.replace(/^\s+|\s+$/g, '');
}
})();

View File

@ -5,7 +5,7 @@
@author Michael Mathews <micmath@gmail.com>
@license Apache License 2.0 - See file 'LICENSE.md' in this project.
*/
(function() {
var dictionary = require('jsdoc/tag/dictionary');
@ -49,4 +49,3 @@
}
TagValueNotPermittedError.prototype = Error.prototype;
})();

View File

@ -4,7 +4,7 @@
@author Michael Mathews <micmath@gmail.com>
@license Apache License 2.0 - See file 'LICENSE.md' in this project.
*/
(function() {
/**
@param {any} object
*/
@ -155,4 +155,3 @@
(typeof o.constructor !== 'undefined' && o.constructor.name === 'Object');
}
})();

17
rhino_modules/path.js Normal file
View File

@ -0,0 +1,17 @@
exports.basename = function(path) {
var parts = path.split('/');
parts.pop();
path = parts.join('/');
return path;
};
exports.existsSync = function(path) {
var file = new java.io.File(path);
if (file.isFile()) {
return true;
}
return false;
};

View File

@ -1,18 +0,0 @@
module.exports = {
basename : function(path) {
var parts = path.split('/');
parts.pop();
path = parts.join('/');
return path;
},
existsSync: function(path) {
var file = new java.io.File(path);
if (file.isFile()) {
return true;
}
return false;
}
};

4
rhino_modules/sys.js Normal file
View File

@ -0,0 +1,4 @@
exports.puts = function(str) {
print(String(str));
};

View File

@ -1,6 +0,0 @@
module.exports = {
'puts' : function(str) {
print(String(str));
}
};