Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: let user pass package name to the generator #148

Merged
merged 8 commits into from
Nov 20, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion templates/typescript_gapic/package.json.njk
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ limitations under the License.

-#}
{
"name": "{{ api.naming.productName.toKebabCase() }}",
"name": "{{ api.publishName}}",
xiaozhenliu-gg5 marked this conversation as resolved.
Show resolved Hide resolved
"version": "0.1.0",
"description": "{{ api.naming.productName }} client for Node.js",
"repository": "googleapis/nodejs-{{ api.naming.productName.toKebabCase() }}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ limitations under the License.
{{license.license()}}

/* eslint-disable node/no-missing-require, no-unused-vars */
const {{ api.naming.productName.toKebabCase()}} = require('{{ api.naming.productName.toKebabCase() }}');
const {{ api.naming.productName.toKebabCase()}} = require('{{ api.publishName }}');

function main() {
{%- for service in api.services %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
{{- serviceJoiner() -}}
{{- service.name.toPascalCase() + 'Client' -}}
{%- endfor -%}
} from '{{ api.naming.productName.toKebabCase() }}';
} from '{{ api.publishName }}';

function main() {
{%- for service in api.services %}
Expand Down
13 changes: 11 additions & 2 deletions typescript/src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ export class Generator {
request: plugin.google.protobuf.compiler.CodeGeneratorRequest;
response: plugin.google.protobuf.compiler.CodeGeneratorResponse;
grpcServiceConfig: plugin.grpc.service_config.ServiceConfig;
//This field is for users passing proper module name for system test.
xiaozhenliu-gg5 marked this conversation as resolved.
Show resolved Hide resolved
publishName?: string;

constructor() {
this.request = plugin.google.protobuf.compiler.CodeGeneratorRequest.create();
Expand Down Expand Up @@ -66,7 +68,7 @@ export class Generator {
}

private async readGrpcServiceConfig(parameter: string) {
const match = parameter.match(/^["']?grpc-service-config=([^"]+)["']?$/);
const match = parameter.match(/^["']?grpc-service-config=([^"]+)["']?/);
xiaozhenliu-gg5 marked this conversation as resolved.
Show resolved Hide resolved
if (!match) {
throw new Error(`Parameter ${parameter} was not recognized.`);
}
Expand All @@ -83,13 +85,19 @@ export class Generator {
);
}

private async readPublishPackageName(parameter: string) {
const match = parameter.match(/["']?package-name=([^"]+)["']?$/);
if (match && match.length > 1) this.publishName = match[1];
}

async initializeFromStdin() {
const inputBuffer = await getStdin.buffer();
this.request = plugin.google.protobuf.compiler.CodeGeneratorRequest.decode(
inputBuffer
);
if (this.request.parameter) {
await this.readGrpcServiceConfig(this.request.parameter);
await this.readPublishPackageName(this.request.parameter);
}
}

Expand Down Expand Up @@ -125,7 +133,8 @@ export class Generator {
const api = new API(
this.request.protoFile,
packageName,
this.grpcServiceConfig
this.grpcServiceConfig,
this.publishName
);
return api;
}
Expand Down
11 changes: 10 additions & 1 deletion typescript/src/schema/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,28 @@ export class API {
hostName?: string;
port?: string;
mainServiceName?: string;
//This field is for users passing roper module name for system test.
xiaozhenliu-gg5 marked this conversation as resolved.
Show resolved Hide resolved
publishName: string;
// oauth_scopes: plugin.google.protobuf.IServiceOptions.prototype[".google.api.oauthScopes"];
// TODO: subpackages

constructor(
fileDescriptors: plugin.google.protobuf.IFileDescriptorProto[],
packageName: string,
grpcServiceConfig: plugin.grpc.service_config.ServiceConfig
grpcServiceConfig: plugin.grpc.service_config.ServiceConfig,
publishName?: string
) {
this.naming = new Naming(
fileDescriptors.filter(
fd => fd.package && fd.package.startsWith(packageName)
)
);
// users specify the actual package name, if not, set it to product name.
if (publishName) {
this.publishName = publishName;
xiaozhenliu-gg5 marked this conversation as resolved.
Show resolved Hide resolved
} else {
this.publishName = this.naming.productName.toKebabCase();
}
// construct resource map
const resourceMap = getResourceMap(fileDescriptors);
// parse resource map to Proto constructor
Expand Down
6 changes: 6 additions & 0 deletions typescript/src/start_script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ const argv = yargs
.describe('output_dir', 'Path to a directory for the generated code')
.alias('grpc-service-config', 'grpc_service_config')
.describe('grpc-service-config', 'Path to gRPC service config JSON')
.alias('package-name', 'package-name')
xiaozhenliu-gg5 marked this conversation as resolved.
Show resolved Hide resolved
.describe('package-name', 'Publish package name')
.alias('common-proto-path', 'common_protos_path')
.describe(
'common_proto_path',
Expand All @@ -43,6 +45,7 @@ const argv = yargs
google/example/api/v1/api.proto`).argv;
const outputDir = argv.outputDir as string;
const grpcServiceConfig = argv.grpcServiceConfig as string | undefined;
const packageName = argv.packageName as string | undefined;

const protoDirs: string[] = [];
if (argv.I) {
Expand Down Expand Up @@ -71,6 +74,9 @@ if (grpcServiceConfig) {
`--typescript_gapic_opt="grpc-service-config=${grpcServiceConfig}"`
);
}
if (packageName) {
protocCommand.push(`--typescript_gapic_opt="package-name=${packageName}"`);
xiaozhenliu-gg5 marked this conversation as resolved.
Show resolved Hide resolved
}
protocCommand.push(...protoDirsArg);
protocCommand.push(...protoFiles);
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "texttospeech",
"name": "@google-cloud/text-to-speech",
"version": "0.1.0",
"description": "Texttospeech client for Node.js",
"repository": "googleapis/nodejs-texttospeech",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@


/* eslint-disable node/no-missing-require, no-unused-vars */
const texttospeech = require('texttospeech');
const texttospeech = require('@google-cloud/text-to-speech');

function main() {
const textToSpeechClient = new texttospeech.TextToSpeechClient();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
// ** https://github.com/googleapis/gapic-generator-typescript **
// ** All changes to this file may be overwritten. **

import {TextToSpeechClient} from 'texttospeech';
import {TextToSpeechClient} from '@google-cloud/text-to-speech';

function main() {
const textToSpeechClient = new TextToSpeechClient();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,13 @@ const BASELINE_DIR = path.join(
'texttospeech'
);

const PACKAGE_NAME = '@google-cloud/text-to-speech';
const SRCDIR = path.join(cwd, 'build', 'src');
const CLI = path.join(SRCDIR, 'cli.js');

describe('gRPC Client Config', () => {
describe('Package Name & grpc Config', () => {
describe('Generate Text-to-Speech library', () => {
it('Generated proto list should have same output with baseline.', function() {
it('Generated library name & grpc Config should be same with baseline.', function() {
this.timeout(10000);
if (fs.existsSync(OUTPUT_DIR)) {
rimraf.sync(OUTPUT_DIR);
Expand All @@ -82,6 +83,7 @@ describe('gRPC Client Config', () => {
`-I ${GOOGLE_GAX_PROTOS_DIR} ` +
`-I ${PROTOS_DIR} ` +
`--grpc-service-config=${GRPC_SERVICE_CONFIG} ` +
`--package-name=${PACKAGE_NAME} ` +
TTS_PROTO_FILE
);
assert(equalToBaseline(OUTPUT_DIR, BASELINE_DIR));
Expand Down