Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement optional powershell verb parsing #8252

Merged
merged 2 commits into from
Jan 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/generators/powershell.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|powershellGalleryUrl|URL to the module in PowerShell Gallery (e.g. https://www.powershellgallery.com/packages/PSTwitter/).| |null|
|projectUri|A URL to the main website for this project| |null|
|releaseNotes|Release notes of the generated PowerShell module| |null|
|skipVerbParsing|Set skipVerbParsing to not try get powershell verbs of operation names| |null|
|tags|Tags applied to the generated PowerShell module. These help with module discovery in online galleries| |null|
|useOneOfDiscriminatorLookup|Use the discriminator's mapping in oneOf to speed up the model lookup. IMPORTANT: Validation (e.g. one and onlye one match in oneOf's schemas) will be skipped.| |null|

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public class PowerShellClientCodegen extends DefaultCodegen implements CodegenCo
protected HashSet methodNames; // store a list of method names to detect duplicates
protected boolean useOneOfDiscriminatorLookup = false; // use oneOf discriminator's mapping for model lookup
protected boolean discardReadOnly = false; // Discard the readonly property in initialize cmdlet
protected boolean skipVerbParsing = false; // Attempt to parse cmdlets from operation names
protected String projectUri;
protected String licenseUri;
protected String releaseNotes;
Expand Down Expand Up @@ -511,6 +512,8 @@ public PowerShellClientCodegen() {
cliOptions.add(new CliOption("licenseUri","A URL to the license for the generated PowerShell module"));
cliOptions.add(new CliOption("iconUri","A URL to an icon representing the generated PowerShell module"));
cliOptions.add(new CliOption("releaseNotes","Release notes of the generated PowerShell module"));
cliOptions.add(new CliOption("skipVerbParsing", "Set skipVerbParsing to not try get powershell verbs of operation names"));

// option to change how we process + set the data in the 'additionalProperties' keyword.
CliOption disallowAdditionalPropertiesIfNotPresentOpt = CliOption.newBoolean(
CodegenConstants.DISALLOW_ADDITIONAL_PROPERTIES_IF_NOT_PRESENT,
Expand Down Expand Up @@ -601,6 +604,7 @@ public void setIconUri(String iconUri){
this.iconUri = iconUri;
}

public void setSkipVerbParsing(boolean skipVerbParsing) { this.skipVerbParsing = skipVerbParsing; };

@Override
public void processOpts() {
Expand Down Expand Up @@ -628,7 +632,13 @@ public void processOpts() {
setDiscardReadOnly(convertPropertyToBooleanAndWriteBack("discardReadOnly"));
} else {
additionalProperties.put("discardReadOnly", discardReadOnly);
}
}

if (additionalProperties.containsKey("skipVerbParsing")) {
setSkipVerbParsing(convertPropertyToBoolean("skipVerbParsing"));
} else {
additionalProperties.put("skipVerbParsing", skipVerbParsing);
}

if (additionalProperties.containsKey("tags")) {
String[] entries = ((String) additionalProperties.get("tags")).split(",");
Expand Down Expand Up @@ -1212,20 +1222,22 @@ private String getPSDataType(CodegenParameter cp) {
private String toMethodName(String operationId) {
String methodName = camelize(operationId);

// check if method name starts with powershell verbs
for (String verb : (HashSet<String>) powershellVerbs) {
if (methodName.startsWith(verb)) {
methodName = verb + "-" + apiNamePrefix + methodName.substring(verb.length());
LOGGER.info("Naming the method using the PowerShell verb: {} => {}", operationId, methodName);
return methodName;
if (!skipVerbParsing) {
// check if method name starts with powershell verbs
for (String verb : (HashSet<String>) powershellVerbs) {
if (methodName.startsWith(verb)) {
methodName = verb + "-" + apiNamePrefix + methodName.substring(verb.length());
LOGGER.info("Naming the method using the PowerShell verb: {} => {}", operationId, methodName);
return methodName;
}
}
}

for (Map.Entry<String, String> entry : commonVerbs.entrySet()) {
if (methodName.startsWith(entry.getKey())) {
methodName = entry.getValue() + "-" + apiNamePrefix + methodName.substring(entry.getKey().length());
LOGGER.info("Naming the method by mapping the common verbs (e.g. Create, Change) to PS verbs: {} => {}", operationId, methodName);
return methodName;
for (Map.Entry<String, String> entry : commonVerbs.entrySet()) {
if (methodName.startsWith(entry.getKey())) {
methodName = entry.getValue() + "-" + apiNamePrefix + methodName.substring(entry.getKey().length());
LOGGER.info("Naming the method by mapping the common verbs (e.g. Create, Change) to PS verbs: {} => {}", operationId, methodName);
return methodName;
}
}
}

Expand Down