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

Action.py template based refactor #804

Merged
merged 18 commits into from
Apr 7, 2021
Merged
12 changes: 10 additions & 2 deletions src/azlinter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,21 @@ export async function processRequest(host: Host): Promise<void> {
try {
const folder = AzConfiguration.getValue(CodeGenConstants.azOutputFolder);
const azextFolder = AzConfiguration.getValue(CodeGenConstants.azextFolder);
const fileName = path.join(
const azLinter = new AzLinter();
let fileName = path.join(
folder,
azextFolder,
PathConstants.generatedFolder,
PathConstants.commandsFile,
);
const azLinter = new AzLinter();
await azLinter.process(fileName);

fileName = path.join(
folder,
azextFolder,
PathConstants.generatedFolder,
PathConstants.actionFile,
);
await azLinter.process(fileName);

if (NeedPreparers().size > 0) {
Expand Down
5 changes: 3 additions & 2 deletions src/generate/CodeModelAz.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export interface CodeModelAz {
Extension_ClientAuthenticationPolicy: string;
Extension_Mode: string;
ResourceType: string | undefined;
isComplexSchema(type: string): boolean;
isComplexSchema(type: string, param: any): boolean;

SelectFirstCommandGroup(needRefer?: boolean): boolean;
SelectNextCommandGroup(needRefer?: boolean): boolean;
Expand Down Expand Up @@ -225,7 +225,7 @@ export interface CodeModelAz {
Parameter_IsList(Parameter): boolean;
Parameter_IsListOfSimple(Parameter): boolean;
Parameter_IsPolyOfSimple(Parameter): boolean;
Schema_ActionName(Schema): string;
MethodParameter_ActionName: string;
Parameter_SetAzNameMapsTo(string, Parameter): void;
Parameter_InGlobal(Parameter): boolean;
Parameter_IsHidden(Parameter): boolean;
Expand Down Expand Up @@ -290,5 +290,6 @@ export interface CodeModelAz {
inputProperties: Map<CodeModelTypes, RenderInput>,
dependencies: DataGraph,
);
GetActionData(): any[];
GetTestUniqueResource: boolean;
}
124 changes: 107 additions & 17 deletions src/generate/CodeModelAzImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export class CodeModelCliImpl implements CodeModelAz {
substack: Array<[Parameter[], number]>;
currentSubOptionIndex: number;
paramActionNameReference: Map<Schema, string>;
allActions: Map<Parameter, string>;
private _testScenario: any;
private _defaultTestScenario: any[];
private _configuredScenario: boolean;
Expand Down Expand Up @@ -475,6 +476,7 @@ export class CodeModelCliImpl implements CodeModelAz {

private setParamAzUniqueNames() {
this.paramActionNameReference = new Map<Schema, string>();
this.allActions = new Map<Parameter, string>();
const nameActionReference: Map<string, ActionParam> = new Map<string, ActionParam>();
const pythonReserveWord = ['all', 'id', 'format', 'type', 'filter'];
if (this.SelectFirstCommandGroup()) {
Expand Down Expand Up @@ -692,10 +694,15 @@ export class CodeModelCliImpl implements CodeModelAz {
preAction.action.schema,
preActionUniqueName,
);
this.allActions.set(
preAction.action,
preActionUniqueName,
);
this.paramActionNameReference.set(
param.schema,
actionUniqueName,
);
this.allActions.set(param, actionUniqueName);
nameActionReference.set(
preActionUniqueName,
preAction,
Expand All @@ -711,6 +718,7 @@ export class CodeModelCliImpl implements CodeModelAz {
param.schema,
actionName,
);
this.allActions.set(param, actionName);
}
}
} while (this.SelectNextMethodParameter());
Expand Down Expand Up @@ -752,6 +760,87 @@ export class CodeModelCliImpl implements CodeModelAz {
}
});
}

public GetActionData() {
const actions = [];
SchemaType.Array;
this.allActions.forEach((actionName: string, param: Parameter) => {
if (actionName === 'AddEventGridDataConnection') {
param;
}
const action = {
actionName: actionName,
actionType: 'KeyValue',
};
action.actionType = this.Parameter_IsPositional(param)
? 'Positional'
: action.actionType;
action.actionType = this.Parameter_IsShorthandSyntax(param)
? 'ShortHandSyntax'
: action.actionType;
action['mapsTo'] = this.Parameter_MapsTo(param);
action['type'] = this.Schema_Type(param.schema);
action['nameAz'] = this.Parameter_NameAz(param);
if (action['type'] === SchemaType.Array) {
action['baseClass'] = '_AppendAction';
} else {
action['baseClass'] = 'Action';
}
action['subProperties'] = [];
action['subPropertiesMapsTo'] = [];
action['subPropertiesNamePython'] = [];
action['subPropertiesNameAz'] = [];
action['constants'] = {};
const baseParam = param['polyBaseParam'];
const keyToMatch = baseParam?.schema?.discriminator?.property?.language.python?.name;
const valueToMatch = param.schema?.['discriminatorValue'];
const allSubParameters = [];
if (this.EnterSubMethodParameters(param) && this.SelectFirstMethodParameter(true)) {
do {
const tmpParam = this.SubMethodParameter;
allSubParameters.push(tmpParam);
const pythonName = this.Parameter_NamePython(tmpParam);
const mapsTo = this.Parameter_MapsTo(tmpParam);
const nameAz = this.Parameter_NameAz(tmpParam);
const subType = this.Parameter_Type(tmpParam);
if (
this.Parameter_Type(tmpParam) === SchemaType.Constant &&
!isNullOrUndefined(tmpParam.schema['value']?.['value'])
) {
action['constants'][
`'${pythonName}'`
] = `'${tmpParam.schema['value']['value']}'`;
} else if (tmpParam['readOnly']) {
continue;
} else if (
keyToMatch === pythonName &&
!isNullOrUndefined(keyToMatch) &&
!isNullOrUndefined(valueToMatch) &&
tmpParam['isDiscriminator']
) {
action['constants'][`'${keyToMatch}'`] = `'${valueToMatch}'`;
} else {
action['subProperties'].push({
namePython: pythonName,
nameAz: nameAz,
type: subType,
});
action['subPropertiesMapsTo'].push(mapsTo);
action['subPropertiesNamePython'].push(pythonName);
action['subPropertiesNameAz'].push(nameAz);
}
} while (this.SelectNextMethodParameter(true));
if (action['actionType'] === 'Positional') {
const keys = this.Parameter_PositionalKeys(param, allSubParameters);
action['subPropertiesNamePython'] = keys;
}
}
SchemaType.Dictionary;
actions.push(action);
this.ExitSubMethodParameters();
});
return actions;
}
//= ================================================================================================================
// Extension level information
// autorest.az will have support for multiple extensions from single swagger file.
Expand Down Expand Up @@ -1659,11 +1748,12 @@ export class CodeModelCliImpl implements CodeModelAz {
param.language['az'].mapsto = newName;
}

public Schema_ActionName(schema: Schema = this.MethodParameter.schema) {
public get MethodParameter_ActionName() {
const schema = this.MethodParameter.schema;
if (this.paramActionNameReference.has(schema)) {
return this.paramActionNameReference.get(schema);
}
return null;
return undefined;
}

public get MethodParameter_Name(): string {
Expand Down Expand Up @@ -1762,13 +1852,13 @@ export class CodeModelCliImpl implements CodeModelAz {
return this.Parameter_IsShorthandSyntax(this.MethodParameter);
}

public isComplexSchema(type: string): boolean {
public isComplexSchema(type: string, param: any): boolean {
if (
type === SchemaType.Array ||
type === SchemaType.Object ||
type === SchemaType.Dictionary ||
type === SchemaType.Any ||
this.MethodParameter.language['cli'].json === true
param?.language?.['cli']?.json === true
) {
return true;
} else {
Expand Down Expand Up @@ -1861,22 +1951,22 @@ export class CodeModelCliImpl implements CodeModelAz {
) {
return false;
} else if (p['schema'].type === SchemaType.Array) {
if (this.isComplexSchema(p['schema']?.elementType?.type)) {
if (this.isComplexSchema(p['schema']?.elementType?.type, p['schema'])) {
return false;
}
for (const mp of values(p['schema']?.elementType?.properties)) {
if (this.isComplexSchema(mp['schema'].type)) {
if (this.isComplexSchema(mp['schema'].type, mp['schema'])) {
return false;
}
}
for (const parent of values(p['schema']?.elementType?.parents?.all)) {
for (const pp of values(parent['properties'])) {
if (this.isComplexSchema(pp['schema'].type)) {
if (this.isComplexSchema(pp['schema'].type, pp['schema'])) {
return false;
}
}
}
} else if (this.isComplexSchema(p['schema'].type)) {
} else if (this.isComplexSchema(p['schema'].type, p['schema'])) {
return false;
}
}
Expand All @@ -1901,18 +1991,18 @@ export class CodeModelCliImpl implements CodeModelAz {
return false;
} else if (p['schema'].type === SchemaType.Array) {
for (const mp of values(p['schema']?.elementType?.properties)) {
if (this.isComplexSchema(mp['schema'].type)) {
if (this.isComplexSchema(mp['schema'].type, mp['schema'])) {
return false;
}
}
for (const parent of values(p['schema']?.elementType?.parents?.all)) {
for (const pp of values(parent['properties'])) {
if (this.isComplexSchema(pp['schema'].type)) {
if (this.isComplexSchema(pp['schema'].type, pp['schema'])) {
return false;
}
}
}
} else if (this.isComplexSchema(p['schema'].type)) {
} else if (this.isComplexSchema(p['schema'].type, p['schema'])) {
// objects.objects
return false;
}
Expand All @@ -1934,7 +2024,7 @@ export class CodeModelCliImpl implements CodeModelAz {
if (mp['readOnly']) {
continue;
}
if (this.isComplexSchema(mp['schema'].type)) {
if (this.isComplexSchema(mp['schema'].type, mp['schema'])) {
return false;
}
}
Expand All @@ -1943,12 +2033,12 @@ export class CodeModelCliImpl implements CodeModelAz {
if (pp['readOnly']) {
continue;
}
if (this.isComplexSchema(pp['schema'].type)) {
if (this.isComplexSchema(pp['schema'].type, pp['schema'])) {
return false;
}
}
}
} else if (this.isComplexSchema(p.type)) {
} else if (this.isComplexSchema(p.type, p)) {
// dicts.objects or dicts.dictionaries
return false;
}
Expand Down Expand Up @@ -2005,7 +2095,7 @@ export class CodeModelCliImpl implements CodeModelAz {
private Parameter_IsSimpleArray(param: Parameter): boolean {
if (this.Parameter_Type(param) === SchemaType.Array) {
const elementType = param.schema['elementType'].type;
if (!this.isComplexSchema(elementType)) {
if (!this.isComplexSchema(elementType, param.schema)) {
return true;
}
}
Expand All @@ -2017,7 +2107,7 @@ export class CodeModelCliImpl implements CodeModelAz {
return false;
}

if (this.isComplexSchema(this.Parameter_Type(param))) {
if (this.isComplexSchema(this.Parameter_Type(param), param)) {
return true;
}
return false;
Expand Down Expand Up @@ -2109,7 +2199,7 @@ export class CodeModelCliImpl implements CodeModelAz {
if (schema.language['cli'].json === true) {
return true;
}
if (this.isComplexSchema(this.Schema_Type(schema))) {
if (this.isComplexSchema(this.Schema_Type(schema), schema)) {
return true;
}
return false;
Expand Down
8 changes: 3 additions & 5 deletions src/generate/generators/CoreFull.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { CliTopInit } from '../renders/CliTopInit';
import { CliMainDocSourceJsonMap } from '../renders/extraMain/CliMainDocSourceJsonMap';
import { CliMainRequirement } from '../renders/extraMain/CliMainRequirement';
import { CliMainSetupPy } from '../renders/extraMain/CliMainSetupPy';
import { GenerateAzureCliActions } from '../renders/generated/CliActions';
import { GenerateAzureCliClientFactory } from '../renders/generated/CliClientFactory';
import { CliCommands } from '../renders/generated/CliCommands';
import { GenerateAzureCliCustom } from '../renders/generated/CliCustom';
Expand All @@ -26,6 +25,7 @@ import { CliTestStep, NeedPreparers } from '../renders/tests/CliTestStep';
import { GenerateMetaFile } from '../renders/CliMeta';
import { CliCmdletTest } from '../renders/tests/CliTestCmdlet';
import { SimpleTemplate } from '../renders/TemplateBase';
import { CliActions } from '../renders/generated/CliActions';

export class AzCoreFullGenerator extends GeneratorBase {
constructor(model: CodeModelAz) {
Expand Down Expand Up @@ -53,9 +53,6 @@ export class AzCoreFullGenerator extends GeneratorBase {
files[
path.join(model.azOutputFolder, 'generated/_validators.py')
] = GenerateAzureCliValidators(model);
files[
path.join(model.azOutputFolder, 'generated/action.py')
] = GenerateAzureCliActions(model);
files[
path.join(model.azOutputFolder, 'generated/__init__.py')
] = GenerateNamespaceInit(model);
Expand All @@ -74,6 +71,7 @@ export class AzCoreFullGenerator extends GeneratorBase {
files[
path.join(model.azOutputFolder, 'manual/__init__.py')
] = GenerateNamespaceInit(model);
await this.generateFullSingleAndAddtoOutput(new CliActions(model));
await this.generateFullSingleAndAddtoOutput(new CliCommands(model));
await this.generateFullSingleAndAddtoOutput(new CliTopAction(model));
await this.generateFullSingleAndAddtoOutput(new CliTopCustom(model));
Expand Down Expand Up @@ -151,7 +149,7 @@ export class AzCoreFullGenerator extends GeneratorBase {
PathConstants.templateRootFolder,
PathConstants.testFolder,
PathConstants.cmdletFolder,
PathConstants.conftestFile + '.njx',
PathConstants.conftestFile + PathConstants.njxFileExtension,
),
),
);
Expand Down
8 changes: 3 additions & 5 deletions src/generate/generators/CoreIncre.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { CliTopHelp } from '../renders/CliTopHelp';
import { CliTopInit } from '../renders/CliTopInit';
import { CliMainRequirement } from '../renders/extraMain/CliMainRequirement';
import { CliMainSetupPy } from '../renders/extraMain/CliMainSetupPy';
import { GenerateAzureCliActions } from '../renders/generated/CliActions';
import { GenerateAzureCliClientFactory } from '../renders/generated/CliClientFactory';
import { CliCommands } from '../renders/generated/CliCommands';
import { GenerateAzureCliCustom } from '../renders/generated/CliCustom';
Expand All @@ -31,6 +30,7 @@ import { GenerateMetaFile } from '../renders/CliMeta';
import { CliExtSetupPy } from '../renders/extraExt/CliExtSetupPy';
import { CliCmdletTest } from '../renders/tests/CliTestCmdlet';
import { SimpleTemplate } from '../renders/TemplateBase';
import { CliActions } from '../renders/generated/CliActions';

export class AzCoreIncrementalGenerator extends GeneratorBase {
constructor(model: CodeModelAz) {
Expand Down Expand Up @@ -59,9 +59,6 @@ export class AzCoreIncrementalGenerator extends GeneratorBase {
this.files[
path.join(PathConstants.generatedFolder, PathConstants.validatorsFile)
] = GenerateAzureCliValidators(this.model);
this.files[
path.join(PathConstants.generatedFolder, PathConstants.actionFile)
] = GenerateAzureCliActions(this.model);
this.files[
path.join(PathConstants.generatedFolder, PathConstants.initFile)
] = GenerateNamespaceInit(this.model);
Expand All @@ -81,6 +78,7 @@ export class AzCoreIncrementalGenerator extends GeneratorBase {
] = GenerateNamespaceInit(this.model);
}

await this.generateIncrementalSingleAndAddtoOutput(new CliActions(this.model));
await this.generateIncrementalSingleAndAddtoOutput(new CliCommands(this.model));
// Add Import and run method from generated folder (Init)
await this.generateIncrementalSingleAndAddtoOutput(new CliTopInit(this.model));
Expand Down Expand Up @@ -177,7 +175,7 @@ export class AzCoreIncrementalGenerator extends GeneratorBase {
PathConstants.templateRootFolder,
PathConstants.testFolder,
PathConstants.cmdletFolder,
PathConstants.conftestFile + '.njx',
PathConstants.conftestFile + PathConstants.njxFileExtension,
),
),
);
Expand Down
Loading