This commit is contained in:
qiaozha 2020-02-09 15:35:36 +08:00
Родитель 8a780d257d
Коммит cdf70688c2
5 изменённых файлов: 160 добавлений и 48 удалений

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

@ -9,19 +9,37 @@ use-extension:
try-require: ./readme.cli.md
pipeline-model: v3
modelerfour:
group-parameters: true
flatten-models: true
flatten-payloads: true
#clicommon: true
pipeline:
clicommon:
input: modelerfour
output-artifact: source-file-common
scope: clicommon
output-artifact: source-file-clicommon
#scope: clicommon
#clicommon/clinamer:
# input: clicommon
#output-artifact: source-file-commonnamer
#clicommon/climodifiers:
# input: clinamer
# output-artifact: source-file-commonmodifiers
clicommon/emitter:
input: clicommon
scope: here
input:
- clicommon
#- clinamer
#- climodifiers
scope: scope-clicommon
scope-here:
scope-clicommon:
is-object: false
output-artifact:
- source-file-common
- source-file-clicommon
#- source-file-commonnamer
#- source-file-commonmodifiers
```

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

@ -38,10 +38,11 @@
"@types/js-yaml": "3.12.1"
},
"dependencies": {
"@autorest/modelerfour": "^4.1.60",
"@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",
"@azure-tools/codegen": "~2.1.222",
"@azure-tools/codemodel": "3.0.260",
"@azure-tools/linq": "~3.1.212",
"node-yaml": "^3.2.0"
},
"files": [

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

@ -1,8 +1,8 @@
import { AutoRestExtension, Channel, Host, startSession } from '@azure-tools/autorest-extension-base';
import { AutoRestExtension, Session, 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 './plugins/namer';
import { Modifiers } from './plugins/modifiers';
import { CommonNamer } from './plugins/namer';
import { CommonModifiers } from './plugins/modifiers';
export type LogCallback = (message: string) => void;
export type FileCallback = (path: string, rows: string[]) => void;
@ -14,17 +14,16 @@ extension.Add("clicommon", async autoRestApi => {
try
{
const isDebugFlagSet = await autoRestApi.GetValue("debug");
let cliCommonSettings = await autoRestApi.GetValue("cli");
const inputFileUris = await autoRestApi.ListInputs();
const inputFiles: string[] = await Promise.all(inputFileUris.map(uri => autoRestApi.ReadFile(uri)));
const session = await startSession<CodeModel>(autoRestApi, {}, codeModelSchema);
let cliCommonSettings = autoRestApi.GetValue("cli");
// at this point namer and modifirers are in a single plug-in
const namer = await new Namer(session).init();
const namer = await new CommonNamer(session).init();
let result = namer.process();
const modifiers = new Modifiers(session);
const modifiers = new CommonModifiers(session);
modifiers.codeModel = result;
modifiers.directives = (cliCommonSettings != null) ? cliCommonSettings['directives'] : null;
result = await modifiers.process();
@ -43,4 +42,11 @@ extension.Add("clicommon", async autoRestApi => {
}
});
/*async function initializePlugins(pluginHost: AutoRestExtension) {
pluginHost.Add("clinamer", clinamer);
pluginHost.Add("climodifiers", climodifiers);
}
initializePlugins(extension);*/
extension.Run();

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

@ -96,7 +96,7 @@ function hasSpecialChars(str: string): boolean {
return !/^[a-zA-Z0-9]+$/.test(str);
}
export class Modifiers {
export class CommonModifiers {
codeModel: CodeModel;
directives: any;
@ -170,9 +170,9 @@ export async function processRequest(host: Host) {
try {
const session = await startSession<CodeModel>(host, {}, codeModelSchema);
const plugin = new Modifiers(session);
const plugin = new CommonModifiers(session);
const result = await plugin.process();
host.WriteFile("modifiers-temp-output.yaml", serialize(result));
host.WriteFile("modifiers-code-model-v4-cli.yaml", serialize(result));
} catch (E) {
if (debug) {
console.error(`${__filename} - FAILURE ${JSON.stringify(E)} ${E.stack}`);

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

@ -1,38 +1,125 @@
import { CodeModel, Schema, ObjectSchema, SchemaType, Property } from '@azure-tools/codemodel';
import { Session } from '@azure-tools/autorest-extension-base';
import { CodeModel, codeModelSchema, Property, Language} from '@azure-tools/codemodel';
import { Session, Host, startSession, Channel } from '@azure-tools/autorest-extension-base';
import { serialize, deserialize } from '@azure-tools/codegen';
import { values, items, length, Dictionary } from '@azure-tools/linq';
export class Namer {
codeModel: CodeModel
export class CommonNamer {
codeModel: CodeModel
constructor(protected session: Session<CodeModel>) {
this.codeModel = session.model;
}
constructor(protected session: Session<CodeModel>) {
this.codeModel = session.model;
}
async init() {
// any configuration if necessary
return this;
}
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;
process() {
this.getCliName(this.codeModel);
this.processGlobalParam();
this.processSchemas();
this.processOperationGroups();
return this.codeModel;
}
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;
getCliName(obj: any) {
if(obj == null || obj.language == null) {
//return [];
this.session.message({Channel: Channel.Warning, Text: "working in obj has problems"});
return;
//this.session.message({Channel: Channel.Warning, Text:"Object don't have language property: " + serialize(obj)});
}
obj.language['cli'] = new Language();
obj.language['cli']['name'] = obj.language.default.name;
obj.language['cli']['description'] = obj.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;
processSchemas() {
let schemas = this.codeModel.schemas;
for (let obj of values(schemas.objects)) {
this.getCliName(obj);
for (let property of values(obj.properties)) {
this.getCliName(property);
}
}
for(let dict of values(schemas.dictionaries)) {
this.getCliName(dict);
this.getCliName(dict.elementType);
}
for(let enumn of values(schemas.choices)) {
this.getCliName(enumn);
for(let item of values(enumn.choices)) {
this.getCliName(item);
}
}
for(let enumn of values(schemas.sealedChoices)) {
this.getCliName(enumn);
for(let item of values(enumn.choices)) {
this.getCliName(item);
}
}
for(let arr of values(schemas.arrays)) {
this.getCliName(arr);
this.getCliName(arr.elementType);
}
for(let cons of values(schemas.constants)) {
this.getCliName(cons);
this.getCliName(cons.value);
}
for(let num of values(schemas.numbers)) {
this.getCliName(num);
}
for(let str of values(schemas.strings)) {
this.getCliName(str);
}
}
processOperationGroups() {
// cleanup
for (const operationGroup of values(this.codeModel.operationGroups)) {
this.getCliName(operationGroup);
for (const operation of values(operationGroup.operations)) {
this.getCliName(operation);
for (const parameter of values(operation.request.parameters)) {
this.getCliName(parameter);
}
}
}
}
return this.codeModel;
}
processGlobalParam() {
for(let para of values(this.codeModel.globalParameters)) {
this.getCliName(para);
}
}
}
export async function processRequest(host: Host) {
const debug = await host.GetValue('debug') || false;
//host.Message({Channel:Channel.Warning, Text:"in aznamer processRequest"});
//console.error(extensionName);
try {
const session = await startSession<CodeModel>(host, {}, codeModelSchema);
const plugin = new CommonNamer(session);
let result = plugin.process();
host.WriteFile('namer-code-model-v4-cli.yaml', serialize(result));
} catch (E) {
if (debug) {
console.error(`${__filename} - FAILURE ${JSON.stringify(E)} ${E.stack}`);
}
throw E;
}
}