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

Commit

Permalink
Add cli-core-lib config to support replace lib (#456)
Browse files Browse the repository at this point in the history
Co-authored-by: xichen <xichen@microsoft.com>
  • Loading branch information
shawncx and msxichen authored Jun 23, 2020
1 parent 44930fb commit 256f976
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 19 deletions.
2 changes: 2 additions & 0 deletions readme.az.common.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
12 changes: 6 additions & 6 deletions src/plugins/azgenerator/Generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/azgenerator/TemplateAzureCliClientFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/azgenerator/TemplateAzureCliCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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("");
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/azgenerator/TemplateAzureCliCustom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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']) {
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/azgenerator/TemplateAzureCliInit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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#{}',");
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/azgenerator/TemplateAzureCliParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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("");
Expand Down Expand Up @@ -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");
Expand All @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/azgenerator/azgenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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("-", "_") + "/";
Expand Down

0 comments on commit 256f976

Please sign in to comment.