Merge pull request #12 from Azure/proper-parsing-and-modification

Proper parsing and adding default "cli" section in codemodel v4
This commit is contained in:
Zim Kalinowski 2020-01-06 10:26:55 +08:00 коммит произвёл GitHub
Родитель 79064a4fb4 fb94a0c70f
Коммит 14ccc56d27
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 57 добавлений и 17 удалений

Просмотреть файл

@ -34,10 +34,14 @@
"devDependencies": {
"@types/node": "10.17.0",
"eslint": "~5.4.0",
"node-yaml": "^3.2.0"
"node-yaml": "^3.2.0",
"@types/js-yaml": "3.12.1"
},
"dependencies": {
"@azure-tools/autorest-extension-base": "~3.1.0",
"@azure-tools/codemodel": "~3.0.0",
"@azure-tools/codegen": "~2.1.0",
"@azure-tools/linq": "~3.1.0",
"node-yaml": "^3.2.0"
},
"files": [

Просмотреть файл

@ -1,5 +1,7 @@
import { AutoRestExtension, Channel } from '@azure-tools/autorest-extension-base';
import { AutoRestExtension, Channel, Host, startSession } from '@azure-tools/autorest-extension-base';
import { codeModelSchema, CodeModel } from '@azure-tools/codemodel';
import { serialize } from '@azure-tools/codegen';
import { Namer } from './namer';
export type LogCallback = (message: string) => void;
export type FileCallback = (path: string, rows: string[]) => void;
@ -11,18 +13,10 @@ extension.Add("cli.common", async autoRestApi => {
try
{
// read files offered to this plugin
const inputFileUris = await autoRestApi.ListInputs();
const inputFiles: string[] = await Promise.all(inputFileUris.map(uri => autoRestApi.ReadFile(uri)));
// read a setting
const isDebugFlagSet = await autoRestApi.GetValue("debug");
let cliCommonSettings = await autoRestApi.GetValue("cli");
// emit messages
const session = await startSession<CodeModel>(autoRestApi, {}, codeModelSchema);
autoRestApi.Message({
Channel: Channel.Warning,
@ -34,13 +28,17 @@ extension.Add("cli.common", async autoRestApi => {
Text: "cli.common settings " + JSON.stringify(cliCommonSettings)
});
autoRestApi.Message({
Channel: Channel.Information,
Text: "AutoRest offers the following input files: " + inputFileUris.join(", "),
});
const plugin = await new Namer(session).init();
const result = plugin.process();
// add test scenario from common settings
if (cliCommonSettings) {
result["test-scenario"] = cliCommonSettings['test-scenario'];
}
// emit a file (all input files concatenated)
autoRestApi.WriteFile("code-model-v4-cli.yaml", inputFiles[inputFileUris.indexOf("code-model-v4-no-tags.yaml")]);
autoRestApi.WriteFile("code-model-v4-cli.yaml", serialize(result, codeModelSchema));
}
catch (e)
{

38
src/namer.ts Normal file
Просмотреть файл

@ -0,0 +1,38 @@
import { CodeModel, Schema, ObjectSchema, SchemaType, Property } from '@azure-tools/codemodel';
import { Session } from '@azure-tools/autorest-extension-base';
import { values, items, length, Dictionary, refCount } from '@azure-tools/linq';
export class Namer {
codeModel: CodeModel
constructor(protected session: Session<CodeModel>) {
this.codeModel = session.model;
}
async init() {
// any configuration if necessary
return this;
}
process() {
// cleanup
for (const operationGroup of values(this.codeModel.operationGroups)) {
operationGroup.language['cli'] = {};
operationGroup.language['cli']['name'] = operationGroup.language.default.name;
operationGroup.language['cli']['description'] = operationGroup.language.default.description;
for (const operation of values (operationGroup.operations)) {
operation.language['cli'] = {}
operation.language['cli']['name'] = operation.language.default.name;
operation.language['cli']['description'] = operation.language.default.description;
for (const parameter of values (operation.request.parameters)) {
parameter.language['cli'] = {}
parameter.language['cli']['name'] = parameter.language.default.name;
parameter.language['cli']['description'] = parameter.language.default.description;
}
}
}
return this.codeModel;
}
}