Skip to content

Commit

Permalink
run "mvn formatter:format" on maven plug-in
Browse files Browse the repository at this point in the history
  • Loading branch information
wing328 committed Jul 13, 2017
1 parent 1e53557 commit eb498e2
Showing 1 changed file with 84 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@

/*
* Copyright 2001-2005 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/

import static io.swagger.codegen.config.CodegenConfiguratorUtils.applyAdditionalPropertiesKvp;
Expand Down Expand Up @@ -49,7 +47,7 @@
@Mojo(name = "generate", defaultPhase = LifecyclePhase.GENERATE_SOURCES)
public class CodeGenMojo extends AbstractMojo {

@Parameter(name="verbose", required = false, defaultValue = "false")
@Parameter(name = "verbose", required = false, defaultValue = "false")
private boolean verbose;

/**
Expand All @@ -61,8 +59,7 @@ public class CodeGenMojo extends AbstractMojo {
/**
* Location of the output directory.
*/
@Parameter(name = "output",
property = "swagger.codegen.maven.plugin.output",
@Parameter(name = "output", property = "swagger.codegen.maven.plugin.output",
defaultValue = "${project.build.directory}/generated-sources/swagger")
private File output;

Expand Down Expand Up @@ -91,10 +88,10 @@ public class CodeGenMojo extends AbstractMojo {
private File templateDirectory;

/**
* Adds authorization headers when fetching the swagger definitions remotely.
" Pass in a URL-encoded string of name:header with a comma separating multiple values
* Adds authorization headers when fetching the swagger definitions remotely. " Pass in a
* URL-encoded string of name:header with a comma separating multiple values
*/
@Parameter(name="auth")
@Parameter(name = "auth")
private String auth;

/**
Expand All @@ -106,13 +103,13 @@ public class CodeGenMojo extends AbstractMojo {
/**
* Specifies if the existing files should be overwritten during the generation.
*/
@Parameter(name="skipOverwrite", required=false)
@Parameter(name = "skipOverwrite", required = false)
private Boolean skipOverwrite;

/**
* Specifies if the existing files should be overwritten during the generation.
*/
@Parameter(name="removeOperationIdPrefix", required=false)
@Parameter(name = "removeOperationIdPrefix", required = false)
private Boolean removeOperationIdPrefix;

/**
Expand Down Expand Up @@ -197,7 +194,7 @@ public class CodeGenMojo extends AbstractMojo {
private Boolean generateModels = true;

/**
* A comma separated list of models to generate. All models is the default.
* A comma separated list of models to generate. All models is the default.
*/
@Parameter(name = "modelsToGenerate", required = false)
private String modelsToGenerate = "";
Expand All @@ -209,11 +206,11 @@ public class CodeGenMojo extends AbstractMojo {
private Boolean generateSupportingFiles = true;

/**
* A comma separated list of models to generate. All models is the default.
* A comma separated list of models to generate. All models is the default.
*/
@Parameter(name = "supportingFilesToGenerate", required = false)
private String supportingFilesToGenerate = "";

/**
* Generate the model tests
*/
Expand Down Expand Up @@ -251,8 +248,8 @@ public class CodeGenMojo extends AbstractMojo {
private Boolean skip;

/**
* Add the output directory to the project as a source root, so that the
* generated java types are compiled and included in the project artifact.
* Add the output directory to the project as a source root, so that the generated java types
* are compiled and included in the project artifact.
*/
@Parameter(defaultValue = "true")
private boolean addCompileSourceRoot = true;
Expand All @@ -277,89 +274,89 @@ public class CodeGenMojo extends AbstractMojo {
@Override
public void execute() throws MojoExecutionException {

if(skip) {
if (skip) {
getLog().info("Code generation is skipped.");
// Even when no new sources are generated, the existing ones should
// still be compiled if needed.
addCompileSourceRootIfConfigured();
return;
}

//attempt to read from config file
// attempt to read from config file
CodegenConfigurator configurator = CodegenConfigurator.fromFile(configurationFile);

//if a config file wasn't specified or we were unable to read it
if(configurator == null) {
// if a config file wasn't specified or we were unable to read it
if (configurator == null) {
configurator = new CodegenConfigurator();
}

configurator.setVerbose(verbose);

if(skipOverwrite != null) {
if (skipOverwrite != null) {
configurator.setSkipOverwrite(skipOverwrite);
}

if(removeOperationIdPrefix != null) {
if (removeOperationIdPrefix != null) {
configurator.setRemoveOperationIdPrefix(removeOperationIdPrefix);
}

if(isNotEmpty(inputSpec)) {
if (isNotEmpty(inputSpec)) {
configurator.setInputSpec(inputSpec);
}

if(isNotEmpty(gitUserId)) {
if (isNotEmpty(gitUserId)) {
configurator.setGitUserId(gitUserId);
}

if(isNotEmpty(gitRepoId)) {
if (isNotEmpty(gitRepoId)) {
configurator.setGitRepoId(gitRepoId);
}

if(isNotEmpty(ignoreFileOverride)) {
if (isNotEmpty(ignoreFileOverride)) {
configurator.setIgnoreFileOverride(ignoreFileOverride);
}

configurator.setLang(language);

configurator.setOutputDir(output.getAbsolutePath());

if(isNotEmpty(auth)) {
if (isNotEmpty(auth)) {
configurator.setAuth(auth);
}

if(isNotEmpty(apiPackage)) {
if (isNotEmpty(apiPackage)) {
configurator.setApiPackage(apiPackage);
}

if(isNotEmpty(modelPackage)) {
if (isNotEmpty(modelPackage)) {
configurator.setModelPackage(modelPackage);
}

if(isNotEmpty(invokerPackage)) {
if (isNotEmpty(invokerPackage)) {
configurator.setInvokerPackage(invokerPackage);
}

if(isNotEmpty(groupId)) {
if (isNotEmpty(groupId)) {
configurator.setGroupId(groupId);
}

if(isNotEmpty(artifactId)) {
if (isNotEmpty(artifactId)) {
configurator.setArtifactId(artifactId);
}

if(isNotEmpty(artifactVersion)) {
if (isNotEmpty(artifactVersion)) {
configurator.setArtifactVersion(artifactVersion);
}

if(isNotEmpty(library)) {
if (isNotEmpty(library)) {
configurator.setLibrary(library);
}

if(isNotEmpty(modelNamePrefix)) {
if (isNotEmpty(modelNamePrefix)) {
configurator.setModelNamePrefix(modelNamePrefix);
}

if(isNotEmpty(modelNameSuffix)) {
if (isNotEmpty(modelNameSuffix)) {
configurator.setModelNameSuffix(modelNameSuffix);
}

Expand All @@ -385,7 +382,7 @@ public void execute() throws MojoExecutionException {
} else {
System.clearProperty("supportingFiles");
}

System.setProperty("modelTests", generateModelTests.toString());
System.setProperty("modelDocs", generateModelDocumentation.toString());
System.setProperty("apiTests", generateApiTests.toString());
Expand All @@ -394,66 +391,74 @@ public void execute() throws MojoExecutionException {

if (configOptions != null) {

if(configOptions.containsKey("instantiation-types")) {
applyInstantiationTypesKvp(configOptions.get("instantiation-types").toString(), configurator);
if (configOptions.containsKey("instantiation-types")) {
applyInstantiationTypesKvp(configOptions.get("instantiation-types").toString(),
configurator);
}

if(importMappings == null && configOptions.containsKey("import-mappings")) {
applyImportMappingsKvp(configOptions.get("import-mappings").toString(), configurator);
if (importMappings == null && configOptions.containsKey("import-mappings")) {
applyImportMappingsKvp(configOptions.get("import-mappings").toString(),
configurator);
}

if(configOptions.containsKey("type-mappings")) {
if (configOptions.containsKey("type-mappings")) {
applyTypeMappingsKvp(configOptions.get("type-mappings").toString(), configurator);
}

if(configOptions.containsKey("language-specific-primitives")) {
applyLanguageSpecificPrimitivesCsv(configOptions.get("language-specific-primitives").toString(), configurator);
if (configOptions.containsKey("language-specific-primitives")) {
applyLanguageSpecificPrimitivesCsv(configOptions
.get("language-specific-primitives").toString(), configurator);
}

if(configOptions.containsKey("additional-properties")) {
applyAdditionalPropertiesKvp(configOptions.get("additional-properties").toString(), configurator);
if (configOptions.containsKey("additional-properties")) {
applyAdditionalPropertiesKvp(configOptions.get("additional-properties").toString(),
configurator);
}

if(configOptions.containsKey("reserved-words-mappings")) {
applyReservedWordsMappingsKvp(configOptions.get("reserved-words-mappings").toString(), configurator);
if (configOptions.containsKey("reserved-words-mappings")) {
applyReservedWordsMappingsKvp(configOptions.get("reserved-words-mappings")
.toString(), configurator);
}
}

if (importMappings != null && !configOptions.containsKey("import-mappings")) {
String importMappingsAsString = importMappings.toString();
applyImportMappingsKvp(importMappingsAsString.substring(0, importMappingsAsString.length() - 1), configurator);
applyImportMappingsKvp(
importMappingsAsString.substring(0, importMappingsAsString.length() - 1),
configurator);
}

if (environmentVariables != null) {

for(String key : environmentVariables.keySet()) {
for (String key : environmentVariables.keySet()) {
originalEnvironmentVariables.put(key, System.getProperty(key));
String value = environmentVariables.get(key);
if(value == null) {
if (value == null) {
// don't put null values
value = "";
}
System.setProperty(key, value);
configurator.addSystemProperty(key, value);
}
}

final ClientOptInput input = configurator.toClientOptInput();
final CodegenConfig config = input.getConfig();

if(configOptions != null) {
if (configOptions != null) {
for (CliOption langCliOption : config.cliOptions()) {
if (configOptions.containsKey(langCliOption.getOpt())) {
input.getConfig().additionalProperties().put(langCliOption.getOpt(),
configOptions.get(langCliOption.getOpt()));
input.getConfig().additionalProperties()
.put(langCliOption.getOpt(), configOptions.get(langCliOption.getOpt()));
}
}
}

if(configHelp) {
if (configHelp) {
for (CliOption langCliOption : config.cliOptions()) {
System.out.println("\t" + langCliOption.getOpt());
System.out.println("\t " + langCliOption.getOptionHelp().replaceAll("\n", "\n\t "));
System.out.println("\t "
+ langCliOption.getOptionHelp().replaceAll("\n", "\n\t "));
System.out.println();
}
return;
Expand All @@ -464,26 +469,31 @@ public void execute() throws MojoExecutionException {
// Maven logs exceptions thrown by plugins only if invoked with -e
// I find it annoying to jump through hoops to get basic diagnostic information,
// so let's log it in any case:
getLog().error(e);
throw new MojoExecutionException("Code generation failed. See above for the full exception.");
getLog().error(e);
throw new MojoExecutionException(
"Code generation failed. See above for the full exception.");
}

addCompileSourceRootIfConfigured();
}

private void addCompileSourceRootIfConfigured() {
if(addCompileSourceRoot) {
final Object sourceFolderObject = configOptions == null ? null : configOptions.get(CodegenConstants.SOURCE_FOLDER);
final String sourceFolder = sourceFolderObject == null ? "src/main/java" : sourceFolderObject.toString();
if (addCompileSourceRoot) {
final Object sourceFolderObject =
configOptions == null ? null : configOptions
.get(CodegenConstants.SOURCE_FOLDER);
final String sourceFolder =
sourceFolderObject == null ? "src/main/java" : sourceFolderObject.toString();

String sourceJavaFolder = output.toString() + "/" + sourceFolder;
project.addCompileSourceRoot(sourceJavaFolder);
}

// Reset all environment variables to their original value. This prevents unexpected behaviour
// Reset all environment variables to their original value. This prevents unexpected
// behaviour
// when running the plugin multiple consecutive times with different configurations.
for(Map.Entry<String, String> entry : originalEnvironmentVariables.entrySet()) {
if(entry.getValue() == null) {
for (Map.Entry<String, String> entry : originalEnvironmentVariables.entrySet()) {
if (entry.getValue() == null) {
System.clearProperty(entry.getKey());
} else {
System.setProperty(entry.getKey(), entry.getValue());
Expand Down

0 comments on commit eb498e2

Please sign in to comment.