Skip to content

Commit

Permalink
Merge pull request #12 from joheredi/generate-operations
Browse files Browse the repository at this point in the history
Generate operations
  • Loading branch information
joheredi committed Nov 27, 2019
2 parents 0fa7d93 + 3973127 commit 5ad1230
Show file tree
Hide file tree
Showing 27 changed files with 1,322 additions and 354 deletions.
61 changes: 38 additions & 23 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,39 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "attach",
"name": "Attach",
"port": 9229
},
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}\\dist\\src\\main.js",
"preLaunchTask": "tsc: build - tsconfig.json",
"outFiles": [
"${workspaceFolder}/dist/**/*.js"
]
}
]
}
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "attach",
"name": "Attach",
"port": 9229
},
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}\\dist\\src\\main.js",
"preLaunchTask": "tsc: build - tsconfig.json",
"outFiles": ["${workspaceFolder}/dist/**/*.js"]
},
{
"type": "node",
"request": "launch",
"name": "UnitTests",
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
"args": [
"-r",
"ts-node/register",
"--timeout",
"999999",
"--colors",
"${workspaceFolder}/test/unit/**/*.spec.ts"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"protocol": "inspector"
}
]
}
12 changes: 9 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@
"scripts": {
"build": "tsc -p .",
"start": "node ./dist/src/main.js",
"test": "npm run unit-test & npm run integration-test",
"test": "npm-run-all unit-test integration-test",
"unit-test": "mocha -r ts-node/register './test/unit/**/*spec.ts'",
"integration-test": "ts-node test/utils/start-server.ts & mocha -r ts-node/register './test/integration/**/*spec.ts' & stop-autorest-testserver",
"debug": "node --inspect-brk ./dist/src/main.js"
"integration-test": "npm-run-all start-test-server generate-bodystring integration-test:alone stop-test-server",
"integration-test:alone": "mocha -r ts-node/register './test/integration/**/*spec.ts'",
"start-test-server": "ts-node test/utils/start-server.ts",
"stop-test-server": "stop-autorest-testserver",
"debug": "node --inspect-brk ./dist/src/main.js",
"generate-bodystring": "npm run build && autorest-beta --typescript --output-folder=./generated/bodyString --use=. --title=BodyStringClient --input-file=node_modules/@autorest/test-server/__files/swagger/body-string.json --package-name=bodyString --package-version=1.0.0-preview1",
"generate-bodycomplex": "npm run build && autorest-beta --typescript --output-folder=./generated/bodyComplex --use=. --title=BodyComplexClient --input-file=node_modules/@autorest/test-server/__files/swagger/body-complex.json --package-name=bodyString --package-version=1.0.0-preview1"
},
"dependencies": {
"@autorest/autorest": "^3.0.6122",
Expand Down Expand Up @@ -36,6 +41,7 @@
"mocha": "6.2.1",
"mocha-typescript": "1.1.17",
"node-cmd": "^3.0.0",
"npm-run-all": "^4.1.5",
"ts-node": "^8.5.2",
"typescript": "~3.6.2",
"wait-port": "^0.2.6"
Expand Down
2 changes: 1 addition & 1 deletion src/generators/clientContextFileGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export function generateClientContext(
{
name: "options",
hasQuestionToken: true,
type: `Models.${clientDetails.className}Options`
type: "any" // TODO: Use the correct type from models `Models.${clientDetails.className}Options`
}
]
});
Expand Down
54 changes: 35 additions & 19 deletions src/generators/clientFileGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { Project } from "ts-morph";
import { Project, PropertyDeclarationStructure } from "ts-morph";
import { ClientDetails } from "../models/clientDetails";
import { getModelsName, getMappersName } from "../utils/nameUtils";
import {
getModelsName,
getMappersName,
normalizeName,
NameType
} from "../utils/nameUtils";
import { CodeModel } from "@azure-tools/codemodel";

export function generateClient(clientDetails: ClientDetails, project: Project) {
export function generateClient(
codeModel: CodeModel,
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`,
Expand Down Expand Up @@ -44,15 +53,21 @@ export function generateClient(clientDetails: ClientDetails, project: Project) {
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 operations = clientDetails.operationGroups.map(og => {
return {
name: normalizeName(og.name, NameType.Property),
typeName: `operations.${normalizeName(og.name, NameType.Class)}`
};
});

clientClass.addProperties(
operations.map(op => {
return {
name: op.name,
type: op.typeName
} as PropertyDeclarationStructure;
})
);

const clientConstructor = clientClass.addConstructor({
docs: [
Expand All @@ -65,20 +80,21 @@ export function generateClient(clientDetails: ClientDetails, project: Project) {
{
name: "options",
hasQuestionToken: true,
type: `Models.${clientDetails.className}Options`
type: "any" // TODO Use the right 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);"
"super(options);",
...operations.map(
({ name, typeName }) => `this.${name} = new ${typeName}(this)`
)
]);

clientFile.addExportDeclaration({
leadingTrivia: writer => writer.write("// Operation Specifications\n\n"),
leadingTrivia: (writer: any) =>
writer.write("// Operation Specifications\n\n"),
namedExports: [
{ name: clientDetails.className },
{ name: clientContextClassName },
Expand Down
2 changes: 1 addition & 1 deletion src/generators/mappersGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT License.

import { CodeModel } from "@azure-tools/codemodel";
import { transformMapper } from "../mapperTransforms";
import { transformMapper } from "../transforms/mapperTransforms";
import { Project, VariableDeclarationKind } from "ts-morph";

export function generateMappers(codeModel: CodeModel, project: Project) {
Expand Down
2 changes: 1 addition & 1 deletion src/generators/modelsGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT License.

import { CodeModel } from "@azure-tools/codemodel";
import { transformCodeModel } from "../transforms";
import { transformCodeModel } from "../transforms/transforms";
import { Project, PropertySignatureStructure, StructureKind } from "ts-morph";

export function generateModels(codeModel: CodeModel, project: Project) {
Expand Down
Loading

0 comments on commit 5ad1230

Please sign in to comment.