This commit is contained in:
Rodge Fu 2020-02-18 15:53:59 +08:00
Родитель f8700938f2
Коммит 18b84f4d9b
5 изменённых файлов: 97 добавлений и 46 удалений

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

@ -4,9 +4,7 @@ See documentation [here](doc/00-overview.md)
``` yaml
use-extension:
"@autorest/modelerfour": "~4.4.162"
try-require: ./readme.cli.md
"@autorest/modelerfour": "4.6.200"
pipeline-model: v3
@ -14,19 +12,24 @@ modelerfour:
group-parameters: true
flatten-models: true
flatten-payloads: true
#clicommon: true
# standardize to snake in modelerfour for selecting and formatting in clicommon
# further naming will be done in clicommon to corresonding convention
naming:
parameter: 'snake'
operation: 'snake'
operationGroup: 'snake'
pipeline:
clicommon:
input: modelerfour
output-artifact: source-file-common
input: modelerfour/identity
output-artifact: clicommon-output-file
clicommon/emitter:
input:
- clicommon
input: clicommon
scope: scope-clicommon
scope-clicommon:
is-object: false
output-artifact:
- source-file-common
- clicommon-output-file
```

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

@ -38,10 +38,10 @@
"@types/js-yaml": "3.12.1"
},
"dependencies": {
"@azure-tools/autorest-extension-base": "~3.1.0",
"@azure-tools/codegen": "~2.1.222",
"@azure-tools/autorest-extension-base": "^3.1.228",
"@azure-tools/codegen": "^2.4.250",
"@azure-tools/codemodel": "3.0.260",
"@azure-tools/linq": "~3.1.212",
"@azure-tools/linq": "^3.1.225",
"node-yaml": "^3.2.0"
},
"files": [

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

@ -19,6 +19,12 @@ export abstract class Action {
}
public abstract process(metadata: Metadata): void;
protected createNodeIfNotExist(metadata: Metadata, nodeName: string) : any {
if (isNullOrUndefined(metadata.language[CliConst.CLI][nodeName]))
metadata.language[CliConst.CLI][nodeName] = {};
return metadata.language[CliConst.CLI][nodeName];
}
protected setProperty(metadata: Metadata, key: string, value: any): void {
if (isNullOrUndefined(metadata.language[CliConst.CLI]))
metadata.language[CliConst.CLI] = {};
@ -56,6 +62,9 @@ export abstract class Action {
case 'replace':
arr.push(new ActionReplace(value));
break;
case 'formattable':
arr.push(new ActionFormatTable(value));
break;
default:
// TODO: better to log instead of throw here?
throw Error("Unknown directive operation");
@ -84,6 +93,20 @@ class ActionSet extends Action {
}
}
class ActionFormatTable extends Action {
constructor(private directiveFormatTable: CliCommonSchema.CliDirective.FormatTableClause) {
super();
}
public process(metadata: Metadata): void {
if (!isNullOrUndefined(this.directiveFormatTable.properties)) {
var n = this.createNodeIfNotExist(metadata, CliConst.CLI_FORMATTABLE);
n[CliConst.CLI_FORMATTABLE_PROPERTIES] = this.directiveFormatTable.properties;
}
}
}
class ActionSetName extends Action {
private newName: string;
private naming: CliCommonSchema.CliDirective.NamingStyleSetting;

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

@ -9,7 +9,8 @@ import {
} from "@azure-tools/autorest-extension-base";
import { serialize, deserialize } from "@azure-tools/codegen";
import { CliDirectiveManager } from "./cliDirective";
import { isNullOrUndefined } from "util";
import { isNullOrUndefined, isString, isObject, isArray } from "util";
import { keys, items } from "@azure-tools/linq";
export class Modifier {
private manager: CliDirectiveManager;
@ -61,21 +62,37 @@ export class Modifier {
public generateReport(): string {
const INDENT = 2;
const GROUP_INDENT = 4;
const OPERATION_INDENT = 6;
const PARAM_INDENT = 8;
let generateValue = (o: any, i: number) => `'${o.language.default.name}'` +
const NEW_LINE = '\n';
// TODO: refactor the yaml simple parser to helper
let withIndent = (i: number, s: string = '') => `${' '.repeat(i)}${s}`;
let nextIndent = (i, level = 1) => i + INDENT * level;
let formatValue = (o: any, i: number) => {
if (isString(o))
return o;
else if (isArray(o))
return o.map(v => NEW_LINE + withIndent(i, "- " + formatValue(v, nextIndent(i, 2 /* one more indent for array*/)))).join('');
else if (isObject(o))
return keys(o).select(k => NEW_LINE + `${withIndent(i, k)}: ${formatValue(o[k], nextIndent(i))}`).join('');
else
return o.toString();
};
let generateCliValue = (o: any, i: number) => o.language.default.name +
(isNullOrUndefined(o.language.cli) ? '' : Object.getOwnPropertyNames(o.language.cli)
.filter(key => o.language.cli[key] !== o.language.default[key])
.reduce((pv, cv, ci) => pv.concat((ci === 0 ? `\n${' '.repeat(i)}cli:` : '') + `\n${' '.repeat(i + INDENT)}${cv}: ${o.language.cli[cv]}`), ''));
.reduce((pv, cv, ci) => pv.concat((ci === 0 ? NEW_LINE + withIndent(i, 'cli:') : '') +
NEW_LINE + `${withIndent(nextIndent(i), cv)}: ${formatValue(o.language.cli[cv], nextIndent(i, 2 /*next next level*/))}`), ''));
// TODO: include schema... when we support schema in modifier
return this.codeModel.operationGroups.map(
v => `- operationGroup: ${generateValue(v, GROUP_INDENT)}\n`.concat(
v.operations.map(vv => ` - operation: ${generateValue(vv, OPERATION_INDENT)}\n`.concat(
vv.request.parameters.map(vvv => ` - parameter: ${generateValue(vvv, PARAM_INDENT)}\n`)
.join(''))
).join(''))
).join('');
let initialIndent = 0;
return `${withIndent(initialIndent)}operationGroups:${NEW_LINE}`.concat(
this.codeModel.operationGroups.map(
v => `${withIndent(initialIndent)}- operationGroupName: ${generateCliValue(v, nextIndent(initialIndent))}` +
`${NEW_LINE}${withIndent(nextIndent(initialIndent))}operations:${NEW_LINE}`.concat(
v.operations.map(vv => `${withIndent(nextIndent(initialIndent))}- operationName: ${generateCliValue(vv, nextIndent(initialIndent, 2))}` +
`${NEW_LINE}${withIndent(nextIndent(initialIndent,2))}parameters:${NEW_LINE}`.concat(
vv.request.parameters.map(vvv => `${withIndent(nextIndent(initialIndent, 2))}- parameterName: ${generateCliValue(vvv, nextIndent(initialIndent, 3))}` + NEW_LINE)
.join(''))
).join(''))
).join(''));
}
}

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

@ -6,29 +6,32 @@ export type NamingStyle = 'camel' | 'pascal' | 'snake' | 'upper' | 'kebab' | 'sp
export type SelectType = 'operationGroup' | 'operation' | 'parameter';
export namespace CliConst {
export const CLI: string = "cli";
export const CLI_DIRECTIVE: string = "cli-directive";
// Todo: merge this into code model?
export const CLI: string = "cli";
export const CLI_FORMATTABLE: string = "formatTable";
export const CLI_FORMATTABLE_PROPERTIES: string = "properties";
export const CLI_DIRECTIVE: string = "cli-directive";
export class NamingStyle {
/** camelCase */
static readonly camel = "camel";
/** PascalCase */
static readonly pascal = "pascal";
/** snake_case */
static readonly snake = "snake";
/** kebab-case */
static readonly kebab = "kebab";
/** space case */
static readonly space = "space";
/** UPPER_CASE */
static readonly upper = "upper";
};
export class NamingStyle {
/** camelCase */
static readonly camel = "camel";
/** PascalCase */
static readonly pascal = "pascal";
/** snake_case */
static readonly snake = "snake";
/** kebab-case */
static readonly kebab = "kebab";
/** space case */
static readonly space = "space";
/** UPPER_CASE */
static readonly upper = "upper";
};
export class SelectType {
static readonly operationGroup = 'operationGroup';
static readonly operation = 'operation';
static readonly parameter = 'parameter';
}
export class SelectType {
static readonly operationGroup = 'operationGroup';
static readonly operation = 'operation';
static readonly parameter = 'parameter';
}
}
export namespace CliCommonSchema {
@ -61,6 +64,10 @@ export namespace CliCommonSchema {
parameter?: string;
}
export interface FormatTableClause {
properties?: string[];
}
export interface Directive {
select?: SelectType;
where?: WhereClause;
@ -69,6 +76,7 @@ export namespace CliCommonSchema {
setName?: SetNameClause;
log?: LogClause;
replace?: ReplaceClause;
formatTable?: FormatTableClause;
}
export interface NamingStyleSetting {