You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by ni...@apache.org on 2019/06/21 07:09:59 UTC
[servicecomb-toolkit] 43/49: code refactor
This is an automated email from the ASF dual-hosted git repository.
ningjiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-toolkit.git
commit 5e1fa09b126b93dfc0add65bf677e51c5f23daf5
Author: MabinGo <bi...@huawei.com>
AuthorDate: Thu Jun 6 16:55:06 2019 +0800
code refactor
Signed-off-by: MabinGo <bi...@huawei.com>
---
LICENSE | 6 +-
README-ZH.md | 4 +-
README.md | 4 +-
{toolkit-cli => cli}/pom.xml | 7 +-
.../servicecomb/toolkit/cli/CodeGenerate.java | 22 ++++-
.../servicecomb/toolkit/cli/DocGenerate.java | 41 ++++++----
.../servicecomb/toolkit/cli/ToolkitMain.java | 0
.../src/main/resources/application.properties | 0
{toolkit-cli => cli}/src/main/resources/log4j2.xml | 0
.../apache/servicecomb/toolkit/cli/CliTest.java | 0
.../resources/contracts/CalculateEndpoint.yaml | 0
.../test/resources/contracts/HelloEndPoint.yaml | 0
.../contracts/pojo/CodeFirstHelloEndPoint.yaml | 0
.../src/test/resources/swagger.yaml | 0
{code-generator => codegen}/pom.xml | 8 +-
.../toolkit/codegen/DefaultCodeGenerator.java | 22 +++--
.../toolkit/codegen/GetGenericClassTypeLambda.java | 0
.../toolkit/codegen/RemoveImplSuffixLambda.java | 0
.../toolkit/codegen/ServiceCombCodegen.java | 0
.../services/io.swagger.codegen.CodegenConfig | 0
.../org.apache.servicecomb.toolkit.CodeGenerator | 2 +-
.../ServiceComb/consumer/Application.mustache | 0
.../ServiceComb/consumer/apiConsumer.mustache | 0
.../consumer/bodyParamsConsumer.mustache | 0
.../consumer/formParamsConsumer.mustache | 0
.../consumer/headerParamsConsumer.mustache | 0
.../ServiceComb/consumer/microservice.mustache | 0
.../consumer/pathParamsConsumer.mustache | 0
.../ServiceComb/consumer/pojo/apiConsumer.mustache | 0
.../resources/ServiceComb/consumer/pom.mustache | 0
.../consumer/queryParamsConsumer.mustache | 0
.../consumer/springboot}/Application.mustache | 0
.../ServiceComb/consumer/springboot/pom.mustache | 0
.../main/resources/ServiceComb/enumClass.mustache | 0
.../resources/ServiceComb/enumOuterClass.mustache | 0
.../libraries/JAX-RS}/Application.mustache | 0
.../ServiceComb/libraries/JAX-RS/api.mustache | 0
.../libraries/JAX-RS}/bodyParams.mustache | 0
.../libraries/JAX-RS/formParams.mustache | 0
.../libraries/JAX-RS/headerParams.mustache | 0
.../libraries/JAX-RS/pathParams.mustache | 0
.../ServiceComb/libraries/JAX-RS/pom.mustache | 0
.../libraries/JAX-RS/queryParams.mustache | 0
.../libraries/POJO/Application.mustache | 0
.../ServiceComb/libraries/POJO/api.mustache | 0
.../ServiceComb/libraries/POJO/apiImpl.mustache | 0
.../libraries/POJO}/bodyParams.mustache | 0
.../ServiceComb/libraries/POJO/formParams.mustache | 0
.../libraries/POJO/headerParams.mustache | 0
.../ServiceComb/libraries/POJO/pathParams.mustache | 0
.../ServiceComb/libraries/POJO/pom.mustache | 0
.../libraries/POJO/queryParams.mustache | 0
.../libraries/SpringBoot}/Application.mustache | 0
.../ServiceComb/libraries/SpringBoot/api.mustache | 0
.../libraries/SpringBoot}/bodyParams.mustache | 0
.../libraries/SpringBoot}/formParams.mustache | 0
.../libraries/SpringBoot}/headerParams.mustache | 0
.../libraries/SpringBoot/operationMethod.mustache | 0
.../libraries/SpringBoot}/pathParams.mustache | 0
.../ServiceComb/libraries/SpringBoot/pom.mustache | 0
.../libraries/SpringBoot}/queryParams.mustache | 0
.../libraries/SpringMVC}/Application.mustache | 0
.../ServiceComb/libraries/SpringMVC/api.mustache | 0
.../libraries/SpringMVC}/bodyParams.mustache | 0
.../libraries/SpringMVC}/formParams.mustache | 0
.../libraries/SpringMVC}/headerParams.mustache | 0
.../libraries/SpringMVC}/pathParams.mustache | 0
.../ServiceComb/libraries/SpringMVC/pom.mustache | 0
.../libraries/SpringMVC}/queryParams.mustache | 0
.../src/main/resources/ServiceComb/log4j2.mustache | 0
.../resources/ServiceComb/model/model.mustache | 0
.../main/resources/ServiceComb/model/pom.mustache | 0
.../src/main/resources/ServiceComb/pojo.mustache | 0
.../resources/ServiceComb/project/pom.mustache | 0
.../ServiceComb/provider/microservice.mustache | 0
.../resources/ServiceComb/returnTypes.mustache | 0
.../servicecomb/toolkit/codegen/GeneratorTest.java | 22 ++++-
.../servicecomb/toolkit/codegen/ReflectUtils.java | 0
.../toolkit/codegen/ServiceCombCodegenTest.java | 0
.../src/test/resources/swagger.yaml | 0
common/pom.xml | 6 ++
.../servicecomb/toolkit/common/ContractsUtils.java | 95 ----------------------
...tsUtils.java => DefaultContractsGenerator.java} | 76 +++++++++--------
...g.apache.servicecomb.toolkit.ContractsGenerator | 2 +-
.../toolkit/common/ContractsGeneratorTest.java | 26 ++++--
.../toolkit/common/OtherContractsGenerator.java | 33 ++++----
...g.apache.servicecomb.toolkit.ContractsGenerator | 2 +-
{code-generator => core}/pom.xml | 31 ++-----
.../apache/servicecomb/toolkit}/CodeGenerator.java | 13 +--
.../servicecomb/toolkit/ContractsGenerator.java | 10 +--
.../apache/servicecomb/toolkit}/DocGenerator.java | 10 +--
.../org/apache/servicecomb/toolkit/Generator.java | 10 ++-
.../servicecomb/toolkit/GeneratorFactory.java | 61 ++++++++++++++
.../toolkit/docgen/DocGeneratorManager.java | 48 -----------
{doc-generator => docgen}/pom.xml | 8 +-
.../toolkit/docgen/ContractsAsciidocGenerator.java | 28 ++++++-
.../docgen/ContractsSwaggerUIGenerator.java | 27 +++++-
.../org.apache.servicecomb.toolkit.DocGenerator | 0
.../src/main/resources/webroot/swagger-ui.html | 0
.../src/test/java/docgen/DocGeneratorTest.java | 45 +++++-----
.../src/test/resources/HelloEndPoint.yaml | 0
pom.xml | 7 +-
toolkit-maven-plugin/pom.xml | 2 +-
.../toolkit/plugin/ContractGenerator.java | 12 ++-
.../toolkit/plugin/GenerateContractsDocMojo.java | 17 ++--
105 files changed, 367 insertions(+), 340 deletions(-)
diff --git a/LICENSE b/LICENSE
index 3d03b1b..7769b07 100755
--- a/LICENSE
+++ b/LICENSE
@@ -201,9 +201,9 @@
limitations under the License.
================================================================
-For code-generator/src/main/resources/ServiceComb/*
-For toolkit-cli/src/test/resources/swagger.yaml
-For code-generator/src/test/resources/swagger.yaml
+For codegen/src/main/resources/ServiceComb/*
+For cli/src/test/resources/swagger.yaml
+For codegen/src/test/resources/swagger.yaml
================================================================
This product bundles files from swagger-codegen which is licensed under the Apache License v2.
For details, see https://github.com/swagger-api/swagger-codegen
diff --git a/README-ZH.md b/README-ZH.md
index 2a97256..4e961f6 100755
--- a/README-ZH.md
+++ b/README-ZH.md
@@ -83,8 +83,8 @@ $ cd toolkit
$ mvn clean install
```
-### 3.2 使用toolkit-cli工具
-可执行jar包位于toolkit/toolkit-cli/target/bin目录下
+### 3.2 使用toolkit cli工具
+可执行jar包位于toolkit/cli/target/bin目录下
```shell
$ java -jar toolkit-cli-{version}.jar help
```
diff --git a/README.md b/README.md
index af77348..86558ba 100755
--- a/README.md
+++ b/README.md
@@ -82,8 +82,8 @@ $ cd toolkit
$ mvn clean install
```
-### 3.2 Use the toolkit-cli
-The executable jar package is located in the toolkit/toolkit-cli/target/bin directory
+### 3.2 Use the toolkit cli
+The executable jar package is located in the toolkit/cli/target/bin directory
```shell
$ java -jar toolkit-cli-{version}.jar help
```
diff --git a/toolkit-cli/pom.xml b/cli/pom.xml
similarity index 95%
rename from toolkit-cli/pom.xml
rename to cli/pom.xml
index dc8ac81..474e22c 100755
--- a/toolkit-cli/pom.xml
+++ b/cli/pom.xml
@@ -26,7 +26,7 @@
</parent>
<modelVersion>4.0.0</modelVersion>
- <artifactId>toolkit-cli</artifactId>
+ <artifactId>cli</artifactId>
<properties>
<main.class>org.apache.servicecomb.toolkit.cli.ToolkitMain</main.class>
@@ -48,7 +48,7 @@
<dependency>
<groupId>org.apache.servicecomb</groupId>
- <artifactId>code-generator</artifactId>
+ <artifactId>codegen</artifactId>
<version>${project.version}</version>
</dependency>
@@ -61,7 +61,7 @@
<dependency>
<groupId>org.apache.servicecomb</groupId>
- <artifactId>doc-generator</artifactId>
+ <artifactId>docgen</artifactId>
<version>${project.version}</version>
</dependency>
@@ -113,6 +113,7 @@
<fork>true</fork>
<mainClass>${main.class}</mainClass>
<outputDirectory>target/bin</outputDirectory>
+ <finalName>toolkit-cli-${project.version}</finalName>
</configuration>
<executions>
<execution>
diff --git a/toolkit-cli/src/main/java/org/apache/servicecomb/toolkit/cli/CodeGenerate.java b/cli/src/main/java/org/apache/servicecomb/toolkit/cli/CodeGenerate.java
similarity index 82%
rename from toolkit-cli/src/main/java/org/apache/servicecomb/toolkit/cli/CodeGenerate.java
rename to cli/src/main/java/org/apache/servicecomb/toolkit/cli/CodeGenerate.java
index df01175..b46b67e 100755
--- a/toolkit-cli/src/main/java/org/apache/servicecomb/toolkit/cli/CodeGenerate.java
+++ b/cli/src/main/java/org/apache/servicecomb/toolkit/cli/CodeGenerate.java
@@ -27,8 +27,10 @@ import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
+import java.util.Collections;
-import org.apache.servicecomb.toolkit.codegen.DefaultCodeGenerator;
+import org.apache.servicecomb.toolkit.GeneratorFactory;
+import org.apache.servicecomb.toolkit.CodeGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -70,18 +72,28 @@ public class CodeGenerate implements Runnable {
description = "artifact version in generated microservice project")
private String artifactVersion;
+ @Option(name = {"--api-package"}, title = "api package",
+ description = "api package in generated microservice project")
+ private String apiPackage;
+
+ @Option(name = {"--model-package"}, title = "model package",
+ description = "model package in generated microservice project")
+ private String modelPackage;
@Override
public void run() {
CodegenConfigurator configurator = new CodegenConfigurator();
+ CodeGenerator codegenerator = GeneratorFactory.getGenerator(CodeGenerator.class,"default");
configurator.setOutputDir(output)
.setGroupId(groupId)
.setArtifactId(artifactId)
.setArtifactVersion(artifactVersion)
.setLibrary(programmingModel)
- .setLang(framework);
+ .setLang(framework)
+ .setApiPackage(apiPackage)
+ .setModelPackage(modelPackage);
if (isNotEmpty(specFile)) {
@@ -97,7 +109,8 @@ public class CodeGenerate implements Runnable {
.addAdditionalProperty("apiName", file.toFile().getName().split("\\.")[0]);
try {
- new DefaultCodeGenerator().opts(configurator).generate();
+ codegenerator.configure(Collections.singletonMap("configurator",configurator));
+ codegenerator.generate();
} catch (RuntimeException e) {
throw new RuntimeException("Failed to generate code base on file " + file.toFile().getName());
}
@@ -111,7 +124,8 @@ public class CodeGenerate implements Runnable {
}
} else {
configurator.setInputSpec(specFile);
- new DefaultCodeGenerator().opts(configurator).generate();
+ codegenerator.configure(Collections.singletonMap("configurator",configurator));
+ codegenerator.generate();
}
LOGGER.info("Success to generate code, the directory is: {}", output);
diff --git a/toolkit-cli/src/main/java/org/apache/servicecomb/toolkit/cli/DocGenerate.java b/cli/src/main/java/org/apache/servicecomb/toolkit/cli/DocGenerate.java
similarity index 67%
rename from toolkit-cli/src/main/java/org/apache/servicecomb/toolkit/cli/DocGenerate.java
rename to cli/src/main/java/org/apache/servicecomb/toolkit/cli/DocGenerate.java
index ca007d6..290feea 100755
--- a/toolkit-cli/src/main/java/org/apache/servicecomb/toolkit/cli/DocGenerate.java
+++ b/cli/src/main/java/org/apache/servicecomb/toolkit/cli/DocGenerate.java
@@ -26,10 +26,12 @@ import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
+import java.util.HashMap;
+import java.util.Map;
-import org.apache.commons.lang3.StringUtils;
import org.apache.servicecomb.swagger.SwaggerUtils;
-import org.apache.servicecomb.toolkit.docgen.DocGeneratorManager;
+import org.apache.servicecomb.toolkit.GeneratorFactory;
+import org.apache.servicecomb.toolkit.DocGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -60,19 +62,23 @@ public class DocGenerate implements Runnable {
try {
Path specPath = Paths.get(specFile);
- String[] retValues = new String[1];
+ boolean[] retValues = new boolean[1];
String[] fileName = new String[1];
+ DocGenerator docGenerator = GeneratorFactory.getGenerator(DocGenerator.class, format);
+ Map<String, Object> docGeneratorConfig = new HashMap<>();
+
if (Files.isDirectory(specPath)) {
Files.walkFileTree(specPath, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
- retValues[0] = DocGeneratorManager.generate(SwaggerUtils.parseSwagger(file.toUri().toURL()),
- output + File.separator + file.toFile().getName().substring(0, file.toFile().getName().indexOf(".")),
- format);
- if (StringUtils.isEmpty(retValues[0])) {
+ docGeneratorConfig.put("contractContent", SwaggerUtils.parseSwagger(file.toUri().toURL()));
+ docGeneratorConfig.put("outputPath", output + File.separator + file.toFile().getName().substring(0, file.toFile().getName().indexOf(".")));
+ docGenerator.configure(docGeneratorConfig);
+ retValues[0] = docGenerator.generate();
+ if (retValues[0]) {
fileName[0] = file.toFile().getName();
return FileVisitResult.TERMINATE;
}
@@ -83,20 +89,23 @@ public class DocGenerate implements Runnable {
} else if (Files.isRegularFile(specPath)) {
fileName[0] = specPath.toFile().getName();
- retValues[0] = DocGeneratorManager.generate(SwaggerUtils.parseSwagger(specPath.toUri().toURL()),
- output + File.separator + new File(specFile).getName()
- .substring(0, new File(specFile).getName().indexOf(".")),
- format);
+ docGeneratorConfig.put("contractContent", SwaggerUtils.parseSwagger(specPath.toUri().toURL()));
+ docGeneratorConfig.put("outputPath", output + File.separator + new File(specFile).getName()
+ .substring(0, new File(specFile).getName().indexOf(".")));
+ docGenerator.configure(docGeneratorConfig);
+ retValues[0] = docGenerator.generate();
+
} else {
fileName[0] = specFile;
- retValues[0] = DocGeneratorManager.generate(SwaggerUtils.parseSwagger(URI.create(specFile).toURL()),
- output + File.separator + new File(specFile).getName()
- .substring(0, new File(specFile).getName().indexOf(".")),
- format);
+ docGeneratorConfig.put("contractContent", SwaggerUtils.parseSwagger(URI.create(specFile).toURL()));
+ docGeneratorConfig.put("outputPath",output + File.separator + new File(specFile).getName()
+ .substring(0, new File(specFile).getName().indexOf(".")));
+ docGenerator.configure(docGeneratorConfig);
+ retValues[0] = docGenerator.generate();
}
- if (StringUtils.isEmpty(retValues[0])) {
+ if (retValues[0]) {
LOGGER.error("Failed to generate document base on file {}", fileName[0]);
return;
}
diff --git a/toolkit-cli/src/main/java/org/apache/servicecomb/toolkit/cli/ToolkitMain.java b/cli/src/main/java/org/apache/servicecomb/toolkit/cli/ToolkitMain.java
similarity index 100%
rename from toolkit-cli/src/main/java/org/apache/servicecomb/toolkit/cli/ToolkitMain.java
rename to cli/src/main/java/org/apache/servicecomb/toolkit/cli/ToolkitMain.java
diff --git a/toolkit-cli/src/main/resources/application.properties b/cli/src/main/resources/application.properties
similarity index 100%
copy from toolkit-cli/src/main/resources/application.properties
copy to cli/src/main/resources/application.properties
diff --git a/toolkit-cli/src/main/resources/log4j2.xml b/cli/src/main/resources/log4j2.xml
similarity index 100%
rename from toolkit-cli/src/main/resources/log4j2.xml
rename to cli/src/main/resources/log4j2.xml
diff --git a/toolkit-cli/src/test/java/org/apache/servicecomb/toolkit/cli/CliTest.java b/cli/src/test/java/org/apache/servicecomb/toolkit/cli/CliTest.java
similarity index 100%
rename from toolkit-cli/src/test/java/org/apache/servicecomb/toolkit/cli/CliTest.java
rename to cli/src/test/java/org/apache/servicecomb/toolkit/cli/CliTest.java
diff --git a/toolkit-cli/src/test/resources/contracts/CalculateEndpoint.yaml b/cli/src/test/resources/contracts/CalculateEndpoint.yaml
similarity index 100%
rename from toolkit-cli/src/test/resources/contracts/CalculateEndpoint.yaml
rename to cli/src/test/resources/contracts/CalculateEndpoint.yaml
diff --git a/toolkit-cli/src/test/resources/contracts/HelloEndPoint.yaml b/cli/src/test/resources/contracts/HelloEndPoint.yaml
similarity index 100%
rename from toolkit-cli/src/test/resources/contracts/HelloEndPoint.yaml
rename to cli/src/test/resources/contracts/HelloEndPoint.yaml
diff --git a/toolkit-cli/src/test/resources/contracts/pojo/CodeFirstHelloEndPoint.yaml b/cli/src/test/resources/contracts/pojo/CodeFirstHelloEndPoint.yaml
similarity index 100%
rename from toolkit-cli/src/test/resources/contracts/pojo/CodeFirstHelloEndPoint.yaml
rename to cli/src/test/resources/contracts/pojo/CodeFirstHelloEndPoint.yaml
diff --git a/toolkit-cli/src/test/resources/swagger.yaml b/cli/src/test/resources/swagger.yaml
similarity index 100%
rename from toolkit-cli/src/test/resources/swagger.yaml
rename to cli/src/test/resources/swagger.yaml
diff --git a/code-generator/pom.xml b/codegen/pom.xml
similarity index 93%
copy from code-generator/pom.xml
copy to codegen/pom.xml
index ae53cd5..0a9348a 100755
--- a/code-generator/pom.xml
+++ b/codegen/pom.xml
@@ -26,7 +26,7 @@
</parent>
<modelVersion>4.0.0</modelVersion>
- <artifactId>code-generator</artifactId>
+ <artifactId>codegen</artifactId>
<properties>
@@ -38,6 +38,12 @@
<dependencies>
<dependency>
+ <groupId>org.apache.servicecomb</groupId>
+ <artifactId>core</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+
+ <dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-codegen</artifactId>
<version>${swagger-codegen-version}</version>
diff --git a/code-generator/src/main/java/org/apache/servicecomb/toolkit/codegen/DefaultCodeGenerator.java b/codegen/src/main/java/org/apache/servicecomb/toolkit/codegen/DefaultCodeGenerator.java
similarity index 74%
rename from code-generator/src/main/java/org/apache/servicecomb/toolkit/codegen/DefaultCodeGenerator.java
rename to codegen/src/main/java/org/apache/servicecomb/toolkit/codegen/DefaultCodeGenerator.java
index 04f14d3..05760c5 100755
--- a/code-generator/src/main/java/org/apache/servicecomb/toolkit/codegen/DefaultCodeGenerator.java
+++ b/codegen/src/main/java/org/apache/servicecomb/toolkit/codegen/DefaultCodeGenerator.java
@@ -17,8 +17,9 @@
package org.apache.servicecomb.toolkit.codegen;
-import java.io.File;
-import java.util.List;
+import java.util.Map;
+
+import org.apache.servicecomb.toolkit.CodeGenerator;
import io.swagger.codegen.DefaultGenerator;
import io.swagger.codegen.config.CodegenConfigurator;
@@ -28,15 +29,24 @@ public class DefaultCodeGenerator implements CodeGenerator {
private DefaultGenerator generator = new DefaultGenerator();
@Override
- public CodeGenerator opts(CodegenConfigurator opts) {
+ public boolean canProcess(String type) {
+ return "default".equals(type);
+ }
+ @Override
+ public void configure(Map<String, Object> config) {
+ CodegenConfigurator opts = (CodegenConfigurator) config.get("configurator");
generator.setGenerateSwaggerMetadata(false);
generator.opts(opts.toClientOptInput());
- return this;
}
@Override
- public List<File> generate() {
- return generator.generate();
+ public boolean generate() {
+
+ if (generator.generate().size() > 0) {
+ return true;
+ }
+
+ return false;
}
}
diff --git a/code-generator/src/main/java/org/apache/servicecomb/toolkit/codegen/GetGenericClassTypeLambda.java b/codegen/src/main/java/org/apache/servicecomb/toolkit/codegen/GetGenericClassTypeLambda.java
similarity index 100%
rename from code-generator/src/main/java/org/apache/servicecomb/toolkit/codegen/GetGenericClassTypeLambda.java
rename to codegen/src/main/java/org/apache/servicecomb/toolkit/codegen/GetGenericClassTypeLambda.java
diff --git a/code-generator/src/main/java/org/apache/servicecomb/toolkit/codegen/RemoveImplSuffixLambda.java b/codegen/src/main/java/org/apache/servicecomb/toolkit/codegen/RemoveImplSuffixLambda.java
similarity index 100%
rename from code-generator/src/main/java/org/apache/servicecomb/toolkit/codegen/RemoveImplSuffixLambda.java
rename to codegen/src/main/java/org/apache/servicecomb/toolkit/codegen/RemoveImplSuffixLambda.java
diff --git a/code-generator/src/main/java/org/apache/servicecomb/toolkit/codegen/ServiceCombCodegen.java b/codegen/src/main/java/org/apache/servicecomb/toolkit/codegen/ServiceCombCodegen.java
similarity index 100%
rename from code-generator/src/main/java/org/apache/servicecomb/toolkit/codegen/ServiceCombCodegen.java
rename to codegen/src/main/java/org/apache/servicecomb/toolkit/codegen/ServiceCombCodegen.java
diff --git a/code-generator/src/main/resources/META-INF/services/io.swagger.codegen.CodegenConfig b/codegen/src/main/resources/META-INF/services/io.swagger.codegen.CodegenConfig
similarity index 100%
rename from code-generator/src/main/resources/META-INF/services/io.swagger.codegen.CodegenConfig
rename to codegen/src/main/resources/META-INF/services/io.swagger.codegen.CodegenConfig
diff --git a/toolkit-cli/src/main/resources/application.properties b/codegen/src/main/resources/META-INF/services/org.apache.servicecomb.toolkit.CodeGenerator
similarity index 93%
copy from toolkit-cli/src/main/resources/application.properties
copy to codegen/src/main/resources/META-INF/services/org.apache.servicecomb.toolkit.CodeGenerator
index a278788..1216792 100755
--- a/toolkit-cli/src/main/resources/application.properties
+++ b/codegen/src/main/resources/META-INF/services/org.apache.servicecomb.toolkit.CodeGenerator
@@ -15,4 +15,4 @@
# limitations under the License.
#
-version=${project.version}
\ No newline at end of file
+org.apache.servicecomb.toolkit.codegen.DefaultCodeGenerator
\ No newline at end of file
diff --git a/code-generator/src/main/resources/ServiceComb/consumer/Application.mustache b/codegen/src/main/resources/ServiceComb/consumer/Application.mustache
similarity index 100%
rename from code-generator/src/main/resources/ServiceComb/consumer/Application.mustache
rename to codegen/src/main/resources/ServiceComb/consumer/Application.mustache
diff --git a/code-generator/src/main/resources/ServiceComb/consumer/apiConsumer.mustache b/codegen/src/main/resources/ServiceComb/consumer/apiConsumer.mustache
similarity index 100%
rename from code-generator/src/main/resources/ServiceComb/consumer/apiConsumer.mustache
rename to codegen/src/main/resources/ServiceComb/consumer/apiConsumer.mustache
diff --git a/code-generator/src/main/resources/ServiceComb/consumer/bodyParamsConsumer.mustache b/codegen/src/main/resources/ServiceComb/consumer/bodyParamsConsumer.mustache
similarity index 100%
rename from code-generator/src/main/resources/ServiceComb/consumer/bodyParamsConsumer.mustache
rename to codegen/src/main/resources/ServiceComb/consumer/bodyParamsConsumer.mustache
diff --git a/code-generator/src/main/resources/ServiceComb/consumer/formParamsConsumer.mustache b/codegen/src/main/resources/ServiceComb/consumer/formParamsConsumer.mustache
similarity index 100%
rename from code-generator/src/main/resources/ServiceComb/consumer/formParamsConsumer.mustache
rename to codegen/src/main/resources/ServiceComb/consumer/formParamsConsumer.mustache
diff --git a/code-generator/src/main/resources/ServiceComb/consumer/headerParamsConsumer.mustache b/codegen/src/main/resources/ServiceComb/consumer/headerParamsConsumer.mustache
similarity index 100%
rename from code-generator/src/main/resources/ServiceComb/consumer/headerParamsConsumer.mustache
rename to codegen/src/main/resources/ServiceComb/consumer/headerParamsConsumer.mustache
diff --git a/code-generator/src/main/resources/ServiceComb/consumer/microservice.mustache b/codegen/src/main/resources/ServiceComb/consumer/microservice.mustache
similarity index 100%
rename from code-generator/src/main/resources/ServiceComb/consumer/microservice.mustache
rename to codegen/src/main/resources/ServiceComb/consumer/microservice.mustache
diff --git a/code-generator/src/main/resources/ServiceComb/consumer/pathParamsConsumer.mustache b/codegen/src/main/resources/ServiceComb/consumer/pathParamsConsumer.mustache
similarity index 100%
rename from code-generator/src/main/resources/ServiceComb/consumer/pathParamsConsumer.mustache
rename to codegen/src/main/resources/ServiceComb/consumer/pathParamsConsumer.mustache
diff --git a/code-generator/src/main/resources/ServiceComb/consumer/pojo/apiConsumer.mustache b/codegen/src/main/resources/ServiceComb/consumer/pojo/apiConsumer.mustache
similarity index 100%
rename from code-generator/src/main/resources/ServiceComb/consumer/pojo/apiConsumer.mustache
rename to codegen/src/main/resources/ServiceComb/consumer/pojo/apiConsumer.mustache
diff --git a/code-generator/src/main/resources/ServiceComb/consumer/pom.mustache b/codegen/src/main/resources/ServiceComb/consumer/pom.mustache
similarity index 100%
rename from code-generator/src/main/resources/ServiceComb/consumer/pom.mustache
rename to codegen/src/main/resources/ServiceComb/consumer/pom.mustache
diff --git a/code-generator/src/main/resources/ServiceComb/consumer/queryParamsConsumer.mustache b/codegen/src/main/resources/ServiceComb/consumer/queryParamsConsumer.mustache
similarity index 100%
rename from code-generator/src/main/resources/ServiceComb/consumer/queryParamsConsumer.mustache
rename to codegen/src/main/resources/ServiceComb/consumer/queryParamsConsumer.mustache
diff --git a/code-generator/src/main/resources/ServiceComb/libraries/SpringBoot/Application.mustache b/codegen/src/main/resources/ServiceComb/consumer/springboot/Application.mustache
similarity index 100%
rename from code-generator/src/main/resources/ServiceComb/libraries/SpringBoot/Application.mustache
rename to codegen/src/main/resources/ServiceComb/consumer/springboot/Application.mustache
diff --git a/code-generator/src/main/resources/ServiceComb/consumer/springboot/pom.mustache b/codegen/src/main/resources/ServiceComb/consumer/springboot/pom.mustache
similarity index 100%
rename from code-generator/src/main/resources/ServiceComb/consumer/springboot/pom.mustache
rename to codegen/src/main/resources/ServiceComb/consumer/springboot/pom.mustache
diff --git a/code-generator/src/main/resources/ServiceComb/enumClass.mustache b/codegen/src/main/resources/ServiceComb/enumClass.mustache
similarity index 100%
rename from code-generator/src/main/resources/ServiceComb/enumClass.mustache
rename to codegen/src/main/resources/ServiceComb/enumClass.mustache
diff --git a/code-generator/src/main/resources/ServiceComb/enumOuterClass.mustache b/codegen/src/main/resources/ServiceComb/enumOuterClass.mustache
similarity index 100%
rename from code-generator/src/main/resources/ServiceComb/enumOuterClass.mustache
rename to codegen/src/main/resources/ServiceComb/enumOuterClass.mustache
diff --git a/code-generator/src/main/resources/ServiceComb/libraries/SpringMVC/Application.mustache b/codegen/src/main/resources/ServiceComb/libraries/JAX-RS/Application.mustache
similarity index 100%
rename from code-generator/src/main/resources/ServiceComb/libraries/SpringMVC/Application.mustache
rename to codegen/src/main/resources/ServiceComb/libraries/JAX-RS/Application.mustache
diff --git a/code-generator/src/main/resources/ServiceComb/libraries/JAX-RS/api.mustache b/codegen/src/main/resources/ServiceComb/libraries/JAX-RS/api.mustache
similarity index 100%
rename from code-generator/src/main/resources/ServiceComb/libraries/JAX-RS/api.mustache
rename to codegen/src/main/resources/ServiceComb/libraries/JAX-RS/api.mustache
diff --git a/code-generator/src/main/resources/ServiceComb/libraries/POJO/bodyParams.mustache b/codegen/src/main/resources/ServiceComb/libraries/JAX-RS/bodyParams.mustache
old mode 100644
new mode 100755
similarity index 100%
rename from code-generator/src/main/resources/ServiceComb/libraries/POJO/bodyParams.mustache
rename to codegen/src/main/resources/ServiceComb/libraries/JAX-RS/bodyParams.mustache
diff --git a/code-generator/src/main/resources/ServiceComb/libraries/JAX-RS/formParams.mustache b/codegen/src/main/resources/ServiceComb/libraries/JAX-RS/formParams.mustache
similarity index 100%
rename from code-generator/src/main/resources/ServiceComb/libraries/JAX-RS/formParams.mustache
rename to codegen/src/main/resources/ServiceComb/libraries/JAX-RS/formParams.mustache
diff --git a/code-generator/src/main/resources/ServiceComb/libraries/JAX-RS/headerParams.mustache b/codegen/src/main/resources/ServiceComb/libraries/JAX-RS/headerParams.mustache
similarity index 100%
rename from code-generator/src/main/resources/ServiceComb/libraries/JAX-RS/headerParams.mustache
rename to codegen/src/main/resources/ServiceComb/libraries/JAX-RS/headerParams.mustache
diff --git a/code-generator/src/main/resources/ServiceComb/libraries/JAX-RS/pathParams.mustache b/codegen/src/main/resources/ServiceComb/libraries/JAX-RS/pathParams.mustache
similarity index 100%
rename from code-generator/src/main/resources/ServiceComb/libraries/JAX-RS/pathParams.mustache
rename to codegen/src/main/resources/ServiceComb/libraries/JAX-RS/pathParams.mustache
diff --git a/code-generator/src/main/resources/ServiceComb/libraries/JAX-RS/pom.mustache b/codegen/src/main/resources/ServiceComb/libraries/JAX-RS/pom.mustache
similarity index 100%
rename from code-generator/src/main/resources/ServiceComb/libraries/JAX-RS/pom.mustache
rename to codegen/src/main/resources/ServiceComb/libraries/JAX-RS/pom.mustache
diff --git a/code-generator/src/main/resources/ServiceComb/libraries/JAX-RS/queryParams.mustache b/codegen/src/main/resources/ServiceComb/libraries/JAX-RS/queryParams.mustache
similarity index 100%
rename from code-generator/src/main/resources/ServiceComb/libraries/JAX-RS/queryParams.mustache
rename to codegen/src/main/resources/ServiceComb/libraries/JAX-RS/queryParams.mustache
diff --git a/code-generator/src/main/resources/ServiceComb/libraries/POJO/Application.mustache b/codegen/src/main/resources/ServiceComb/libraries/POJO/Application.mustache
similarity index 100%
rename from code-generator/src/main/resources/ServiceComb/libraries/POJO/Application.mustache
rename to codegen/src/main/resources/ServiceComb/libraries/POJO/Application.mustache
diff --git a/code-generator/src/main/resources/ServiceComb/libraries/POJO/api.mustache b/codegen/src/main/resources/ServiceComb/libraries/POJO/api.mustache
similarity index 100%
rename from code-generator/src/main/resources/ServiceComb/libraries/POJO/api.mustache
rename to codegen/src/main/resources/ServiceComb/libraries/POJO/api.mustache
diff --git a/code-generator/src/main/resources/ServiceComb/libraries/POJO/apiImpl.mustache b/codegen/src/main/resources/ServiceComb/libraries/POJO/apiImpl.mustache
similarity index 100%
rename from code-generator/src/main/resources/ServiceComb/libraries/POJO/apiImpl.mustache
rename to codegen/src/main/resources/ServiceComb/libraries/POJO/apiImpl.mustache
diff --git a/code-generator/src/main/resources/ServiceComb/libraries/JAX-RS/bodyParams.mustache b/codegen/src/main/resources/ServiceComb/libraries/POJO/bodyParams.mustache
old mode 100755
new mode 100644
similarity index 100%
rename from code-generator/src/main/resources/ServiceComb/libraries/JAX-RS/bodyParams.mustache
rename to codegen/src/main/resources/ServiceComb/libraries/POJO/bodyParams.mustache
diff --git a/code-generator/src/main/resources/ServiceComb/libraries/POJO/formParams.mustache b/codegen/src/main/resources/ServiceComb/libraries/POJO/formParams.mustache
similarity index 100%
rename from code-generator/src/main/resources/ServiceComb/libraries/POJO/formParams.mustache
rename to codegen/src/main/resources/ServiceComb/libraries/POJO/formParams.mustache
diff --git a/code-generator/src/main/resources/ServiceComb/libraries/POJO/headerParams.mustache b/codegen/src/main/resources/ServiceComb/libraries/POJO/headerParams.mustache
similarity index 100%
rename from code-generator/src/main/resources/ServiceComb/libraries/POJO/headerParams.mustache
rename to codegen/src/main/resources/ServiceComb/libraries/POJO/headerParams.mustache
diff --git a/code-generator/src/main/resources/ServiceComb/libraries/POJO/pathParams.mustache b/codegen/src/main/resources/ServiceComb/libraries/POJO/pathParams.mustache
similarity index 100%
rename from code-generator/src/main/resources/ServiceComb/libraries/POJO/pathParams.mustache
rename to codegen/src/main/resources/ServiceComb/libraries/POJO/pathParams.mustache
diff --git a/code-generator/src/main/resources/ServiceComb/libraries/POJO/pom.mustache b/codegen/src/main/resources/ServiceComb/libraries/POJO/pom.mustache
similarity index 100%
rename from code-generator/src/main/resources/ServiceComb/libraries/POJO/pom.mustache
rename to codegen/src/main/resources/ServiceComb/libraries/POJO/pom.mustache
diff --git a/code-generator/src/main/resources/ServiceComb/libraries/POJO/queryParams.mustache b/codegen/src/main/resources/ServiceComb/libraries/POJO/queryParams.mustache
similarity index 100%
rename from code-generator/src/main/resources/ServiceComb/libraries/POJO/queryParams.mustache
rename to codegen/src/main/resources/ServiceComb/libraries/POJO/queryParams.mustache
diff --git a/code-generator/src/main/resources/ServiceComb/consumer/springboot/Application.mustache b/codegen/src/main/resources/ServiceComb/libraries/SpringBoot/Application.mustache
similarity index 100%
rename from code-generator/src/main/resources/ServiceComb/consumer/springboot/Application.mustache
rename to codegen/src/main/resources/ServiceComb/libraries/SpringBoot/Application.mustache
diff --git a/code-generator/src/main/resources/ServiceComb/libraries/SpringBoot/api.mustache b/codegen/src/main/resources/ServiceComb/libraries/SpringBoot/api.mustache
similarity index 100%
rename from code-generator/src/main/resources/ServiceComb/libraries/SpringBoot/api.mustache
rename to codegen/src/main/resources/ServiceComb/libraries/SpringBoot/api.mustache
diff --git a/code-generator/src/main/resources/ServiceComb/libraries/SpringMVC/bodyParams.mustache b/codegen/src/main/resources/ServiceComb/libraries/SpringBoot/bodyParams.mustache
similarity index 100%
rename from code-generator/src/main/resources/ServiceComb/libraries/SpringMVC/bodyParams.mustache
rename to codegen/src/main/resources/ServiceComb/libraries/SpringBoot/bodyParams.mustache
diff --git a/code-generator/src/main/resources/ServiceComb/libraries/SpringMVC/formParams.mustache b/codegen/src/main/resources/ServiceComb/libraries/SpringBoot/formParams.mustache
similarity index 100%
rename from code-generator/src/main/resources/ServiceComb/libraries/SpringMVC/formParams.mustache
rename to codegen/src/main/resources/ServiceComb/libraries/SpringBoot/formParams.mustache
diff --git a/code-generator/src/main/resources/ServiceComb/libraries/SpringMVC/headerParams.mustache b/codegen/src/main/resources/ServiceComb/libraries/SpringBoot/headerParams.mustache
similarity index 100%
rename from code-generator/src/main/resources/ServiceComb/libraries/SpringMVC/headerParams.mustache
rename to codegen/src/main/resources/ServiceComb/libraries/SpringBoot/headerParams.mustache
diff --git a/code-generator/src/main/resources/ServiceComb/libraries/SpringBoot/operationMethod.mustache b/codegen/src/main/resources/ServiceComb/libraries/SpringBoot/operationMethod.mustache
similarity index 100%
rename from code-generator/src/main/resources/ServiceComb/libraries/SpringBoot/operationMethod.mustache
rename to codegen/src/main/resources/ServiceComb/libraries/SpringBoot/operationMethod.mustache
diff --git a/code-generator/src/main/resources/ServiceComb/libraries/SpringMVC/pathParams.mustache b/codegen/src/main/resources/ServiceComb/libraries/SpringBoot/pathParams.mustache
similarity index 100%
rename from code-generator/src/main/resources/ServiceComb/libraries/SpringMVC/pathParams.mustache
rename to codegen/src/main/resources/ServiceComb/libraries/SpringBoot/pathParams.mustache
diff --git a/code-generator/src/main/resources/ServiceComb/libraries/SpringBoot/pom.mustache b/codegen/src/main/resources/ServiceComb/libraries/SpringBoot/pom.mustache
similarity index 100%
rename from code-generator/src/main/resources/ServiceComb/libraries/SpringBoot/pom.mustache
rename to codegen/src/main/resources/ServiceComb/libraries/SpringBoot/pom.mustache
diff --git a/code-generator/src/main/resources/ServiceComb/libraries/SpringMVC/queryParams.mustache b/codegen/src/main/resources/ServiceComb/libraries/SpringBoot/queryParams.mustache
similarity index 100%
rename from code-generator/src/main/resources/ServiceComb/libraries/SpringMVC/queryParams.mustache
rename to codegen/src/main/resources/ServiceComb/libraries/SpringBoot/queryParams.mustache
diff --git a/code-generator/src/main/resources/ServiceComb/libraries/JAX-RS/Application.mustache b/codegen/src/main/resources/ServiceComb/libraries/SpringMVC/Application.mustache
similarity index 100%
rename from code-generator/src/main/resources/ServiceComb/libraries/JAX-RS/Application.mustache
rename to codegen/src/main/resources/ServiceComb/libraries/SpringMVC/Application.mustache
diff --git a/code-generator/src/main/resources/ServiceComb/libraries/SpringMVC/api.mustache b/codegen/src/main/resources/ServiceComb/libraries/SpringMVC/api.mustache
similarity index 100%
rename from code-generator/src/main/resources/ServiceComb/libraries/SpringMVC/api.mustache
rename to codegen/src/main/resources/ServiceComb/libraries/SpringMVC/api.mustache
diff --git a/code-generator/src/main/resources/ServiceComb/libraries/SpringBoot/bodyParams.mustache b/codegen/src/main/resources/ServiceComb/libraries/SpringMVC/bodyParams.mustache
similarity index 100%
rename from code-generator/src/main/resources/ServiceComb/libraries/SpringBoot/bodyParams.mustache
rename to codegen/src/main/resources/ServiceComb/libraries/SpringMVC/bodyParams.mustache
diff --git a/code-generator/src/main/resources/ServiceComb/libraries/SpringBoot/formParams.mustache b/codegen/src/main/resources/ServiceComb/libraries/SpringMVC/formParams.mustache
similarity index 100%
rename from code-generator/src/main/resources/ServiceComb/libraries/SpringBoot/formParams.mustache
rename to codegen/src/main/resources/ServiceComb/libraries/SpringMVC/formParams.mustache
diff --git a/code-generator/src/main/resources/ServiceComb/libraries/SpringBoot/headerParams.mustache b/codegen/src/main/resources/ServiceComb/libraries/SpringMVC/headerParams.mustache
similarity index 100%
rename from code-generator/src/main/resources/ServiceComb/libraries/SpringBoot/headerParams.mustache
rename to codegen/src/main/resources/ServiceComb/libraries/SpringMVC/headerParams.mustache
diff --git a/code-generator/src/main/resources/ServiceComb/libraries/SpringBoot/pathParams.mustache b/codegen/src/main/resources/ServiceComb/libraries/SpringMVC/pathParams.mustache
similarity index 100%
rename from code-generator/src/main/resources/ServiceComb/libraries/SpringBoot/pathParams.mustache
rename to codegen/src/main/resources/ServiceComb/libraries/SpringMVC/pathParams.mustache
diff --git a/code-generator/src/main/resources/ServiceComb/libraries/SpringMVC/pom.mustache b/codegen/src/main/resources/ServiceComb/libraries/SpringMVC/pom.mustache
similarity index 100%
rename from code-generator/src/main/resources/ServiceComb/libraries/SpringMVC/pom.mustache
rename to codegen/src/main/resources/ServiceComb/libraries/SpringMVC/pom.mustache
diff --git a/code-generator/src/main/resources/ServiceComb/libraries/SpringBoot/queryParams.mustache b/codegen/src/main/resources/ServiceComb/libraries/SpringMVC/queryParams.mustache
similarity index 100%
rename from code-generator/src/main/resources/ServiceComb/libraries/SpringBoot/queryParams.mustache
rename to codegen/src/main/resources/ServiceComb/libraries/SpringMVC/queryParams.mustache
diff --git a/code-generator/src/main/resources/ServiceComb/log4j2.mustache b/codegen/src/main/resources/ServiceComb/log4j2.mustache
similarity index 100%
rename from code-generator/src/main/resources/ServiceComb/log4j2.mustache
rename to codegen/src/main/resources/ServiceComb/log4j2.mustache
diff --git a/code-generator/src/main/resources/ServiceComb/model/model.mustache b/codegen/src/main/resources/ServiceComb/model/model.mustache
similarity index 100%
rename from code-generator/src/main/resources/ServiceComb/model/model.mustache
rename to codegen/src/main/resources/ServiceComb/model/model.mustache
diff --git a/code-generator/src/main/resources/ServiceComb/model/pom.mustache b/codegen/src/main/resources/ServiceComb/model/pom.mustache
similarity index 100%
rename from code-generator/src/main/resources/ServiceComb/model/pom.mustache
rename to codegen/src/main/resources/ServiceComb/model/pom.mustache
diff --git a/code-generator/src/main/resources/ServiceComb/pojo.mustache b/codegen/src/main/resources/ServiceComb/pojo.mustache
similarity index 100%
rename from code-generator/src/main/resources/ServiceComb/pojo.mustache
rename to codegen/src/main/resources/ServiceComb/pojo.mustache
diff --git a/code-generator/src/main/resources/ServiceComb/project/pom.mustache b/codegen/src/main/resources/ServiceComb/project/pom.mustache
similarity index 100%
rename from code-generator/src/main/resources/ServiceComb/project/pom.mustache
rename to codegen/src/main/resources/ServiceComb/project/pom.mustache
diff --git a/code-generator/src/main/resources/ServiceComb/provider/microservice.mustache b/codegen/src/main/resources/ServiceComb/provider/microservice.mustache
similarity index 100%
rename from code-generator/src/main/resources/ServiceComb/provider/microservice.mustache
rename to codegen/src/main/resources/ServiceComb/provider/microservice.mustache
diff --git a/code-generator/src/main/resources/ServiceComb/returnTypes.mustache b/codegen/src/main/resources/ServiceComb/returnTypes.mustache
similarity index 100%
rename from code-generator/src/main/resources/ServiceComb/returnTypes.mustache
rename to codegen/src/main/resources/ServiceComb/returnTypes.mustache
diff --git a/code-generator/src/test/java/org/apache/servicecomb/toolkit/codegen/GeneratorTest.java b/codegen/src/test/java/org/apache/servicecomb/toolkit/codegen/GeneratorTest.java
similarity index 78%
rename from code-generator/src/test/java/org/apache/servicecomb/toolkit/codegen/GeneratorTest.java
rename to codegen/src/test/java/org/apache/servicecomb/toolkit/codegen/GeneratorTest.java
index e7ae3d4..2e4a7db 100755
--- a/code-generator/src/test/java/org/apache/servicecomb/toolkit/codegen/GeneratorTest.java
+++ b/codegen/src/test/java/org/apache/servicecomb/toolkit/codegen/GeneratorTest.java
@@ -17,14 +17,15 @@
package org.apache.servicecomb.toolkit.codegen;
-import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
-import java.util.List;
+import java.util.Collections;
+import org.apache.servicecomb.toolkit.CodeGenerator;
+import org.apache.servicecomb.toolkit.GeneratorFactory;
import org.junit.Assert;
import org.junit.Test;
@@ -54,7 +55,8 @@ public class GeneratorTest {
configurator.setOutputDir(tempDir.toFile().getCanonicalPath() + "/ServiceComb");
configurator.setInputSpec(specFilePath.toFile().getCanonicalPath());
DefaultCodeGenerator codeGenerator = new DefaultCodeGenerator();
- List<File> generatedFiles = codeGenerator.opts(configurator).generate();
+ codeGenerator.configure(Collections.singletonMap("configurator",configurator));
+ boolean generateResult = codeGenerator.generate();
Object internalGenerator = ReflectUtils.getProperty(codeGenerator, "generator");
Assert.assertEquals(DefaultGenerator.class, internalGenerator.getClass());
@@ -63,7 +65,19 @@ public class GeneratorTest {
Assert.assertEquals("ServiceComb", ((ServiceCombCodegen) swaggerCodegenConfig).getName());
Assert.assertEquals(CodegenType.SERVER, ((ServiceCombCodegen) swaggerCodegenConfig).getTag());
- Assert.assertTrue(0 < generatedFiles.size());
+ Assert.assertTrue(generateResult);
tempDir.toFile().deleteOnExit();
}
+
+ @Test
+ public void getCodeGeneratorInstanse() {
+
+ CodeGenerator defaultCodeGenerator = GeneratorFactory.getGenerator(CodeGenerator.class,"default");
+ Assert.assertNotNull(defaultCodeGenerator);
+ Assert.assertTrue(defaultCodeGenerator.canProcess("default"));
+
+ CodeGenerator unknownCodeGenerator = GeneratorFactory.getGenerator(CodeGenerator.class,"unknown");
+ Assert.assertNull(unknownCodeGenerator);
+ }
}
+
diff --git a/code-generator/src/test/java/org/apache/servicecomb/toolkit/codegen/ReflectUtils.java b/codegen/src/test/java/org/apache/servicecomb/toolkit/codegen/ReflectUtils.java
similarity index 100%
copy from code-generator/src/test/java/org/apache/servicecomb/toolkit/codegen/ReflectUtils.java
copy to codegen/src/test/java/org/apache/servicecomb/toolkit/codegen/ReflectUtils.java
diff --git a/code-generator/src/test/java/org/apache/servicecomb/toolkit/codegen/ServiceCombCodegenTest.java b/codegen/src/test/java/org/apache/servicecomb/toolkit/codegen/ServiceCombCodegenTest.java
similarity index 100%
copy from code-generator/src/test/java/org/apache/servicecomb/toolkit/codegen/ServiceCombCodegenTest.java
copy to codegen/src/test/java/org/apache/servicecomb/toolkit/codegen/ServiceCombCodegenTest.java
diff --git a/code-generator/src/test/resources/swagger.yaml b/codegen/src/test/resources/swagger.yaml
similarity index 100%
rename from code-generator/src/test/resources/swagger.yaml
rename to codegen/src/test/resources/swagger.yaml
diff --git a/common/pom.xml b/common/pom.xml
index 9a718c0..87a7655 100755
--- a/common/pom.xml
+++ b/common/pom.xml
@@ -30,6 +30,12 @@
<dependencies>
<dependency>
+ <groupId>org.apache.servicecomb</groupId>
+ <artifactId>core</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+
+ <dependency>
<groupId>org.eclipse.jgit</groupId>
<artifactId>org.eclipse.jgit</artifactId>
<version>5.3.0.201903130848-r</version>
diff --git a/common/src/main/java/org/apache/servicecomb/toolkit/common/ContractsUtils.java b/common/src/main/java/org/apache/servicecomb/toolkit/common/ContractsUtils.java
index 01143f0..3dabb7b 100755
--- a/common/src/main/java/org/apache/servicecomb/toolkit/common/ContractsUtils.java
+++ b/common/src/main/java/org/apache/servicecomb/toolkit/common/ContractsUtils.java
@@ -19,8 +19,6 @@ package org.apache.servicecomb.toolkit.common;
import java.io.File;
import java.io.IOException;
-import java.lang.reflect.Field;
-import java.net.URL;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
@@ -29,25 +27,15 @@ import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.HashMap;
import java.util.Map;
-import java.util.Vector;
-import org.apache.servicecomb.provider.pojo.RpcSchema;
-import org.apache.servicecomb.provider.rest.common.RestSchema;
import org.apache.servicecomb.swagger.SwaggerUtils;
-import org.apache.servicecomb.swagger.generator.core.CompositeSwaggerGeneratorContext;
-import org.apache.servicecomb.swagger.generator.core.SwaggerGenerator;
-import org.apache.servicecomb.swagger.generator.core.SwaggerGeneratorContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
import io.swagger.models.Swagger;
public class ContractsUtils {
- private static CompositeSwaggerGeneratorContext compositeSwaggerGeneratorContext = new CompositeSwaggerGeneratorContext();
-
private static Logger LOGGER = LoggerFactory.getLogger(ContractsUtils.class);
public static Map<String, Swagger> getContractsFromFileSystem(String dir) throws IOException {
@@ -81,87 +69,4 @@ public class ContractsUtils {
return contracts;
}
-
- public static void generateAndOutputContracts(String outputDir, String format, URL[] classpathUrls) {
-
- ImmediateClassLoader immediateClassLoader = new ImmediateClassLoader(classpathUrls,
- Thread.currentThread().getContextClassLoader());
-
- try {
-
- Vector allClass = getAllClass(immediateClassLoader);
-
- for (int i = 0; i < allClass.size(); i++) {
-
- Class loadClass = (Class) allClass.get(i);
- if (!canProcess(loadClass)) {
- continue;
- }
-
- SwaggerGeneratorContext generatorContext =
- compositeSwaggerGeneratorContext.selectContext(loadClass);
-
- SwaggerGenerator generator = new SwaggerGenerator(generatorContext, loadClass);
-
- String swaggerString = SwaggerUtils.swaggerToString(generator.generate());
-
- File outputFile = new File(outputDir + File.separator + loadClass.getSimpleName() + format);
-
- if (!outputFile.exists()) {
- if (!outputFile.getParentFile().exists()) {
- outputFile.getParentFile().mkdirs();
- }
- outputFile.createNewFile();
- }
-
- Files.write(Paths.get(outputFile.toURI()), swaggerString.getBytes());
- }
- } catch (IOException e) {
- LOGGER.error(e.getMessage());
- }
- }
-
- private static boolean canProcess(Class<?> loadClass) {
-
- if (loadClass == null) {
- return false;
- }
- RestSchema restSchema = loadClass.getAnnotation(RestSchema.class);
- if (restSchema != null) {
- return true;
- }
-
- RestController controller = loadClass.getAnnotation(RestController.class);
- if (controller != null) {
- return true;
- }
-
- RpcSchema rpcSchema = loadClass.getAnnotation(RpcSchema.class);
- if (rpcSchema != null) {
- return true;
- }
-
- RequestMapping requestMapping = loadClass.getAnnotation(RequestMapping.class);
- if (requestMapping != null) {
- return true;
- }
-
- return false;
- }
-
-
- private static Vector getAllClass(ClassLoader classLoader) {
- Field classesField;
- try {
- classesField = ClassLoader.class.getDeclaredField("classes");
- classesField.setAccessible(true);
-
- if (classesField.get(classLoader) instanceof Vector) {
- return (Vector) classesField.get(classLoader);
- }
- } catch (Exception e) {
- LOGGER.warn("cannot get all class from ClassLoader " + classLoader.getClass());
- }
- return new Vector<>();
- }
}
diff --git a/common/src/main/java/org/apache/servicecomb/toolkit/common/ContractsUtils.java b/common/src/main/java/org/apache/servicecomb/toolkit/common/DefaultContractsGenerator.java
similarity index 74%
copy from common/src/main/java/org/apache/servicecomb/toolkit/common/ContractsUtils.java
copy to common/src/main/java/org/apache/servicecomb/toolkit/common/DefaultContractsGenerator.java
index 01143f0..590020f 100755
--- a/common/src/main/java/org/apache/servicecomb/toolkit/common/ContractsUtils.java
+++ b/common/src/main/java/org/apache/servicecomb/toolkit/common/DefaultContractsGenerator.java
@@ -21,13 +21,8 @@ import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.net.URL;
-import java.nio.file.FileVisitResult;
import java.nio.file.Files;
-import java.nio.file.Path;
import java.nio.file.Paths;
-import java.nio.file.SimpleFileVisitor;
-import java.nio.file.attribute.BasicFileAttributes;
-import java.util.HashMap;
import java.util.Map;
import java.util.Vector;
@@ -37,53 +32,56 @@ import org.apache.servicecomb.swagger.SwaggerUtils;
import org.apache.servicecomb.swagger.generator.core.CompositeSwaggerGeneratorContext;
import org.apache.servicecomb.swagger.generator.core.SwaggerGenerator;
import org.apache.servicecomb.swagger.generator.core.SwaggerGeneratorContext;
+import org.apache.servicecomb.toolkit.ContractsGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
-import io.swagger.models.Swagger;
+public class DefaultContractsGenerator implements ContractsGenerator {
-public class ContractsUtils {
+ private static Logger LOGGER = LoggerFactory.getLogger(ContractsUtils.class);
private static CompositeSwaggerGeneratorContext compositeSwaggerGeneratorContext = new CompositeSwaggerGeneratorContext();
- private static Logger LOGGER = LoggerFactory.getLogger(ContractsUtils.class);
+ private Map<String, Object> config;
- public static Map<String, Swagger> getContractsFromFileSystem(String dir) throws IOException {
+ private URL[] classpathUrls;
- Map<String, Swagger> contracts = new HashMap<>();
- File outputDir = new File(dir);
+ private String outputDir = ".";
- Files.walkFileTree(Paths.get(outputDir.toURI()), new SimpleFileVisitor<Path>() {
- @Override
- public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
- contracts.put(file.toFile().getName(), SwaggerUtils.parseSwagger(file.toUri().toURL()));
- return super.visitFile(file, attrs);
- }
- });
+ private String format = "yaml";
- return contracts;
+ @Override
+ public boolean canProcess(String type) {
+ return "default".equals(type);
}
- public static Map<String, byte[]> getFilesGroupByFilename(String dir) throws IOException {
+ @Override
+ public void configure(Map<String, Object> config) {
+ this.config = config;
- Map<String, byte[]> contracts = new HashMap<>();
- File outputDir = new File(dir);
+ if (config == null) {
+ return;
+ }
- Files.walkFileTree(Paths.get(outputDir.toURI()), new SimpleFileVisitor<Path>() {
- @Override
- public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
- contracts.put(file.toFile().getName(), Files.readAllBytes(file));
- return super.visitFile(file, attrs);
- }
- });
+ Object classpathUrlsObj = config.get("classpathUrls");
+ if (classpathUrlsObj instanceof URL[]) {
+ classpathUrls = (URL[]) classpathUrlsObj;
+ }
- return contracts;
+ Object outputDirObj = config.get("outputDir");
+ if (outputDirObj instanceof String) {
+ outputDir = (String) outputDirObj;
+ }
}
- public static void generateAndOutputContracts(String outputDir, String format, URL[] classpathUrls) {
+ @Override
+ public boolean generate() {
+ if (!checkConfig()) {
+ return false;
+ }
ImmediateClassLoader immediateClassLoader = new ImmediateClassLoader(classpathUrls,
Thread.currentThread().getContextClassLoader());
@@ -118,7 +116,22 @@ public class ContractsUtils {
}
} catch (IOException e) {
LOGGER.error(e.getMessage());
+ return false;
}
+
+ return true;
+ }
+
+ private boolean checkConfig() {
+ if (config == null) {
+ return false;
+ }
+
+ if (classpathUrls == null) {
+ return false;
+ }
+
+ return true;
}
private static boolean canProcess(Class<?> loadClass) {
@@ -149,7 +162,6 @@ public class ContractsUtils {
return false;
}
-
private static Vector getAllClass(ClassLoader classLoader) {
Field classesField;
try {
diff --git a/toolkit-cli/src/main/resources/application.properties b/common/src/main/resources/META-INF/services/org.apache.servicecomb.toolkit.ContractsGenerator
similarity index 92%
copy from toolkit-cli/src/main/resources/application.properties
copy to common/src/main/resources/META-INF/services/org.apache.servicecomb.toolkit.ContractsGenerator
index a278788..e465598 100755
--- a/toolkit-cli/src/main/resources/application.properties
+++ b/common/src/main/resources/META-INF/services/org.apache.servicecomb.toolkit.ContractsGenerator
@@ -15,4 +15,4 @@
# limitations under the License.
#
-version=${project.version}
\ No newline at end of file
+org.apache.servicecomb.toolkit.common.DefaultContractsGenerator
\ No newline at end of file
diff --git a/code-generator/src/test/java/org/apache/servicecomb/toolkit/codegen/ServiceCombCodegenTest.java b/common/src/test/java/org/apache/servicecomb/toolkit/common/ContractsGeneratorTest.java
similarity index 53%
rename from code-generator/src/test/java/org/apache/servicecomb/toolkit/codegen/ServiceCombCodegenTest.java
rename to common/src/test/java/org/apache/servicecomb/toolkit/common/ContractsGeneratorTest.java
index 5846136..d5ed5e6 100755
--- a/code-generator/src/test/java/org/apache/servicecomb/toolkit/codegen/ServiceCombCodegenTest.java
+++ b/common/src/test/java/org/apache/servicecomb/toolkit/common/ContractsGeneratorTest.java
@@ -15,19 +15,27 @@
* limitations under the License.
*/
-package org.apache.servicecomb.toolkit.codegen;
+package org.apache.servicecomb.toolkit.common;
-import org.junit.Assert;
-import org.junit.Test;
+import static org.junit.Assert.*;
-import io.swagger.codegen.CodegenConfig;
-import io.swagger.codegen.CodegenConfigLoader;
+import org.apache.servicecomb.toolkit.ContractsGenerator;
+import org.apache.servicecomb.toolkit.GeneratorFactory;
+import org.junit.Test;
-public class ServiceCombCodegenTest {
+public class ContractsGeneratorTest {
@Test
- public void loadImpl() {
- CodegenConfig codegenConfig = CodegenConfigLoader.forName("ServiceComb");
- Assert.assertEquals(ServiceCombCodegen.class, codegenConfig.getClass());
+ public void getContractsGeneratorInstance() {
+
+ ContractsGenerator defaultGenerator = GeneratorFactory.getGenerator(ContractsGenerator.class,"default");
+ assertNotNull(defaultGenerator);
+
+ ContractsGenerator unknownGenerator = GeneratorFactory.getGenerator(ContractsGenerator.class,"unknown");
+ assertNull(unknownGenerator);
+
+ ContractsGenerator other = GeneratorFactory.getGenerator(ContractsGenerator.class,"other");
+ assertNotNull(other);
+ assertTrue(other.canProcess("other"));
}
}
diff --git a/code-generator/src/test/java/org/apache/servicecomb/toolkit/codegen/ReflectUtils.java b/common/src/test/java/org/apache/servicecomb/toolkit/common/OtherContractsGenerator.java
similarity index 63%
rename from code-generator/src/test/java/org/apache/servicecomb/toolkit/codegen/ReflectUtils.java
rename to common/src/test/java/org/apache/servicecomb/toolkit/common/OtherContractsGenerator.java
index b5892a3..ef8eb34 100755
--- a/code-generator/src/test/java/org/apache/servicecomb/toolkit/codegen/ReflectUtils.java
+++ b/common/src/test/java/org/apache/servicecomb/toolkit/common/OtherContractsGenerator.java
@@ -15,24 +15,29 @@
* limitations under the License.
*/
-package org.apache.servicecomb.toolkit.codegen;
+package org.apache.servicecomb.toolkit.common;
-import java.lang.reflect.Field;
+import java.util.Map;
-class ReflectUtils {
+import org.apache.servicecomb.toolkit.ContractsGenerator;
- static Object getProperty(Object obj, String propName) {
+public class OtherContractsGenerator implements ContractsGenerator {
- try {
- Field propFiled = obj.getClass().getDeclaredField(propName);
- propFiled.setAccessible(true);
- return propFiled.get(obj);
- } catch (NoSuchFieldException e) {
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- e.printStackTrace();
+ @Override
+ public boolean canProcess(String type) {
+ if ("other".equals(type)) {
+ return true;
}
+ return false;
+ }
+
+ @Override
+ public void configure(Map<String, Object> config) {
+ return;
+ }
- return null;
+ @Override
+ public boolean generate() {
+ return false;
}
-}
+}
\ No newline at end of file
diff --git a/toolkit-cli/src/main/resources/application.properties b/common/src/test/resources/META-INF/services/org.apache.servicecomb.toolkit.ContractsGenerator
similarity index 92%
rename from toolkit-cli/src/main/resources/application.properties
rename to common/src/test/resources/META-INF/services/org.apache.servicecomb.toolkit.ContractsGenerator
index a278788..0bbcaee 100755
--- a/toolkit-cli/src/main/resources/application.properties
+++ b/common/src/test/resources/META-INF/services/org.apache.servicecomb.toolkit.ContractsGenerator
@@ -15,4 +15,4 @@
# limitations under the License.
#
-version=${project.version}
\ No newline at end of file
+org.apache.servicecomb.toolkit.common.OtherContractsGenerator
\ No newline at end of file
diff --git a/code-generator/pom.xml b/core/pom.xml
similarity index 76%
rename from code-generator/pom.xml
rename to core/pom.xml
index ae53cd5..290baeb 100755
--- a/code-generator/pom.xml
+++ b/core/pom.xml
@@ -26,14 +26,10 @@
</parent>
<modelVersion>4.0.0</modelVersion>
- <artifactId>code-generator</artifactId>
-
+ <artifactId>core</artifactId>
<properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<swagger-codegen-version>2.4.3</swagger-codegen-version>
- <maven-plugin-version>1.0.0</maven-plugin-version>
- <junit-version>4.8.1</junit-version>
</properties>
<dependencies>
@@ -52,13 +48,6 @@
</exclusion>
</exclusions>
</dependency>
-
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.12</version>
- <scope>test</scope>
- </dependency>
</dependencies>
<build>
@@ -66,21 +55,13 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
- <version>3.6.1</version>
+ <version>3.1</version>
<configuration>
- <source>8</source>
- <target>8</target>
+ <source>1.8</source>
+ <target>1.8</target>
</configuration>
</plugin>
</plugins>
-
- <resources>
- <resource>
- <directory>src/main/resources</directory>
- <includes>
- <include>**/*.*</include>
- </includes>
- </resource>
- </resources>
</build>
-</project>
+
+</project>
\ No newline at end of file
diff --git a/code-generator/src/main/java/org/apache/servicecomb/toolkit/codegen/CodeGenerator.java b/core/src/main/java/org/apache/servicecomb/toolkit/CodeGenerator.java
similarity index 75%
rename from code-generator/src/main/java/org/apache/servicecomb/toolkit/codegen/CodeGenerator.java
rename to core/src/main/java/org/apache/servicecomb/toolkit/CodeGenerator.java
index 2ac18c5..f1f7078 100755
--- a/code-generator/src/main/java/org/apache/servicecomb/toolkit/codegen/CodeGenerator.java
+++ b/core/src/main/java/org/apache/servicecomb/toolkit/CodeGenerator.java
@@ -15,16 +15,7 @@
* limitations under the License.
*/
-package org.apache.servicecomb.toolkit.codegen;
+package org.apache.servicecomb.toolkit;
-import java.io.File;
-import java.util.List;
-
-import io.swagger.codegen.config.CodegenConfigurator;
-
-public interface CodeGenerator {
-
- CodeGenerator opts(CodegenConfigurator opts);
-
- List<File> generate();
+public interface CodeGenerator extends Generator {
}
diff --git a/doc-generator/src/main/java/org/apache/servicecomb/toolkit/docgen/DocGenerator.java b/core/src/main/java/org/apache/servicecomb/toolkit/ContractsGenerator.java
similarity index 79%
copy from doc-generator/src/main/java/org/apache/servicecomb/toolkit/docgen/DocGenerator.java
copy to core/src/main/java/org/apache/servicecomb/toolkit/ContractsGenerator.java
index 6fb053a..ad6bbea 100755
--- a/doc-generator/src/main/java/org/apache/servicecomb/toolkit/docgen/DocGenerator.java
+++ b/core/src/main/java/org/apache/servicecomb/toolkit/ContractsGenerator.java
@@ -15,13 +15,7 @@
* limitations under the License.
*/
-package org.apache.servicecomb.toolkit.docgen;
+package org.apache.servicecomb.toolkit;
-import io.swagger.models.Swagger;
-
-public interface DocGenerator {
-
- boolean canProcess(String type);
-
- String generate(Swagger source, String outputPath);
+public interface ContractsGenerator extends Generator {
}
diff --git a/doc-generator/src/main/java/org/apache/servicecomb/toolkit/docgen/DocGenerator.java b/core/src/main/java/org/apache/servicecomb/toolkit/DocGenerator.java
similarity index 79%
copy from doc-generator/src/main/java/org/apache/servicecomb/toolkit/docgen/DocGenerator.java
copy to core/src/main/java/org/apache/servicecomb/toolkit/DocGenerator.java
index 6fb053a..812c448 100755
--- a/doc-generator/src/main/java/org/apache/servicecomb/toolkit/docgen/DocGenerator.java
+++ b/core/src/main/java/org/apache/servicecomb/toolkit/DocGenerator.java
@@ -15,13 +15,7 @@
* limitations under the License.
*/
-package org.apache.servicecomb.toolkit.docgen;
+package org.apache.servicecomb.toolkit;
-import io.swagger.models.Swagger;
-
-public interface DocGenerator {
-
- boolean canProcess(String type);
-
- String generate(Swagger source, String outputPath);
+public interface DocGenerator extends Generator {
}
diff --git a/doc-generator/src/main/java/org/apache/servicecomb/toolkit/docgen/DocGenerator.java b/core/src/main/java/org/apache/servicecomb/toolkit/Generator.java
similarity index 83%
rename from doc-generator/src/main/java/org/apache/servicecomb/toolkit/docgen/DocGenerator.java
rename to core/src/main/java/org/apache/servicecomb/toolkit/Generator.java
index 6fb053a..7941d3a 100755
--- a/doc-generator/src/main/java/org/apache/servicecomb/toolkit/docgen/DocGenerator.java
+++ b/core/src/main/java/org/apache/servicecomb/toolkit/Generator.java
@@ -15,13 +15,15 @@
* limitations under the License.
*/
-package org.apache.servicecomb.toolkit.docgen;
+package org.apache.servicecomb.toolkit;
-import io.swagger.models.Swagger;
+import java.util.Map;
-public interface DocGenerator {
+public interface Generator {
boolean canProcess(String type);
- String generate(Swagger source, String outputPath);
+ void configure(Map<String, Object> config);
+
+ boolean generate();
}
diff --git a/core/src/main/java/org/apache/servicecomb/toolkit/GeneratorFactory.java b/core/src/main/java/org/apache/servicecomb/toolkit/GeneratorFactory.java
new file mode 100755
index 0000000..fef3fbb
--- /dev/null
+++ b/core/src/main/java/org/apache/servicecomb/toolkit/GeneratorFactory.java
@@ -0,0 +1,61 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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://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.
+ */
+
+package org.apache.servicecomb.toolkit;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.ServiceLoader;
+import java.util.concurrent.ConcurrentHashMap;
+
+public class GeneratorFactory {
+
+ private final static Map<String, List> interfaceMap = new ConcurrentHashMap<>();
+
+ static {
+ addGeneratorType(CodeGenerator.class);
+ addGeneratorType(DocGenerator.class);
+ addGeneratorType(ContractsGenerator.class);
+ }
+
+ public static void addGeneratorType(Class<? extends Generator> generatorClass) {
+
+ if (interfaceMap.get(generatorClass.getName()) == null) {
+ interfaceMap.put(generatorClass.getName(), loadInitialCodeGenerators(generatorClass));
+ }
+ }
+
+ private static List<Generator> loadInitialCodeGenerators(Class<? extends Generator> generatorClass) {
+ List<Generator> registeredGenerators = new ArrayList<>();
+ ServiceLoader.load(generatorClass).forEach(registeredGenerators::add);
+ return registeredGenerators;
+ }
+
+ public static <T extends Generator> T getGenerator(Class<? extends T> generatorClass, String type) {
+
+ @SuppressWarnings("unchecked")
+ List<T> registeredList = (List<T>) interfaceMap.get(generatorClass.getName());
+ for (T generator : registeredList) {
+ if (generator.canProcess(type)) {
+ return generator;
+ }
+ }
+
+ return null;
+ }
+}
diff --git a/doc-generator/src/main/java/org/apache/servicecomb/toolkit/docgen/DocGeneratorManager.java b/doc-generator/src/main/java/org/apache/servicecomb/toolkit/docgen/DocGeneratorManager.java
deleted file mode 100755
index a2b6bc3..0000000
--- a/doc-generator/src/main/java/org/apache/servicecomb/toolkit/docgen/DocGeneratorManager.java
+++ /dev/null
@@ -1,48 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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://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.
- */
-
-package org.apache.servicecomb.toolkit.docgen;
-
-import java.util.ServiceLoader;
-import java.util.concurrent.CopyOnWriteArrayList;
-
-import io.swagger.models.Swagger;
-
-public class DocGeneratorManager {
-
- private final static CopyOnWriteArrayList<DocGenerator> registeredGenerators = new CopyOnWriteArrayList<>();
-
- static {
- loadInitialDocGenerators();
- }
-
- private static void loadInitialDocGenerators() {
-
- ServiceLoader.load(DocGenerator.class).forEach(registeredGenerators::add);
- }
-
- public static String generate(Swagger source, String outputPath, String outputType) {
-
- for (DocGenerator docGenerator : registeredGenerators) {
- if (docGenerator.canProcess(outputType)) {
- return docGenerator.generate(source, outputPath);
- }
- }
-
- return null;
- }
-}
diff --git a/doc-generator/pom.xml b/docgen/pom.xml
similarity index 94%
rename from doc-generator/pom.xml
rename to docgen/pom.xml
index 0b7db64..d5fda74 100755
--- a/doc-generator/pom.xml
+++ b/docgen/pom.xml
@@ -26,7 +26,7 @@
</parent>
<modelVersion>4.0.0</modelVersion>
- <artifactId>doc-generator</artifactId>
+ <artifactId>docgen</artifactId>
<repositories>
<repository>
@@ -41,6 +41,12 @@
<dependencies>
<dependency>
+ <groupId>org.apache.servicecomb</groupId>
+ <artifactId>core</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+
+ <dependency>
<groupId>org.asciidoctor</groupId>
<artifactId>asciidoctorj</artifactId>
<version>1.6.2</version>
diff --git a/doc-generator/src/main/java/org/apache/servicecomb/toolkit/docgen/ContractsAsciidocGenerator.java b/docgen/src/main/java/org/apache/servicecomb/toolkit/docgen/ContractsAsciidocGenerator.java
similarity index 85%
rename from doc-generator/src/main/java/org/apache/servicecomb/toolkit/docgen/ContractsAsciidocGenerator.java
rename to docgen/src/main/java/org/apache/servicecomb/toolkit/docgen/ContractsAsciidocGenerator.java
index 42133f7..a082d98 100755
--- a/doc-generator/src/main/java/org/apache/servicecomb/toolkit/docgen/ContractsAsciidocGenerator.java
+++ b/docgen/src/main/java/org/apache/servicecomb/toolkit/docgen/ContractsAsciidocGenerator.java
@@ -22,6 +22,7 @@ import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Map;
+import org.apache.servicecomb.toolkit.DocGenerator;
import org.asciidoctor.Asciidoctor;
import org.asciidoctor.Attributes;
import org.asciidoctor.AttributesBuilder;
@@ -46,13 +47,35 @@ public class ContractsAsciidocGenerator implements DocGenerator {
private final static String DOC_SUFFIX = ".html";
+ private Swagger contractContent;
+
+ private String outputPath = ".";
+
@Override
public boolean canProcess(String type) {
return type != null && DOC_FORMAT.equals(type.toLowerCase());
}
@Override
- public String generate(Swagger contractContent, String outputPath) {
+ public void configure(Map<String, Object> config) {
+
+ this.contractContent = (Swagger) config.get("contractContent");
+ this.outputPath = (String) config.get("outputPath");
+ }
+
+ private boolean checkConfig() {
+ if (contractContent == null) {
+ return false;
+ }
+ return true;
+ }
+
+ @Override
+ public boolean generate() {
+
+ if (!checkConfig()) {
+ return false;
+ }
Asciidoctor asciidoctor = Asciidoctor.Factory.create();
Swagger2MarkupConverter.Builder markupBuilder = Swagger2MarkupConverter.from(contractContent);
@@ -80,9 +103,10 @@ public class ContractsAsciidocGenerator implements DocGenerator {
Files.write(Paths.get(outputPath), asciidocResult.getBytes());
} catch (IOException e) {
LOGGER.error(e.getMessage());
+ return false;
}
- return asciidocResult;
+ return true;
}
private String correctPath(String filepath) {
diff --git a/doc-generator/src/main/java/org/apache/servicecomb/toolkit/docgen/ContractsSwaggerUIGenerator.java b/docgen/src/main/java/org/apache/servicecomb/toolkit/docgen/ContractsSwaggerUIGenerator.java
similarity index 85%
rename from doc-generator/src/main/java/org/apache/servicecomb/toolkit/docgen/ContractsSwaggerUIGenerator.java
rename to docgen/src/main/java/org/apache/servicecomb/toolkit/docgen/ContractsSwaggerUIGenerator.java
index f39cd22..6926cdb 100755
--- a/doc-generator/src/main/java/org/apache/servicecomb/toolkit/docgen/ContractsSwaggerUIGenerator.java
+++ b/docgen/src/main/java/org/apache/servicecomb/toolkit/docgen/ContractsSwaggerUIGenerator.java
@@ -28,6 +28,7 @@ import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
+import org.apache.servicecomb.toolkit.DocGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -44,6 +45,10 @@ public class ContractsSwaggerUIGenerator implements DocGenerator {
private final static String DOC_SUFFIX = ".html";
+ private Swagger contractContent;
+
+ private String outputPath = ".";
+
@Override
public boolean canProcess(String type) {
@@ -51,7 +56,24 @@ public class ContractsSwaggerUIGenerator implements DocGenerator {
}
@Override
- public String generate(Swagger contractContent, String outputPath) {
+ public void configure(Map<String, Object> config) {
+ this.contractContent = (Swagger) config.get("contractContent");
+ this.outputPath = (String) config.get("outputPath");
+ }
+
+ private boolean checkConfig() {
+ if (contractContent == null) {
+ return false;
+ }
+ return true;
+ }
+
+ @Override
+ public boolean generate() {
+
+ if (!checkConfig()) {
+ return false;
+ }
String swaggerUiHtml = null;
try {
@@ -71,9 +93,10 @@ public class ContractsSwaggerUIGenerator implements DocGenerator {
Files.write(outputFile, swaggerUiHtml.getBytes());
} catch (IOException e) {
LOGGER.error(e.getMessage());
+ return false;
}
- return swaggerUiHtml;
+ return true;
}
private String correctPath(String filepath) {
diff --git a/doc-generator/src/main/resources/META-INF/services/org.apache.servicecomb.toolkit.docgen.DocGenerator b/docgen/src/main/resources/META-INF/services/org.apache.servicecomb.toolkit.DocGenerator
similarity index 100%
rename from doc-generator/src/main/resources/META-INF/services/org.apache.servicecomb.toolkit.docgen.DocGenerator
rename to docgen/src/main/resources/META-INF/services/org.apache.servicecomb.toolkit.DocGenerator
diff --git a/doc-generator/src/main/resources/webroot/swagger-ui.html b/docgen/src/main/resources/webroot/swagger-ui.html
similarity index 100%
rename from doc-generator/src/main/resources/webroot/swagger-ui.html
rename to docgen/src/main/resources/webroot/swagger-ui.html
diff --git a/doc-generator/src/test/java/docgen/DocGeneratorTest.java b/docgen/src/test/java/docgen/DocGeneratorTest.java
similarity index 71%
rename from doc-generator/src/test/java/docgen/DocGeneratorTest.java
rename to docgen/src/test/java/docgen/DocGeneratorTest.java
index c4dbb64..278766c 100755
--- a/doc-generator/src/test/java/docgen/DocGeneratorTest.java
+++ b/docgen/src/test/java/docgen/DocGeneratorTest.java
@@ -21,7 +21,8 @@ import io.swagger.models.Swagger;
import io.swagger.parser.Swagger20Parser;
import org.apache.commons.io.FileUtils;
-import org.apache.servicecomb.toolkit.docgen.DocGeneratorManager;
+import org.apache.servicecomb.toolkit.GeneratorFactory;
+import org.apache.servicecomb.toolkit.DocGenerator;
import org.junit.Assert;
import org.junit.Test;
@@ -31,6 +32,8 @@ import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
+import java.util.HashMap;
+import java.util.Map;
public class DocGeneratorTest {
@@ -53,7 +56,12 @@ public class DocGeneratorTest {
Path outputPath = Paths.get(tempDir.toFile().getAbsolutePath()
+ File.separator + "asciidoc.html");
- DocGeneratorManager.generate(swagger, outputPath.toFile().getCanonicalPath(), "asciidoc-html");
+ DocGenerator docGenerator = GeneratorFactory.getGenerator(DocGenerator.class, "asciidoc-html");
+ Map<String, Object> docGeneratorConfig = new HashMap<>();
+ docGeneratorConfig.put("contractContent", swagger);
+ docGeneratorConfig.put("outputPath", outputPath.toFile().getCanonicalPath());
+ docGenerator.configure(docGeneratorConfig);
+ docGenerator.generate();
Assert.assertTrue(Files.exists(outputPath));
FileUtils.deleteDirectory(tempDir.toFile());
@@ -78,36 +86,21 @@ public class DocGeneratorTest {
Path tempDir = Files.createTempDirectory(null);
Path outputPath = Paths.get(tempDir.toFile().getAbsolutePath()
+ File.separator + "swagger-ui.html");
- DocGeneratorManager.generate(swagger, outputPath.toFile().getCanonicalPath(), "swagger-ui");
+ DocGenerator docGenerator = GeneratorFactory.getGenerator(DocGenerator.class, "swagger-ui");
+ Map<String, Object> docGeneratorConfig = new HashMap<>();
+ docGeneratorConfig.put("contractContent", swagger);
+ docGeneratorConfig.put("outputPath", outputPath.toFile().getCanonicalPath());
+ docGenerator.configure(docGeneratorConfig);
+ docGenerator.generate();
Assert.assertTrue(Files.exists(outputPath));
FileUtils.deleteDirectory(tempDir.toFile());
}
@Test
- public void contractTransferToOther() throws IOException {
+ public void contractTransferToOther() {
- Swagger20Parser swagger20Parser = new Swagger20Parser();
-
- InputStream in = DocGeneratorTest.class.getClassLoader().getResourceAsStream("HelloEndPoint.yaml");
-
- StringBuilder sb = new StringBuilder();
- byte[] bytes = new byte[1024];
- int len = -1;
- while ((len = in.read(bytes)) != -1) {
- sb.append(new String(bytes, 0, len));
- }
-
- Swagger swagger = swagger20Parser.parse(sb.toString());
-
- Path tempDir = Files.createTempDirectory(null);
- Path outputPath = Paths.get(tempDir.toFile().getAbsolutePath()
- + File.separator + "swagger-ui.html");
-
- DocGeneratorManager.generate(swagger, outputPath.toFile().getCanonicalPath(), "other");
-
- Assert.assertFalse(Files.exists(outputPath));
-
- FileUtils.deleteDirectory(tempDir.toFile());
+ DocGenerator docGenerator = GeneratorFactory.getGenerator(DocGenerator.class, "other");
+ Assert.assertNull(docGenerator);
}
}
diff --git a/doc-generator/src/test/resources/HelloEndPoint.yaml b/docgen/src/test/resources/HelloEndPoint.yaml
similarity index 100%
rename from doc-generator/src/test/resources/HelloEndPoint.yaml
rename to docgen/src/test/resources/HelloEndPoint.yaml
diff --git a/pom.xml b/pom.xml
index 1ec4144..a93f31c 100755
--- a/pom.xml
+++ b/pom.xml
@@ -33,11 +33,12 @@
</properties>
<modules>
- <module>code-generator</module>
- <module>toolkit-cli</module>
- <module>doc-generator</module>
+ <module>codegen</module>
+ <module>cli</module>
+ <module>docgen</module>
<module>common</module>
<module>toolkit-maven-plugin</module>
+ <module>core</module>
</modules>
<build>
diff --git a/toolkit-maven-plugin/pom.xml b/toolkit-maven-plugin/pom.xml
index 37661fb..8eb2f86 100755
--- a/toolkit-maven-plugin/pom.xml
+++ b/toolkit-maven-plugin/pom.xml
@@ -31,7 +31,7 @@
<dependencies>
<dependency>
<groupId>org.apache.servicecomb</groupId>
- <artifactId>doc-generator</artifactId>
+ <artifactId>docgen</artifactId>
<version>${project.version}</version>
</dependency>
diff --git a/toolkit-maven-plugin/src/main/java/org/apache/servicecomb/toolkit/plugin/ContractGenerator.java b/toolkit-maven-plugin/src/main/java/org/apache/servicecomb/toolkit/plugin/ContractGenerator.java
index 128dcaa..227899f 100755
--- a/toolkit-maven-plugin/src/main/java/org/apache/servicecomb/toolkit/plugin/ContractGenerator.java
+++ b/toolkit-maven-plugin/src/main/java/org/apache/servicecomb/toolkit/plugin/ContractGenerator.java
@@ -20,11 +20,14 @@ package org.apache.servicecomb.toolkit.plugin;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
import org.apache.maven.artifact.DependencyResolutionRequiredException;
import org.apache.maven.project.MavenProject;
-import org.apache.servicecomb.toolkit.common.ContractsUtils;
+import org.apache.servicecomb.toolkit.ContractsGenerator;
+import org.apache.servicecomb.toolkit.GeneratorFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -56,6 +59,11 @@ public class ContractGenerator {
}
}
- ContractsUtils.generateAndOutputContracts(outputDir, format, runtimeUrls);
+ ContractsGenerator contractGenerator = GeneratorFactory.getGenerator(ContractsGenerator.class,"default");
+ Map<String,Object> contractConfig = new HashMap<>();
+ contractConfig.put("classpathUrls",runtimeUrls);
+ contractConfig.put("outputDir",outputDir);
+ contractGenerator.configure(contractConfig);
+ contractGenerator.generate();
}
}
diff --git a/toolkit-maven-plugin/src/main/java/org/apache/servicecomb/toolkit/plugin/GenerateContractsDocMojo.java b/toolkit-maven-plugin/src/main/java/org/apache/servicecomb/toolkit/plugin/GenerateContractsDocMojo.java
index 3c5d975..ba46c53 100755
--- a/toolkit-maven-plugin/src/main/java/org/apache/servicecomb/toolkit/plugin/GenerateContractsDocMojo.java
+++ b/toolkit-maven-plugin/src/main/java/org/apache/servicecomb/toolkit/plugin/GenerateContractsDocMojo.java
@@ -25,6 +25,8 @@ import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
+import java.util.HashMap;
+import java.util.Map;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
@@ -36,7 +38,8 @@ import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;
import org.apache.servicecomb.swagger.SwaggerUtils;
-import org.apache.servicecomb.toolkit.docgen.DocGeneratorManager;
+import org.apache.servicecomb.toolkit.GeneratorFactory;
+import org.apache.servicecomb.toolkit.DocGenerator;
@Mojo(name = "generateDoc", defaultPhase = LifecyclePhase.COMPILE, requiresDependencyResolution = ResolutionScope.COMPILE)
@Execute(goal = "generateDoc",
@@ -69,10 +72,14 @@ public class GenerateContractsDocMojo extends AbstractMojo {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
- DocGeneratorManager.generate(SwaggerUtils.parseSwagger(file.toUri().toURL()),
- docOutputDir + File.separator
- + file.toFile().getName().substring(0, file.toFile().getName().indexOf(".")),
- format);
+ DocGenerator docGenerator = GeneratorFactory.getGenerator(DocGenerator.class, format);
+ Map<String, Object> docGeneratorConfig = new HashMap<>();
+ docGeneratorConfig.put("contractContent", SwaggerUtils.parseSwagger(file.toUri().toURL()));
+ docGeneratorConfig.put("outputPath",docOutputDir + File.separator
+ + file.toFile().getName().substring(0, file.toFile().getName().indexOf(".")));
+ docGenerator.configure(docGeneratorConfig);
+ docGenerator.generate();
+
return super.visitFile(file, attrs);
}
});