Skip to content

Commit

Permalink
#1215 Merge branch 'dev_cli' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
maybeec committed Sep 15, 2020
2 parents f929963 + a0a15df commit c769ede
Show file tree
Hide file tree
Showing 12 changed files with 236 additions and 62 deletions.
2 changes: 1 addition & 1 deletion cobigen-cli/class-loader-agent/pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>class-loader-agent</artifactId>
<version>1.2.0</version>
<version>7.0.0</version>
<name>Class loader agent</name>
<description>Class loader agent for CobiGen CLI</description>

Expand Down
6 changes: 3 additions & 3 deletions cobigen-cli/cli/pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>cli</artifactId>
<version>1.2.0</version>
<version>7.0.0</version>
<name>cobigen-cli</name>
<description>Command Line Interface for CobiGen</description>

Expand All @@ -24,12 +24,12 @@
<dependency>
<groupId>com.devonfw.cobigen</groupId>
<artifactId>core</artifactId>
<version>6.1.0</version>
<version>7.0.0</version>
</dependency>
<dependency>
<groupId>com.devonfw.cobigen</groupId>
<artifactId>core-api</artifactId>
<version>6.1.0</version>
<version>7.0.0</version>
</dependency>
<dependency>
<groupId>info.picocli</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.stream.Collectors;

import org.apache.commons.text.similarity.JaccardDistance;
import org.apache.maven.shared.utils.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -303,9 +306,9 @@ public void generate(File inputFile, File inputProject, List<? extends Generable
inputFile = ConfigurationUtils.preprocessInputFile(inputFile);
try {
Object input;
String extension = inputFile.getName().toLowerCase();
Boolean isJavaInput = extension.endsWith(".java");
Boolean isOpenApiInput = extension.endsWith(".yaml") || extension.endsWith(".yml");
String extension = FileUtils.getExtension(inputFile.getName());
Boolean isJavaInput = extension.equals("java");
Boolean isOpenApiInput = extension.equals("yaml") || extension.equals("yml");

input = CobiGenUtils.getValidCobiGenInput(cg, inputFile, isJavaInput);

Expand Down Expand Up @@ -345,16 +348,22 @@ public void generate(File inputFile, File inputProject, List<? extends Generable
report = cg.generate(input, finalTos, Paths.get(outputRootPath.getAbsolutePath()), false, classLoader,
templateFolder);
}
if (ValidationUtils.checkGenerationReport(report) && isJavaInput) {
try {
ParsingUtils.formatJavaSources(report.getGeneratedFiles());
} catch (FormatterException e) {
LOG.info(
"Generation was successful but we were not able to format your code. Maybe you will see strange formatting.");
if (ValidationUtils.checkGenerationReport(report)) {
Set<Path> generatedJavaFiles = report.getGeneratedFiles().stream()
.filter(e -> FileUtils.getExtension(e.toAbsolutePath().toString()).equals("java"))
.collect(Collectors.toSet());
if (!generatedJavaFiles.isEmpty()) {
try {
ParsingUtils.formatJavaSources(generatedJavaFiles);
} catch (FormatterException e) {
LOG.info(
"Generation was successful but we were not able to format your code. Maybe you will see strange formatting.",
LOG.isDebugEnabled() ? e : null);
}
}
}
} catch (InputReaderException e) {
LOG.error("Invalid input for CobiGen, please check your input file.");
LOG.error("Invalid input for CobiGen, please check your input file.", e);

}
}
Expand Down Expand Up @@ -561,25 +570,54 @@ private ArrayList<? extends GenerableArtifact> search(String userInput, List<? e
Map<? super GenerableArtifact, Double> scores = new HashMap<>();

for (int i = 0; i < matching.size(); i++) {
String description = isIncrements ? ((IncrementTo) matching.get(i)).getDescription()
: ((TemplateTo) matching.get(i)).getId();
JaccardDistance distance = new JaccardDistance();
scores.put(matching.get(i), distance.apply(description.toUpperCase(), userInput.toUpperCase()));
if (!isIncrements) {
String description = ((TemplateTo) matching.get(i)).getId();
JaccardDistance distance = new JaccardDistance();
scores.put(matching.get(i), distance.apply(
description.toUpperCase(), userInput.toUpperCase()));
} else {
String description =
((IncrementTo) matching.get(i)).getDescription();
String id = ((IncrementTo) matching.get(i)).getId();
JaccardDistance distance = new JaccardDistance();
Double descriptionDistance = distance.apply(
description.toUpperCase(), userInput.toUpperCase());
Double idDistance = distance.apply(id.toUpperCase(),
userInput.toUpperCase());
scores.put(matching.get(i),
Math.min(idDistance, descriptionDistance));
}
}

Map<? super GenerableArtifact, Double> sorted = scores.entrySet().stream().sorted(comparingByValue())
.collect(toMap(e -> e.getKey(), e -> e.getValue(), (e1, e2) -> e2, LinkedHashMap::new));
Map<? super GenerableArtifact, Double> sorted =
scores.entrySet().stream().sorted(comparingByValue())
.collect(toMap(e -> e.getKey(), e -> e.getValue(),
(e1, e2) -> e2, LinkedHashMap::new));

ArrayList<? super GenerableArtifact> chosen = new ArrayList<>();

for (Object artifact : sorted.keySet()) {
GenerableArtifact tmp;
tmp = isIncrements ? (IncrementTo) artifact : (TemplateTo) artifact;
String description =
isIncrements ? ((IncrementTo) artifact).getDescription() : ((TemplateTo) artifact).getId();
if (description.toUpperCase().contains(userInput.toUpperCase())
|| sorted.get(artifact) <= SELECTION_THRESHOLD) {
chosen.add(tmp);
if (!isIncrements) {
String description = ((TemplateTo) artifact).getId();
if (description.toUpperCase().contains(userInput.toUpperCase())
|| sorted.get(artifact) <= SELECTION_THRESHOLD) {
chosen.add(tmp);
}
} else {
String description = ((IncrementTo) artifact).getDescription();
String id = ((IncrementTo) artifact).getId();
if (description.equalsIgnoreCase(userInput)
|| id.equalsIgnoreCase(userInput)) {
chosen.add(tmp);
return (ArrayList<? extends GenerableArtifact>) chosen;
}
if ((description.toUpperCase().contains(userInput.toUpperCase())
|| id.toUpperCase().contains(userInput.toUpperCase()))
|| sorted.get(artifact) <= SELECTION_THRESHOLD) {
chosen.add(tmp);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,11 @@ public void addJarsToClassLoader(String allJars) {
LOG.error("Not able to form URL of jar file.", e);
} catch (SecurityException e) {
LOG.error(
"Security exception. Most probably you do not have enough permissions. Please execute the CLI using admin rights.");
"Security exception. Most probably you do not have enough permissions. Please execute the CLI using admin rights.",
e);
} catch (IOException e) {
LOG.error("CobiGen plug-in jar file that was being loaded was not found. "
+ "Please try again or file an issue in cobigen GitHub repo.");
+ "Please try again or file an issue in cobigen GitHub repo.", e);
}

}
Expand Down Expand Up @@ -363,7 +364,7 @@ public static Object process(InputInterpreter inputInterpreter, File file, Class
try {
input = inputInterpreter.read(Paths.get(file.toURI()), Charsets.UTF_8, cl);
} catch (InputReaderException e) {
// nothing
LOG.debug("No input reader was able to read file {}", file.toURI(), e);
}
if (input != null) {
return input;
Expand Down
28 changes: 11 additions & 17 deletions cobigen-cli/cli/src/main/resources/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.devonfw.cobigen</groupId>
<artifactId>cli</artifactId>
<version>1.2.0-SNAPSHOT</version>
<version>7.0.0</version>
<name>cobigen-cli</name>

<properties>
Expand All @@ -13,58 +13,52 @@
<dependency>
<groupId>com.devonfw.cobigen</groupId>
<artifactId>javaplugin</artifactId>
<version>2.1.0</version>
<version>7.0.0</version>
</dependency>
<dependency>
<groupId>com.devonfw.cobigen</groupId>
<artifactId>tsplugin</artifactId>
<version>2.4.2</version>
<version>7.0.0</version>
</dependency>
<dependency>
<groupId>com.devonfw.cobigen</groupId>
<artifactId>xmlplugin</artifactId>
<version>4.1.0</version>
<version>7.0.0</version>
</dependency>
<dependency>
<groupId>com.devonfw.cobigen</groupId>
<artifactId>textmerger</artifactId>
<version>2.0.0</version>
<version>7.0.0</version>
</dependency>
<dependency>
<groupId>com.devonfw.cobigen</groupId>
<artifactId>openapiplugin</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.23</version>
<scope>compile</scope>
<version>7.0.0</version>
</dependency>
<dependency>
<groupId>com.devonfw.cobigen</groupId>
<artifactId>tempeng-freemarker</artifactId>
<version>2.0.0</version>
<version>7.0.0</version>
</dependency>
<dependency>
<groupId>com.devonfw.cobigen</groupId>
<artifactId>htmlplugin</artifactId>
<version>2.0.1</version>
<version>7.0.0</version>
</dependency>
<dependency>
<groupId>com.devonfw.cobigen</groupId>
<artifactId>propertyplugin</artifactId>
<version>2.0.0</version>
<version>7.0.0</version>
</dependency>
<dependency>
<groupId>com.devonfw.cobigen</groupId>
<artifactId>jsonplugin</artifactId>
<version>2.0.0</version>
<version>7.0.0</version>
</dependency>
<dependency>
<groupId>com.devonfw.cobigen</groupId>
<artifactId>templates-devon4j</artifactId>
<version>3.1.8</version>
<version>20.08.001</version>
</dependency>
<dependency>
<groupId>net.sf.m-m-m</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,29 @@ public void generateFromEntityTest() {
generatedList.clear();
}

/**
* Test generating non-java files from java input
*/
@Test
public void generateNonJavaFilesFromJavaInputTest() {
File inputFile = new File(testFileRootPath
+ "localmavenproject/maven.project/api/src/main/java/com/maven/project/sampledatamanagement/logic/api/to/SampleDataEto.java");

String args[] = new String[4];
args[0] = "generate";
args[1] = inputFile.getAbsolutePath();
args[2] = "--increments";
args[3] = "16";

commandLine.execute(args);

// clean up generated files
File generatedFiles = new File(testFileRootPath + "localmavenproject/devon4ng-application-template");
generatedList.add(generatedFiles);
GenerateCommandTest.deleteGeneratedFiles(generatedList);
generatedList.clear();
}

/**
* Integration test of the generation of templates from a Java Entity. It will generate all the templates
* in the output root path passed.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>testing</groupId>
<parent>
<groupId>testing</groupId>
<artifactId>maven.project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<artifactId>maven.project</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>api</artifactId>
<packaging>jar</packaging>
<name>Test_Local_Maven_Project</name>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
Expand All @@ -24,8 +25,8 @@
</dependency>
</dependencies>

<profiles>
<profile>
<profiles>
<profile>
<!-- separate eclipse build from command-line... -->
<id>eclipse</id>
<activation>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.maven.project.sampledatamanagement.logic.api.to;

public class SampleDataEto {

}
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>testing</groupId>
<artifactId>maven.project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<groupId>testing</groupId>
<artifactId>maven.project</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>core</artifactId>
<packaging>jar</packaging>
<name>Test_Local_Maven_Project</name>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<dependency>
<groupId>testing</groupId>
<artifactId>api</artifactId>
<version>0.0.1-SNAPSHOT</version>
Expand All @@ -28,7 +29,7 @@
</dependencies>

<profiles>
<profile>
<profile>
<!-- separate eclipse build from command-line... -->
<id>eclipse</id>
<activation>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>maven.project</artifactId>
<groupId>testing</groupId>
<version>1.2.0-SNAPSHOT</version>
<version>7.0.0</version>
<packaging>pom</packaging>
<name>Test_Local_Maven_Project</name>

Expand All @@ -12,8 +12,8 @@
<module>core</module>
</modules>

<profiles>
<profile>
<profiles>
<profile>
<!-- separate eclipse build from command-line... -->
<id>eclipse</id>
<activation>
Expand Down
Loading

0 comments on commit c769ede

Please sign in to comment.