Skip to content

Commit

Permalink
correctly generate apps nested inside other projects
Browse files Browse the repository at this point in the history
  • Loading branch information
vegegoku committed Jun 23, 2020
1 parent d051dd8 commit ca263bf
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 35 deletions.
1 change: 0 additions & 1 deletion src/main/java/org/dominokit/cli/PomUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,4 @@ public static Model asModel(String path) throws IOException {
ModelReader modelReader = new DefaultModelReader();
return modelReader.read(new File(pomPath.toAbsolutePath().toString()), null);
}

}
71 changes: 58 additions & 13 deletions src/main/java/org/dominokit/cli/commands/GenerateAppCommand.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
package org.dominokit.cli.commands;

import org.apache.commons.io.FileUtils;
import org.apache.maven.model.Model;
import org.dominokit.cli.PomUtil;
import org.dominokit.cli.model.Module;
import org.dominokit.cli.model.Project;
import org.dominokit.cli.structure.files.VelocityContentProcessor;
import org.dominokit.cli.structure.folders.Folder;
import org.dominokit.cli.structure.folders.Folder_MapperImpl;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;
import java.util.logging.Level;
import java.util.logging.Logger;

import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;
import static picocli.CommandLine.*;

@Command(
Expand All @@ -31,8 +38,7 @@ public class GenerateAppCommand implements Runnable {

@Option(
names = {"-g", "--groupId"},
description = "The project group ID, this will be used also for root package name",
required = true
description = "The project group ID, this will be used also for root package name"
)
private String groupId;

Expand All @@ -59,18 +65,37 @@ public class GenerateAppCommand implements Runnable {
@Override
public void run() {

Model parentPom = null;
PathUtils.setWorkingDir(workingDire);
try {
parentPom = PomUtil.asModel("");
} catch (Exception e) {
LOGGER.info("No parent pom was found, creating a root project.");
}

Project project = new Project();

project.setName(name);
project.setArtifactId(name);
project.setGroupId(groupId);
project.setVersion(VERSION);


if (nonNull(parentPom)) {
project.setVersion(parentPom.getVersion());
project.setParentArtifactId(parentPom.getArtifactId());
project.setHasParent(true);
project.setGroupId(parentPom.getGroupId());
if(isNull(groupId)) {
groupId = parentPom.getGroupId();
}
} else {
project.setVersion(VERSION);
}

project.setRootPackage(groupId);
project.setModuleShortName(name
.replace("-", "")
.replace(".","")
.replace(" ","")
.replace(".", "")
.replace(" ", "")
);

try {
Expand All @@ -81,20 +106,40 @@ public void run() {
.read(projectTemplateConfig);
folder.write(Paths.get(PathUtils.getUserDir()), project);

if(project.isHasParent()){
addProjectToParent(project, parentPom);
}

} catch (IOException e) {
e.printStackTrace();
}
}

private String getTemplateByType(boolean j2cl, String type) {
String compiler = j2cl?"j2cl":"gwt";
String compiler = j2cl ? "j2cl" : "gwt";

if (type.equalsIgnoreCase("mvp")) {
return "template/project/" + compiler + "/domino-mvp.json";
} else if (type.equalsIgnoreCase("basic")) {
return "template/project/" + compiler + "/domino-basic.json";
}
LOGGER.log(Level.SEVERE, "Unrecognized application type : " + type);
throw new IllegalArgumentException("Unrecognized application type : " + type);
}

if(type.equalsIgnoreCase("mvp")){
return "template/project/"+compiler+"/domino-mvp.json";
}else if(type.equalsIgnoreCase("basic")){
return "template/project/"+compiler+"/domino-basic.json";
private void addProjectToParent(Project project, Model parentPomModel) throws IOException {

String parentPom;
try {
parentPom = PomUtil.asString(parentPomModel);
if (parentPom.contains("<modules>")) {
parentPom = parentPom.replace("</modules>", "\t<module>" + project.getArtifactId() + "</module>\n\t</modules>");
} else {
parentPom = parentPom.replace("</project>", "\n\t<modules>\n\t\t<module>" + project.getArtifactId() + "</module>\n\t</modules>\n</project>");
}
FileUtils.write(parentPomModel.getPomFile(), parentPom, StandardCharsets.UTF_8);
} catch (Exception e) {
e.printStackTrace();
}
LOGGER.log(Level.SEVERE, "Unrecognized application type : "+type);
throw new IllegalArgumentException("Unrecognized application type : "+type);
}
}
20 changes: 20 additions & 0 deletions src/main/java/org/dominokit/cli/model/Project.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public class Project implements IsContext {
private String version;
private String rootPackage;
private String moduleShortName;
private boolean hasParent = false;
private String parentArtifactId;

public String getName() {
return name;
Expand Down Expand Up @@ -61,6 +63,22 @@ public void setModuleShortName(String moduleShortName) {
this.moduleShortName = moduleShortName;
}

public boolean isHasParent() {
return hasParent;
}

public void setHasParent(boolean hasParent) {
this.hasParent = hasParent;
}

public String getParentArtifactId() {
return parentArtifactId;
}

public void setParentArtifactId(String parentArtifactId) {
this.parentArtifactId = parentArtifactId;
}

@Override
public VelocityContext asContext() {
VelocityContext context = new VelocityContext();
Expand All @@ -70,6 +88,8 @@ public VelocityContext asContext() {
context.put("version", version);
context.put("rootPackage", rootPackage);
context.put("moduleShortName", moduleShortName);
context.put("hasParent", hasParent);
context.put("parentArtifactId", parentArtifactId);
return context;
}
}
12 changes: 12 additions & 0 deletions src/main/resources/app/gwt/mvp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,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">
<modelVersion>4.0.0</modelVersion>
#if( $hasParent )

<parent>
<groupId>${groupId}</groupId>
<artifactId>${parentArtifactId}</artifactId>
<version>${version}</version>
</parent>
<artifactId>${artifactId}</artifactId>

#else

<groupId>${groupId}</groupId>
<artifactId>${artifactId}</artifactId>
<version>${version}</version>

#end
<packaging>pom</packaging>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,9 @@
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">

<!-- Servlets -->
<servlet>
<servlet-name>greetServlet</servlet-name>
<servlet-class>${rootPackage}.GreetingService</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>greetServlet</servlet-name>
<url-pattern>/app/greet</url-pattern>
</servlet-mapping>

<!-- Default page to serve -->
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>

<servlet>
<servlet-name>WebjarsServlet</servlet-name>
<servlet-class>org.webjars.servlet.WebjarsServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>WebjarsServlet</servlet-name>
<url-pattern>/webjars/*</url-pattern>
</servlet-mapping>

</web-app>

0 comments on commit ca263bf

Please sign in to comment.