Skip to content

Commit

Permalink
chore(deps): update dependency gts to v1 (#21)
Browse files Browse the repository at this point in the history
* chore(deps): update dependency gts to v1

* gts fix
  • Loading branch information
renovate-bot authored and alexander-fenster committed Aug 30, 2019
1 parent 06576ee commit 3715ec4
Show file tree
Hide file tree
Showing 13 changed files with 439 additions and 262 deletions.
9 changes: 8 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,17 @@ jobs:
sudo apt-get update
sudo apt-get install protobuf-compiler
- run:
name: Run tests
name: Run npm install
command: |
npm install
- run:
name: Run tests
command: |
npm test
- run:
name: Run linting
command: |
npm run lint
workflows:
version: 2
tests:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"codecov": "^3.0.4",
"espower-typescript": "^9.0.0",
"google-gax": "^1.5.0",
"gts": "^0.9.0",
"gts": "^1.0.0",
"intelli-espower-loader": "^1.0.1",
"mocha": "^6.0.0",
"power-assert": "^1.6.0",
Expand Down
8 changes: 4 additions & 4 deletions typescript/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
// See the License for the specific language governing permissions and
// limitations under the License.


import * as commandLineArgs from 'command-line-args';
import {Generator} from './generator';
import { Generator } from './generator';

async function main() {
const optionDefinitions: commandLineArgs.OptionDefinition[] =
[{name: 'descriptor', type: String}];
const optionDefinitions: commandLineArgs.OptionDefinition[] = [
{ name: 'descriptor', type: String },
];
const options = commandLineArgs(optionDefinitions);

if (options.descriptor) {
Expand Down
36 changes: 17 additions & 19 deletions typescript/src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@
// See the License for the specific language governing permissions and
// limitations under the License.


import * as getStdin from 'get-stdin';
import * as path from 'path';

import * as plugin from '../../pbjs-genfiles/plugin';

import {API} from './schema/api';
import {processTemplates} from './templater';
import {commonPrefix} from './util';
import { API } from './schema/api';
import { processTemplates } from './templater';
import { commonPrefix } from './util';

const templateDirectory = 'templates/typescript_gapic';
// If needed, we can make it possible to load templates from different locations
Expand All @@ -31,16 +30,15 @@ export class Generator {
response: plugin.google.protobuf.compiler.CodeGeneratorResponse;

constructor() {
this.request =
plugin.google.protobuf.compiler.CodeGeneratorRequest.create();
this.response =
plugin.google.protobuf.compiler.CodeGeneratorResponse.create();
this.request = plugin.google.protobuf.compiler.CodeGeneratorRequest.create();
this.response = plugin.google.protobuf.compiler.CodeGeneratorResponse.create();
}

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

addProtosToResponse() {
Expand All @@ -50,18 +48,19 @@ export class Generator {
protoFilenames.push(proto.name);
}
}
const protoList =
plugin.google.protobuf.compiler.CodeGeneratorResponse.File.create();
const protoList = plugin.google.protobuf.compiler.CodeGeneratorResponse.File.create();
protoList.name = 'proto.list';
protoList.content = protoFilenames.join('\n') + '\n';
this.response.file.push(protoList);
}

buildAPIObject(): API {
const protoFilesToGenerate = this.request.protoFile.filter(
pf => pf.name && this.request.fileToGenerate.includes(pf.name));
const packageNamesToGenerate =
protoFilesToGenerate.map(pf => pf.package || '');
pf => pf.name && this.request.fileToGenerate.includes(pf.name)
);
const packageNamesToGenerate = protoFilesToGenerate.map(
pf => pf.package || ''
);
const packageName = commonPrefix(packageNamesToGenerate).replace(/\.$/, '');
if (packageName === '') {
throw new Error('Cannot get package name to generate.');
Expand All @@ -78,8 +77,7 @@ export class Generator {
async generate() {
const fileToGenerate = this.request.fileToGenerate;

this.response =
plugin.google.protobuf.compiler.CodeGeneratorResponse.create();
this.response = plugin.google.protobuf.compiler.CodeGeneratorResponse.create();

this.addProtosToResponse();
const api = this.buildAPIObject();
Expand All @@ -88,9 +86,9 @@ export class Generator {
// console.warn(JSON.stringify(api.services, null, ' '));
// console.warn(JSON.stringify(api, null, ' '));

const outputBuffer = plugin.google.protobuf.compiler.CodeGeneratorResponse
.encode(this.response)
.finish();
const outputBuffer = plugin.google.protobuf.compiler.CodeGeneratorResponse.encode(
this.response
).finish();
process.stdout.write(outputBuffer);
}
}
54 changes: 34 additions & 20 deletions typescript/src/schema/api.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,55 @@
import * as plugin from '../../../pbjs-genfiles/plugin';

import {Naming} from './naming';
import {Proto} from './proto';
import { Naming } from './naming';
import { Proto } from './proto';

export type ProtosMap = {
[filename: string]: Proto
};
export interface ProtosMap {
[filename: string]: Proto;
}

export class API {
naming: Naming;
protos: ProtosMap;
// TODO: subpackages

constructor(
fileDescriptors: plugin.google.protobuf.IFileDescriptorProto[],
packageName: string) {
this.naming = new Naming(fileDescriptors.filter(
fd => fd.package && fd.package.startsWith(packageName)));
this.protos = fileDescriptors.filter(fd => fd.name).reduce((map, fd) => {
map[fd.name!] = new Proto(fd, packageName);
return map;
}, {} as ProtosMap);
fileDescriptors: plugin.google.protobuf.IFileDescriptorProto[],
packageName: string
) {
this.naming = new Naming(
fileDescriptors.filter(
fd => fd.package && fd.package.startsWith(packageName)
)
);
this.protos = fileDescriptors
.filter(fd => fd.name)
.reduce(
(map, fd) => {
map[fd.name!] = new Proto(fd, packageName);
return map;
},
{} as ProtosMap
);
}

get services() {
return Object.keys(this.protos)
.map(filename => this.protos[filename])
.filter(proto => proto.fileToGenerate)
.reduce((retval, proto) => {
.map(filename => this.protos[filename])
.filter(proto => proto.fileToGenerate)
.reduce(
(retval, proto) => {
retval.push(
...Object.keys(proto.services).map(name => proto.services[name]));
...Object.keys(proto.services).map(name => proto.services[name])
);
return retval;
}, [] as plugin.google.protobuf.IServiceDescriptorProto[]);
},
[] as plugin.google.protobuf.IServiceDescriptorProto[]
);
}

get filesToGenerate() {
return Object.keys(this.protos)
.filter(proto => this.protos[proto].fileToGenerate);
return Object.keys(this.protos).filter(
proto => this.protos[proto].fileToGenerate
);
}
}
13 changes: 7 additions & 6 deletions typescript/src/schema/naming.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as plugin from '../../../pbjs-genfiles/plugin';
import {commonPrefix} from '../util';
import { commonPrefix } from '../util';

export class Naming {
name: string;
Expand All @@ -20,16 +20,16 @@ export class Naming {

// Define the regular expression to match a version component
// (e.g. "v1", "v1beta4", etc.).
const pattern =
/^((?:[a-z0-9_.]+?)\.)?([a-z0-9_]+)(?:\.(v[0-9]+(p[0-9]+)?((alpha|beta)[0-9]+)?[^.]*))?$/;
const pattern = /^((?:[a-z0-9_.]+?)\.)?([a-z0-9_]+)(?:\.(v[0-9]+(p[0-9]+)?((alpha|beta)[0-9]+)?[^.]*))?$/;
const match = rootPackage.match(pattern);
if (!match) {
throw new Error(`Cannot parse package name ${rootPackage}.`);
}
const [, namespaces, name, version] = match;
if (!namespaces) {
throw new Error(`Cannot parse package name ${
rootPackage}: namespace is not defined.`);
throw new Error(
`Cannot parse package name ${rootPackage}: namespace is not defined.`
);
}
this.name = name.capitalize();
this.productName = this.name;
Expand All @@ -39,7 +39,8 @@ export class Naming {

if (!this.version && protoPackages.length > 1) {
throw new Error(
'All protos must have the same proto package up to and including the version.');
'All protos must have the same proto package up to and including the version.'
);
}
}
}
Loading

0 comments on commit 3715ec4

Please sign in to comment.