|
@@ -2,9 +2,14 @@ package com.rthoni.intellij.codefromds.business;
|
2
|
2
|
|
3
|
3
|
import com.intellij.database.psi.DbDataSource;
|
4
|
4
|
import com.rthoni.intellij.codefromds.dbo.GenerateOptions;
|
|
5
|
+import com.rthoni.intellij.codefromds.dbo.TableSelection;
|
5
|
6
|
import org.json.JSONObject;
|
|
7
|
+import org.jtwig.JtwigModel;
|
|
8
|
+import org.jtwig.JtwigTemplate;
|
6
|
9
|
|
|
10
|
+import java.io.File;
|
7
|
11
|
import java.io.FileOutputStream;
|
|
12
|
+import java.io.IOException;
|
8
|
13
|
import java.nio.charset.StandardCharsets;
|
9
|
14
|
import java.nio.file.Files;
|
10
|
15
|
import java.nio.file.Paths;
|
|
@@ -15,7 +20,8 @@ import java.util.HashMap;
|
15
|
20
|
*/
|
16
|
21
|
public abstract class Generator {
|
17
|
22
|
|
18
|
|
- public static void saveOptions(GenerateOptions options) throws Exception {
|
|
23
|
+ public static void saveOptions(GenerateOptions options) throws Exception
|
|
24
|
+ {
|
19
|
25
|
HashMap<String, Object> map = options.toMap();
|
20
|
26
|
JSONObject obj = new JSONObject(map);
|
21
|
27
|
FileOutputStream file = new FileOutputStream(options.getConfigPath());
|
|
@@ -23,8 +29,9 @@ public abstract class Generator {
|
23
|
29
|
file.close();
|
24
|
30
|
}
|
25
|
31
|
|
26
|
|
- public static GenerateOptions loadOptions(String configPath) throws Exception {
|
27
|
|
- String data = Files.readAllLines(Paths.get(configPath), StandardCharsets.UTF_8).stream().reduce("", (s1, s2) -> s1 + s2);
|
|
32
|
+ public static GenerateOptions loadOptions(String configPath) throws Exception
|
|
33
|
+ {
|
|
34
|
+ String data = Files.readAllLines(Paths.get(configPath), StandardCharsets.UTF_8).stream().reduce("", (s1, s2) -> s1 + s2 + "\n");
|
28
|
35
|
|
29
|
36
|
JSONObject obj = new JSONObject(data);
|
30
|
37
|
String src = obj.getJSONObject("selection").getString("source");
|
|
@@ -40,8 +47,27 @@ public abstract class Generator {
|
40
|
47
|
return options;
|
41
|
48
|
}
|
42
|
49
|
|
43
|
|
- public static void generate(GenerateOptions options)
|
|
50
|
+ public static void generateFile(String templatePath, String outputFile, GenerateOptions options, TableSelection table) throws IOException
|
44
|
51
|
{
|
|
52
|
+ String data = Helper.readFile(templatePath);
|
|
53
|
+
|
|
54
|
+ FileOutputStream file = new FileOutputStream(outputFile);
|
|
55
|
+
|
|
56
|
+ JtwigTemplate template = JtwigTemplate.inlineTemplate(data);
|
|
57
|
+ JtwigModel model = JtwigModel.newModel().with("options", options).with("table", table);
|
|
58
|
+
|
|
59
|
+ template.render(model, file);
|
45
|
60
|
|
|
61
|
+ file.close();
|
|
62
|
+ }
|
|
63
|
+
|
|
64
|
+ public static void generate(GenerateOptions options) throws IOException
|
|
65
|
+ {
|
|
66
|
+ generateFile(options.getDataSourceTemplatePath(), options.getModelsPath() + File.separator + "Database.cs", options, null);
|
|
67
|
+ for (TableSelection table : options.getSelection().getTables()) {
|
|
68
|
+ if (!table.hasNone()) {
|
|
69
|
+ generateFile(options.getModelsTemplatePath(), options.getModelsPath() + File.separator + table.getTable().getName() + ".cs", options, table);
|
|
70
|
+ }
|
|
71
|
+ }
|
46
|
72
|
}
|
47
|
73
|
}
|