Merge pull request #2866 from jimschubert/cs/ignore_model_tests_option

[generator] Individual options to exclude tests and docs for apis and models
This commit is contained in:
wing328 2016-05-16 23:10:41 +08:00
commit 5af4156c02
6 changed files with 182 additions and 92 deletions

View File

@ -386,6 +386,26 @@ To control the specific files being generated, you can pass a CSV list of what y
-Dmodels=User -DsupportingFiles=StringUtil.java -Dmodels=User -DsupportingFiles=StringUtil.java
``` ```
To control generation of docs and tests for api and models, pass false to the option. For api, these options are `-DapiTest=false` and `-DapiDocs=false`. For models, `-DmodelTest=false` and `-DmodelDocs=false`.
These options default to true and don't limit the generation of the feature options listed above (like `-Dapi`):
```
# generate only models (with tests and documentation)
java -Dmodels {opts}
# generate only models (with tests but no documentation)
java -Dmodels -DmodelDocs=false {opts}
# generate only User and Pet models (no tests and no documentation)
java -Dmodels=User,Pet -DmodelTests=false {opts}
# generate only apis (without tests)
java -Dapis -DapiTests=false {opts}
# generate only apis (modelTests option is ignored)
java -Dapis -DmodelTests=false {opts}
```
When using selective generation, _only_ the templates needed for the specific generation will be used. When using selective generation, _only_ the templates needed for the specific generation will be used.
### Customizing the generator ### Customizing the generator

View File

@ -105,4 +105,14 @@ public class CodegenConstants {
public static final String SUPPORTS_ES6 = "supportsES6"; public static final String SUPPORTS_ES6 = "supportsES6";
public static final String SUPPORTS_ES6_DESC = "Generate code that conforms to ES6."; public static final String SUPPORTS_ES6_DESC = "Generate code that conforms to ES6.";
public static final String EXCLUDE_TESTS = "excludeTests";
public static final String EXCLUDE_TESTS_DESC = "Specifies that no tests are to be generated.";
public static final String GENERATE_API_TESTS = "generateApiTests";
public static final String GENERATE_API_TESTS_DESC = "Specifies that api tests are to be generated.";
public static final String GENERATE_MODEL_TESTS = "generateModelTests";
public static final String GENERATE_MODEL_TESTS_DESC = "Specifies that model tests are to be generated.";
} }

View File

@ -41,6 +41,10 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
Boolean generateApis = null; Boolean generateApis = null;
Boolean generateModels = null; Boolean generateModels = null;
Boolean generateSupportingFiles = null; Boolean generateSupportingFiles = null;
Boolean generateApiTests = null;
Boolean generateApiDocumentation = null;
Boolean generateModelTests = null;
Boolean generateModelDocumentation = null;
Set<String> modelsToGenerate = null; Set<String> modelsToGenerate = null;
Set<String> apisToGenerate = null; Set<String> apisToGenerate = null;
@ -68,6 +72,18 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
supportingFilesToGenerate = new HashSet<String>(Arrays.asList(supportingFiles.split(","))); supportingFilesToGenerate = new HashSet<String>(Arrays.asList(supportingFiles.split(",")));
} }
} }
if(System.getProperty("modelTests") != null) {
generateModelTests = Boolean.valueOf(System.getProperty("modelTests"));
}
if(System.getProperty("modelDocs") != null) {
generateModelDocumentation = Boolean.valueOf(System.getProperty("modelDocs"));
}
if(System.getProperty("apiTests") != null) {
generateApiTests = Boolean.valueOf(System.getProperty("apiTests"));
}
if(System.getProperty("apiDocs") != null) {
generateApiDocumentation = Boolean.valueOf(System.getProperty("apiDocs"));
}
if(generateApis == null && generateModels == null && generateSupportingFiles == null) { if(generateApis == null && generateModels == null && generateSupportingFiles == null) {
// no specifics are set, generate everything // no specifics are set, generate everything
@ -85,6 +101,28 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
} }
} }
// model/api tests and documentation options rely on parent generate options (api or model) and no other options.
// They default to true in all scenarios and can only be marked false explicitly
if (generateModelTests == null) {
generateModelTests = true;
}
if (generateModelDocumentation == null) {
generateModelDocumentation = true;
}
if (generateApiTests == null) {
generateApiTests = true;
}
if (generateApiDocumentation == null) {
generateApiDocumentation = true;
}
// Additional properties added for tests to exclude references in project related files
config.additionalProperties().put(CodegenConstants.GENERATE_API_TESTS, generateApiTests);
config.additionalProperties().put(CodegenConstants.GENERATE_MODEL_TESTS, generateModelTests);
if(Boolean.FALSE.equals(generateApiTests) && Boolean.FALSE.equals(generateModelTests)) {
config.additionalProperties().put(CodegenConstants.EXCLUDE_TESTS, Boolean.TRUE);
}
if (swagger == null || config == null) { if (swagger == null || config == null) {
throw new RuntimeException("missing swagger input or config!"); throw new RuntimeException("missing swagger input or config!");
} }
@ -282,6 +320,7 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
files.add(new File(filename)); files.add(new File(filename));
} }
if(generateModelTests) {
// to generate model test files // to generate model test files
for (String templateName : config.modelTestTemplateFiles().keySet()) { for (String templateName : config.modelTestTemplateFiles().keySet()) {
String suffix = config.modelTestTemplateFiles().get(templateName); String suffix = config.modelTestTemplateFiles().get(templateName);
@ -305,7 +344,9 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
writeToFile(filename, tmpl.execute(models)); writeToFile(filename, tmpl.execute(models));
files.add(new File(filename)); files.add(new File(filename));
} }
}
if(generateModelDocumentation) {
// to generate model documentation files // to generate model documentation files
for (String templateName : config.modelDocTemplateFiles().keySet()) { for (String templateName : config.modelDocTemplateFiles().keySet()) {
String suffix = config.modelDocTemplateFiles().get(templateName); String suffix = config.modelDocTemplateFiles().get(templateName);
@ -328,6 +369,7 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
writeToFile(filename, tmpl.execute(models)); writeToFile(filename, tmpl.execute(models));
files.add(new File(filename)); files.add(new File(filename));
} }
}
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException("Could not generate model '" + name + "'", e); throw new RuntimeException("Could not generate model '" + name + "'", e);
} }
@ -417,6 +459,7 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
files.add(new File(filename)); files.add(new File(filename));
} }
if(generateApiTests) {
// to generate api test files // to generate api test files
for (String templateName : config.apiTestTemplateFiles().keySet()) { for (String templateName : config.apiTestTemplateFiles().keySet()) {
String filename = config.apiTestFilename(templateName, tag); String filename = config.apiTestFilename(templateName, tag);
@ -440,7 +483,9 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
writeToFile(filename, tmpl.execute(operation)); writeToFile(filename, tmpl.execute(operation));
files.add(new File(filename)); files.add(new File(filename));
} }
}
if(generateApiDocumentation) {
// to generate api documentation files // to generate api documentation files
for (String templateName : config.apiDocTemplateFiles().keySet()) { for (String templateName : config.apiDocTemplateFiles().keySet()) {
String filename = config.apiDocFilename(templateName, tag); String filename = config.apiDocFilename(templateName, tag);
@ -464,6 +509,7 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
writeToFile(filename, tmpl.execute(operation)); writeToFile(filename, tmpl.execute(operation));
files.add(new File(filename)); files.add(new File(filename));
} }
}
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException("Could not generate api file for '" + tag + "'", e); throw new RuntimeException("Could not generate api file for '" + tag + "'", e);

View File

@ -137,6 +137,11 @@ public class CSharpClientCodegen extends AbstractCSharpCodegen {
@Override @Override
public void processOpts() { public void processOpts() {
super.processOpts(); super.processOpts();
Boolean excludeTests = false;
if(additionalProperties.containsKey(CodegenConstants.EXCLUDE_TESTS)) {
excludeTests = Boolean.valueOf(additionalProperties.get(CodegenConstants.EXCLUDE_TESTS).toString());
}
apiPackage = "Api"; apiPackage = "Api";
modelPackage = "Model"; modelPackage = "Model";
@ -234,7 +239,10 @@ public class CSharpClientCodegen extends AbstractCSharpCodegen {
// copy package.config to nuget's standard location for project-level installs // copy package.config to nuget's standard location for project-level installs
supportingFiles.add(new SupportingFile("packages.config.mustache", packageFolder + File.separator, "packages.config")); supportingFiles.add(new SupportingFile("packages.config.mustache", packageFolder + File.separator, "packages.config"));
if(Boolean.FALSE.equals(excludeTests)) {
supportingFiles.add(new SupportingFile("packages_test.config.mustache", testPackageFolder + File.separator, "packages.config")); supportingFiles.add(new SupportingFile("packages_test.config.mustache", testPackageFolder + File.separator, "packages.config"));
}
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md")); supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh")); supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh"));
@ -247,12 +255,10 @@ public class CSharpClientCodegen extends AbstractCSharpCodegen {
supportingFiles.add(new SupportingFile("Solution.mustache", "", packageName + ".sln")); supportingFiles.add(new SupportingFile("Solution.mustache", "", packageName + ".sln"));
supportingFiles.add(new SupportingFile("Project.mustache", packageFolder, packageName + ".csproj")); supportingFiles.add(new SupportingFile("Project.mustache", packageFolder, packageName + ".csproj"));
// TODO: Check if test project output is enabled, partially related to #2506. Should have options for: if(Boolean.FALSE.equals(excludeTests)) {
// 1) No test project
// 2) No model tests
// 3) No api tests
supportingFiles.add(new SupportingFile("TestProject.mustache", testPackageFolder, testPackageName + ".csproj")); supportingFiles.add(new SupportingFile("TestProject.mustache", testPackageFolder, testPackageName + ".csproj"));
} }
}
additionalProperties.put("apiDocPath", apiDocPath); additionalProperties.put("apiDocPath", apiDocPath);
additionalProperties.put("modelDocPath", modelDocPath); additionalProperties.put("modelDocPath", modelDocPath);

View File

@ -116,6 +116,11 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig
@Override @Override
public void processOpts() { public void processOpts() {
super.processOpts(); super.processOpts();
Boolean excludeTests = false;
if(additionalProperties.containsKey(CodegenConstants.EXCLUDE_TESTS)) {
excludeTests = Boolean.valueOf(additionalProperties.get(CodegenConstants.EXCLUDE_TESTS).toString());
}
if (additionalProperties.containsKey(CodegenConstants.PACKAGE_NAME)) { if (additionalProperties.containsKey(CodegenConstants.PACKAGE_NAME)) {
setPackageName((String) additionalProperties.get(CodegenConstants.PACKAGE_NAME)); setPackageName((String) additionalProperties.get(CodegenConstants.PACKAGE_NAME));
@ -151,7 +156,10 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig
supportingFiles.add(new SupportingFile("__init__package.mustache", swaggerFolder, "__init__.py")); supportingFiles.add(new SupportingFile("__init__package.mustache", swaggerFolder, "__init__.py"));
supportingFiles.add(new SupportingFile("__init__model.mustache", modelPackage, "__init__.py")); supportingFiles.add(new SupportingFile("__init__model.mustache", modelPackage, "__init__.py"));
supportingFiles.add(new SupportingFile("__init__api.mustache", apiPackage, "__init__.py")); supportingFiles.add(new SupportingFile("__init__api.mustache", apiPackage, "__init__.py"));
if(Boolean.FALSE.equals(excludeTests)) {
supportingFiles.add(new SupportingFile("__init__test.mustache", testFolder, "__init__.py")); supportingFiles.add(new SupportingFile("__init__test.mustache", testFolder, "__init__.py"));
}
supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh")); supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh"));
supportingFiles.add(new SupportingFile("gitignore.mustache", "", ".gitignore")); supportingFiles.add(new SupportingFile("gitignore.mustache", "", ".gitignore"));
} }

View File

@ -4,9 +4,9 @@ VisualStudioVersion = 12.0.0.0
MinimumVisualStudioVersion = 10.0.0.1 MinimumVisualStudioVersion = 10.0.0.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "{{packageName}}", "src\{{packageName}}\{{packageName}}.csproj", "{{packageGuid}}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "{{packageName}}", "src\{{packageName}}\{{packageName}}.csproj", "{{packageGuid}}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "{{testPackageName}}", "src\{{testPackageName}}\{{testPackageName}}.csproj", "{19F1DEBC-DE5E-4517-8062-F000CD499087}" {{^excludeTests}}Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "{{testPackageName}}", "src\{{testPackageName}}\{{testPackageName}}.csproj", "{19F1DEBC-DE5E-4517-8062-F000CD499087}"
EndProject EndProject
Global {{/excludeTests}}Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU Release|Any CPU = Release|Any CPU