From 666352a1fd56585aa3c6270645791761c7854ebc Mon Sep 17 00:00:00 2001 From: Tamir Duberstein Date: Mon, 27 Mar 2017 15:53:02 -0400 Subject: [PATCH] scripts: simplify, regenerate es6 too --- google/protobuf/descriptor.json | 90 ++++++++++++++++++++++---------- scripts/gencommons.js | 4 +- scripts/gentests.js | 44 +++++++++------- tests/data/comments.js | 2 +- tests/data/convert.js | 2 +- tests/data/mapbox/vector_tile.js | 2 +- tests/data/package.js | 2 +- tests/data/rpc-es6.js | 68 ++++++++++++++---------- tests/data/rpc.js | 2 +- tests/data/test.js | 2 +- 10 files changed, 135 insertions(+), 83 deletions(-) mode change 100644 => 100755 scripts/gencommons.js mode change 100644 => 100755 scripts/gentests.js diff --git a/google/protobuf/descriptor.json b/google/protobuf/descriptor.json index c241a46c7..941209e3d 100644 --- a/google/protobuf/descriptor.json +++ b/google/protobuf/descriptor.json @@ -9,7 +9,8 @@ "file": { "rule": "repeated", "type": "FileDescriptorProto", - "id": 1 + "id": 1, + "options": {} } } }, @@ -26,7 +27,10 @@ "dependency": { "rule": "repeated", "type": "string", - "id": 3 + "id": 3, + "options": { + "packed": false + } }, "publicDependency": { "rule": "repeated", @@ -47,22 +51,26 @@ "messageType": { "rule": "repeated", "type": "DescriptorProto", - "id": 4 + "id": 4, + "options": {} }, "enumType": { "rule": "repeated", "type": "EnumDescriptorProto", - "id": 5 + "id": 5, + "options": {} }, "service": { "rule": "repeated", "type": "ServiceDescriptorProto", - "id": 6 + "id": 6, + "options": {} }, "extension": { "rule": "repeated", "type": "FieldDescriptorProto", - "id": 7 + "id": 7, + "options": {} }, "options": { "type": "FileOptions", @@ -87,32 +95,38 @@ "field": { "rule": "repeated", "type": "FieldDescriptorProto", - "id": 2 + "id": 2, + "options": {} }, "extension": { "rule": "repeated", "type": "FieldDescriptorProto", - "id": 6 + "id": 6, + "options": {} }, "nestedType": { "rule": "repeated", "type": "DescriptorProto", - "id": 3 + "id": 3, + "options": {} }, "enumType": { "rule": "repeated", "type": "EnumDescriptorProto", - "id": 4 + "id": 4, + "options": {} }, "extensionRange": { "rule": "repeated", "type": "ExtensionRange", - "id": 5 + "id": 5, + "options": {} }, "oneofDecl": { "rule": "repeated", "type": "OneofDescriptorProto", - "id": 8 + "id": 8, + "options": {} }, "options": { "type": "MessageOptions", @@ -121,12 +135,16 @@ "reservedRange": { "rule": "repeated", "type": "ReservedRange", - "id": 9 + "id": 9, + "options": {} }, "reservedName": { "rule": "repeated", "type": "string", - "id": 10 + "id": 10, + "options": { + "packed": false + } } }, "nested": { @@ -252,7 +270,8 @@ "value": { "rule": "repeated", "type": "EnumValueDescriptorProto", - "id": 2 + "id": 2, + "options": {} }, "options": { "type": "EnumOptions", @@ -285,7 +304,8 @@ "method": { "rule": "repeated", "type": "MethodDescriptorProto", - "id": 2 + "id": 2, + "options": {} }, "options": { "type": "ServiceOptions", @@ -388,7 +408,8 @@ "uninterpretedOption": { "rule": "repeated", "type": "UninterpretedOption", - "id": 999 + "id": 999, + "options": {} } }, "extensions": [ @@ -434,7 +455,8 @@ "uninterpretedOption": { "rule": "repeated", "type": "UninterpretedOption", - "id": 999 + "id": 999, + "options": {} } }, "extensions": [ @@ -485,7 +507,8 @@ "uninterpretedOption": { "rule": "repeated", "type": "UninterpretedOption", - "id": 999 + "id": 999, + "options": {} } }, "extensions": [ @@ -522,7 +545,8 @@ "uninterpretedOption": { "rule": "repeated", "type": "UninterpretedOption", - "id": 999 + "id": 999, + "options": {} } }, "extensions": [ @@ -545,7 +569,8 @@ "uninterpretedOption": { "rule": "repeated", "type": "UninterpretedOption", - "id": 999 + "id": 999, + "options": {} } }, "extensions": [ @@ -564,7 +589,8 @@ "uninterpretedOption": { "rule": "repeated", "type": "UninterpretedOption", - "id": 999 + "id": 999, + "options": {} } }, "extensions": [ @@ -583,7 +609,8 @@ "uninterpretedOption": { "rule": "repeated", "type": "UninterpretedOption", - "id": 999 + "id": 999, + "options": {} } }, "extensions": [ @@ -602,7 +629,8 @@ "uninterpretedOption": { "rule": "repeated", "type": "UninterpretedOption", - "id": 999 + "id": 999, + "options": {} } }, "extensions": [ @@ -617,7 +645,8 @@ "name": { "rule": "repeated", "type": "NamePart", - "id": 2 + "id": 2, + "options": {} }, "identifierValue": { "type": "string", @@ -666,7 +695,8 @@ "location": { "rule": "repeated", "type": "Location", - "id": 1 + "id": 1, + "options": {} } }, "nested": { @@ -699,7 +729,10 @@ "leadingDetachedComments": { "rule": "repeated", "type": "string", - "id": 6 + "id": 6, + "options": { + "packed": false + } } } } @@ -710,7 +743,8 @@ "annotation": { "rule": "repeated", "type": "Annotation", - "id": 1 + "id": 1, + "options": {} } }, "nested": { diff --git a/scripts/gencommons.js b/scripts/gencommons.js old mode 100644 new mode 100755 index 79e3faaa2..461d40ad7 --- a/scripts/gencommons.js +++ b/scripts/gencommons.js @@ -1,3 +1,5 @@ +#!/usr/bin/env node + "use strict"; var pbjs = require("../cli/pbjs"); @@ -23,4 +25,4 @@ var pbjs = require("../cli/pbjs"); throw err; process.stdout.write("pbjs: " + file + " -> " + out + "\n"); }); -}); \ No newline at end of file +}); diff --git a/scripts/gentests.js b/scripts/gentests.js old mode 100644 new mode 100755 index 0e730d6cf..5fc922545 --- a/scripts/gentests.js +++ b/scripts/gentests.js @@ -1,3 +1,5 @@ +#!/usr/bin/env node + "use strict"; var fs = require("fs"), path = require("path"), @@ -5,50 +7,54 @@ var fs = require("fs"), pbts = require("../cli/pbts"); [ - "tests/data/package.proto", - "tests/data/rpc.proto", - "tests/data/mapbox/vector_tile.proto", - "tests/data/test.proto", - "tests/data/convert.proto", - "tests/data/comments.proto" + { file: "tests/data/comments.proto", flags: [] }, + { file: "tests/data/convert.proto", flags: [] }, + { file: "tests/data/mapbox/vector_tile.proto", flags: [] }, + { file: "tests/data/package.proto", flags: [] }, + { file: "tests/data/rpc.proto", flags: ["es6"] }, + { file: "tests/data/rpc.proto", flags: [] }, + { file: "tests/data/test.proto", flags: [] }, ] -.forEach(function(file) { - var out = file.replace(/\.proto$/, ".js"); +.forEach(function({ file, flags }) { + var basename = file.replace(/\.proto$/, ""); + var out = [basename].concat(flags).join("-")+".js"; pbjs.main([ "--target", "static-module", "--wrap", "commonjs", - "--root", "test_" + path.basename(out, ".js"), - "--out", out, + "--root", "test_" + basename, file - ], function(err) { + ].concat(flags.map(function(flag) { + return "--" + flag; + })), function(err, output) { if (err) throw err; var pathToProtobufjs = path.relative(path.dirname(out), "minimal").replace(/\\/g, "/"); - fs.writeFileSync(out, fs.readFileSync(out).toString("utf8").replace(/"protobufjs\/minimal"/g, JSON.stringify(pathToProtobufjs)), "utf8"); + fs.writeFileSync(out, output.replace(/"protobufjs\/minimal"/g, JSON.stringify(pathToProtobufjs))); process.stdout.write("pbjs: " + file + " -> " + out + "\n"); try { require(path.join(__dirname, "..", out)); - } catch (err2) { - process.stderr.write("ERROR: " + err2.message + "\n"); + } catch (err) { + if (!flags.includes("es6")) { + process.stderr.write("ERROR: " + err.message + "\n"); + } } }); }); [ + "tests/data/rpc.js", "tests/data/test.js", - "tests/data/rpc.js" ] .forEach(function(file) { var out = file.replace(/\.js$/, ".d.ts"); pbts.main([ - "--out", out, "--no-comments", file - ], function(err) { + ], function(err, output) { if (err) throw err; var pathToProtobufjs = path.relative(path.dirname(out), "").replace(/\\/g, "/"); - fs.writeFileSync(out, fs.readFileSync(out).toString("utf8").replace(/"protobufjs"/g, JSON.stringify(pathToProtobufjs)), "utf8"); + fs.writeFileSync(out, output.replace(/"protobufjs"/g, JSON.stringify(pathToProtobufjs))); process.stdout.write("pbts: " + file + " -> " + out + "\n"); }); -}); \ No newline at end of file +}); diff --git a/tests/data/comments.js b/tests/data/comments.js index 019a61055..20347d67f 100644 --- a/tests/data/comments.js +++ b/tests/data/comments.js @@ -7,7 +7,7 @@ var $protobuf = require("../../minimal"); var $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.util; // Exported root namespace -var $root = $protobuf.roots.test_comments || ($protobuf.roots.test_comments = {}); +var $root = $protobuf.roots["test_tests/data/comments"] || ($protobuf.roots["test_tests/data/comments"] = {}); $root.Test1 = (function() { diff --git a/tests/data/convert.js b/tests/data/convert.js index f45274ff0..539b4f133 100644 --- a/tests/data/convert.js +++ b/tests/data/convert.js @@ -7,7 +7,7 @@ var $protobuf = require("../../minimal"); var $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.util; // Exported root namespace -var $root = $protobuf.roots.test_convert || ($protobuf.roots.test_convert = {}); +var $root = $protobuf.roots["test_tests/data/convert"] || ($protobuf.roots["test_tests/data/convert"] = {}); $root.Message = (function() { diff --git a/tests/data/mapbox/vector_tile.js b/tests/data/mapbox/vector_tile.js index df4d86a66..9a10c0548 100644 --- a/tests/data/mapbox/vector_tile.js +++ b/tests/data/mapbox/vector_tile.js @@ -7,7 +7,7 @@ var $protobuf = require("../../../minimal"); var $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.util; // Exported root namespace -var $root = $protobuf.roots.test_vector_tile || ($protobuf.roots.test_vector_tile = {}); +var $root = $protobuf.roots["test_tests/data/mapbox/vector_tile"] || ($protobuf.roots["test_tests/data/mapbox/vector_tile"] = {}); $root.vector_tile = (function() { diff --git a/tests/data/package.js b/tests/data/package.js index 723afeadb..870f2f18a 100644 --- a/tests/data/package.js +++ b/tests/data/package.js @@ -7,7 +7,7 @@ var $protobuf = require("../../minimal"); var $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.util; // Exported root namespace -var $root = $protobuf.roots.test_package || ($protobuf.roots.test_package = {}); +var $root = $protobuf.roots["test_tests/data/package"] || ($protobuf.roots["test_tests/data/package"] = {}); $root.Package = (function() { diff --git a/tests/data/rpc-es6.js b/tests/data/rpc-es6.js index 7f516666d..36848ed0d 100644 --- a/tests/data/rpc-es6.js +++ b/tests/data/rpc-es6.js @@ -1,14 +1,13 @@ /*eslint-disable block-scoped-var, no-redeclare, no-control-regex, no-prototype-builtins*/ -import * as $protobuf from "protobufjs"; +"use strict"; + +var $protobuf = require("../../minimal"); // Common aliases const $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.util; -// Lazily resolved type references -const $lazyTypes = []; - // Exported root namespace -const $root = $protobuf.roots["default"] || ($protobuf.roots["default"] = {}); +const $root = $protobuf.roots["test_tests/data/rpc"] || ($protobuf.roots["test_tests/data/rpc"] = {}); export const MyService = $root.MyService = (function() { @@ -48,7 +47,7 @@ export const MyService = $root.MyService = (function() { /** * Calls MyMethod. - * @param {MyRequest|Object} request MyRequest message or plain object + * @param {MyRequest|Object.} request MyRequest message or plain object * @param {MyService_myMethod_Callback} callback Node-style callback called with the error, if any, and MyResponse * @returns {undefined} */ @@ -60,7 +59,7 @@ export const MyService = $root.MyService = (function() { * Calls MyMethod. * @name MyService#myMethod * @function - * @param {MyRequest|Object} request MyRequest message or plain object + * @param {MyRequest|Object.} request MyRequest message or plain object * @returns {Promise} Promise * @variation 2 */ @@ -70,11 +69,18 @@ export const MyService = $root.MyService = (function() { export const MyRequest = $root.MyRequest = (function() { + /** + * Properties of a MyRequest. + * @typedef MyRequest$Properties + * @type {Object} + * @property {string} [path] MyRequest path. + */ + /** * Constructs a new MyRequest. * @exports MyRequest * @constructor - * @param {Object} [properties] Properties to set + * @param {MyRequest$Properties=} [properties] Properties to set */ function MyRequest(properties) { if (properties) @@ -90,7 +96,7 @@ export const MyRequest = $root.MyRequest = (function() { /** * Creates a new MyRequest instance using the specified properties. - * @param {Object} [properties] Properties to set + * @param {MyRequest$Properties=} [properties] Properties to set * @returns {MyRequest} MyRequest instance */ MyRequest.create = function create(properties) { @@ -99,21 +105,21 @@ export const MyRequest = $root.MyRequest = (function() { /** * Encodes the specified MyRequest message. Does not implicitly {@link MyRequest.verify|verify} messages. - * @param {MyRequest|Object} message MyRequest message or plain object to encode + * @param {MyRequest$Properties} message MyRequest message or plain object to encode * @param {$protobuf.Writer} [writer] Writer to encode to * @returns {$protobuf.Writer} Writer */ MyRequest.encode = function encode(message, writer) { if (!writer) writer = $Writer.create(); - if (message.path !== undefined && message.hasOwnProperty("path")) + if (message.path != null && message.hasOwnProperty("path")) writer.uint32(/* id 1, wireType 2 =*/10).string(message.path); return writer; }; /** * Encodes the specified MyRequest message, length delimited. Does not implicitly {@link MyRequest.verify|verify} messages. - * @param {MyRequest|Object} message MyRequest message or plain object to encode + * @param {MyRequest$Properties} message MyRequest message or plain object to encode * @param {$protobuf.Writer} [writer] Writer to encode to * @returns {$protobuf.Writer} Writer */ @@ -162,13 +168,13 @@ export const MyRequest = $root.MyRequest = (function() { /** * Verifies a MyRequest message. - * @param {MyRequest|Object} message MyRequest message or plain object to verify + * @param {Object.} message Plain object to verify * @returns {?string} `null` if valid, otherwise the reason why it is not */ MyRequest.verify = function verify(message) { if (typeof message !== "object" || message === null) return "object expected"; - if (message.path !== undefined) + if (message.path != null) if (!$util.isString(message.path)) return "path: string expected"; return null; @@ -183,7 +189,7 @@ export const MyRequest = $root.MyRequest = (function() { if (object instanceof $root.MyRequest) return object; let message = new $root.MyRequest(); - if (object.path !== undefined && object.path !== null) + if (object.path != null) message.path = String(object.path); return message; }; @@ -209,7 +215,7 @@ export const MyRequest = $root.MyRequest = (function() { let object = {}; if (options.defaults) object.path = ""; - if (message.path !== undefined && message.path !== null && message.hasOwnProperty("path")) + if (message.path != null && message.hasOwnProperty("path")) object.path = message.path; return object; }; @@ -236,11 +242,18 @@ export const MyRequest = $root.MyRequest = (function() { export const MyResponse = $root.MyResponse = (function() { + /** + * Properties of a MyResponse. + * @typedef MyResponse$Properties + * @type {Object} + * @property {number} [status] MyResponse status. + */ + /** * Constructs a new MyResponse. * @exports MyResponse * @constructor - * @param {Object} [properties] Properties to set + * @param {MyResponse$Properties=} [properties] Properties to set */ function MyResponse(properties) { if (properties) @@ -256,7 +269,7 @@ export const MyResponse = $root.MyResponse = (function() { /** * Creates a new MyResponse instance using the specified properties. - * @param {Object} [properties] Properties to set + * @param {MyResponse$Properties=} [properties] Properties to set * @returns {MyResponse} MyResponse instance */ MyResponse.create = function create(properties) { @@ -265,21 +278,21 @@ export const MyResponse = $root.MyResponse = (function() { /** * Encodes the specified MyResponse message. Does not implicitly {@link MyResponse.verify|verify} messages. - * @param {MyResponse|Object} message MyResponse message or plain object to encode + * @param {MyResponse$Properties} message MyResponse message or plain object to encode * @param {$protobuf.Writer} [writer] Writer to encode to * @returns {$protobuf.Writer} Writer */ MyResponse.encode = function encode(message, writer) { if (!writer) writer = $Writer.create(); - if (message.status !== undefined && message.hasOwnProperty("status")) + if (message.status != null && message.hasOwnProperty("status")) writer.uint32(/* id 2, wireType 0 =*/16).int32(message.status); return writer; }; /** * Encodes the specified MyResponse message, length delimited. Does not implicitly {@link MyResponse.verify|verify} messages. - * @param {MyResponse|Object} message MyResponse message or plain object to encode + * @param {MyResponse$Properties} message MyResponse message or plain object to encode * @param {$protobuf.Writer} [writer] Writer to encode to * @returns {$protobuf.Writer} Writer */ @@ -328,13 +341,13 @@ export const MyResponse = $root.MyResponse = (function() { /** * Verifies a MyResponse message. - * @param {MyResponse|Object} message MyResponse message or plain object to verify + * @param {Object.} message Plain object to verify * @returns {?string} `null` if valid, otherwise the reason why it is not */ MyResponse.verify = function verify(message) { if (typeof message !== "object" || message === null) return "object expected"; - if (message.status !== undefined) + if (message.status != null) if (!$util.isInteger(message.status)) return "status: integer expected"; return null; @@ -349,7 +362,7 @@ export const MyResponse = $root.MyResponse = (function() { if (object instanceof $root.MyResponse) return object; let message = new $root.MyResponse(); - if (object.status !== undefined && object.status !== null) + if (object.status != null) message.status = object.status | 0; return message; }; @@ -375,7 +388,7 @@ export const MyResponse = $root.MyResponse = (function() { let object = {}; if (options.defaults) object.status = 0; - if (message.status !== undefined && message.status !== null && message.hasOwnProperty("status")) + if (message.status != null && message.hasOwnProperty("status")) object.status = message.status; return object; }; @@ -400,7 +413,4 @@ export const MyResponse = $root.MyResponse = (function() { return MyResponse; })(); -// Resolve lazy type references to actual types -$util.lazyResolve($root, $lazyTypes); - -export { $root as default }; +module.exports = $root; diff --git a/tests/data/rpc.js b/tests/data/rpc.js index 791e62f58..61fa4a65e 100644 --- a/tests/data/rpc.js +++ b/tests/data/rpc.js @@ -7,7 +7,7 @@ var $protobuf = require("../../minimal"); var $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.util; // Exported root namespace -var $root = $protobuf.roots.test_rpc || ($protobuf.roots.test_rpc = {}); +var $root = $protobuf.roots["test_tests/data/rpc"] || ($protobuf.roots["test_tests/data/rpc"] = {}); $root.MyService = (function() { diff --git a/tests/data/test.js b/tests/data/test.js index 77c3e81d6..0d8499340 100644 --- a/tests/data/test.js +++ b/tests/data/test.js @@ -7,7 +7,7 @@ var $protobuf = require("../../minimal"); var $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.util; // Exported root namespace -var $root = $protobuf.roots.test_test || ($protobuf.roots.test_test = {}); +var $root = $protobuf.roots["test_tests/data/test"] || ($protobuf.roots["test_tests/data/test"] = {}); $root.jspb = (function() {