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

Commit

Permalink
Manual config feature gap after refactor (#827)
Browse files Browse the repository at this point in the history
* manual-config-feature-gap-after-refactor

* fix-test

* add test config

* manual config features and tests

* scenario test

* add array type example

* feature type

* remove unused reference
  • Loading branch information
qiaozha authored Apr 19, 2021
1 parent 96ea141 commit 4780d46
Show file tree
Hide file tree
Showing 10 changed files with 162 additions and 30 deletions.
12 changes: 10 additions & 2 deletions src/generate/codemodel/Command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ export interface CommandModel {
Command_Mode: string;
Command_Type: string;
Command_GenericSetterArgName: string;
// Command_Features: [string, string];
// Command_Imports: [string, string[]];
Command_Features: Record<string, string | number>;
Command_Imports: Record<string, any>;
}

export class CommandModelImpl implements CommandModel {
Expand Down Expand Up @@ -169,4 +169,12 @@ export class CommandModelImpl implements CommandModel {
public get Command_ResourceType(): string | undefined {
return this.baseModel.formResourceType(this.Command.language['cli']?.['resource-type']);
}

public get Command_Features(): Record<string, string | number> {
return this.Command.language['az']['features'];
}

public get Command_Imports(): Record<string, any> {
return this.Command.language['az']['imports'];
}
}
12 changes: 10 additions & 2 deletions src/generate/codemodel/CommandGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ export interface CommandGroupModel {
CommandGroup_CustomCommandTypeName(group?: OperationGroup): string;
CommandGroup_Referenced: boolean;
CommandGroup_ShowExample: CommandExample;
// CommandGroup_Features: [string, string];
// CommandGroup_Imports: [string, string[]];
CommandGroup_Features: Record<string, string | number>;
CommandGroup_Imports: Record<string, any>;
}

export class CommandGroupModelImpl implements CommandGroupModel {
Expand Down Expand Up @@ -161,4 +161,12 @@ export class CommandGroupModelImpl implements CommandGroupModel {
this.baseHandler.GetModuleOperationName(group);
return customName;
}

public get CommandGroup_Features(): Record<string, string | number> {
return this.CommandGroup.language['az']['features'];
}

public get CommandGroup_Imports(): Record<string, any> {
return this.CommandGroup.language['az']['imports'];
}
}
2 changes: 1 addition & 1 deletion src/generate/codemodel/Example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ export class ExampleModelImpl implements ExampleModel {
ret['swagger git status'] = getGitStatus(swaggerFolder).split('\n');
}

const azpkg = path.join(__dirname, '..', '..', '..', 'package.json');
const azpkg = path.join(__dirname, '..', '..', '..', '..', 'package.json');
const pjson = JSON.parse(await readFile(azpkg));
ret['package info'] = `${pjson.name} ${pjson.version}`;
return ret;
Expand Down
13 changes: 10 additions & 3 deletions src/generate/codemodel/MethodParameter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ export interface MethodParameterModel {
MethodParameter_IsDiscriminator: boolean;
MethodParameter_IdPart: string;
MethodParameter_ArgGroup: string;
// MethodParameter_Features: [string, string];
// MethodParameter_Imports: [string, string[]];
MethodParameter: Parameter;
SubMethodParameter: Parameter;

Expand All @@ -45,13 +43,14 @@ export interface MethodParameterModel {
MethodParameter_IsPositional: boolean;
MethodParameter_IsShorthandSyntax: boolean;
MethodParameter_PositionalKeys: string[];
MethodParameter_Features: Record<string, string | number>;
MethodParameter_Imports: Record<string, any>;
}

export class MethodParameterModelImpl implements MethodParameterModel {
private commandGroupHandler: CommandGroupModel;
private methodHandler: MethodModel;
private parameterHandler: ParameterModel;
private schemaHandler: SchemaModel;
constructor(public baseHandler: CodeModelCliImpl) {
const { commandGroupHandler, methodHandler, parameterHandler } = baseHandler.GetHandler();
this.commandGroupHandler = commandGroupHandler;
Expand Down Expand Up @@ -326,4 +325,12 @@ export class MethodParameterModelImpl implements MethodParameterModel {
subMethodParams,
);
}

public get MethodParameter_Features(): Record<string, string | number> {
return this.MethodParameter.language['az']['features'];
}

public get MethodParameter_Imports(): Record<string, any> {
return this.MethodParameter.language['az']['imports'];
}
}
55 changes: 51 additions & 4 deletions src/generate/renders/generated/CliCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
import * as path from 'path';
export class CliCommands extends TemplateBase {
private importProfile = false;
private imports: Map<string, string[]> = new Map<string, string[]>();
private importClientFactories = [];
private lineTooLong = false;
private needWaitCommand = false;
Expand Down Expand Up @@ -44,6 +45,19 @@ export class CliCommands extends TemplateBase {
return `'${str}'`;
}

addImport(importKey: string, importValues: string[]) {
let list = [];
if (this.imports.has(importKey)) {
list = this.imports.get(importKey);
}
importValues.forEach((val) => {
if (list.indexOf(val) === -1) {
list.push(val);
}
});
this.imports.set(importKey, importValues);
}

extensionConverter(item: any): any {
if (!isNullOrUndefined(item['parent']) && !isNullOrUndefined(item['name'])) {
item['name'] = item['parent'] + ' ' + item['name'];
Expand All @@ -58,6 +72,20 @@ export class CliCommands extends TemplateBase {
item['propertiesString'][prop] = this.pythonString(item[prop]);
}
});
if (!isNullOrUndefined(item['features'])) {
Object.keys(item['features']).forEach((prop: string) => {
item['propertiesString'][prop] = item['features'][prop];
});
}
if (!isNullOrUndefined(item['imports'])) {
Object.keys(item['imports']).forEach((prop: string) => {
if (typeof item['imports'][prop] === 'string') {
this.addImport(prop, [item['imports'][prop]]);
} else if (Array.isArray(item['imports'][prop])) {
this.addImport(prop, item['imports'][prop]);
}
});
}
this.extraNonStringProperties.forEach((prop) => {
if (!isNullOrUndefined(item[prop])) {
item['propertiesString'][prop] = item[prop];
Expand Down Expand Up @@ -94,6 +122,20 @@ export class CliCommands extends TemplateBase {
item['propertiesString'][prop] = this.pythonString(item[prop]);
}
});
if (!isNullOrUndefined(item['features'])) {
Object.keys(item['features']).forEach((prop: string) => {
item['propertiesString'][prop] = item['features'][prop];
});
}
if (!isNullOrUndefined(item['imports'])) {
Object.keys(item['imports']).forEach((prop: string) => {
if (typeof item['imports'][prop] === 'string') {
this.addImport(prop, [item['imports'][prop]]);
} else if (Array.isArray(item['imports'][prop])) {
this.addImport(prop, item['imports'][prop]);
}
});
}
this.extraNonStringProperties.forEach((prop) => {
if (!isNullOrUndefined(item[prop])) {
item['propertiesString'][prop] = item[prop];
Expand Down Expand Up @@ -134,7 +176,7 @@ export class CliCommands extends TemplateBase {
public async GetRenderData(): Promise<Record<string, unknown>> {
const { configHandler } = this.model.GetHandler();
let data = { imports: [], pylints: [] };
data['imports'].push([configHandler.CliCoreLib + '.commands', ['CliCommandType']]);
this.addImport(configHandler.CliCoreLib + '.commands', ['CliCommandType']);

const inputProperties: Map<CodeModelTypes, RenderInput> = new Map<
CodeModelTypes,
Expand Down Expand Up @@ -164,6 +206,8 @@ export class CliCommands extends TemplateBase {
'resourceType',
'mode',
'hasCommand',
'features',
'imports',
],
{ name: SortOrder.ASEC },
[],
Expand All @@ -186,6 +230,8 @@ export class CliCommands extends TemplateBase {
'needGeneric',
'genericSetterArgName',
'clientFactoryName',
'features',
'imports',
],
{},
[],
Expand All @@ -200,17 +246,17 @@ export class CliCommands extends TemplateBase {
];
data = { ...data, ...this.model.getModelData('extension', inputProperties, dependencies) };
if (this.importProfile) {
data['imports'].push([configHandler.CliCoreLib + '.profiles', ['ResourceType']]);
this.addImport(configHandler.CliCoreLib + '.profiles', ['ResourceType']);
}
if (
!isNullOrUndefined(this.importClientFactories) &&
Array.isArray(this.importClientFactories) &&
this.importClientFactories.length > 0
) {
data['imports'].push([
this.addImport(
configHandler.AzextFolder + '.generated._client_factory',
this.importClientFactories,
]);
);
}
data['pylints'].push(
'# pylint: disable=too-many-statements',
Expand All @@ -221,6 +267,7 @@ export class CliCommands extends TemplateBase {
data['pylints'].push('# pylint: disable=line-too-long');
}
data['azextFolder'] = configHandler.AzextFolder;
data.imports = Array.from(this.imports);
const result = { data: { imports: [], pylints: [] } };
result.data = data;
return result;
Expand Down
10 changes: 6 additions & 4 deletions src/generate/renders/generated/CliParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,9 @@ function getCommandBody(model: CodeModelAz, needGeneric = false, debug = false)
if (param?.schema?.type === SchemaType.Constant || param.readOnly) {
continue;
}
if (!isNullOrUndefined(param?.language?.python?.name)) {
allPythonParam.set(param.language.python.name, true);
const pythonParamName = parameterHandler.Parameter_NamePython(param);
if (!isNullOrUndefined(pythonParamName)) {
allPythonParam.set(pythonParamName, true);
}
}
if (!isNullOrUndefined(originalOperation.requests[0].parameters)) {
Expand All @@ -190,8 +191,9 @@ function getCommandBody(model: CodeModelAz, needGeneric = false, debug = false)
if (param?.schema?.type === SchemaType.Constant || param.readOnly) {
continue;
}
if (!isNullOrUndefined(param?.language?.python?.name)) {
allPythonParam.set(param.language.python.name, true);
const pythonParamName = parameterHandler.Parameter_NamePython(param);
if (!isNullOrUndefined(pythonParamName)) {
allPythonParam.set(pythonParamName, true);
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/modifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,12 @@ export class Modifiers {
) {
operation.language['az'].description =
commandDescriptionReplacer || operation.language['az'].description;
if (!isNullOrUndefined(features)) {
operation.language['az']['features'] = features;
}
if (!isNullOrUndefined(imports)) {
operation.language['az']['imports'] = imports;
}
if (isNullOrUndefined(commandReplacer)) {
return;
}
Expand Down Expand Up @@ -291,12 +297,6 @@ export class Modifiers {
Channel: Channel.Warning,
Text: ' newAzName:' + newAzName,
});
if (!isNullOrUndefined(features)) {
operation.language['az']['features'] = features;
}
if (!isNullOrUndefined(imports)) {
operation.language['az']['imports'] = imports;
}
}
if (
!isNullOrUndefined(operation.language['az'].subCommandGroup) &&
Expand Down
24 changes: 24 additions & 0 deletions test/scenarios/kusto/configuration/readme.az.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,28 @@ az:
package-name: azure-mgmt-kusto
az-output-folder: $(azure-cli-folder)/src/azure-cli/azure/cli/command_modules/kusto
python-sdk-output-folder: "$(az-output-folder)/vendored_sdks/kusto"

directive:
- where:
command: kusto cluster remove-language-extension
features:
exception_handler: handle_template_based_exception
imports:
azure.cli.core.exception: handle_template_based_exception
- where:
command: kusto cluster add-language-extension
features:
completer: get_resource_name_completion_list('Microsoft.Network/expressRouteCircuits')
imports:
azure.cli.core.utils: get_resource_name_completion_list
- where:
group: kusto database
features:
exception_handler: handle_template_based_exception
imports:
azure.cli.core.exception: handle_template_based_exception
- where:
command: kusto database list
features:
option_list: "['--private-cloud', 123, True]"
```
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
# pylint: disable=line-too-long

from azure.cli.core.commands import CliCommandType
from azure.cli.core.utils import get_resource_name_completion_list
from azure.cli.core.exception import handle_template_based_exception
from ..generated._client_factory import (
cf_cluster,
cf_cluster_principal_assignment,
Expand Down Expand Up @@ -68,13 +70,23 @@ def load_command_table(self, _):
g.custom_command('create', 'kusto_cluster_create', supports_no_wait=True)
g.custom_command('update', 'kusto_cluster_update', supports_no_wait=True)
g.custom_command('delete', 'kusto_cluster_delete', supports_no_wait=True, confirmation=True)
g.custom_command('add-language-extension', 'kusto_cluster_add_language_extension', supports_no_wait=True)
g.custom_command(
'add-language-extension',
'kusto_cluster_add_language_extension',
completer=get_resource_name_completion_list('Microsoft.Network/expressRouteCircuits'),
supports_no_wait=True,
)
g.custom_command('detach-follower-database', 'kusto_cluster_detach_follower_database', supports_no_wait=True)
g.custom_command('diagnose-virtual-network', 'kusto_cluster_diagnose_virtual_network', supports_no_wait=True)
g.custom_command('list-follower-database', 'kusto_cluster_list_follower_database')
g.custom_command('list-language-extension', 'kusto_cluster_list_language_extension')
g.custom_command('list-sku', 'kusto_cluster_list_sku')
g.custom_command('remove-language-extension', 'kusto_cluster_remove_language_extension', supports_no_wait=True)
g.custom_command(
'remove-language-extension',
'kusto_cluster_remove_language_extension',
exception_handler=handle_template_based_exception,
supports_no_wait=True,
)
g.custom_command('start', 'kusto_cluster_start', supports_no_wait=True)
g.custom_command('stop', 'kusto_cluster_stop', supports_no_wait=True)
g.custom_wait_command('wait', 'kusto_cluster_show')
Expand All @@ -96,8 +108,14 @@ def load_command_table(self, _):
)
g.custom_wait_command('wait', 'kusto_cluster_principal_assignment_show')

with self.command_group('kusto database', kusto_database, client_factory=cf_database, is_experimental=True) as g:
g.custom_command('list', 'kusto_database_list')
with self.command_group(
'kusto database',
kusto_database,
client_factory=cf_database,
exception_handler=handle_template_based_exception,
is_experimental=True,
) as g:
g.custom_command('list', 'kusto_database_list', option_list=['--private-cloud', 123, True])
g.custom_show_command('show', 'kusto_database_show')
g.custom_command('create', 'kusto_database_create', supports_no_wait=True)
g.custom_command('update', 'kusto_database_update', supports_no_wait=True)
Expand Down
Loading

0 comments on commit 4780d46

Please sign in to comment.