|
30 | 30 | import freemarker.template.Configuration;
|
31 | 31 | import freemarker.template.DefaultMapAdapter;
|
32 | 32 | import freemarker.template.Template;
|
| 33 | +import freemarker.template.TemplateException; |
33 | 34 | import freemarker.template.TemplateExceptionHandler;
|
34 | 35 | import freemarker.template.TemplateMethodModelEx;
|
35 | 36 | import freemarker.template.TemplateModelException;
|
36 | 37 | import java.io.StringWriter;
|
37 | 38 | import java.io.Writer;
|
| 39 | +import java.nio.charset.StandardCharsets; |
38 | 40 | import java.nio.file.FileVisitResult;
|
39 | 41 | import java.nio.file.SimpleFileVisitor;
|
40 | 42 | import java.nio.file.attribute.BasicFileAttributes;
|
|
43 | 45 | import lombok.SneakyThrows;
|
44 | 46 | import org.apache.commons.lang3.StringUtils;
|
45 | 47 |
|
| 48 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 49 | + |
46 | 50 | import java.io.File;
|
47 | 51 | import java.io.IOException;
|
| 52 | +import java.io.OutputStreamWriter; |
48 | 53 | import java.nio.file.Files;
|
49 | 54 | import java.nio.file.Path;
|
50 | 55 | import java.nio.file.StandardCopyOption;
|
@@ -323,17 +328,64 @@ public void postprocess(PackageJson sqrlConfig, Path rootDir, Path targetDir, Ph
|
323 | 328 | ? rootDir.resolve(profile)
|
324 | 329 | : namepath2Path(buildDir.getBuildDir(), NamePath.parse(profile));
|
325 | 330 |
|
326 |
| - copyToDeploy(targetDir, profilePath, plan, testPlan, sqrlConfig, plans); |
| 331 | +// copyToDeploy(targetDir, profilePath, plan, testPlan, sqrlConfig, plans); |
327 | 332 | }
|
328 | 333 |
|
329 | 334 | copyDataFiles(buildDir.getBuildDir());
|
330 | 335 | moveFolder(targetDir, DATA_DIR);
|
331 | 336 | copyJarFiles(buildDir.getBuildDir());
|
332 | 337 | moveFolder(targetDir, LIB_DIR);
|
333 | 338 | copyCompiledPlan(buildDir.getBuildDir(), targetDir);
|
| 339 | + |
| 340 | + // copy deployment files |
| 341 | + Map<String, Object> config = collectConfiguration(sqrlConfig, plans); |
| 342 | + writePostgresSchema(targetDir, config); |
| 343 | + } |
| 344 | + |
| 345 | + private void writePostgresSchema(Path targetDir, Map<String, Object> config) { |
| 346 | + if(config.containsKey("postgres") || config.containsKey("postgres_log")) { |
| 347 | + //postgres |
| 348 | + copyTemplate(targetDir, config, "templates/database-schema.sql.ftl", "postgres/database-schema.sql"); |
| 349 | + copyTemplate(targetDir, config, "templates/database-schema.sql.ftl", "files/postgres-schema.sql"); |
| 350 | + } |
| 351 | + |
| 352 | + if(config.containsKey("flink")) { |
| 353 | + //flink |
| 354 | + copyTemplate(targetDir, config, "templates/flink.sql.ftl", "flink/src/main/resources/flink.sql"); |
| 355 | + copyTemplate(targetDir, config, "templates/flink.sql.ftl", "files/flink.sql"); |
| 356 | + } |
| 357 | + |
| 358 | + if(config.containsKey("vertx")) { |
| 359 | + //vertx server-config |
| 360 | + copyTemplate(targetDir, config, "templates/server-config.json.ftl", "vertx/server-config.json"); |
| 361 | + copyTemplate(targetDir, config, "templates/server-config.json.ftl", "files/vertx-config.json"); |
| 362 | + |
| 363 | + //vertx server-config |
| 364 | + copyTemplate(targetDir, config, "templates/server-model.json.ftl", "vertx/server-model.json"); |
| 365 | + copyTemplate(targetDir, config, "templates/server-model.json.ftl", "files/vertx-model.json"); |
| 366 | + } |
334 | 367 | }
|
335 | 368 |
|
336 | 369 | @SneakyThrows
|
| 370 | + private void copyTemplate(Path targetDir, Map<String, Object> config, String source, String destination) { |
| 371 | + // Set up the FreeMarker configuration to load templates from the classpath. |
| 372 | + Configuration cfg = new Configuration(Configuration.VERSION_2_3_32); |
| 373 | + cfg.setDefaultEncoding("UTF-8"); |
| 374 | + cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER); |
| 375 | + cfg.setNumberFormat("computer"); |
| 376 | + cfg.setSharedVariable("jsonEncode", new JsonEncoderMethod()); // Set the base directory for templates to the root of the classpath. |
| 377 | + cfg.setClassLoaderForTemplateLoading(getClass().getClassLoader(), "/"); |
| 378 | + |
| 379 | + Template template = cfg.getTemplate(source); |
| 380 | + |
| 381 | + Path postgresSchemaFile = targetDir.resolve(destination); |
| 382 | + Files.createDirectories(postgresSchemaFile.getParent()); |
| 383 | + try (Writer writer = new OutputStreamWriter(Files.newOutputStream(postgresSchemaFile), StandardCharsets.UTF_8)) { |
| 384 | + template.process(config, writer); |
| 385 | + } |
| 386 | + } |
| 387 | + |
| 388 | +@SneakyThrows |
337 | 389 | private void copyCompiledPlan(Path buildDir, Path targetDir) {
|
338 | 390 | if (Files.exists(buildDir.resolve(COMPILED_PLAN_JSON))) {
|
339 | 391 | Path destFolder = targetDir.resolve("flink");
|
@@ -422,10 +474,7 @@ private void copyToDeploy(Path targetDir, Path profile, PhysicalPlan plan, TestP
|
422 | 474 | Files.createDirectories(targetDir);
|
423 | 475 | }
|
424 | 476 |
|
425 |
| - Map<String, Object> templateConfig = new HashMap<>(); |
426 |
| - templateConfig.put("config", sqrlConfig.toMap()); //Add SQRL config |
427 |
| - templateConfig.put("environment", System.getenv()); //Add environmental variables |
428 |
| - templateConfig.putAll(plans); |
| 477 | + Map<String, Object> templateConfig = collectConfiguration(sqrlConfig, plans); |
429 | 478 | // Copy each file and directory from the profile path to the target directory
|
430 | 479 | if (!Files.isDirectory(profile)) {
|
431 | 480 | throw new RuntimeException("Could not find profile: " + profile);
|
@@ -455,6 +504,14 @@ private void copyToDeploy(Path targetDir, Path profile, PhysicalPlan plan, TestP
|
455 | 504 | }
|
456 | 505 | }
|
457 | 506 |
|
| 507 | +private Map<String, Object> collectConfiguration(PackageJson sqrlConfig, Map<String, Object> plans) { |
| 508 | + Map<String, Object> templateConfig = new HashMap<>(); |
| 509 | + templateConfig.put("config", sqrlConfig.toMap()); //Add SQRL config |
| 510 | + templateConfig.put("environment", System.getenv()); //Add environmental variables |
| 511 | + templateConfig.putAll(plans); |
| 512 | + return templateConfig; |
| 513 | +} |
| 514 | + |
458 | 515 | private Path trimFtl(Path destinationPath) {
|
459 | 516 | return destinationPath.getFileName().toString().endsWith(".ftl") ?
|
460 | 517 | destinationPath.getParent().resolve(destinationPath.getFileName().toString().substring(0,destinationPath.getFileName().toString().length()-4 ))
|
@@ -490,18 +547,17 @@ public void processTemplate(Path path, Path destination, Map config) throws Exce
|
490 | 547 | return;
|
491 | 548 | }
|
492 | 549 |
|
| 550 | + // extract the template filename |
| 551 | + String templateName = path.getFileName().toString(); |
| 552 | + |
493 | 553 | // configure Freemarker
|
494 | 554 | Configuration cfg = new Configuration(Configuration.VERSION_2_3_32);
|
495 | 555 | cfg.setDirectoryForTemplateLoading(path.getParent().toFile());
|
496 | 556 | cfg.setDefaultEncoding("UTF-8");
|
497 | 557 | cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
|
498 | 558 | cfg.setNumberFormat("computer");
|
499 |
| - |
500 | 559 | cfg.setSharedVariable("jsonEncode", new JsonEncoderMethod());
|
501 | 560 |
|
502 |
| - // extract the template filename |
503 |
| - String templateName = path.getFileName().toString(); |
504 |
| - |
505 | 561 | // load and process the template
|
506 | 562 | Template template = cfg.getTemplate(templateName);
|
507 | 563 | Writer out = new StringWriter();
|
|
0 commit comments