-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathBarista.java
155 lines (127 loc) · 6.83 KB
/
Barista.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Scanner;
class Barista {
public static void main(String[] args) {
String baseInputDirectoryName = "src/main/resources/templates/barista";
String baseOutputDirectoryName = "src/main/java";
String resourceSlugs = "";
System.out.println("Hello dear customer :)");
System.out.println("What can I do for you today?");
System.out.println("1 - I want an entity");
System.out.println("2 - I want a resource controller");
System.out.println("3 - I want a resource REST controller");
System.out.println("4 - I want a repository");
Scanner scanner = new Scanner(System.in);
int choice = 0;
try {
choice = Integer.parseInt(scanner.nextLine());
}
catch(Exception e) {
e.printStackTrace();
}
String outputDirectoryName = baseOutputDirectoryName;
switch(choice) {
case 1:
outputDirectoryName += "/entities";
System.out.println("Do you need an entity? Just tell its name:");
String entityName = scanner.nextLine();
String entityNameNormalize = entityName.substring(0,1).toUpperCase()+entityName.substring(1);
try {
String fileContent = new String(
Files.readAllBytes(
Paths.get(baseInputDirectoryName + "/Entity.java")
)
);
String newFileContent = fileContent.replace("${Entity}", entityNameNormalize);
System.out.println("Writing " + entityNameNormalize + ".java for you...");
Files.createDirectories(Paths.get(outputDirectoryName));
Files.write(Paths.get(outputDirectoryName + "/" + entityNameNormalize + ".java"), newFileContent.getBytes());
}
catch(Exception e) {
e.printStackTrace();
}
break;
case 2: {
outputDirectoryName += "/controllers";
System.out.println("Do you need a resource controller? Just tell the name of your resource:");
String resourceName = scanner.nextLine();
String resourceNameNormalize = resourceName.substring(0,1).toUpperCase()+resourceName.substring(1);
String resourceLowerCase = resourceName.toLowerCase();
if(resourceLowerCase.endsWith("y")) {
resourceSlugs = resourceLowerCase.substring(0, resourceLowerCase.length() - 1) + "ies";
} else if(!resourceLowerCase.endsWith("s")){
resourceSlugs = resourceLowerCase + "s";
}
try {
String fileContent = new String(
Files.readAllBytes(
Paths.get(baseInputDirectoryName + "/Controller.java")
)
);
String newFileContent = fileContent.replace("${UpperName}", resourceNameNormalize);
newFileContent = newFileContent.replace("${lowerName}", resourceLowerCase);
newFileContent = newFileContent.replace("${slugs}", resourceSlugs );
System.out.println("Writing " + resourceNameNormalize + "Controller.java for you...");
Files.createDirectories(Paths.get(outputDirectoryName));
Files.write(Paths.get(outputDirectoryName + "/" + resourceNameNormalize + "Controller.java"), newFileContent.getBytes());
}
catch(Exception e) {
e.printStackTrace();
}
} break;
case 3: {
outputDirectoryName += "/controllers";
System.out.println("Do you need a resource restController? Just tell the name of your resource:");
String resourceName = scanner.nextLine();
String resourceNameNormalize = resourceName.substring(0,1).toUpperCase()+resourceName.substring(1);
String resourceLowerCase = resourceName.toLowerCase();
if(resourceLowerCase.endsWith("y")) {
resourceSlugs = resourceLowerCase.substring(0, resourceLowerCase.length() - 1) + "ies";
} else if(!resourceLowerCase.endsWith("s")){
resourceSlugs = resourceLowerCase + "s";
}
try {
String fileContent = new String(
Files.readAllBytes(
Paths.get(baseInputDirectoryName + "/RestController.java")
)
);
String newFileContent = fileContent.replace("${UpperName}", resourceNameNormalize);
newFileContent = newFileContent.replace("${lowerName}", resourceLowerCase);
newFileContent = newFileContent.replace("${slugs}", resourceSlugs );
System.out.println("Writing " + resourceNameNormalize + "RestController.java for you...");
Files.createDirectories(Paths.get(outputDirectoryName));
Files.write(Paths.get(outputDirectoryName + "/" + resourceNameNormalize + "RestController.java"), newFileContent.getBytes());
}
catch(Exception e) {
e.printStackTrace();
}
} break;
case 4:
outputDirectoryName += "/repositories";
System.out.println("Do you need a repository? Just say the name of its entity:");
String repositoryName = scanner.nextLine();
String repositoryNameNormalize = repositoryName.substring(0,1).toUpperCase()+repositoryName.substring(1);
try {
String fileContent = new String(
Files.readAllBytes(
Paths.get(baseInputDirectoryName + "/Repository.java")
)
);
String newFileContent = fileContent.replace("${Repository}", repositoryNameNormalize);
System.out.println("Writing " + repositoryNameNormalize + "Repository.java for you...");
Files.createDirectories(Paths.get(outputDirectoryName));
Files.write(Paths.get(outputDirectoryName + "/" + repositoryNameNormalize + "Repository.java"), newFileContent.getBytes());
}
catch(Exception e) {
e.printStackTrace();
}
break;
default:
System.out.println("Sorry, but I didn't understand your choice");
}
scanner.close();
System.out.println("Have a nice day :)");
}
}