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 try-require: ./readme.cli.md
pipeline-model: v3 pipeline-model: v3
modelerfour:
group-parameters: true
flatten-models: true
flatten-payloads: true
#clicommon: true #clicommon: true
pipeline: pipeline:
clicommon: clicommon:
input: modelerfour input: modelerfour
output-artifact: source-file-common output-artifact: source-file-clicommon
scope: clicommon #scope: clicommon
#clicommon/clinamer:
# input: clicommon
#output-artifact: source-file-commonnamer
#clicommon/climodifiers:
# input: clinamer
# output-artifact: source-file-commonmodifiers
clicommon/emitter: clicommon/emitter:
input: clicommon input:
scope: here - clicommon
#- clinamer
#- climodifiers
scope: scope-clicommon
scope-here: scope-clicommon:
is-object: false is-object: false
output-artifact: output-artifact:
- source-file-common - source-file-clicommon
#- source-file-commonnamer
#- source-file-commonmodifiers
``` ```

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

@ -38,10 +38,11 @@
"@types/js-yaml": "3.12.1" "@types/js-yaml": "3.12.1"
}, },
"dependencies": { "dependencies": {
"@autorest/modelerfour": "^4.1.60",
"@azure-tools/autorest-extension-base": "~3.1.0", "@azure-tools/autorest-extension-base": "~3.1.0",
"@azure-tools/codemodel": "~3.0.0", "@azure-tools/codegen": "~2.1.222",
"@azure-tools/codegen": "~2.1.0", "@azure-tools/codemodel": "3.0.260",
"@azure-tools/linq": "~3.1.0", "@azure-tools/linq": "~3.1.212",
"node-yaml": "^3.2.0" "node-yaml": "^3.2.0"
}, },
"files": [ "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 { codeModelSchema, CodeModel } from '@azure-tools/codemodel';
import { serialize } from '@azure-tools/codegen'; import { serialize } from '@azure-tools/codegen';
import { Namer } from './plugins/namer'; import { CommonNamer } from './plugins/namer';
import { Modifiers } from './plugins/modifiers'; import { CommonModifiers } from './plugins/modifiers';
export type LogCallback = (message: string) => void; export type LogCallback = (message: string) => void;
export type FileCallback = (path: string, rows: string[]) => void; export type FileCallback = (path: string, rows: string[]) => void;
@ -14,17 +14,16 @@ extension.Add("clicommon", async autoRestApi => {
try try
{ {
const isDebugFlagSet = await autoRestApi.GetValue("debug"); const inputFileUris = await autoRestApi.ListInputs();
let cliCommonSettings = await autoRestApi.GetValue("cli"); const inputFiles: string[] = await Promise.all(inputFileUris.map(uri => autoRestApi.ReadFile(uri)));
const session = await startSession<CodeModel>(autoRestApi, {}, codeModelSchema); const session = await startSession<CodeModel>(autoRestApi, {}, codeModelSchema);
let cliCommonSettings = autoRestApi.GetValue("cli");
// at this point namer and modifirers are in a single plug-in // 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(); let result = namer.process();
const modifiers = new Modifiers(session); const modifiers = new CommonModifiers(session);
modifiers.codeModel = result; modifiers.codeModel = result;
modifiers.directives = (cliCommonSettings != null) ? cliCommonSettings['directives'] : null; modifiers.directives = (cliCommonSettings != null) ? cliCommonSettings['directives'] : null;
result = await modifiers.process(); 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(); extension.Run();

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

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

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

@ -1,38 +1,125 @@
import { CodeModel, Schema, ObjectSchema, SchemaType, Property } from '@azure-tools/codemodel'; import { CodeModel, codeModelSchema, Property, Language} from '@azure-tools/codemodel';
import { Session } from '@azure-tools/autorest-extension-base'; 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'; import { values, items, length, Dictionary } from '@azure-tools/linq';
export class Namer { export class CommonNamer {
codeModel: CodeModel codeModel: CodeModel
constructor(protected session: Session<CodeModel>) { constructor(protected session: Session<CodeModel>) {
this.codeModel = session.model; this.codeModel = session.model;
} }
async init() { async init() {
// any configuration if necessary // any configuration if necessary
return this; return this;
} }
process() { process() {
// cleanup this.getCliName(this.codeModel);
for (const operationGroup of values(this.codeModel.operationGroups)) { this.processGlobalParam();
operationGroup.language['cli'] = {}; this.processSchemas();
operationGroup.language['cli']['name'] = operationGroup.language.default.name; this.processOperationGroups();
operationGroup.language['cli']['description'] = operationGroup.language.default.description; return this.codeModel;
}
for (const operation of values (operationGroup.operations)) { getCliName(obj: any) {
operation.language['cli'] = {} if(obj == null || obj.language == null) {
operation.language['cli']['name'] = operation.language.default.name; //return [];
operation.language['cli']['description'] = operation.language.default.description; 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)) { processSchemas() {
parameter.language['cli'] = {} let schemas = this.codeModel.schemas;
parameter.language['cli']['name'] = parameter.language.default.name;
parameter.language['cli']['description'] = parameter.language.default.description; 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;
}
}