Skip to content

Commit

Permalink
Merge e683884 into e3eea77
Browse files Browse the repository at this point in the history
  • Loading branch information
MabinGo committed Jul 26, 2019
2 parents e3eea77 + e683884 commit 94c1e5b
Show file tree
Hide file tree
Showing 21 changed files with 337 additions and 132 deletions.
33 changes: 28 additions & 5 deletions README-ZH.md
Expand Up @@ -101,14 +101,27 @@ $ mvn clean install
<contractFileType>yaml</contractFileType>
<!-- 生成文档的类型,不设置则默认为 html -->
<documentType>html</documentType>
<!-- 生成契约文件、文档的根目录,不设置则默认为运行命令所在目录下的 target 目录,生成的契约文件在 contract 目录,生成的文档在 document 目录 -->
<!-- 生成契约文件、文档的根目录,不设置则默认为运行命令所在目录下的 target 目录,生成的微服务工程在 project 目录,契约文件在 contract 目录,文档在 document 目录 -->
<outputDirectory>./target</outputDirectory>
<!-- 被解析的契约文件路径,在 sourceType 设置为 contract 时有效,且必须设置 -->
<contractLocation>./contract</contractLocation>
<!-- 被校验的契约文件目录,在 sourceType 设置为 contract 时有效,且必须设置 -->
<sourceContractPath>./target/contract</sourceContractPath>
<!-- 样本契约文件目录,必须设置 -->
<destinationContractPath>./contract</destinationContractPath>
<!-- 生成的微服务代码工程配置 -->
<service>
<!-- 微服务的类型,可生成 provider/consumer/all,默认值为 all -->
<serviceType>all</serviceType>
<!-- 微服务的 groupid,用户可选,默认值为 domain.orgnization.project -->
<groupId>domain.orgnization.project</groupId>
<!-- 微服务的 artifactId,用户可选,默认值为 sample -->
<artifactId>sample</artifactId>
<!-- 微服务的 artifactVersion,用户可选,默认值为 0.1.0-SNAPSHOT -->
<artifactVersion>0.1.0-SNAPSHOT</artifactVersion>
<!-- 微服务的 packageName,用户可选,默认值为 domain.orgnization.project.sample -->
<packageName>domain.orgnization.project.sample</packageName>
</service>
</configuration>
</plugin>
```
Expand All @@ -122,7 +135,7 @@ mvn toolkit:generate
mvn toolkit:verify
```

#### 3.2.2.1 解析代码,生成OpenAPI规范契约、文档
#### 3.2.2.1 解析代码,生成微服务代码工程、OpenAPI规范契约、文档

配置项(不显式设置 `<configuration>` 则使用默认配置)
例:
Expand All @@ -134,8 +147,13 @@ mvn toolkit:verify
<configuration>
<!-- 输入源。设置为 code,表示解析当前代码;设置为 contract,表示解析指定目录的契约文件。不设置则默认为 code -->
<sourceType>code</sourceType>
<!-- 生成契约文件、文档的根目录,不设置则默认为运行命令所在目录下的 target 目录,生成的契约文件在 contract 目录,生成的文档在 document 目录 -->
<!-- 生成契约文件、文档的根目录,不设置则默认为运行命令所在目录下的 target 目录,生成的微服务工程在 project 目录,契约文件在 contract 目录,文档在 document 目录 -->
<outputDirectory>./target</outputDirectory>
<!-- 生成的微服务代码工程配置 -->
<service>
<!-- 微服务的类型,可生成 provider/consumer/all,默认值为 all -->
<serviceType>all</serviceType>
</service>
</configuration>
</plugin>
```
Expand All @@ -145,7 +163,7 @@ mvn toolkit:verify
mvn toolkit:generate
```

#### 3.2.2.2 解析契约,生成文档
#### 3.2.2.2 解析契约,生成微服务工程、文档

配置项(不显式设置 `<configuration>` 则使用默认配置)
例:
Expand All @@ -159,8 +177,13 @@ mvn toolkit:generate
<sourceType>contract</sourceType>
<!-- 被解析的契约文件路径,在 sourceType 设置为 contract 时有效,且必须设置 -->
<contractLocation>./contract</contractLocation>
<!-- 生成契约文件、文档的根目录,不设置则默认为运行命令所在目录下的 target 目录,生成的契约文件在 contract 目录,生成的文档在 document 目录 -->
<!-- 生成契约文件、文档的根目录,不设置则默认为运行命令所在目录下的 target 目录,生成的微服务工程在 project 目录,契约文件在 contract 目录,文档在 document 目录 -->
<outputDirectory>./target</outputDirectory>
<!-- 生成的微服务代码工程配置 -->
<service>
<!-- 微服务的类型,可生成 provider/consumer/all,默认值为 all -->
<serviceType>provider</serviceType>
</service>
</configuration>
</plugin>
```
Expand Down
29 changes: 26 additions & 3 deletions README.md
Expand Up @@ -99,14 +99,27 @@ Configured in the pom file of the maven project
<contractFileType>yaml</contractFileType>
<!-- The type of the generated document. If not set, the default is 'html' -->
<documentType>html</documentType>
<!-- The root directory to save contract file and document. If it is not set, the default is the 'target' under the directory where the command is run -->
<!-- The root directory to save microservice project,contract file and document. If it is not set, the default is the 'target' under the directory where the command is run -->
<outputDirectory>./target</outputDirectory>
<!-- Input contract file path. Valid when sourceType is set to 'contract', must be set -->
<contractLocation>./contract</contractLocation>
<!-- Checked contract file path. Valid when sourceType is set to 'contract', must be set -->
<sourceContractPath>./target/contract</sourceContractPath>
<!-- Sample contract file path, must be set -->
<destinationContractPath>./contract</destinationContractPath>
<!-- Generated microservice project configuration -->
<service>
<!-- Microservice type,can generated 'provider/consumer/all',the default is 'all' -->
<serviceType>all</serviceType>
<!-- Microservice project 'groupid',optional,the default is 'domain.orgnization.project' -->
<groupId>domain.orgnization.project</groupId>
<!-- Microservice project 'artifactId',optional,the default is 'sample' -->
<artifactId>sample</artifactId>
<!-- Microservice project 'artifactVersion',optional,the default is '0.1.0-SNAPSHOT' -->
<artifactVersion>0.1.0-SNAPSHOT</artifactVersion>
<!-- Microservice project 'packageName',optional,the default is 'domain.orgnization.project.sample' -->
<packageName>domain.orgnization.project.sample</packageName>
</service>
</configuration>
</plugin>
```
Expand All @@ -120,7 +133,7 @@ mvn toolkit:generate
mvn toolkit:verify
```

#### 3.2.2.1 Extract the OpenAPI contract file and document from the code
#### 3.2.2.1 Extract the microservice project, OpenAPI contract file and document from the code

Configuration(use default configuration if not set `<configuration>`)

Expand All @@ -135,6 +148,11 @@ example
<sourceType>code</sourceType>
<!-- The root directory to save contract file and document. If it is not set, the default is the 'target' under the directory where the command is run -->
<outputDirectory>./target</outputDirectory>
<!-- Generated microservice project configuration -->
<service>
<!-- Microservice type,can generated 'provider/consumer/all',the default is 'all' -->
<serviceType>all</serviceType>
</service>
</configuration>
</plugin>
```
Expand All @@ -145,7 +163,7 @@ mvn toolkit:generate
```


#### 3.2.2.2 Generate document from contract
#### 3.2.2.2 Generate the microservice project and document from contract

Configuration(use default configuration if not set `<configuration>`)

Expand All @@ -162,6 +180,11 @@ example
<outputDirectory>./target</outputDirectory>
<!-- Input contract file path. Valid when sourceType is set to 'contract', must be set -->
<contractLocation>./contract</contractLocation>
<!-- Generated microservice project configuration -->
<service>
<!-- Microservice type,can generated 'provider/consumer/all',the default is 'all' -->
<serviceType>provider</serviceType>
</service>
</configuration>
</plugin>
```
Expand Down
Expand Up @@ -62,7 +62,6 @@ public void run() {
try {
Path specPath = Paths.get(specFile);

boolean[] retValues = new boolean[1];
String[] fileName = new String[1];

DocGenerator docGenerator = GeneratorFactory.getGenerator(DocGenerator.class, format);
Expand All @@ -78,11 +77,7 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO
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;
}
docGenerator.generate();

return super.visitFile(file, attrs);
}
Expand All @@ -94,20 +89,15 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO
docGeneratorConfig.put("outputPath", output + File.separator + new File(specFile).getName()
.substring(0, new File(specFile).getName().indexOf(".")));
docGenerator.configure(docGeneratorConfig);
retValues[0] = docGenerator.generate();
docGenerator.generate();
} else {
fileName[0] = specFile;

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 (!retValues[0]) {
LOGGER.error("Failed to generate document base on file {}", fileName[0]);
return;
docGenerator.generate();
}

LOGGER.info("Success to generate document, the directory is: {}", output);
Expand Down
Expand Up @@ -41,12 +41,7 @@ public void configure(Map<String, Object> config) {
}

@Override
public boolean generate() {

if (generator.generate().size() > 0) {
return true;
}

return false;
public void generate() {
generator.generate();
}
}
Expand Up @@ -99,18 +99,17 @@ public ServiceCombCodegen() {
modelTemplateFiles.put(modelTemplateFolder + "/model.mustache", ".java");
modelTemplateFiles.remove("model.mustache");

groupId = "org.apache.servicecomb.toolkit";
artifactId = "app";
groupId = "domain.orgnization.project";
artifactId = "sample";

apiPackage = groupId + "." + artifactId + ".api";
modelPackage = groupId + "." + artifactId + ".model";
mainClassPackage = groupId + "." + artifactId;

supportedLibraries.put(DEFAULT_LIBRARY, "ServiceComb Server application using the springboot programming model.");
supportedLibraries.put(POJO_LIBRARY, "ServiceComb Server application using the pojo programming model.");
supportedLibraries.put(JAX_RS_LIBRARY, "ServiceComb Server application using the jax-rs programming model.");
supportedLibraries
.put(SPRING_BOOT_LIBRARY, "ServiceComb Server application using the SpringBoot programming model.");
supportedLibraries.put(DEFAULT_LIBRARY, "ServiceComb Server application using the SpringMVC programming model.");
supportedLibraries.put(POJO_LIBRARY, "ServiceComb Server application using the POJO programming model.");
supportedLibraries.put(JAX_RS_LIBRARY, "ServiceComb Server application using the JAX-RS programming model.");
supportedLibraries.put(SPRING_BOOT_LIBRARY, "ServiceComb Server application using the SpringBoot programming model.");

setLibrary(DEFAULT_LIBRARY);

Expand Down
Expand Up @@ -17,6 +17,8 @@

package org.apache.servicecomb.toolkit.codegen;

import static org.junit.Assert.fail;

import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
Expand Down Expand Up @@ -55,8 +57,13 @@ private void generateCode(String programmingModel) throws IOException, URISyntax
configurator.setOutputDir(tempDir.toFile().getCanonicalPath() + "/ServiceComb");
configurator.setInputSpec(specFilePath.toFile().getCanonicalPath());
DefaultCodeGenerator codeGenerator = new DefaultCodeGenerator();
codeGenerator.configure(Collections.singletonMap("configurator",configurator));
boolean generateResult = codeGenerator.generate();
codeGenerator.configure(Collections.singletonMap("configurator", configurator));

try {
codeGenerator.generate();
} catch (RuntimeException e) {
fail();
}

Object internalGenerator = ReflectUtils.getProperty(codeGenerator, "generator");
Assert.assertEquals(DefaultGenerator.class, internalGenerator.getClass());
Expand All @@ -65,18 +72,17 @@ private void generateCode(String programmingModel) throws IOException, URISyntax
Assert.assertEquals("ServiceComb", ((ServiceCombCodegen) swaggerCodegenConfig).getName());
Assert.assertEquals(CodegenType.SERVER, ((ServiceCombCodegen) swaggerCodegenConfig).getTag());

Assert.assertTrue(generateResult);
tempDir.toFile().deleteOnExit();
}

@Test
public void getCodeGeneratorInstanse() {

CodeGenerator defaultCodeGenerator = GeneratorFactory.getGenerator(CodeGenerator.class,"default");
CodeGenerator defaultCodeGenerator = GeneratorFactory.getGenerator(CodeGenerator.class, "default");
Assert.assertNotNull(defaultCodeGenerator);
Assert.assertTrue(defaultCodeGenerator.canProcess("default"));

CodeGenerator unknownCodeGenerator = GeneratorFactory.getGenerator(CodeGenerator.class,"unknown");
CodeGenerator unknownCodeGenerator = GeneratorFactory.getGenerator(CodeGenerator.class, "unknown");
Assert.assertNull(unknownCodeGenerator);
}
}
Expand Down
Expand Up @@ -34,7 +34,7 @@ public class FileUtils {
public static void createDirectory(String pathName) throws IOException {

if (pathName == null) {
throw new IOException("path is null");
throw new IOException("Path is null");
}

File path = new File(pathName);
Expand All @@ -43,7 +43,7 @@ public static void createDirectory(String pathName) throws IOException {
}

if (!path.mkdirs()) {
throw new IOException("failed to create directory");
throw new IOException("Failed to create directory");
}
}

Expand All @@ -57,11 +57,11 @@ public static Path createTempDirectory(String pathName) throws IOException {
public static Map<String, byte[]> getFilesGroupByFilename(String pathName) throws IOException {

if (pathName == null) {
throw new IOException("path is null");
throw new IOException("Path is null");
}

if (!new File(pathName).exists()) {
throw new IOException("path " + pathName + " is not exists");
throw new IOException("Path " + pathName + " is not exists");
}

Map<String, byte[]> filesGroup = new HashMap<>();
Expand All @@ -82,12 +82,6 @@ private static void deleteDirectory(String pathName) throws IOException {

File path = new File(pathName);

/*
if (!path.exists()) {
return;
}
*/

if (!path.isDirectory()) {
Files.delete(Paths.get(pathName));
return;
Expand Down
Expand Up @@ -35,8 +35,8 @@ public class MyersAlgorithm implements CompareAlgorithm {
public List<Comparison> compare(String source, String dest) {

if ((source == null) || (dest == null)) {
LOGGER.error("source is {} and dest is {}", source, dest);
throw new RuntimeException("source and dest must not be null");
LOGGER.error("Source is {} and dest is {}", source, dest);
throw new RuntimeException("Source and dest must not be null");
}

EditList diffList = new EditList();
Expand Down
4 changes: 2 additions & 2 deletions common/src/test/java/org/apache/servicecomb/toolkit/common/FileUtilsTest.java 100644 → 100755
Expand Up @@ -35,7 +35,7 @@ public void createDirectoryTest() {
try {
FileUtils.createDirectory(null);
} catch (IOException e) {
assertEquals("path is null", e.getMessage());
assertEquals("Path is null", e.getMessage());
}

Path path;
Expand All @@ -52,7 +52,7 @@ public void getFilesGroupByFilenameTest() {
try {
FileUtils.getFilesGroupByFilename(null);
} catch (IOException e) {
assertEquals("path is null", e.getMessage());
assertEquals("Path is null", e.getMessage());
}

try {
Expand Down
Expand Up @@ -126,7 +126,7 @@ public void contractCompareException() throws IOException {
assertEquals(MyersAlgorithm.class, contractComparator.getAlgorithm().getClass());
contractComparator.splitPrint(bout);
} catch (RuntimeException e) {
assertEquals("source and dest must not be null", e.getMessage());
assertEquals("Source and dest must not be null", e.getMessage());
}
}
}

0 comments on commit 94c1e5b

Please sign in to comment.