Add cli-core-lib config to support replace lib (#456)

Co-authored-by: xichen <xichen@microsoft.com>
This commit is contained in:
xichen 2020-06-23 20:32:45 +08:00 коммит произвёл GitHub
Родитель 44930fb6f2
Коммит 256f976810
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
8 изменённых файлов: 22 добавлений и 19 удалений

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

@ -2,6 +2,8 @@
``` yaml $(az)
extension-mode: experimental
# customize library used in extension. azure.cli.core by default
# cli-core-lib: azure.cli.core
cli:
naming:

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

@ -25,7 +25,7 @@ import { GenerateNamespaceInit } from "./TemplateAzureCliNamespaceInit"
import { GenerateAzureCliTestInit } from "./TemplateAzureCliTestInit"
export async function GenerateAll(model: CodeModelAz,
generateReport: any, debug: boolean) {
generateReport: any, debug: boolean, cliCoreLib: string) {
let files: any = {};
await model.init();
@ -37,10 +37,10 @@ export async function GenerateAll(model: CodeModelAz,
let pathTop = "";
let path = "azext_" + model.Extension_NameUnderscored + "/";
files[path + "generated/_params.py"] = GenerateAzureCliParams(model, debug);
files[path + "generated/commands.py"] = GenerateAzureCliCommands(model);
files[path + "generated/custom.py"] = GenerateAzureCliCustom(model);
files[path + "generated/_client_factory.py"] = GenerateAzureCliClientFactory(model);
files[path + "generated/_params.py"] = GenerateAzureCliParams(model, debug, cliCoreLib);
files[path + "generated/commands.py"] = GenerateAzureCliCommands(model, cliCoreLib);
files[path + "generated/custom.py"] = GenerateAzureCliCustom(model, cliCoreLib);
files[path + "generated/_client_factory.py"] = GenerateAzureCliClientFactory(model, cliCoreLib);
files[path + "generated/_validators.py"] = GenerateAzureCliValidators(model);
files[path + "generated/action.py"] = GenerateAzureCliActions(model);
files[path + "generated/_help.py"] = GenerateAzureCliHelp(model, debug);
@ -54,7 +54,7 @@ export async function GenerateAll(model: CodeModelAz,
files[path + "manual/__init__.py"] = GenerateNamespaceInit(model);
files[path + "action.py"] = GenerateTopLevelImport(model, "action");
files[path + "custom.py"] = GenerateTopLevelImport(model, "custom");
files[path + "__init__.py"] = GenerateAzureCliInit(model);
files[path + "__init__.py"] = GenerateAzureCliInit(model, cliCoreLib);
files[pathTop + "HISTORY.rst"] = GenerateAzureCliHistory(model);
files[pathTop + "README.md"] = GenerateAzureCliReadme(model);
files[pathTop + "setup.cfg"] = GenerateAzureCliSetupCfg(model);

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

@ -7,14 +7,14 @@ import { CodeModelAz } from "./CodeModelAz"
import { HeaderGenerator } from "./Header";
import { isNullOrUndefined } from "util";
export function GenerateAzureCliClientFactory(model: CodeModelAz): string[] {
export function GenerateAzureCliClientFactory(model: CodeModelAz, cliCoreLib: string): string[] {
let header: HeaderGenerator = new HeaderGenerator();
var output: string[] = header.getLines();
model.SelectFirstCommandGroup();
output.push("");
output.push("");
output.push("def cf_" + model.Extension_NameUnderscored + "_cl(cli_ctx, *_):");
output.push(" from azure.cli.core.commands.client_factory import get_mgmt_service_client");
output.push(" from " + cliCoreLib + ".commands.client_factory import get_mgmt_service_client");
output.push(" from ..vendored_sdks." + model.PythonOperationsName + " import " + model.PythonMgmtClient);
if (!isNullOrUndefined(model.Extension_ClientAuthenticationPolicy)) {

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

@ -10,13 +10,13 @@ import { isNullOrUndefined } from "util";
import { SchemaType } from "@azure-tools/codemodel";
let showCommandFunctionName = undefined;
export function GenerateAzureCliCommands(model: CodeModelAz): string[] {
export function GenerateAzureCliCommands(model: CodeModelAz, cliCoreLib: string): string[] {
let header: HeaderGenerator = new HeaderGenerator();
// this can't be currently reproduced
header.disableTooManyStatements = true;
header.disableTooManyLocals = true;
header.addFromImport("azure.cli.core.commands", ["CliCommandType"]);
header.addFromImport(cliCoreLib + ".commands", ["CliCommandType"]);
let output: string[] = []
output.push("");

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

@ -9,7 +9,7 @@ import { Capitalize, ToCamelCase, ToMultiLine, ToPythonString } from '../../util
import { CodeModelAz } from "./CodeModelAz";
import { HeaderGenerator } from "./Header";
export function GenerateAzureCliCustom(model: CodeModelAz): string[] {
export function GenerateAzureCliCustom(model: CodeModelAz, cliCoreLib: string): string[] {
let header: HeaderGenerator = new HeaderGenerator();
header.disableTooManyLines = true;
@ -29,7 +29,7 @@ export function GenerateAzureCliCustom(model: CodeModelAz): string[] {
}
if(required['nowait']) {
header.addFromImport("azure.cli.core.util", ["sdk_no_wait"]);
header.addFromImport(cliCoreLib + ".util", ["sdk_no_wait"]);
}
if(required['disableUnusedArgument']) {

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

@ -7,9 +7,9 @@ import { CodeModelAz } from "./CodeModelAz"
import { HeaderGenerator } from "./Header";
import { ToMultiLine } from "../../utils/helper"
export function GenerateAzureCliInit(model: CodeModelAz): string[] {
export function GenerateAzureCliInit(model: CodeModelAz, cliCoreLib: string): string[] {
let header: HeaderGenerator = new HeaderGenerator();
header.addFromImport("azure.cli.core", ["AzCommandsLoader"]);
header.addFromImport(cliCoreLib, ["AzCommandsLoader"]);
var output: string[] = header.getLines();
output.push("from azext_" + model.Extension_NameUnderscored + ".generated._help import helps # pylint: disable=unused-import");
@ -22,7 +22,7 @@ export function GenerateAzureCliInit(model: CodeModelAz): string[] {
output.push("class " + model.Extension_NameClass + "CommandsLoader(AzCommandsLoader):");
output.push("");
output.push(" def __init__(self, cli_ctx=None):");
output.push(" from azure.cli.core.commands import CliCommandType");
output.push(" from " + cliCoreLib + ".commands import CliCommandType");
output.push(" from azext_" + model.Extension_NameUnderscored + ".generated._client_factory import cf_" + model.Extension_NameUnderscored + "_cl");
output.push(" " + model.Extension_NameUnderscored + "_custom = CliCommandType(");
output.push(" operations_tmpl='azext_" + model.Extension_NameUnderscored + ".custom#{}',");

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

@ -20,7 +20,7 @@ let hasLocationValidator = false;
let hasTags = false;
let actions: string[] = [];
export function GenerateAzureCliParams(model: CodeModelAz, debug: boolean): string[] {
export function GenerateAzureCliParams(model: CodeModelAz, debug: boolean, cliCoreLib: string): string[] {
var output_args: string[] = [];
output_args.push("");
@ -65,7 +65,7 @@ export function GenerateAzureCliParams(model: CodeModelAz, debug: boolean): stri
if (hasResourceGroup) parameterImports.push("resource_group_name_type");
if (hasLocation) parameterImports.push("get_location_type");
header.addFromImport("azure.cli.core.commands.parameters", parameterImports);
header.addFromImport(cliCoreLib + ".commands.parameters", parameterImports);
let validatorImports: string[] = [];
if (hasLocationValidator) {
validatorImports.push("get_default_location_from_resource_group");
@ -74,7 +74,7 @@ export function GenerateAzureCliParams(model: CodeModelAz, debug: boolean): stri
validatorImports.push("validate_file_or_dict");
}
if (validatorImports.length > 0) {
header.addFromImport('azure.cli.core.commands.validators', validatorImports);
header.addFromImport(cliCoreLib + '.commands.validators', validatorImports);
}
if (hasActions) {

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

@ -9,9 +9,10 @@ export async function processRequest(host: Host) {
const debug = await host.GetValue('debug') || false;
//host.Message({Channel:Channel.Warning, Text:"in azgenerator processRequest"});
try {
const cliCoreLib = await host.GetValue('cli-core-lib') || 'azure.cli.core';
const session = await startSession<CodeModel>(host, {}, codeModelSchema);
let model = new CodeModelCliImpl(session);
let files: any = await GenerateAll(model, true, debug);
let files: any = await GenerateAll(model, true, debug, cliCoreLib);
if (model.SelectFirstExtension()) {
do {
let path = "azext_" + model.Extension_Name.replace("-", "_") + "/";