|
| 1 | +/* |
| 2 | + * Jooby https://jooby.io |
| 3 | + * Apache License Version 2.0 https://jooby.io/LICENSE.txt |
| 4 | + * Copyright 2014 Edgar Espina |
| 5 | + */ |
| 6 | +package io.jooby.gradle; |
| 7 | + |
| 8 | +import cz.habarta.typescript.generator.DateMapping; |
| 9 | +import cz.habarta.typescript.generator.EnumMapping; |
| 10 | +import cz.habarta.typescript.generator.JsonLibrary; |
| 11 | +import io.jooby.trpc.TrpcGenerator; |
| 12 | +import org.gradle.api.tasks.Input; |
| 13 | +import org.gradle.api.tasks.Optional; |
| 14 | +import org.gradle.api.tasks.TaskAction; |
| 15 | + |
| 16 | +import java.io.File; |
| 17 | +import java.util.List; |
| 18 | +import java.util.Map; |
| 19 | + |
| 20 | +/** |
| 21 | + * Generate a tRpc script file from a jooby application. |
| 22 | + * |
| 23 | + * Usage: https://jooby.io/modules/trpc |
| 24 | + * |
| 25 | + * @author edgar |
| 26 | + * @since 4.0.17 |
| 27 | + */ |
| 28 | +public class TrpcTask extends BaseTask { |
| 29 | + |
| 30 | + private Map<String, String> customTypeMappings; |
| 31 | + |
| 32 | + private Map<String, String> customTypeNaming; |
| 33 | + |
| 34 | + private JsonLibrary jsonLibrary = JsonLibrary.jackson2; |
| 35 | + |
| 36 | + private DateMapping mapDate = DateMapping.asString; |
| 37 | + |
| 38 | + private EnumMapping mapEnum = EnumMapping.asInlineUnion; |
| 39 | + |
| 40 | + private File outputDir; |
| 41 | + |
| 42 | + private String outputFile = "trpc.d.ts"; |
| 43 | + |
| 44 | + private List<String> importDeclarations; |
| 45 | + |
| 46 | + /** |
| 47 | + * Creates a tRPC task. |
| 48 | + */ |
| 49 | + public TrpcTask() {} |
| 50 | + |
| 51 | + /** |
| 52 | + * Generate tRPC files from Jooby application. |
| 53 | + * |
| 54 | + * @throws Throwable If something goes wrong. |
| 55 | + */ |
| 56 | + @TaskAction |
| 57 | + public void generate() throws Throwable { |
| 58 | + var projects = getProjects(); |
| 59 | + var classLoader = createClassLoader(projects); |
| 60 | + |
| 61 | + // Default to the compiled classes directory if the user hasn't overridden outputDir |
| 62 | + var outDir = outputDir != null ? outputDir.toPath() : classes(getProject(), false); |
| 63 | + |
| 64 | + var generator = new TrpcGenerator(); |
| 65 | + generator.setClassLoader(classLoader); |
| 66 | + generator.setOutputDir(outDir); |
| 67 | + generator.setOutputFile(outputFile); |
| 68 | + |
| 69 | + if (customTypeMappings != null) { |
| 70 | + generator.setCustomTypeMappings(customTypeMappings); |
| 71 | + } |
| 72 | + generator.setJsonLibrary(jsonLibrary); |
| 73 | + generator.setMapDate(mapDate); |
| 74 | + generator.setMapEnum(mapEnum); |
| 75 | + if (importDeclarations != null) { |
| 76 | + generator.setImportDeclarations(importDeclarations); |
| 77 | + } |
| 78 | + if (customTypeNaming != null) { |
| 79 | + generator.setCustomTypeNaming(customTypeNaming); |
| 80 | + } |
| 81 | + |
| 82 | + getLogger().info("Generating: " + outDir.resolve(outputFile)); |
| 83 | + |
| 84 | + generator.generate(); |
| 85 | + } |
| 86 | + |
| 87 | + @Input |
| 88 | + @Optional |
| 89 | + public Map<String, String> getCustomTypeMappings() { |
| 90 | + return customTypeMappings; |
| 91 | + } |
| 92 | + |
| 93 | + public void setCustomTypeMappings(Map<String, String> customTypeMappings) { |
| 94 | + this.customTypeMappings = customTypeMappings; |
| 95 | + } |
| 96 | + |
| 97 | + @Input |
| 98 | + @Optional |
| 99 | + public Map<String, String> getCustomTypeNaming() { |
| 100 | + return customTypeNaming; |
| 101 | + } |
| 102 | + |
| 103 | + public void setCustomTypeNaming(Map<String, String> customTypeNaming) { |
| 104 | + this.customTypeNaming = customTypeNaming; |
| 105 | + } |
| 106 | + |
| 107 | + @Input |
| 108 | + @Optional |
| 109 | + public JsonLibrary getJsonLibrary() { |
| 110 | + return jsonLibrary; |
| 111 | + } |
| 112 | + |
| 113 | + public void setJsonLibrary(JsonLibrary jsonLibrary) { |
| 114 | + this.jsonLibrary = jsonLibrary; |
| 115 | + } |
| 116 | + |
| 117 | + @Input |
| 118 | + @Optional |
| 119 | + public DateMapping getMapDate() { |
| 120 | + return mapDate; |
| 121 | + } |
| 122 | + |
| 123 | + public void setMapDate(DateMapping mapDate) { |
| 124 | + this.mapDate = mapDate; |
| 125 | + } |
| 126 | + |
| 127 | + @Input |
| 128 | + @Optional |
| 129 | + public EnumMapping getMapEnum() { |
| 130 | + return mapEnum; |
| 131 | + } |
| 132 | + |
| 133 | + public void setMapEnum(EnumMapping mapEnum) { |
| 134 | + this.mapEnum = mapEnum; |
| 135 | + } |
| 136 | + |
| 137 | + @Input |
| 138 | + @Optional |
| 139 | + public File getOutputDir() { |
| 140 | + return outputDir; |
| 141 | + } |
| 142 | + |
| 143 | + public void setOutputDir(File outputDir) { |
| 144 | + this.outputDir = outputDir; |
| 145 | + } |
| 146 | + |
| 147 | + @Input |
| 148 | + @Optional |
| 149 | + public String getOutputFile() { |
| 150 | + return outputFile; |
| 151 | + } |
| 152 | + |
| 153 | + public void setOutputFile(String outputFile) { |
| 154 | + this.outputFile = outputFile; |
| 155 | + } |
| 156 | + |
| 157 | + @Input |
| 158 | + @Optional |
| 159 | + public List<String> getImportDeclarations() { |
| 160 | + return importDeclarations; |
| 161 | + } |
| 162 | + |
| 163 | + public void setImportDeclarations(List<String> importDeclarations) { |
| 164 | + this.importDeclarations = importDeclarations; |
| 165 | + } |
| 166 | +} |
0 commit comments