Skip to content

Commit

Permalink
feat: add starter script for micro-gen (#49)
Browse files Browse the repository at this point in the history
* init

* add yargs dependency

* format

* feedback

* gts check

* plugin path

* add __dirname to PATH

* add test for starter script

* gts fix

* feedback: allow -I in command line

* remove used files

* gts fix

* npm link

* circle CI failure

* feedback

* throw  error

* copy protos to client

* gts fix

* non zero exit code

* test failure

* fix

* add error msg before exit

* loop proto.list to copy proto files

* check common gax proto list in real time

* feedback

* fix

* exec params

* format

* try catch block
  • Loading branch information
xiaozhenliu-gg5 authored and alexander-fenster committed Oct 17, 2019
1 parent eda7c44 commit 9bad51d
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 2 deletions.
1 change: 1 addition & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ jobs:
- run:
name: Run baseline tests
command: |
sudo npm link
npm test
- run:
name: Run showcase test for js users
Expand Down
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"pbjs-genfiles"
],
"bin": {
"protoc-gen-typescript_gapic": "build/src/cli.js"
"protoc-gen-typescript_gapic": "build/src/cli.js",
"gapic-generator-typescript": "build/src/start_script.js"
},
"scripts": {
"test": "c8 --reporter=lcov mocha build/test/unit",
Expand Down Expand Up @@ -53,9 +54,14 @@
"typescript": "~3.6.0"
},
"dependencies": {
"@types/fs-extra": "^8.0.1",
"@types/yargs": "^13.0.3",
"command-line-args": "^5.0.2",
"file-system": "^2.2.2",
"fs-extra": "^8.1.0",
"get-stdin": "^7.0.0",
"nunjucks": "^3.1.3",
"protobufjs": "^6.8.8"
"protobufjs": "^6.8.8",
"yargs": "^14.2.0"
}
}
96 changes: 96 additions & 0 deletions typescript/src/start_script.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#!/usr/bin/env node
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import { execFileSync } from 'child_process';
import * as path from 'path';
import { argv } from 'yargs';
import * as fs from 'fs-extra';
const fileSystem = require('file-system');

const GOOGLE_GAX_PROTOS_DIR = path.join(
__dirname,
'..',
'..',
'node_modules',
'google-gax',
'protos'
);

// Add folder of plugin to PATH
process.env['PATH'] = __dirname + path.delimiter + process.env['PATH'];

let outputDir = '';
if (argv.output_dir) {
outputDir = argv.output_dir as string;
} else {
console.error('Output directory is required: --output_dir path');
process.exit(1);
}

const protoDirs: string[] = [];
if (argv.I) {
if (Array.isArray(argv.I)) {
protoDirs.push(...argv.I);
} else {
protoDirs.push(argv.I as string);
}
}
const protoDirsArg = protoDirs.map(dir => `-I${dir}`);

const protoFiles = [];
if (Array.isArray(argv._)) {
protoFiles.push(...argv._);
} else {
protoFiles.push(argv._);
}

// run protoc command to generate client library
const protocCommand = [
`-I${GOOGLE_GAX_PROTOS_DIR}`,
`--typescript_gapic_out=${outputDir}`,
];
protocCommand.push(...protoDirsArg);
protocCommand.push(...protoFiles);
try {
execFileSync(`protoc`, protocCommand, { stdio: 'inherit' });
} catch (err) {
console.error(err.toString());
process.exit(1);
}

try {
// create protos folder to copy proto file
const copyProtoDir = path.join(outputDir, 'protos');
if (!fs.existsSync(copyProtoDir)) {
fs.mkdirSync(copyProtoDir);
}
// copy proto file to generated folder
const protoList = path.join(outputDir, 'proto.list');
fs.readFileSync(protoList)
.toString()
.split('\n')
.filter(proto => !fs.existsSync(path.join(GOOGLE_GAX_PROTOS_DIR, proto)))
.forEach(proto => {
protoDirs.forEach(dir => {
const protoFile = path.join(dir, proto);
if (fs.existsSync(protoFile)) {
fileSystem.copyFileSync(protoFile, path.join(copyProtoDir, proto));
}
});
});
} catch (err) {
console.error(err.toString());
process.exit(1);
}
63 changes: 63 additions & 0 deletions typescript/test/unit/starter_script_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import * as assert from 'assert';
import { execSync } from 'child_process';
import * as fs from 'fs';
import * as path from 'path';

const OUTPUT_DIR = path.join(__dirname, '..', '..', '..', '.client_library');
const PROTOS_DIR = path.join(process.cwd(), 'build', 'test', 'protos');
const PROTO_FILE = path.join(
PROTOS_DIR,
'google',
'showcase',
'v1beta1',
'echo.proto'
);
const CLIENT_LIBRARY_BASELINE = path.join(
__dirname,
'..',
'..',
'..',
'typescript',
'test',
'testdata',
'echo_client_baseline.ts.txt'
);

describe('StarterScriptTest', () => {
describe('use start script for generating showcase library ', () => {
it('use custom folder for generated client library.', function() {
this.timeout(10000);
fs.mkdirSync(OUTPUT_DIR);
execSync(
`gapic-generator-typescript` +
` -I${PROTOS_DIR}` +
` ${PROTO_FILE}` +
` --output_dir=${OUTPUT_DIR}`
);
const GENERATED_CLIENT_FILE = path.join(
OUTPUT_DIR,
'src',
'v1beta1',
'echo_client.ts'
);
assert.strictEqual(
fs.readFileSync(GENERATED_CLIENT_FILE).toString(),
fs.readFileSync(CLIENT_LIBRARY_BASELINE).toString()
);
});
});
});

0 comments on commit 9bad51d

Please sign in to comment.