Browse Source

generate code

tags/v1.1.0
Robin Thoni 8 years ago
parent
commit
a49be91a02

+ 1
- 8
.idea/misc.xml View File

@@ -3,13 +3,6 @@
3 3
   <component name="EntryPointsManager">
4 4
     <entry_points version="2.0" />
5 5
   </component>
6
-  <component name="MavenImportPreferences">
7
-    <option name="generalSettings">
8
-      <MavenGeneralSettings>
9
-        <option name="mavenHome" value="Bundled (Maven 3)" />
10
-      </MavenGeneralSettings>
11
-    </option>
12
-  </component>
13 6
   <component name="ProjectLevelVcsManager" settingsEditedManually="false">
14 7
     <OptionsSetting value="true" id="Add" />
15 8
     <OptionsSetting value="true" id="Remove" />
@@ -20,7 +13,7 @@
20 13
     <ConfirmationsSetting value="0" id="Add" />
21 14
     <ConfirmationsSetting value="0" id="Remove" />
22 15
   </component>
23
-  <component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="IntelliJ IDEA RS-163.7608" project-jdk-type="IDEA JDK">
16
+  <component name="ProjectRootManager" version="2" languageLevel="JDK_1_3" default="false" assert-keyword="false" jdk-15="false" project-jdk-name="IntelliJ IDEA RS-163.7608" project-jdk-type="IDEA JDK">
24 17
     <output url="file://$PROJECT_DIR$/out" />
25 18
   </component>
26 19
 </project>

+ 3
- 2
code-from-ds.iml View File

@@ -1,7 +1,7 @@
1 1
 <?xml version="1.0" encoding="UTF-8"?>
2 2
 <module type="PLUGIN_MODULE" version="4">
3 3
   <component name="DevKit.ModuleBuildProperties" url="file://$MODULE_DIR$/resources/META-INF/plugin.xml" />
4
-  <component name="NewModuleRootManager" inherit-compiler-output="true">
4
+  <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8" inherit-compiler-output="true">
5 5
     <exclude-output />
6 6
     <content url="file://$MODULE_DIR$">
7 7
       <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
@@ -9,6 +9,7 @@
9 9
     </content>
10 10
     <orderEntry type="inheritedJdk" />
11 11
     <orderEntry type="sourceFolder" forTests="false" />
12
-    <orderEntry type="library" name="json-20160810" level="project" />
12
+    <orderEntry type="library" name="org.json:json:20160810" level="project" />
13
+    <orderEntry type="library" name="jtwig-core" level="project" />
13 14
   </component>
14 15
 </module>

BIN
lib/jtwig-core.jar View File


+ 30
- 4
src/com/rthoni/intellij/codefromds/business/Generator.java View File

@@ -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
 }

+ 9
- 0
src/com/rthoni/intellij/codefromds/business/Helper.java View File

@@ -8,6 +8,10 @@ import com.rthoni.intellij.codefromds.dbo.TableSelection;
8 8
 import org.json.JSONArray;
9 9
 import org.json.JSONObject;
10 10
 
11
+import java.io.IOException;
12
+import java.nio.charset.StandardCharsets;
13
+import java.nio.file.Files;
14
+import java.nio.file.Paths;
11 15
 import java.util.Arrays;
12 16
 import java.util.Collection;
13 17
 import java.util.List;
@@ -55,4 +59,9 @@ public abstract class Helper {
55 59
         }
56 60
         return null;
57 61
     }
62
+
63
+    public static String readFile(String path) throws IOException {
64
+        return Files.readAllLines(Paths.get(path), StandardCharsets.UTF_8)
65
+                .stream().reduce("", (s1, s2) -> s1 + s2 + "\n");
66
+    }
58 67
 }

+ 5
- 1
src/com/rthoni/intellij/codefromds/ui/actions/GenerateAction.java View File

@@ -42,7 +42,11 @@ public class GenerateAction extends AnAction {
42 42
             } catch (Exception e1) {
43 43
                 e1.printStackTrace();
44 44
             }
45
-            Generator.generate(options);
45
+            try {
46
+                Generator.generate(options);
47
+            } catch (Exception e1) {
48
+                e1.printStackTrace();
49
+            }
46 50
         }
47 51
     }
48 52
 

Loading…
Cancel
Save