Skip to content
This repository has been archived by the owner on Jul 14, 2023. It is now read-only.

Commit

Permalink
support split createorupdate (#416)
Browse files Browse the repository at this point in the history
  • Loading branch information
qiaozha authored Jun 17, 2020
1 parent 87ce6d3 commit b2a807b
Show file tree
Hide file tree
Showing 28 changed files with 16,913 additions and 16,855 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ debug-output-folder: $(az-output-folder)/_az_debug

use-extension:
"@autorest/python": "5.0.0-preview.7"
"@autorest/clicommon": "0.4.9"
"@autorest/clicommon": "0.4.10"
#"@autorest/python": "latest"

cli:
Expand Down Expand Up @@ -49,6 +49,11 @@ modelerfour:
pipeline:
python/m2r:
input: clicommon/identity
az/hider:
input: python/namer
#output-artifact: source-file-az-hider
python/codegen:
input: az/hider
az/merger:
input: python/namer
#output-artifact: source-file-merger
Expand All @@ -63,6 +68,7 @@ pipeline:
output-artifact: source-file-extension
az/emitter:
input:
#- az/hider
#- az/clicommon
#- az/merger
#- az/aznamer
Expand All @@ -73,6 +79,7 @@ pipeline:
scope-az:
is-object: false
output-artifact:
#- source-file-az-hider
#- source-file-pynamer
#- source-file-aznamer
#- source-file-modifiers
Expand Down
14 changes: 11 additions & 3 deletions readme.az.common.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
# configuration for az common

``` yaml $(az)
extension-mode: experimental

cli:
naming:
default:
singularize:
- operationGroup
- operation
split-operation:
cli-split-operation-enabled: true
cli-split-operation-extend-poly-resource: true
cli-directive:
- where:
operation: CheckNameAvailability
Expand All @@ -17,13 +20,18 @@ cli:
operationGroup: Operations
operation: List
hidden: true
- where:
op: CreateOrUpdate
split-operation-names:
- Create
- Update
flatten:
cli-flatten-set-enabled: true
cli-flatten-payload: true
cli-flatten-schema: false
cli-flatten-all-overwrite-swagger: false
```
``` yaml $(python)
add-credential: true
no-namespace-folders: true
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AutoRestExtension, Channel, Host } from "@azure-tools/autorest-extension-base";
import { processRequest as hider } from './plugins/hider';
import { processRequest as aznamer } from './plugins/aznamer';
import { processRequest as modifiers } from './plugins/modifiers';
import { processRequest as merger } from './plugins/merger';
Expand All @@ -8,6 +9,7 @@ export type LogCallback = (message: string) => void;
export type FileCallback = (path: string, rows: string[]) => void;

export async function initializePlugins(pluginHost: AutoRestExtension) {
pluginHost.Add('hider', hider);
pluginHost.Add("aznamer", aznamer);
pluginHost.Add("modifiers", modifiers);
pluginHost.Add('merger', merger);
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/azgenerator/CodeModelAz.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,10 @@ export interface CodeModelAz {
Command_MethodName: string;
Command_FunctionName: string;
Command_GetOriginalOperation: any;
Command_NeedGeneric: boolean;
Command_GenericSetterParameter(Operation): Parameter;

Command_Help: string;

Command_CanSplit: boolean;
Command_IsLongRun: boolean;
Command_SubGroupName: string;

Expand All @@ -106,8 +105,9 @@ export interface CodeModelAz {
Method_BodyParameterName: string;
Method_IsLongRun: boolean;
Method_GetOriginalOperation: any;
Method_CanSplit: boolean;
Method_GenericSetterParameter(Operation): Parameter;
Method_NeedGeneric: boolean;
Operation_IsHidden(op?: Operation): boolean;


SelectFirstMethodParameter(containHidden?: boolean): boolean;
Expand Down
92 changes: 67 additions & 25 deletions src/plugins/azgenerator/CodeModelAzImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,9 @@ export class CodeModelCliImpl implements CodeModelAz {
let param = this.MethodParameter;
let originParam = this.MethodParameter;
let flattenedNames = param?.['targetProperty']?.['flattenedNames'];
if (isNullOrUndefined(flattenedNames) && !isNullOrUndefined(param.language['cli']['flattenedNames'])) {
flattenedNames = param.language['cli']['flattenedNames'];
}
let mapName: Array<string> = [];
let paramFlattenedName = this.Parameter_MapsTo(param);
let names = this.Method_NameAz.split(' ');
Expand Down Expand Up @@ -587,14 +590,14 @@ export class CodeModelCliImpl implements CodeModelAz {
let operation = this.Command;
this.preMethodIndex = this.currentOperationIndex;
let needNext = false;
if (operation.language['cli'].hidden || operation.language['cli'].removed) {
if (this.Operation_IsHidden(operation)) {
needNext = true;
}
while (this.currentOperationIndex + 1 < this.CommandGroup.operations.length) {
let tmpOperation = this.CommandGroup.operations[this.currentOperationIndex + 1];
if (tmpOperation.language['az'].command == operation.language['az'].command) {
this.currentOperationIndex++;
if (tmpOperation.language['cli'].hidden != true && tmpOperation.language['cli'].removed != true) {
if (!this.Operation_IsHidden(tmpOperation)) {
needNext = false;
}
} else {
Expand All @@ -619,14 +622,14 @@ export class CodeModelCliImpl implements CodeModelAz {
this.preMethodIndex = this.currentOperationIndex;
let operation = this.Command;
let needNext = false;
if (operation.language['cli'].hidden || operation.language['cli'].removed) {
if (this.Operation_IsHidden(operation)) {
needNext = true;
}
while (this.currentOperationIndex < this.CommandGroup.operations.length - 1) {
let tmpOperation = this.CommandGroup.operations[this.currentOperationIndex + 1];
if (operation.language['az'].command == tmpOperation.language['az'].command) {
this.currentOperationIndex++;
if (tmpOperation.language['cli'].hidden != true && tmpOperation.language['cli'].removed != true) {
if (!this.Operation_IsHidden(tmpOperation)) {
needNext = false;
}
} else {
Expand All @@ -645,6 +648,13 @@ export class CodeModelCliImpl implements CodeModelAz {
}
}

public Operation_IsHidden(op: Operation = this.Method): boolean {
if (op.language['cli'].hidden || op.language['cli'].removed || op.language['cli']['cli-operation-splitted']) {
return true;
}
return false;
}

public get Command() {
return this.CommandGroup.operations[this.currentOperationIndex];
}
Expand All @@ -662,26 +672,34 @@ export class CodeModelCliImpl implements CodeModelAz {
}

public Command_GenericSetterParameter(op: Operation = this.Command): Parameter {
if (isNullOrUndefined(op)) {
return null;
}
return op['genericSetterParam'];
}

public get Command_Help(): string {
return this.Command.language['az'].description.replace(/\n/g, " ");
}

public get Command_CanSplit(): boolean {
if(this.CommandGroup.language['az']['hasUpdate']) {
return false;
public get Command_GetOriginalOperation(): any {
let polyOriginal = this.Command.extensions?.['cli-poly-as-resource-original-operation'];
if(!isNullOrUndefined(polyOriginal) && !isNullOrUndefined(polyOriginal.extensions['cli-split-operation-original-operation'])) {
let splitOriginal = polyOriginal.extensions['cli-split-operation-original-operation'];
return splitOriginal;
}
this.SelectFirstMethod();
if(this.Method_IsLast && this.Method_IsFirst) {
return this.Command['canSplitOperation'] ? true : false;
}
return false;
let splittedOriginal = this.Command.extensions['cli-split-operation-original-operation'];
if(!isNullOrUndefined(splittedOriginal)) {
return splittedOriginal;
}
return polyOriginal;
}

public get Command_GetOriginalOperation(): any {
return this.Command.extensions?.['cli-poly-as-resource-original-operation'];
public get Command_NeedGeneric(): boolean {
if(this.Command.language['az']['isSplitUpdate'] && !isNullOrUndefined(this.Command_GenericSetterParameter(this.Command_GetOriginalOperation))) {
return true;
}
return false;
}

public get Command_IsLongRun(): boolean {
Expand Down Expand Up @@ -710,7 +728,7 @@ export class CodeModelCliImpl implements CodeModelAz {
if (this.currentOperationIndex >= this.preMethodIndex) {
this.currentMethodIndex = this.preMethodIndex;
let method = this.Method;
if (method.language['cli'].removed || method.language['cli'].hidden) {
if (this.Operation_IsHidden(method)) {
if (!this.SelectNextMethod()) {
return false;
}
Expand All @@ -727,7 +745,7 @@ export class CodeModelCliImpl implements CodeModelAz {
if (this.currentMethodIndex < this.currentOperationIndex) {
this.currentMethodIndex++;
let method = this.Method;
if (method.language['cli'].removed || method.language['cli'].hidden) {
if (this.Operation_IsHidden(method)) {
if (!this.SelectNextMethod()) {
return false;
}
Expand Down Expand Up @@ -762,7 +780,16 @@ export class CodeModelCliImpl implements CodeModelAz {
if (this.currentMethodIndex == this.currentOperationIndex) {
return true;
} else {
return false;
let curIndex = this.currentMethodIndex + 1;
let hasNext = false;
while(curIndex <= this.currentOperationIndex) {
if(!this.Operation_IsHidden(this.CommandGroup.operations[curIndex])) {
hasNext = true;
break;
}
curIndex++;
}
return hasNext? false: true;
}
}

Expand Down Expand Up @@ -798,23 +825,35 @@ export class CodeModelCliImpl implements CodeModelAz {
return ret.toLowerCase();
}

public get Method_CanSplit(): boolean {
if(this.CommandGroup.language['az']['hasUpdate']) {
return false;
public Method_GenericSetterParameter(op: Operation = this.Method): Parameter {
if (isNullOrUndefined(op)) {
return null;
}
return this.Method['canSplitOperation'] ? true : false;
return op['genericSetterParam'];
}

public Method_GenericSetterParameter(op: Operation = this.Method): Parameter {
return op['genericSetterParam'];
public get Method_NeedGeneric(): boolean {
if(this.Method.language['az']['isSplitUpdate'] && !isNullOrUndefined(this.Method_GenericSetterParameter(this.Method_GetOriginalOperation))) {
return true;
}
return false;
}

public Get_Method_Name(language = "az"): string {
return this.Method.language[language].name;
}

public get Method_GetOriginalOperation(): any {
return this.Method.extensions?.['cli-poly-as-resource-original-operation'];
let polyOriginal = this.Method.extensions?.['cli-poly-as-resource-original-operation'];
if(!isNullOrUndefined(polyOriginal) && !isNullOrUndefined(polyOriginal.extensions?.['cli-split-operation-original-operation'])) {
let splitOriginal = polyOriginal.extensions?.['cli-split-operation-original-operation'];
return splitOriginal;
}
let splittedOriginal = this.Method.extensions?.['cli-split-operation-original-operation'];
if(!isNullOrUndefined(splittedOriginal)) {
return splittedOriginal;
}
return polyOriginal;
}
//=================================================================================================================
// Methods Parameters.
Expand Down Expand Up @@ -1226,7 +1265,7 @@ export class CodeModelCliImpl implements CodeModelAz {
return false;
}

if (this.isComplexSchema(this.MethodParameter_Type)) {
if (this.isComplexSchema(this.Parameter_Type(param))) {
return true;
}
return false;
Expand Down Expand Up @@ -1330,6 +1369,9 @@ export class CodeModelCliImpl implements CodeModelAz {
this.ExitSubMethodParameters();
}

if (parameter.language['az']['name'] == 'identity') {
parameter;
}
// Handle simple parameter
if (parameter?.language?.['cli']?.removed || parameter?.language?.['cli']?.hidden) {
if (this.Parameter_DefaultValue(parameter) == undefined && parameter.required == true) {
Expand Down
23 changes: 5 additions & 18 deletions src/plugins/azgenerator/TemplateAzureCliCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ export function GenerateAzureCliCommands(model: CodeModelAz): string[] {
needWait = true;
}
output = output.concat(getCommandBody(model));
if (model.Command_CanSplit) {
output = output.concat(getCommandBody(model, true));
}
}
while (model.SelectNextCommand());
if (needWait) {
Expand All @@ -74,7 +71,7 @@ export function GenerateAzureCliCommands(model: CodeModelAz): string[] {
return header.getLines().concat(output);
}

function getCommandBody(model: CodeModelAz, needUpdate: boolean = false) {
function getCommandBody(model: CodeModelAz) {
let output: string[] = [];
let functionName = model.Command_FunctionName;
let methodName = model.Command_MethodName;
Expand All @@ -83,32 +80,22 @@ function getCommandBody(model: CodeModelAz, needUpdate: boolean = false) {
endStr = ", supports_no_wait=True" + endStr;
}
if (methodName != "show") {
if (needUpdate) {
if (model.Command_NeedGeneric) {
let argument = "";
let geneParam = null;
if (model.SelectFirstMethod()) {
let originalOperation = model.Method_GetOriginalOperation;
if (!isNullOrUndefined(originalOperation)) {
geneParam = model.Method_GenericSetterParameter(originalOperation);
} else {
geneParam = model.Method_GenericSetterParameter(model.Method);
}
geneParam = model.Method_GenericSetterParameter(model.Method_GetOriginalOperation);
if (!isNullOrUndefined(geneParam)) {
argument = model.Parameter_NamePython(geneParam);
}
}
if(isNullOrUndefined(geneParam)) {
// generic update doesn't apply here
ToMultiLine(" g.custom_command('" + methodName.replace(/create/g, 'update') + "', '" + functionName.replace(/_create/g, '_update') + "'" + endStr, output);
} else {
let generic_update = " g.generic_update_command('" + model.Command_MethodName.replace(/create/g, 'update');
let generic_update = " g.generic_update_command('" + model.Command_MethodName;
if (argument && argument != "" && argument != "parameters") {
generic_update += "', setter_arg_name='" + argument;
}
if (model.Command_IsLongRun) {
generic_update += "', setter_name='begin_create_or_update";
}
generic_update += "', custom_func_name='" + functionName.replace(/_create/g, '_update') + "'" + endStr;
generic_update += "', custom_func_name='" + functionName + "'" + endStr;
ToMultiLine(generic_update, output);
}
} else {
Expand Down
Loading

0 comments on commit b2a807b

Please sign in to comment.