[typescript-angular] Add fileNaming configuration property (#767)

* resolve #727

* remove commented code
This commit is contained in:
Remi Patriarche 2018-09-05 01:57:11 +02:00 committed by William Cheng
parent 66022a1f22
commit 7a18a1a7b6
3 changed files with 40 additions and 4 deletions

View File

@ -1026,6 +1026,7 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
}
if (mapping != null) {
im.put("import", mapping);
im.put("classname", nextImport);
if (!imports.contains(im)) { // avoid duplicates
imports.add(im);
}

View File

@ -50,6 +50,7 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode
public static final String SERVICE_FILE_SUFFIX = "serviceFileSuffix";
public static final String MODEL_SUFFIX = "modelSuffix";
public static final String MODEL_FILE_SUFFIX = "modelFileSuffix";
public static final String FILE_NAMING = "fileNaming";
protected String npmName = null;
protected String npmVersion = "1.0.0";
@ -58,6 +59,7 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode
protected String serviceFileSuffix = ".service";
protected String modelSuffix = "";
protected String modelFileSuffix = "";
protected String fileNaming = "camelCase";
private boolean taggedUnions = false;
@ -95,6 +97,7 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode
this.cliOptions.add(new CliOption(SERVICE_FILE_SUFFIX, "The suffix of the file of the generated service (service<suffix>.ts). Default is '.service'."));
this.cliOptions.add(new CliOption(MODEL_SUFFIX, "The suffix of the generated model. Default is ''."));
this.cliOptions.add(new CliOption(MODEL_FILE_SUFFIX, "The suffix of the file of the generated model (model<suffix>.ts). Default is ''."));
this.cliOptions.add(new CliOption(FILE_NAMING, "Naming convention for the output files: 'camelCase', 'kebab-case'. Default is 'camelCase'."));
}
@Override
@ -189,6 +192,9 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode
modelFileSuffix = additionalProperties.get(MODEL_FILE_SUFFIX).toString();
validateFileSuffixArgument("Model", modelFileSuffix);
}
if (additionalProperties.containsKey(FILE_NAMING)) {
this.setFileNaming(additionalProperties.get(FILE_NAMING).toString());
}
}
private void addNpmPackageGeneration(SemVer ngVersion) {
@ -375,7 +381,7 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode
List<Map<String, Object>> imports = (List<Map<String, Object>>) operations.get("imports");
for (Map<String, Object> im : imports) {
im.put("filename", im.get("import"));
im.put("classname", getModelnameFromModelFilename(im.get("filename").toString()));
im.put("classname", im.get("classname"));
}
return operations;
@ -456,7 +462,7 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode
if (name.length() == 0) {
return "default.service";
}
return org.openapitools.codegen.utils.StringUtils.camelize(removeModelSuffixIfNecessary(name), true) + serviceFileSuffix;
return this.convertUsingFileNamingConvention(name) + serviceFileSuffix;
}
@Override
@ -466,8 +472,7 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode
@Override
public String toModelFilename(String name) {
String modelName = toModelName(name);
return org.openapitools.codegen.utils.StringUtils.camelize(removeModelSuffixIfNecessary(modelName), true) + modelFileSuffix;
return this.convertUsingFileNamingConvention(name) + modelFileSuffix;
}
@Override
@ -558,4 +563,32 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode
);
}
}
/**
* Set the file naming type.
* @param fileNaming the file naming to use
*/
private void setFileNaming(String fileNaming) {
if ("camelCase".equals(fileNaming) || "kebab-case".equals(fileNaming)) {
this.fileNaming = fileNaming;
} else {
throw new IllegalArgumentException("Invalid file naming '" +
fileNaming + "'. Must be 'camelCase' or 'kebab-case'");
}
}
/**
* Converts the original name according to the current <tt>fileNaming</tt> strategy.
* @param originalName the original name to transform
* @return the transformed name
*/
private String convertUsingFileNamingConvention(String originalName) {
String name = this.removeModelSuffixIfNecessary(originalName);
if ("kebab-case".equals(fileNaming)) {
name = dashize(underscore(name));
} else {
name = camelize(name, true);
}
return name;
}
}

View File

@ -35,6 +35,7 @@ public class TypeScriptAngularClientOptionsProvider implements OptionsProvider {
public static final String ALLOW_UNICODE_IDENTIFIERS_VALUE = "false";
public static final String NG_VERSION = "2";
public static final String PREPEND_FORM_OR_BODY_PARAMETERS_VALUE = "true";
public static final String FILE_NAMING_VALUE = "camelCase";
public static String SERVICE_SUFFIX = "Service";
public static String SERVICE_FILE_SUFFIX = ".service";
public static String MODEL_SUFFIX = "";
@ -66,6 +67,7 @@ public class TypeScriptAngularClientOptionsProvider implements OptionsProvider {
.put(TypeScriptAngularClientCodegen.MODEL_FILE_SUFFIX, MODEL_FILE_SUFFIX)
.put(CodegenConstants.ALLOW_UNICODE_IDENTIFIERS, ALLOW_UNICODE_IDENTIFIERS_VALUE)
.put(CodegenConstants.PREPEND_FORM_OR_BODY_PARAMETERS, PREPEND_FORM_OR_BODY_PARAMETERS_VALUE)
.put(TypeScriptAngularClientCodegen.FILE_NAMING, FILE_NAMING_VALUE)
.build();
}