Skip to content

Commit

Permalink
Merge pull request #62 from NetoDevel/feature/new-model-generator
Browse files Browse the repository at this point in the history
Feature/new model generator
  • Loading branch information
netodevel authored Aug 4, 2019
2 parents 00b01d7 + ae9fb56 commit 5fb1658
Show file tree
Hide file tree
Showing 48 changed files with 762 additions and 92 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ spring-scaffold-cli/target/
spring-scaffold-cli/*.iml
generator-core/target/
generator-core/*.iml
templates-java/target/
templates-java/*.iml
templates/target/
templates/*.iml

*.iml
.idea/
Expand Down
2 changes: 1 addition & 1 deletion generator-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>spring-boot-cli</artifactId>
<groupId>br.com.netodevel</groupId>
<version>0.0.2.BUILD-SNAPSHOT</version>
<version>1.1.0.BUILD-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>generator-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ public File generate(GeneratorOptions options) throws IOException {

public File addDependecies(GeneratorOptions options) throws IOException {
String contentTemplate = loadPom(options.getTemplatePath());

Boolean alreadyDeps = false;
if (options.getDependencies() != null) {
for (int i = 0; i < options.getDependencies().size(); i++) {
if (contentTemplate.contains(options.getDependencies().get(i))) {
System.out.println("[INFO] ".concat(options.getDependencies().get(i)).concat(" already added."));
alreadyDeps = true;
}
}
}
if (alreadyDeps) return null;

String contentReplaced = templateEngine.replaceValues(contentTemplate, options.getKeyValue());

File fileGenerated = new File(options.getDestination());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package br.com.generator.core;

import java.util.List;
import java.util.Map;

public class GeneratorOptions {
Expand All @@ -10,6 +11,7 @@ public class GeneratorOptions {
private String properties;
private Map<String, String> pluginValues;
private Map<String, String> keyValue;
private List<String> dependencies;

public String getDestination() {
return destination;
Expand Down Expand Up @@ -58,4 +60,12 @@ public Map<String, String> getPluginValues() {
public void setPluginValues(Map<String, String> pluginValues) {
this.pluginValues = pluginValues;
}

public List<String> getDependencies() {
return dependencies;
}

public void setDependencies(List<String> dependencies) {
this.dependencies = dependencies;
}
}
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>br.com.netodevel</groupId>
<artifactId>spring-boot-cli</artifactId>
<version>0.0.2.BUILD-SNAPSHOT</version>
<version>1.1.0.BUILD-SNAPSHOT</version>

<packaging>pom</packaging>
<name>cli-spring-boot-scaffold</name>
Expand All @@ -14,7 +14,7 @@
<module>spring-scaffold-cli</module>
<module>spring-boot-generate</module>
<module>generator-core</module>
<module>templates-java</module>
<module>templates</module>
</modules>

<profiles>
Expand Down
2 changes: 1 addition & 1 deletion spring-boot-generate/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>br.com</groupId>
<artifactId>spring-boot-generate</artifactId>
<version>0.0.2-SNAPSHOT</version>
<version>1.1.0.BUILD-SNAPSHOT</version>

<properties>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
Expand Down
8 changes: 4 additions & 4 deletions spring-scaffold-cli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>br.com.netodevel</groupId>
<artifactId>spring-scaffold-cli</artifactId>
<version>0.0.2-SNAPSHOT</version>
<version>1.1.0.BUILD-SNAPSHOT</version>

<parent>
<groupId>org.springframework.boot</groupId>
Expand All @@ -26,13 +26,13 @@
<dependency>
<groupId>br.com</groupId>
<artifactId>spring-boot-generate</artifactId>
<version>0.0.2-SNAPSHOT</version>
<version>1.1.0.BUILD-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>br.com.netodevel</groupId>
<artifactId>templates-java</artifactId>
<version>0.0.2.BUILD-SNAPSHOT</version>
<artifactId>templates</artifactId>
<version>1.1.0.BUILD-SNAPSHOT</version>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package br.com.command;

import br.com.command.model.ModelHandler;
import br.com.generate.helpers.ScaffoldInfoHelper;
import br.com.command.controller.ControllerCommand;
import br.com.command.controller.ControllerHandler;
import br.com.command.model.ModelCommand;
import br.com.command.model.ModelHandler;
import br.com.command.repository.RepositoryCommand;
import br.com.command.repository.RepositoryHandler;
import br.com.command.scaffold.ScaffoldCommand;
Expand All @@ -15,6 +14,7 @@
import br.com.command.setup.SetupScaffoldHandler;
import br.com.command.template.TemplateCommand;
import br.com.command.template.TemplateHandler;
import br.com.generate.helpers.ScaffoldInfoHelper;
import org.springframework.boot.cli.command.Command;
import org.springframework.boot.cli.command.CommandFactory;

Expand All @@ -30,14 +30,15 @@
public class ScaffoldFactoryCommands implements CommandFactory {

public Collection<Command> getCommands() {
ScaffoldInfoHelper scaffoldInfoHelper = new ScaffoldInfoHelper();
return Arrays.<Command>asList(
new ModelCommand("model", "generate entities", new ModelHandler()),
new ModelCommand("model", "generate entities", new ModelHandler(scaffoldInfoHelper)),
new RepositoryCommand("repository", "generate repositories", new RepositoryHandler()),
new ServiceCommand("service", "generate services", new ServiceHandler()),
new ControllerCommand("controller", "generate controllers", new ControllerHandler()),
new ScaffoldCommand("scaffold", "generate api scaffold", new ScaffoldHandler()),
new SetupScaffoldCommand("setup:scaffold", "setup scaffold", new SetupScaffoldHandler()),
new TemplateCommand("template", "generate setup project", new TemplateHandler(new ScaffoldInfoHelper())));
new TemplateCommand("template", "generate setup project", new TemplateHandler(scaffoldInfoHelper)));
}

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
package br.com.command.model;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.springframework.boot.cli.command.HelpExample;
import org.springframework.boot.cli.command.OptionParsingCommand;
import org.springframework.boot.cli.command.options.OptionHandler;

Expand All @@ -16,20 +11,11 @@
public class ModelCommand extends OptionParsingCommand {

public ModelCommand(String name, String description, OptionHandler handler) {
super("model", "generate entities", new ModelHandler());
}

public String getUsageHelp() {
// TODO Auto-generated method stub
return "[name-entity] [attributes] [language]";
super(name, description, handler);
}

@Override
public Collection<HelpExample> getExamples() {
List<HelpExample> list = new ArrayList<HelpExample>();
list.add(new HelpExample("create entities java", "model -n User -p name:String"));
list.add(new HelpExample("create entities kotlin", "model -n User -p name:String -l kotlin"));
return list;
public String getUsageHelp() {
return "<entity> <attributes>";
}

}
Original file line number Diff line number Diff line change
@@ -1,45 +1,96 @@
package br.com.command.model;

import java.io.IOException;
import java.util.Arrays;

import br.com.generate.helpers.ScaffoldInfoHelper;
import br.com.generator.core.GeneratorOptions;
import br.com.templates.entity.EntityCache;
import br.com.templates.entity.EntityExecutor;
import br.com.templates.entity.EntityGenerator;
import br.com.templates.entity.LombokDependencyGenerator;
import joptsimple.OptionSet;
import joptsimple.OptionSpec;

import org.springframework.boot.cli.command.options.OptionHandler;
import org.springframework.boot.cli.command.status.ExitStatus;

import br.com.generate.source.model.ModelGenerator;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
*
* @author NetoDevel
* @since 0.0.1
*/
public class ModelHandler extends OptionHandler {

@SuppressWarnings("unused")
private OptionSpec<String> nameEntity;

@SuppressWarnings("unused")
private OptionSpec<String> parametersEntity;

@Override
protected void options() {
this.nameEntity = option(Arrays.asList("nameEntity", "n"), "Name of entity to generate").withRequiredArg();
this.parametersEntity = option(Arrays.asList("parameterEntity", "p"), "Parameter of entity to generate").withRequiredArg();
}

@Override
protected ExitStatus run(OptionSet options) throws Exception {
String nameClass = (String) options.valueOf("n");
String parametersClass = (String) options.valueOf("p");
generateModelJava(nameClass, parametersClass);
return ExitStatus.OK;
}

private void generateModelJava(String nameClass, String parameters) throws IOException {
new ModelGenerator().generate(nameClass, parameters, "template-model.txt");
}

@SuppressWarnings("unused")
private OptionSpec<String> nameEntity;

@SuppressWarnings("unused")
private OptionSpec<String> parametersEntity;

private ScaffoldInfoHelper scaffoldInfoHelper;

public ModelHandler() {
}

public ModelHandler(ScaffoldInfoHelper scaffoldInfoHelper) {
this.scaffoldInfoHelper = scaffoldInfoHelper;
}

@Override
protected void options() {
this.nameEntity = option(Arrays.asList("nameEntity", "n"), "Name of entity").withRequiredArg();
this.parametersEntity = option(Arrays.asList("parameterEntity", "p"), "Parameters of entity").withRequiredArg();
}

@Override
protected ExitStatus run(OptionSet options) throws Exception {
String nameClass = (String) options.valueOf("n");
String parametersClass = (String) options.valueOf("p");

if (nameClass == null || nameClass.replace(" ", "").isEmpty()) {
System.out.println("[INFO] - name of entity is required. use: -n ${entity_name}");
return ExitStatus.ERROR;
}
if (parametersClass == null || parametersClass.replace(" ", "").isEmpty()){
System.out.println("[INFO] - parameters of entity is required. use: -p ${parameters}");
return ExitStatus.ERROR;
}

generateModelJava(nameClass, parametersClass);
return ExitStatus.OK;
}

private void generateModelJava(String nameClass, String parameters) throws IOException {
EntityExecutor entityExecutor = new EntityExecutor();
entityExecutor.run(nameClass, parameters);

lombokGenerate();

for (EntityCache entity : entityExecutor.getEntities()) {
GeneratorOptions generatorOptions = new GeneratorOptions();
generatorOptions.setName(entity.getName().concat(".java"));
generatorOptions.setDestination(scaffoldInfoHelper.getPathPackage().concat("models"));

Map<String, String> keyValue = new HashMap<>();
keyValue.put("${content}", entity.getContent());
keyValue.put("${package}", scaffoldInfoHelper.getPackage().concat(".models"));

generatorOptions.setKeyValue(keyValue);

EntityGenerator entityGenerator = new EntityGenerator(generatorOptions);
entityGenerator.runGenerate();
}

}

private void lombokGenerate() throws IOException {
GeneratorOptions lombokDepsOptions = new GeneratorOptions();
lombokDepsOptions.setTemplatePath(scaffoldInfoHelper.getPomPath());
lombokDepsOptions.setDestination(scaffoldInfoHelper.getPomDest());

LombokDependencyGenerator lombokDependencyGenerator = new LombokDependencyGenerator(lombokDepsOptions);
lombokDependencyGenerator.runGenerate();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import br.com.generate.helpers.ScaffoldInfoHelper;
import br.com.generator.core.GeneratorOptions;
import br.com.templates_java.ComposeTemplate;
import br.com.templates_java.config.jms_aws_sqs.*;
import br.com.templates_java.config.openj9.OpenJ9DockerfileGenerator;
import br.com.templates_java.config.openj9.OpenJ9MavenPluginGenerator;
import br.com.templates.ComposeTemplate;
import br.com.templates.config.jms_aws_sqs.*;
import br.com.templates.config.openj9.OpenJ9DockerfileGenerator;
import br.com.templates.config.openj9.OpenJ9MavenPluginGenerator;
import joptsimple.OptionSet;
import joptsimple.OptionSpec;
import org.springframework.boot.cli.command.options.OptionHandler;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package br.com.command.model;

import br.com.generate.helpers.ScaffoldInfoHelper;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.mockito.Mockito;
import org.springframework.boot.cli.command.status.ExitStatus;

import java.io.File;
import java.io.IOException;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;

public class ModelHandlerTest {

@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();

private File temporaryPath;

@Before
public void setUp() throws IOException {
temporaryPath = temporaryFolder.newFolder("test-path");
}

@Test
public void shouldReturnOk() throws Exception {
ScaffoldInfoHelper scaffoldInfoHelper = mock(ScaffoldInfoHelper.class);

Mockito.when(scaffoldInfoHelper.getPackage()).thenReturn("com.example");
Mockito.when(scaffoldInfoHelper.getPathPackage()).thenReturn(temporaryPath.getAbsolutePath().concat("\\com\\example\\"));
Mockito.when(scaffoldInfoHelper.getPomPath()).thenReturn(getClass().getResource("/templates/template-fake-pom.xml").toURI().getPath());
Mockito.when(scaffoldInfoHelper.getPomDest()).thenReturn(temporaryPath.getAbsolutePath().concat("/pom.xml"));

ModelHandler modelHandler = new ModelHandler(scaffoldInfoHelper);
ExitStatus exitStatus = modelHandler.run("-n", "User", "-p", "name:String Foo:references(relation:hasMany, name:String)");
assertEquals(ExitStatus.OK, exitStatus);
}

@Test
public void givenNoParameters_shouldReturnError() throws Exception {
ScaffoldInfoHelper scaffoldInfoHelper = mock(ScaffoldInfoHelper.class);

ModelHandler modelHandler = new ModelHandler(scaffoldInfoHelper);
ExitStatus exitStatus = modelHandler.run("-n", "USer");
assertEquals(ExitStatus.ERROR, exitStatus);
}

@Test
public void givenNoClass_shouldReturnError() throws Exception {
ScaffoldInfoHelper scaffoldInfoHelper = mock(ScaffoldInfoHelper.class);

ModelHandler modelHandler = new ModelHandler(scaffoldInfoHelper);
ExitStatus exitStatus = modelHandler.run("-p", "foo:String");
assertEquals(ExitStatus.ERROR, exitStatus);
}

}
Loading

0 comments on commit 5fb1658

Please sign in to comment.