-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #62 from NetoDevel/feature/new-model-generator
Feature/new model generator
- Loading branch information
Showing
48 changed files
with
762 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 81 additions & 30 deletions
111
spring-scaffold-cli/src/main/java/br/com/command/model/ModelHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
spring-scaffold-cli/src/test/java/br/com/command/model/ModelHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
Oops, something went wrong.