|
| 1 | +/* |
| 2 | + * Copyright (C) 2025 DiffPlug |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.diffplug.webtools.jte; |
| 17 | + |
| 18 | +import gg.jte.ContentType; |
| 19 | +import gg.jte.TemplateConfig; |
| 20 | +import gg.jte.compiler.TemplateParser; |
| 21 | +import gg.jte.compiler.TemplateParserVisitorAdapter; |
| 22 | +import gg.jte.compiler.TemplateType; |
| 23 | +import java.io.File; |
| 24 | +import java.io.IOException; |
| 25 | +import java.nio.charset.StandardCharsets; |
| 26 | +import java.nio.file.Files; |
| 27 | +import java.util.LinkedHashMap; |
| 28 | +import java.util.LinkedHashSet; |
| 29 | +import org.gradle.api.file.FileType; |
| 30 | +import org.gradle.work.ChangeType; |
| 31 | +import org.gradle.work.InputChanges; |
| 32 | + |
| 33 | +public class JteRenderer { |
| 34 | + public static void renderTask(JtePlugin.RenderModelClassesTask task, InputChanges changes) throws IOException { |
| 35 | + var templateConfig = new TemplateConfig((ContentType) task.getContentType().get(), task.getPackageName().get()); |
| 36 | + var renderer = new JteRenderer(task.getInputDir().getAsFile().get(), templateConfig); |
| 37 | + for (var change : changes.getFileChanges(task.getInputDir())) { |
| 38 | + if (change.getFileType() == FileType.DIRECTORY) { |
| 39 | + return; |
| 40 | + } |
| 41 | + String name = change.getFile().getName(); |
| 42 | + if (!name.endsWith(".jte") && !name.endsWith(".kte")) { |
| 43 | + continue; |
| 44 | + } |
| 45 | + var targetFileJte = task.getOutputDir().file(change.getNormalizedPath()).get().getAsFile().getAbsolutePath(); |
| 46 | + var targetFile = new File(targetFileJte.substring(0, targetFileJte.length() - 4) + ".kt"); |
| 47 | + if (change.getChangeType() == ChangeType.REMOVED) { |
| 48 | + targetFile.delete(); |
| 49 | + } else { |
| 50 | + targetFile.getParentFile().mkdirs(); |
| 51 | + Files.write(targetFile.toPath(), renderer.render(change.getFile()).getBytes(StandardCharsets.UTF_8)); |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + static String convertJavaToKotlin(String javaType) { |
| 57 | + if (javaType.equals("boolean")) { |
| 58 | + return "Boolean"; |
| 59 | + } else if (javaType.equals("int")) { |
| 60 | + return "Int"; |
| 61 | + } else { |
| 62 | + // e.g. `@param Result<?> records` -> `val records: Result<*>` |
| 63 | + return javaType |
| 64 | + .replace("<?>", "<*>") |
| 65 | + .replace("java.util.Collection", "Collection") |
| 66 | + .replace("java.util.List", "List") |
| 67 | + .replace("java.util.Map", "Map") |
| 68 | + .replace("java.util.Set", "Set"); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + final File rootDir; |
| 73 | + final TemplateConfig config; |
| 74 | + |
| 75 | + JteRenderer(File rootDir, TemplateConfig config) { |
| 76 | + this.rootDir = rootDir; |
| 77 | + this.config = config; |
| 78 | + } |
| 79 | + |
| 80 | + String render(File file) throws IOException { |
| 81 | + var pkg = file.getParentFile().getAbsolutePath().substring(rootDir.getAbsolutePath().length() + 1).replace(File.separatorChar, '.'); |
| 82 | + var name = file.getName(); |
| 83 | + var lastDot = name.lastIndexOf('.'); |
| 84 | + name = name.substring(0, lastDot); |
| 85 | + String ext = file.getName().substring(lastDot); |
| 86 | + |
| 87 | + var imports = new LinkedHashSet<String>(); |
| 88 | + imports.add("gg.jte.TemplateEngine"); |
| 89 | + imports.add("gg.jte.TemplateOutput"); |
| 90 | + var params = new LinkedHashMap<String, String>(); |
| 91 | + |
| 92 | + new TemplateParser(Files.readString(file.toPath()), TemplateType.Template, new TemplateParserVisitorAdapter() { |
| 93 | + @Override |
| 94 | + public void onImport(String importClass) { |
| 95 | + imports.add(importClass.replace("static ", "")); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public void onParam(String parameter) { |
| 100 | + var idxOfColon = parameter.indexOf(':'); |
| 101 | + if (idxOfColon == -1) { // .jte |
| 102 | + // lastIndexOf accounts for valid multiple spaces, e.g `Map<String, String> featureMap` |
| 103 | + var spaceIdx = parameter.lastIndexOf(' '); |
| 104 | + var type = parameter.substring(0, spaceIdx).trim(); |
| 105 | + var name = parameter.substring(spaceIdx + 1).trim(); |
| 106 | + if (name.endsWith("Nullable")) { |
| 107 | + type += "?"; |
| 108 | + } |
| 109 | + params.put(name, convertJavaToKotlin(type)); |
| 110 | + } else { // .kte |
| 111 | + var name = parameter.substring(0, idxOfColon).trim(); |
| 112 | + var type = parameter.substring(idxOfColon + 1).trim(); |
| 113 | + params.put(name, type); |
| 114 | + } |
| 115 | + } |
| 116 | + }, config).parse(); |
| 117 | + |
| 118 | + var builder = new StringBuilder(); |
| 119 | + builder.append("package " + pkg + "\n"); |
| 120 | + builder.append("\n"); |
| 121 | + for (var imp : imports) { |
| 122 | + builder.append("import " + imp + "\n"); |
| 123 | + } |
| 124 | + builder.append("\n"); |
| 125 | + builder.append("class " + name + "(\n"); |
| 126 | + params.forEach((paramName, type) -> { |
| 127 | + builder.append("\tval " + paramName + ": " + type + ",\n"); |
| 128 | + }); |
| 129 | + builder.append("\t) : common.JteModel {\n"); |
| 130 | + builder.append("\n"); |
| 131 | + builder.append("\toverride fun render(engine: TemplateEngine, output: TemplateOutput) {\n"); |
| 132 | + builder.append("\t\tengine.render(\"" + pkg.replace('.', '/') + "/" + name + ext + "\", mapOf(\n"); |
| 133 | + params.forEach((paramName, type) -> { |
| 134 | + builder.append("\t\t\t\"" + paramName + "\" to " + paramName + ",\n"); |
| 135 | + }); |
| 136 | + builder.append("\t\t), output)\n"); |
| 137 | + builder.append("\t}\n"); |
| 138 | + builder.append("}"); |
| 139 | + return builder.toString(); |
| 140 | + } |
| 141 | +} |
0 commit comments