Skip to content

Commit

Permalink
Merge 3dc42de into 9b19903
Browse files Browse the repository at this point in the history
  • Loading branch information
kakulisen committed Dec 18, 2019
2 parents 9b19903 + 3dc42de commit ec68162
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 5 deletions.
Expand Up @@ -27,10 +27,12 @@
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Arrays;
import java.util.Collections;
import java.util.Optional;

import org.apache.servicecomb.toolkit.GeneratorFactory;
import org.apache.servicecomb.toolkit.CodeGenerator;
import org.apache.servicecomb.toolkit.GeneratorFactory;
import org.apache.servicecomb.toolkit.codegen.ProjectMetaConstant;
import org.openapitools.codegen.config.CodegenConfigurator;
import org.slf4j.Logger;
Expand Down Expand Up @@ -85,12 +87,31 @@ public class CodeGenerate implements Runnable {
description = "microservice type of generated microservice project. optional value is provider,consumer,all")
private String serviceType = "all";

@Option(
name = {"--properties"},
title = "additional properties",
description =
"usage: --properties name=value,name=value. These Properties can be referenced by the mustache templates."
+ " You can specify one or more value")
private String properties;

@Override
public void run() {

CodegenConfigurator configurator = new CodegenConfigurator();

CodeGenerator codegenerator = GeneratorFactory.getGenerator(CodeGenerator.class, "default");

// add additional property
Optional.ofNullable(properties).ifPresent(properties ->
Arrays.stream(properties.split(",")).forEach(property -> {
String[] split = property.split("=");
if (split != null && split.length == 2) {
configurator.addAdditionalProperty(split[0], split[1]);
}
})
);

configurator.setOutputDir(output)
.setGroupId(groupId)
.setArtifactId(artifactId)
Expand Down
Expand Up @@ -52,7 +52,9 @@ public void testGenerateServiceCombCodeFromSingleContract() throws IOException {
"-o",
tempFile.toFile().getCanonicalPath(),
"-p",
model
model,
"--properties",
"prop1=value,prop2=value"
};
Assert.assertTrue(!Files.exists(tempFile));
ToolkitMain.main(args);
Expand Down
Expand Up @@ -72,6 +72,17 @@ public class GenerateMojo extends AbstractMojo {
@Parameter
private ServiceConfig service;

/**
* A map of additional properties that can be referenced by the mustache templates
* <additionalProperties>
* <prop1>value</prop1>
* <prop2>value</prop2>
* ...
* </additionalProperties>
*/
@Parameter
private Map<String, Object> additionalProperties;

@Override
public void execute() {

Expand Down Expand Up @@ -149,7 +160,7 @@ private void generateCode(MavenProject project) {
outputDirectory + File.separator + "project" + File.separator;
try {
FileUtils.createDirectory(codeOutput);
Map<String, Object> externalConfig = new HashMap<>();
Map<String, Object> externalConfig = Optional.ofNullable(additionalProperties).orElse(new HashMap<>());
externalConfig.put(GeneratorExternalConfigConstant.PROVIDER_PROJECT_NAME,
project.getBasedir().getName() + providerProjectNameSuffix);
externalConfig.put(GeneratorExternalConfigConstant.CONSUMER_PROJECT_NAME,
Expand Down
Expand Up @@ -27,14 +27,13 @@
import static org.mockito.Mockito.mock;

import java.io.File;
import java.lang.reflect.Field;
import java.util.Collections;
import java.util.HashMap;
import java.util.Objects;

import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.testing.MojoRule;
import org.apache.maven.plugin.testing.resources.TestResources;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.apache.servicecomb.toolkit.common.FileUtils;
import org.junit.Rule;
Expand Down Expand Up @@ -99,6 +98,7 @@ public void testGenerateMojo() throws Exception {
testResourcesEx.setVariableValueToObject("contractFileType", "yaml");
testResourcesEx.setVariableValueToObject("documentType", "html");
testResourcesEx.setVariableValueToObject("service", new ServiceConfig());
testResourcesEx.setVariableValueToObject("additionalProperties", new HashMap<>());

testResourcesEx.execute();

Expand Down

0 comments on commit ec68162

Please sign in to comment.