Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Generator.java 2.6KB

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