Skip to content

Commit 4ab5e96

Browse files
authored
Merge pull request #57 from NetoDevel/feature/add-command-to-templates
Feature/add command to templates
2 parents 474056b + 87d43b3 commit 4ab5e96

File tree

57 files changed

+1304
-426
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+1304
-426
lines changed

README.md

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,33 @@ And install the Spring Scaffold plugin
5353
$ cd my-project
5454
$ spring setup:scaffold
5555
$ spring scaffold -n "User" -p "name:String email:String"
56-
$ spring db:create -p "mysql"
56+
$ spring db:create -p "mysql" (REMOVED)
5757
$ mvn spring-boot:run
5858

5959
Default is spring 1.x, edit scaffold.info to change to 2.x before run *spring scaffold*.
6060

61+
### Template usage
62+
63+
$ spring template --list
64+
65+
Templates available
66+
* jms-aws-sqs
67+
68+
### Apply template
69+
$ spring template -t jms-aws-sqs
70+
71+
Generate config to: jms-aws-sqs
72+
CREATED src/main/java/com/example/cloudawsmessaging/consumer/MessageListener.java
73+
CREATED src/main/java/com/example/cloudawsmessaging/consumer/EntryPointMessage.java
74+
Add dependencies in C:/Users/jose.da.silva.neto/Desktop/new-github/cloud-aws-messaging/pom.xml
75+
Add properties in C:\Users\jose.da.silva.neto\Desktop\new-github\cloud-aws-messaging/src/main/resources/application.properties
76+
77+
cloud.aws.credentials.accessKey=xxxxxx
78+
cloud.aws.credentials.secretKey=xxxxxx
79+
cloud.aws.region.static=us-east-1
80+
cloud.aws.stack.auto=false
81+
cloud.aws.sqs.queue-name=my-queue.fifo
82+
6183
# Structure
6284

6385
__com
@@ -100,7 +122,7 @@ Default is spring 1.x, edit scaffold.info to change to 2.x before run *spring sc
100122
| spring controller | -n | spring controller -n User
101123
| spring scaffold | -n -p |spring scaffold -n "User" -p "name:String mail:String" |
102124
| spring setup:scaffold| -n -db -u -p | spring setup:scaffold -n "com.example" -db "dbname" -u "root" -p "root"
103-
| spring db:create | -p | spring db:create -p "mysql"
125+
| spring db:create | -p | spring db:create -p "mysql" (REMOVED)
104126

105127

106128
# License

generator-core/pom.xml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>spring-boot-cli</artifactId>
7+
<groupId>br.com.netodevel</groupId>
8+
<version>0.0.1.BUILD-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>generator-core</artifactId>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>junit</groupId>
17+
<artifactId>junit</artifactId>
18+
</dependency>
19+
20+
<dependency>
21+
<groupId>org.apache.commons</groupId>
22+
<artifactId>commons-io</artifactId>
23+
<version>1.3.2</version>
24+
</dependency>
25+
26+
</dependencies>
27+
28+
</project>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package br.com.generator.core;
2+
3+
import java.util.Map;
4+
5+
public interface EngineContract {
6+
7+
String replaceValues(String contentTemplate, Map<String, String> keyValues);
8+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package br.com.generator.core;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
6+
public abstract class Generator implements GeneratorContract {
7+
8+
private GeneratorExecutor generator;
9+
private GeneratorOptions generatorOptions;
10+
11+
public Generator() {
12+
generator = new GeneratorExecutor(new TemplateEngine());
13+
}
14+
15+
public File generate(GeneratorOptions generatorOptions) throws IOException {
16+
this.generatorOptions = generatorOptions;
17+
return generator.generate(generatorOptions);
18+
}
19+
20+
public File addDependency(GeneratorOptions generatorOptions) throws IOException {
21+
return generator.addDependecies(generatorOptions);
22+
}
23+
24+
public File addProperties(GeneratorOptions generatorOptions) throws IOException {
25+
return generator.addProperties(generatorOptions);
26+
}
27+
28+
public void output(String pathPackage, String filename) {
29+
System.out.println("created ".concat(pathPackage.concat(filename)));
30+
}
31+
32+
public GeneratorOptions getGeneratorOptions() {
33+
return generatorOptions;
34+
}
35+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package br.com.generator.core;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
6+
public interface GeneratorContract {
7+
8+
File runGenerate() throws IOException;
9+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package br.com.generator.core;
2+
3+
import org.apache.commons.io.FileUtils;
4+
import org.apache.commons.io.IOUtils;
5+
6+
import java.io.*;
7+
8+
public class GeneratorExecutor {
9+
10+
private EngineContract templateEngine;
11+
12+
public GeneratorExecutor(EngineContract templateEngine) {
13+
this.templateEngine = templateEngine;
14+
}
15+
16+
public File generate(GeneratorOptions options) throws IOException {
17+
String contentTemplate = loadTemplate(options.getTemplatePath());
18+
String contentReplaced = templateEngine.replaceValues(contentTemplate, options.getKeyValue());
19+
File fileGenerated = new File(options.getDestination().concat("/").concat(options.getName()));
20+
21+
if (fileGenerated.exists())
22+
System.out.println("INFO ".concat(options.getDestination().concat("/").concat(options.getName())).concat(" already exists"));
23+
if (!fileGenerated.exists()) {
24+
FileUtils.writeStringToFile(fileGenerated, contentReplaced);
25+
System.out.println("CREATED ".concat(options.getDestination().concat("/").concat(options.getName())));
26+
}
27+
28+
return fileGenerated;
29+
}
30+
31+
public File addDependecies(GeneratorOptions options) throws IOException {
32+
String contentTemplate = loadPom(options.getTemplatePath());
33+
String contentReplaced = templateEngine.replaceValues(contentTemplate, options.getKeyValue());
34+
35+
File fileGenerated = new File(options.getDestination());
36+
FileUtils.writeStringToFile(fileGenerated, contentReplaced);
37+
38+
System.out.println("Add dependencies in ".concat(options.getDestination()));
39+
return fileGenerated;
40+
}
41+
42+
public File addProperties(GeneratorOptions options) throws IOException {
43+
File loadFiled = new File(options.getTemplatePath());
44+
45+
FileWriter fileWritter = new FileWriter(loadFiled, true);
46+
BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
47+
bufferWritter.write(options.getProperties());
48+
bufferWritter.close();
49+
fileWritter.close();
50+
51+
System.out.println("Add properties in ".concat(options.getTemplatePath()));
52+
System.out.println("\t".concat(options.getProperties()));
53+
54+
return loadFiled;
55+
}
56+
57+
public String loadTemplate(String templatePath) throws IOException {
58+
InputStream in = getClass().getResourceAsStream(templatePath);
59+
return IOUtils.toString(in, "UTF-8");
60+
}
61+
62+
public String loadPom(String pomPath) throws IOException {
63+
File file = new File(pomPath);
64+
InputStream in = new FileInputStream(file);
65+
return IOUtils.toString(in, "UTF-8");
66+
}
67+
68+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package br.com.generator.core;
2+
3+
import java.util.Map;
4+
5+
public class GeneratorOptions {
6+
7+
private String destination;
8+
private String templatePath;
9+
private String name;
10+
private String properties;
11+
private Map<String, String> keyValue;
12+
13+
public String getDestination() {
14+
return destination;
15+
}
16+
17+
public void setDestination(String destination) {
18+
this.destination = destination;
19+
}
20+
21+
public String getTemplatePath() {
22+
return templatePath;
23+
}
24+
25+
public void setTemplatePath(String templatePath) {
26+
this.templatePath = templatePath;
27+
}
28+
29+
public String getName() {
30+
return name;
31+
}
32+
33+
public void setName(String name) {
34+
this.name = name;
35+
}
36+
37+
public Map<String, String> getKeyValue() {
38+
return keyValue;
39+
}
40+
41+
public void setKeyValue(Map<String, String> keyValue) {
42+
this.keyValue = keyValue;
43+
}
44+
45+
public String getProperties() {
46+
return properties;
47+
}
48+
49+
public void setProperties(String properties) {
50+
this.properties = properties;
51+
}
52+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package br.com.generator.core;
2+
3+
import br.com.generator.core.exceptions.TemplateEngineException;
4+
5+
import java.util.Iterator;
6+
import java.util.Map;
7+
8+
public class TemplateEngine implements EngineContract {
9+
10+
public String replaceValues(String contentTemplate, Map<String, String> keyValues) {
11+
if (contentTemplate == null) throw new TemplateEngineException("contentTemplate can not be null");
12+
Iterator it = keyValues.entrySet().iterator();
13+
14+
while (it.hasNext()) {
15+
Map.Entry<String, String> pair = (Map.Entry) it.next();
16+
if (contentTemplate.contains(pair.getKey()))
17+
contentTemplate = contentTemplate.replace(pair.getKey(), pair.getValue());
18+
// it.remove();
19+
}
20+
21+
return contentTemplate;
22+
}
23+
24+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package br.com.generator.core.exceptions;
2+
3+
public class TemplateEngineException extends RuntimeException {
4+
5+
public TemplateEngineException(String msg) {
6+
super(msg);
7+
}
8+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
${key}

0 commit comments

Comments
 (0)