2016-02-04 18:29:27 +00:00
|
|
|
/**
|
2016-02-04 22:58:45 +00:00
|
|
|
* @fileoverview Utility to translate test files to CommonJS imports.
|
|
|
|
*
|
|
|
|
* This is a somewhat hacky tool designed to do one very specific thing.
|
|
|
|
* All of the test files in *_test.js are written with Closure-style
|
|
|
|
* imports (goog.require()). This works great for running the tests
|
|
|
|
* against Closure-style generated code, but we also want to run the
|
|
|
|
* tests against CommonJS-style generated code without having to fork
|
|
|
|
* the tests.
|
|
|
|
*
|
|
|
|
* Closure-style imports import each individual type by name. This is
|
|
|
|
* very different than CommonJS imports which are by file. So we put
|
|
|
|
* special comments in these tests like:
|
|
|
|
*
|
|
|
|
* // CommonJS-LoadFromFile: test_pb
|
|
|
|
* goog.require('proto.jspb.test.CloneExtension');
|
|
|
|
* goog.require('proto.jspb.test.Complex');
|
|
|
|
* goog.require('proto.jspb.test.DefaultValues');
|
|
|
|
*
|
|
|
|
* This script parses that special comment and uses it to generate proper
|
|
|
|
* CommonJS require() statements so that the tests can run and pass using
|
2016-02-18 01:06:46 +00:00
|
|
|
* CommonJS imports. The script will change the above statements into:
|
|
|
|
*
|
|
|
|
* var test_pb = require('test_pb');
|
|
|
|
* googleProtobuf.exportSymbol('proto.jspb.test.CloneExtension', test_pb.CloneExtension, global);
|
|
|
|
* googleProtobuf.exportSymbol('proto.jspb.test.Complex', test_pb.Complex, global);
|
|
|
|
* googleProtobuf.exportSymbol('proto.jspb.test.DefaultValues', test_pb.DefaultValues, global);
|
|
|
|
*
|
|
|
|
* (The "exportSymbol" function will define the given names in the global
|
|
|
|
* namespace, taking care not to overwrite any previous value for
|
|
|
|
* "proto.jspb.test").
|
2016-02-04 18:29:27 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
var lineReader = require('readline').createInterface({
|
|
|
|
input: process.stdin,
|
|
|
|
output: process.stdout
|
|
|
|
});
|
|
|
|
|
2016-02-05 00:11:07 +00:00
|
|
|
function tryStripPrefix(str, prefix) {
|
|
|
|
if (str.lastIndexOf(prefix) !== 0) {
|
|
|
|
throw "String: " + str + " didn't start with: " + prefix;
|
|
|
|
}
|
|
|
|
return str.substr(prefix.length);
|
|
|
|
}
|
|
|
|
|
2016-02-04 18:29:27 +00:00
|
|
|
var module = null;
|
2016-02-05 00:11:07 +00:00
|
|
|
var pkg = null;
|
2016-02-04 18:29:27 +00:00
|
|
|
lineReader.on('line', function(line) {
|
2016-02-18 01:06:46 +00:00
|
|
|
var isRequire = line.match(/goog\.require\('([^']*)'\)/);
|
|
|
|
var isLoadFromFile = line.match(/CommonJS-LoadFromFile: (\S*) (.*)/);
|
|
|
|
var isSetTestOnly = line.match(/goog.setTestOnly()/);
|
|
|
|
if (isRequire) {
|
2016-02-04 18:29:27 +00:00
|
|
|
if (module) { // Skip goog.require() lines before the first directive.
|
2016-02-18 01:06:46 +00:00
|
|
|
var fullSym = isRequire[1];
|
|
|
|
var sym = tryStripPrefix(fullSym, pkg);
|
|
|
|
console.log("googleProtobuf.exportSymbol('" + fullSym + "', " + module + sym + ', global);');
|
2016-02-04 18:29:27 +00:00
|
|
|
}
|
2016-02-18 01:06:46 +00:00
|
|
|
} else if (isLoadFromFile) {
|
2016-02-04 18:29:27 +00:00
|
|
|
if (!module) {
|
2016-02-16 23:29:49 +00:00
|
|
|
console.log("var googleProtobuf = require('google-protobuf');");
|
2016-02-04 18:29:27 +00:00
|
|
|
console.log("var asserts = require('closure_asserts_commonjs');");
|
|
|
|
console.log("var global = Function('return this')();");
|
|
|
|
console.log("");
|
|
|
|
console.log("// Bring asserts into the global namespace.");
|
2016-02-16 23:29:49 +00:00
|
|
|
console.log("googleProtobuf.object.extend(global, asserts);");
|
2016-02-04 18:29:27 +00:00
|
|
|
}
|
2016-02-18 01:06:46 +00:00
|
|
|
module = isLoadFromFile[1].replace("-", "_");
|
|
|
|
pkg = isLoadFromFile[2];
|
2016-02-04 18:29:27 +00:00
|
|
|
|
2016-02-16 23:29:49 +00:00
|
|
|
if (module != "googleProtobuf") { // We unconditionally require this in the header.
|
2016-02-18 01:06:46 +00:00
|
|
|
console.log("var " + module + " = require('" + isLoadFromFile[1] + "');");
|
2016-02-04 18:29:27 +00:00
|
|
|
}
|
2016-02-18 01:06:46 +00:00
|
|
|
} else if (!isSetTestOnly) { // Remove goog.setTestOnly() lines.
|
2016-02-04 18:29:27 +00:00
|
|
|
console.log(line);
|
|
|
|
}
|
|
|
|
});
|