Skip to content

Commit

Permalink
Merge pull request #10 from daviwil/convert-to-tsmorph
Browse files Browse the repository at this point in the history
Convert remaining code generators to ts-morph and remove EJS
  • Loading branch information
daviwil committed Nov 22, 2019
2 parents 7d05c9d + 5097dfe commit 0fa7d93
Show file tree
Hide file tree
Showing 31 changed files with 505 additions and 672 deletions.
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
"@azure-tools/openapi": "3.0.209",
"@azure/core-http": "^1.0.0",
"@microsoft.azure/autorest.testserver": "^2.7.0",
"@types/ejs": "^2.6.3",
"ejs": "^2.7.2",
"prettier": "^1.19.1",
"source-map-support": "^0.5.16",
"ts-morph": "^5.0.0"
Expand Down
113 changes: 74 additions & 39 deletions src/generators/clientContextFileGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,78 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { Generator } from "./generator";
import { CodeModel } from '@azure-tools/codemodel';
import { Host } from '@azure-tools/autorest-extension-base';
import * as fs from 'fs';
import * as ejs from 'ejs';
import * as namingUtils from '../utils/nameUtils';
import * as constants from '../utils/constants';
import { ClientContextFileModel } from '../models/clientContextFileModel';

export class ClientContextFileGenerator implements Generator {
templateName: string;
private codeModel:CodeModel;
private host:Host;

constructor(codeModel: CodeModel, host: Host) {
this.codeModel = codeModel;
this.host = host;
this.templateName = 'client_context_file_template.ejs';
}

getTemplate(): string {
return fs.readFileSync(`${constants.TEMPLATE_LOCATION}/${this.templateName}`, {
encoding: 'utf8'
});
}

public async process(): Promise<void> {
let clientContextFileModel = new ClientContextFileModel();
clientContextFileModel.clientContextFileName = `${namingUtils.getClientContextFileName(this.codeModel.info.title)}.ts`;
clientContextFileModel.packageName = await this.host.GetValue('package-name');
clientContextFileModel.packageVersion = await this.host.GetValue('package-version');
clientContextFileModel.contextClassName = `${namingUtils.getClientContextClassName(this.codeModel.info.title)}`;
clientContextFileModel.clientClassName = `${namingUtils.getClientClassName(this.codeModel.info.title)}`;
let template:string = this.getTemplate();
let data = ejs.render(template, { context: clientContextFileModel});
this.host.WriteFile(
`src/${clientContextFileModel.clientContextFileName}`,
data);
}
import { Project } from "ts-morph";
import { normalizeName, NameType } from "../utils/nameUtils";
import { ClientDetails } from "../models/clientDetails";
import { PackageDetails } from "../models/packageDetails";

export function generateClientContext(
clientDetails: ClientDetails,
packageDetails: PackageDetails,
project: Project
) {
const clientContextClassName = `${clientDetails.className}Context`;
const clientContextFileName = normalizeName(
clientContextClassName,
NameType.File
);

const sourceFile = project.createSourceFile(
`src/${clientContextFileName}.ts`,
undefined,
{
overwrite: true
}
);

sourceFile.addImportDeclaration({
namespaceImport: "coreHttp",
moduleSpecifier: "@azure/core-http"
});

sourceFile.addImportDeclaration({
namespaceImport: "Models",
moduleSpecifier: "./models"
});

sourceFile.addStatements([
`\n\n`,
`const packageName = "${packageDetails.name}";`,
`const packageVersion = "${packageDetails.version}";`
]);

const contextClass = sourceFile.addClass({
name: clientContextClassName,
extends: "coreHttp.ServiceClient",
isExported: true
});

const classConstructor = contextClass.addConstructor({
docs: [
`Initializes a new instance of the ${clientContextClassName} class.\n
@param options The parameter options`
],
parameters: [
{
name: "options",
hasQuestionToken: true,
type: `Models.${clientDetails.className}Options`
}
]
});

// This could all be expressed as one string template, but we may need to
// optionally skip some segments based on generation options
classConstructor.addStatements([
`if (!options) {
options = {};
}`,
`if (!options.userAgent) {
const defaultUserAgent = coreHttp.getDefaultUserAgentValue();
options.userAgent = \`\${packageName}/\${packageVersion} \${defaultUserAgent}\`;
}\n`,
`super(undefined, options);\n\n`,
`this.baseUri = options.baseUri || this.baseUri || "http://localhost:3000";
this.requestContentType = "application/json; charset=utf-8";`
]);
}
134 changes: 87 additions & 47 deletions src/generators/clientFileGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,53 +1,93 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { CodeModel } from '@azure-tools/codemodel';
import { Host } from '@azure-tools/autorest-extension-base';
import * as ejs from 'ejs';
import * as fs from 'fs';
import * as constants from '../utils/constants';
import * as namingUtils from '../utils/nameUtils';
import { Generator } from "./generator";
import { ClientFileModel, OperationGroupName } from "../models/clientFileModel";

export class ClientGenerator implements Generator{
templateName: string;
private codeModel:CodeModel;
private host:Host;

constructor(codeModel: CodeModel, host: Host) {
this.codeModel = codeModel;
this.host = host;
this.templateName = 'client_template.ejs';
}

getTemplate(): string {
return fs.readFileSync(`${constants.TEMPLATE_LOCATION}/${this.templateName}`, {
encoding: 'utf8'
});
}

public async process(): Promise<void> {
let clientFileModel = new ClientFileModel();

clientFileModel.clientFileName = `${namingUtils.getClientFileName(this.codeModel.info.title)}.ts`;
clientFileModel.clientClassName = `${namingUtils.getClientClassName(this.codeModel.info.title)}`;
clientFileModel.clientContextClassName = `${namingUtils.getClientContextClassName(this.codeModel.info.title)}`;
clientFileModel.clientContextFileName = `${namingUtils.getClientContextFileName(this.codeModel.info.title)}`;
clientFileModel.modelsName = `${namingUtils.getModelsName(this.codeModel.info.title)}`;
clientFileModel.mappersName = `${namingUtils.getMappersName(this.codeModel.info.title)}`;

this.codeModel.operationGroups.forEach(operationGroup => {
if(operationGroup.$key.length > 0) {
let og = new OperationGroupName();
og.operationGroupName = operationGroup.$key;
og.operationGroupReferenceName = namingUtils.getCamelCaseWithUpperCaseBeginning(operationGroup.$key);
clientFileModel.operationGroupsNameMapper.push(og);
import { Project } from "ts-morph";
import { ClientDetails } from "../models/clientDetails";
import { getModelsName, getMappersName } from "../utils/nameUtils";

export function generateClient(clientDetails: ClientDetails, project: Project) {
const modelsName = getModelsName(clientDetails.className);
const mappersName = getMappersName(clientDetails.className);
const clientContextClassName = `${clientDetails.className}Context`;
const clientContextFileName = `${clientDetails.sourceFileName}Context`;

const clientFile = project.createSourceFile(
`src/${clientDetails.sourceFileName}.ts`,
undefined,
{
overwrite: true
}
);

clientFile.addImportDeclaration({
namespaceImport: "Models",
moduleSpecifier: "./models"
});

clientFile.addImportDeclaration({
namespaceImport: "Mappers",
moduleSpecifier: "./models/mappers"
});

clientFile.addImportDeclaration({
namespaceImport: "operations",
moduleSpecifier: "./operations"
});

clientFile.addImportDeclaration({
namedImports: [clientContextClassName],
moduleSpecifier: `./${clientDetails.sourceFileName}Context`
});

const clientClass = clientFile.addClass({
name: clientDetails.className,
extends: clientContextClassName
});

clientClass.addProperties([
// TODO: Generate these based on operation groups list
// {
// name: "string",
// type: "operations.String",
// leadingTrivia: writer => writer.write("// Operation groups")
// },
// { name: "enumModel", type: "operations.EnumModel" }
]);

const clientConstructor = clientClass.addConstructor({
docs: [
// TODO: Parameter list will need to be generated based on real
// client parameter list.
`Initializes a new instance of the ${clientDetails.className} class.
@param options The parameter options`
],
parameters: [
{
name: "options",
hasQuestionToken: true,
type: `Models.${clientDetails.className}Options`
}
})
]
});

clientConstructor.addStatements([
"super(options);"
// TODO: Generate these based on operation groups list
// "this.string = new operations.String(this);",
// "this.enumModel = new operations.EnumModel(this);"
]);

clientFile.addExportDeclaration({
leadingTrivia: writer => writer.write("// Operation Specifications\n\n"),
namedExports: [
{ name: clientDetails.className },
{ name: clientContextClassName },
{ name: "Models", alias: modelsName },
{ name: "Mappers", alias: mappersName }
]
});

let template:string = this.getTemplate();
let data = ejs.render(template, { client: clientFileModel});
this.host.WriteFile(`src/${clientFileModel.clientFileName}`, data);
}
clientFile.addExportDeclaration({
moduleSpecifier: "./operations"
});
}
6 changes: 0 additions & 6 deletions src/generators/generator.ts

This file was deleted.

5 changes: 1 addition & 4 deletions src/generators/mappersGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ import { CodeModel } from "@azure-tools/codemodel";
import { transformMapper } from "../mapperTransforms";
import { Project, VariableDeclarationKind } from "ts-morph";

export async function generateMappers(
codeModel: CodeModel,
project: Project
): Promise<void> {
export function generateMappers(codeModel: CodeModel, project: Project) {
const mappers = (codeModel.schemas.objects || []).map(transformMapper);
const mappersFile = project.createSourceFile(
"src/models/mappers.ts",
Expand Down
5 changes: 1 addition & 4 deletions src/generators/modelsGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ import { CodeModel } from "@azure-tools/codemodel";
import { transformCodeModel } from "../transforms";
import { Project, PropertySignatureStructure, StructureKind } from "ts-morph";

export async function generateModels(
codeModel: CodeModel,
project: Project
): Promise<void> {
export function generateModels(codeModel: CodeModel, project: Project) {
const clientDetails = transformCodeModel(codeModel);

const modelsIndexFile = project.createSourceFile(
Expand Down
54 changes: 29 additions & 25 deletions src/generators/static/licenseFileGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,34 @@
import { Generator } from "../generator";
import { CodeModel } from '@azure-tools/codemodel';
import { Host } from '@azure-tools/autorest-extension-base';
import * as constants from '../../utils/constants';
import * as fs from 'fs';
import * as ejs from 'ejs';
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

export class LicenseFileGenerator implements Generator {
templateName: string;
private codeModel:CodeModel;
private host:Host;
import { Project } from "ts-morph";

constructor(codeModel: CodeModel, host: Host) {
this.codeModel = codeModel;
this.host = host;
this.templateName = 'license_template.ejs';
}
const mitLicenseText = `
The MIT License (MIT)
getTemplate(): string {
return fs.readFileSync(`${constants.TEMPLATE_LOCATION}/static/${this.templateName}`, {
encoding: 'utf8'
});
}
Copyright (c) ${new Date().getFullYear()} Microsoft
public async process(): Promise<void> {
let template:string = this.getTemplate();
let data = ejs.render(template);
this.host.WriteFile(`LICENSE.txt`, data);
}
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
`;

export function generateLicenseFile(project: Project) {
project.createSourceFile("LICENSE.txt", mitLicenseText.trim(), {
overwrite: true
});
}
Loading

0 comments on commit 0fa7d93

Please sign in to comment.