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

Commit

Permalink
multiline: respect comment and start space (#462)
Browse files Browse the repository at this point in the history
  • Loading branch information
changlong-liu authored Jul 1, 2020
1 parent b4eabd6 commit ebb4d4f
Show file tree
Hide file tree
Showing 14 changed files with 278 additions and 238 deletions.
2 changes: 2 additions & 0 deletions src/plugins/azgenerator/CodeModelAz.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,10 @@ export interface CodeModelAz {
GetPreparerEntities(): any[];
GatherInternalResource();
FindExampleWaitById(id: string): string[][];
GetExampleItems(example: CommandExample, isTest: boolean, commandParams: any): string[];
RandomizeNames: boolean;

// readme config
CliCoreLib: string;

}
12 changes: 8 additions & 4 deletions src/plugins/azgenerator/CodeModelAzImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,16 @@ export class CodeModelCliImpl implements CodeModelAz {

}


public get RandomizeNames(): boolean {
if (this.options?.['randomize-names']) return true;
return false;
}

public get FormalizeNames(): boolean {
if (this.options?.['formalize-names']) return true;
return false;
}

private calcOptionRequiredByMethod() {
if (this.SelectFirstCommandGroup()) {
do {
Expand Down Expand Up @@ -1910,10 +1914,10 @@ export class CodeModelCliImpl implements CodeModelAz {
let hasRG = false;
for (let param of example.Parameters) {
let param_value = param.value;
if (isTest) {
let replaced_value = this.resource_pool.addEndpointResource(param_value, param.isJson, param.isKeyValues, [], [], param);
if (isTest || this.FormalizeNames) {
let replaced_value = this.resource_pool.addEndpointResource(param_value, param.isJson, param.isKeyValues, [], [], param, isTest);
if (replaced_value == param_value) {
replaced_value = this.resource_pool.addParamResource(param.defaultName, param_value, param.isJson, param.isKeyValues);
replaced_value = this.resource_pool.addParamResource(param.defaultName, param_value, param.isJson, param.isKeyValues, isTest);
}
param_value = replaced_value;
}
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/azgenerator/Generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ export async function GenerateAll(model: CodeModelAz,
files[path + "generated/_client_factory.py"] = GenerateAzureCliClientFactory(model);
files[path + "generated/_validators.py"] = GenerateAzureCliValidators(model);
files[path + "generated/action.py"] = GenerateAzureCliActions(model);
files[path + "generated/_help.py"] = GenerateAzureCliHelp(model, debug);
files[path + "generated/__init__.py"] = GenerateNamespaceInit(model);
files[path + "tests/__init__.py"] = GenerateAzureCliTestInit(model);
files[path + "tests/latest/test_" + model.Extension_NameUnderscored + "_scenario.py"] = GenerateAzureCliTestScenario(model);
if (NeedPreparer()) files[path + "tests/latest/preparers.py"] = GenerateAzureCliTestPrepare(model);
files[path + "generated/_help.py"] = GenerateAzureCliHelp(model, debug);
files[path + "tests/latest/__init__.py"] = GenerateNamespaceInit(model);
files[path + "azext_metadata.json"] = GenerateAzureCliAzextMetadata(model);
files[path + "vendored_sdks/__init__.py"] = GenerateNamespaceInit(model);
Expand Down
87 changes: 62 additions & 25 deletions src/plugins/azgenerator/ScenarioTool.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@

import * as path from "path"
import { CommandExample, ExampleParam } from "./CodeModelAz";
import { deepCopy, isDict } from "../../utils/helper"
import { deepCopy, isDict, ToCamelCase, changeCamelToDash } from "../../utils/helper"
import { Example } from "@azure-tools/codemodel";
import { EnglishPluralizationService } from "@azure-tools/codegen";


function MethodToOrder(httpMethod: string): number {
Expand Down Expand Up @@ -214,35 +215,58 @@ class ResourceObject {
return getResourceKey(this.class_name, this.object_name);
}

public get placeholder(): string {
return '{' + this.key + '}';
public placeholder(isTest: boolean): string {
if (isTest) return '{' + this.key + '}';
return getResourceKey(this.class_name, this.object_name, true);

}
}

function singlizeLast(word:string) {
let eps = new EnglishPluralizationService();
let ws = changeCamelToDash(word).split('-');
let l = ws.length;
ws[l-1] = eps.singularize(ws[l-1]);
return ws.join('-');
}

let keyCache = {} //class_name+objectname->key
let formalCache = {}
let keySeq = {} // class_name ->seq
export function getResourceKey(class_name: string, object_name: string): string {
export function getResourceKey(class_name: string, object_name: string, formalName=false): string {
let longKey = (resourceClassKeys[class_name] || class_name) + '_' + object_name;
if (longKey in keyCache) {
if (formalName && longKey in formalCache) {
return formalCache[longKey];
}
if (!formalName && longKey in keyCache) {
return keyCache[longKey];
}
if (class_name in keySeq) {

if (keySeq.hasOwnProperty(class_name)) {
let key = (resourceClassKeys[class_name] || class_name) + '_' + keySeq[class_name];
keySeq[class_name] += 1;
keyCache[longKey] = key;
formalCache[longKey] = ToCamelCase(`my-${singlizeLast(class_name)}${keySeq[class_name]-1}`);
if (preparerInfos[class_name]?.name) { // is external resource
keyCache[longKey] = key;
}
else {
keyCache[longKey] = ToCamelCase(`my-${singlizeLast(class_name)}${keySeq[class_name]-1}`);
}
}
else {
keySeq[class_name] = 2;
formalCache[longKey] = ToCamelCase(`my-${singlizeLast(class_name)}`);
if (preparerInfos[class_name]?.name) { // is external resource
keyCache[longKey] = resourceClassKeys[class_name] || class_name;
}
else { // is internal resource
// generally, internal resource object_name is shorter than class_name
keyCache[longKey] = object_name;
// keyCache[longKey] = object_name;
keyCache[longKey] = ToCamelCase(`my-${singlizeLast(class_name)}`);
}

}
return keyCache[longKey];

return formalName? formalCache[longKey]: keyCache[longKey];
}

export class ResourcePool {
Expand Down Expand Up @@ -425,28 +449,29 @@ export class ResourcePool {
return false;

}
public addEndpointResource(endpoint: any, isJson: boolean, isKeyValues: boolean, placeholders: string[], resources: string[], exampleParam: ExampleParam) {
public addEndpointResource(endpoint: any, isJson: boolean, isKeyValues: boolean, placeholders: string[], resources: string[], exampleParam: ExampleParam, isTest=true) {
if (placeholders == undefined) placeholders = new Array();
if (isJson) {
let body = typeof endpoint == 'string' ? JSON.parse(endpoint) : endpoint;
if (typeof body == 'object') {
if (body instanceof Array) {
body = body.map((value) => {
return this.addEndpointResource(value, typeof value == 'object', isKeyValues, placeholders, resources, exampleParam);
return this.addEndpointResource(value, typeof value == 'object', isKeyValues, placeholders, resources, exampleParam, isTest);
});
}
else if (isDict(body)) {
for (let k in body) {
body[k] = this.addEndpointResource(body[k], typeof body[k] == 'object', isKeyValues, placeholders, resources, exampleParam);
body[k] = this.addEndpointResource(body[k], typeof body[k] == 'object', isKeyValues, placeholders, resources, exampleParam, isTest);
}
}
}
else {
body = this.addEndpointResource(body, false, isKeyValues, placeholders, resources, exampleParam);
body = this.addEndpointResource(body, false, isKeyValues, placeholders, resources, exampleParam, isTest);
}

if (typeof endpoint == 'string') {
return this.formatable(JSON.stringify(body).split(/[\r\n]+/).join(""), placeholders);
let ret = JSON.stringify(body).split(/[\r\n]+/).join("");
return isTest ? this.formatable(ret, placeholders): ret;
}
else {
return body;
Expand All @@ -470,12 +495,22 @@ export class ResourcePool {
let kv = attr.split("=");
if (ret.length > 0) ret += " ";
if (kv[1].length >= 2 && kv[1][0] == '"' && kv[1][kv[1].length - 1] == '"') {
let v = this.addEndpointResource(kv[1].substr(1, kv[1].length - 2), isJson, false, placeholders, resources, exampleParam);
ret += `${kv[0]}="${this.formatable(v, placeholders)}"`;
let v = this.addEndpointResource(kv[1].substr(1, kv[1].length - 2), isJson, false, placeholders, resources, exampleParam, isTest);
if (isTest) {
ret += `${kv[0]}="${this.formatable(v, placeholders)}"`;
}
else {
ret += `${kv[0]}="${v}"`;
}
}
else {
let v = this.addEndpointResource(kv[1], isJson, false, placeholders, resources, exampleParam);
ret += `${kv[0]}=${this.formatable(v, placeholders)}`;
let v = this.addEndpointResource(kv[1], isJson, false, placeholders, resources, exampleParam, isTest);
if (isTest) {
ret += `${kv[0]}=${this.formatable(v, placeholders)}`;
}
else {
ret += `${kv[0]}=${v}`;
}
}
}
return ret;
Expand All @@ -485,7 +520,9 @@ export class ResourcePool {
if (nodes.length < 3 || nodes[0].length > 0 || nodes[1].toLowerCase() != SUBSCRIPTIONS) {
return endpoint;
}
nodes[2] = `{${ResourcePool.KEY_SUBSCRIPTIONID}}`;
if (isTest) {
nodes[2] = `{${ResourcePool.KEY_SUBSCRIPTIONID}}`;
}
if (placeholders.indexOf(nodes[2]) < 0) {
placeholders.push(nodes[2]);
}
Expand All @@ -501,9 +538,9 @@ export class ResourcePool {
}
else {
resource_object = this.addTreeResource(resource, nodes[i + 1], resource_object);
nodes[i + 1] = resource_object.placeholder;
if (placeholders.indexOf(resource_object.placeholder) < 0) {
placeholders.push(resource_object.placeholder);
nodes[i + 1] = resource_object.placeholder(isTest);
if (placeholders.indexOf(resource_object.placeholder(isTest)) < 0) {
placeholders.push(resource_object.placeholder(isTest));
}
if (resources.indexOf(resource) < 0) {
resources.push(resource);
Expand All @@ -515,7 +552,7 @@ export class ResourcePool {
return nodes.join('/');
}

public addParamResource(param_name: string, param_value: string, isJson: boolean, isKeyValues: boolean): string {
public addParamResource(param_name: string, param_value: string, isJson: boolean, isKeyValues: boolean, isTest=true): string {
if (typeof param_value !== 'string' || isJson || isKeyValues) return param_value;

if (param_name.startsWith('--')) {
Expand All @@ -531,7 +568,7 @@ export class ResourcePool {
}
let resource_object = this.addMapResource(resource, param_value);
if (resource_object) {
return resource_object.placeholder;
return resource_object.placeholder(isTest);
}
else {
return param_value;
Expand Down
19 changes: 3 additions & 16 deletions src/plugins/azgenerator/TemplateAzureCliHelp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ function generateCommandGroupHelp(model: CodeModelAz, subCommandGroupName = "",
output.push("helps['" + model.CommandGroup_Name + "'] = \"\"\"");
}
output.push(" type: group");
let shortSummary = " short-summary: " + model.CommandGroup_Help;
let shortSummary = " short-summary: " + model.CommandGroup_Help.trim();
if(subCommandGroupName != "") {
shortSummary = shortSummary + " sub group " + subCommandGroupName.split(" ").pop();
}
Expand Down Expand Up @@ -271,7 +271,7 @@ function generateCommandHelp(model: CodeModelAz, debug: boolean = false) {
// there will be just one method for create, update, delete, show, etc.
// there may be a few list methods, so let's just take description from the first one.
// as we can't use all of them
let shortSummary = " short-summary: " + model.Command_Help;
let shortSummary = " short-summary: " + model.Command_Help.trim();
if (debug) {
if (!shortSummary.trimRight().endsWith(".")) {
shortSummary += ".";
Expand All @@ -293,20 +293,7 @@ function generateCommandHelp(model: CodeModelAz, debug: boolean = false) {

//output.push ("# " + example_id);
let parameters: string[] = [];

parameters.push("az");
parameters = parameters.concat(commandHead.split(" "));
//parameters.push(method);

for (let param of example.Parameters) {
let slp = param.value;
if (!param.isKeyValues) {
slp = ToJsonString(slp);
}
//parameters += " " + k + " " + slp;
parameters.push(param.name);
parameters.push(slp);
}
parameters = model.GetExampleItems(example, false, undefined);
output.push(" - name: " + example.Title);
output.push(" text: |-");
let line = " " + parameters.join(' ');
Expand Down
5 changes: 4 additions & 1 deletion src/plugins/azgenerator/TemplateAzureCliTestScenario.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ export function GenerateAzureCliTestScenario(model: CodeModelAz): string[] {
}
}
}
if (disabled) {
steps.push(" pass");
}
steps.push("");
steps.push("");
funcScenario.push(...ToMultiLine(` ${functionName}(test${parameterLine()})`));
Expand Down Expand Up @@ -193,7 +196,7 @@ function InitiateDependencies(model: CodeModelAz, imports: string[], decorators:
if (hasCreateExample && model.RandomizeNames)
{
let snakeName = ToSnakeCase(class_name);
ToMultiLine(` '${kargs_key}': self.create_random_name(prefix='${snakeName}'[:${Math.floor(snakeName.length/2)}], length=${snakeName.length}),`, initiates);
ToMultiLine(` '${kargs_key}': self.create_random_name(prefix='${object_name}'[:${Math.floor(object_name.length/2)}], length=${object_name.length}),`, initiates);
}
else
initiates.push(` '${kargs_key}': '${object_name}',`); // keep the original name in example if there is no create example in the test-scenario
Expand Down
1 change: 1 addition & 0 deletions src/test/scenarios/datafactory/configuration/readme.az.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ az:
extensions: datafactory
package-name: azure-mgmt-datafactory
namespace: azure.mgmt.datafactory
formalize-names: true
az-output-folder: $(azure-cli-extension-folder)/src/datafactory
python-sdk-output-folder: "$(az-output-folder)/azext_datafactory/vendored_sdks/datafactory"

Expand Down
Loading

0 comments on commit ebb4d4f

Please sign in to comment.