Skip to content

Commit

Permalink
refactor: make sure templates can be located anywhere (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-fenster authored Oct 17, 2019
1 parent 25ca492 commit f737340
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
10 changes: 7 additions & 3 deletions typescript/src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ import { API } from './schema/api';
import { processTemplates } from './templater';
import { commonPrefix } from './util';

const templateDirectory = 'templates/typescript_gapic';
const templateDirectory = path.join(
__dirname,
'..',
'..',
'templates',
'typescript_gapic'
);
// If needed, we can make it possible to load templates from different locations
// to generate code for other languages.

Expand Down Expand Up @@ -83,8 +89,6 @@ export class Generator {
const api = this.buildAPIObject();
await this.processTemplates(api);
// TODO: error handling
// console.warn(JSON.stringify(api.services, null, ' '));
// console.warn(JSON.stringify(api, null, ' '));

const outputBuffer = plugin.google.protobuf.compiler.CodeGeneratorResponse.encode(
this.response
Expand Down
7 changes: 5 additions & 2 deletions typescript/src/schema/proto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,11 @@ function pagingField(messages: MessagesMap, method: MethodDescriptorProto) {

function pagingFieldName(messages: MessagesMap, method: MethodDescriptorProto) {
const repeatedFields = pagingField(messages, method);
if (repeatedFields && repeatedFields.name) return repeatedFields.name;
else return undefined;
if (repeatedFields && repeatedFields.name) {
return repeatedFields.name;
} else {
return undefined;
}
}

function pagingResponseType(
Expand Down
17 changes: 10 additions & 7 deletions typescript/src/templater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ async function recursiveFileList(

function renderFile(
targetFilename: string,
templateFilename: string,
templateName: string,
renderParameters: {}
) {
const processed = nunjucks.render(templateFilename, renderParameters);
const processed = nunjucks.render(templateName, renderParameters);
const output = plugin.google.protobuf.compiler.CodeGeneratorResponse.File.create();
output.name = targetFilename;
output.content = processed;
Expand All @@ -55,9 +55,8 @@ function processOneTemplate(
api: API
) {
const result: plugin.google.protobuf.compiler.CodeGeneratorResponse.File[] = [];
let outputFilename = templateFilename
.substr(basePath.length + 1)
.replace(/\.njk$/, '');
const relativeTemplateName = templateFilename.substr(basePath.length + 1);
let outputFilename = relativeTemplateName.replace(/\.njk$/, '');

// Filename can have one or more variables in it that should be substituted
// with their actual values. Currently supported: $service, $version Note:
Expand All @@ -70,21 +69,25 @@ function processOneTemplate(
result.push(
renderFile(
outputFilename.replace(/\$service/, service.name!.toLowerCase()),
templateFilename,
relativeTemplateName,
{ api, commonParameters, service }
)
);
}
} else {
result.push(
renderFile(outputFilename, templateFilename, { api, commonParameters })
renderFile(outputFilename, relativeTemplateName, {
api,
commonParameters,
})
);
}

return result;
}

export async function processTemplates(basePath: string, api: API) {
nunjucks.configure(basePath);
basePath = basePath.replace(/\/*$/, '');
const templateFiles = await recursiveFileList(basePath, /^(?!_[^_]).*\.njk$/);
const result: plugin.google.protobuf.compiler.CodeGeneratorResponse.File[] = [];
Expand Down

0 comments on commit f737340

Please sign in to comment.