Skip to content

Commit d3eb4c9

Browse files
Ability to supply a root directory with custom templates: "customTemplatesRoot" #1158 (#1198)
* added support for specifying the template source for custom templates * formatted test sources * resolved issues with sbt plugin * formatted source class according to checkstyle rules * updated scala plugin to resolve to the working directory --------- Co-authored-by: Isaac Mercieca <[email protected]>
1 parent a300a40 commit d3eb4c9

File tree

11 files changed

+94
-23
lines changed

11 files changed

+94
-23
lines changed

docs/codegen-options.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
| `typeResolverPrefix` | String | Empty | Sets the prefix for GraphQL type resolver classes. |
3232
| `typeResolverSuffix` | String | `Resolver` | Sets the suffix for GraphQL type resolver classes. |
3333
| `customTypesMapping` | Map(String,String) | Empty | *See [CustomTypesMapping](#option-customtypesmapping)* |
34-
| `customTemplates` | Map(String,String) | Empty | Use to supply paths to custom FreeMarker templates for code generation. |
34+
| `customTemplatesRoot` | File | Project's dir | Use to supply the path the to custom FreeMarker templates root directory. |
35+
| `customTemplates` | Map(String,String) | Empty | Use to supply paths to custom FreeMarker templates for code generation. |
3536
| `customAnnotationsMapping` | Map(String,String[]) | Empty | *See [CustomAnnotationsMapping](#option-customannotationsmapping)* |
3637
| `directiveAnnotationsMapping` | Map(String,String[]) | Empty | *See [DirectiveAnnotationsMapping](#option-directiveannotationsmapping)* |
3738
| `fieldsWithResolvers` | Set(String) | Empty | Fields that require Resolvers should be defined here in format: `TypeName.fieldName` or `TypeName` or `@directive`. E.g.: `Person`, `Person.friends`, `@customResolver`. |

plugins/gradle/graphql-java-codegen-gradle-plugin/src/main/java/io/github/kobylynskyi/graphql/codegen/gradle/GraphQLCodegenGradleTask.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ public class GraphQLCodegenGradleTask extends DefaultTask implements GraphQLCode
5454

5555
private Map<String, String> customTypesMapping = new HashMap<>();
5656
private Map<String, List<String>> customAnnotationsMapping = new HashMap<>();
57+
58+
private File customTemplatesRoot = null;
5759
private Map<String, String> customTemplates = new HashMap<>();
60+
5861
private Map<String, List<String>> directiveAnnotationsMapping = new HashMap<>();
5962
private String packageName;
6063
private String apiPackageName;
@@ -136,6 +139,9 @@ public void generate() throws Exception {
136139
customTypesMapping != null ? customTypesMapping : new HashMap<>());
137140
mappingConfig.setCustomAnnotationsMapping(
138141
customAnnotationsMapping != null ? customAnnotationsMapping : new HashMap<>());
142+
mappingConfig.setCustomTemplatesRoot(
143+
customTemplatesRoot != null ? customTemplatesRoot : getProject().getProjectDir()
144+
);
139145
mappingConfig.setCustomTemplates(
140146
customTemplates != null ? customTemplates : new HashMap<>());
141147
mappingConfig.setDirectiveAnnotationsMapping(
@@ -336,6 +342,17 @@ public void setCustomTypesMapping(Map<String, String> customTypesMapping) {
336342
this.customTypesMapping = customTypesMapping;
337343
}
338344

345+
@InputFile
346+
@Optional
347+
@Override
348+
public File getCustomTemplatesRoot() {
349+
return customTemplatesRoot;
350+
}
351+
352+
public void setCustomTemplatesRoot(File customTemplatesRoot) {
353+
this.customTemplatesRoot = customTemplatesRoot;
354+
}
355+
339356
@Input
340357
@Optional
341358
@Override

plugins/maven/graphql-java-codegen-maven-plugin/src/main/java/io/github/kobylynskyi/graphql/codegen/GraphQLCodegenMojo.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ public class GraphQLCodegenMojo extends AbstractMojo implements GraphQLCodegenCo
6565
@Parameter
6666
private Map<String, Properties> customAnnotationsMapping;
6767

68+
@Parameter
69+
private File customTemplatesRoot;
70+
6871
@Parameter
6972
private Map<String, String> customTemplates;
7073

@@ -249,6 +252,7 @@ public void execute() throws MojoExecutionException {
249252
MappingConfig mappingConfig = new MappingConfig();
250253
mappingConfig.setPackageName(packageName);
251254
mappingConfig.setCustomTypesMapping(convertToMap(customTypesMapping));
255+
mappingConfig.setCustomTemplatesRoot(customTemplatesRoot);
252256
mappingConfig.setCustomTemplates(customTemplates);
253257
mappingConfig.setCustomAnnotationsMapping(convertToListsMap(customAnnotationsMapping));
254258
mappingConfig.setDirectiveAnnotationsMapping(convertToListsMap(directiveAnnotationsMapping));
@@ -739,6 +743,11 @@ private static Map<String, String> convertToMap(Properties properties) {
739743
result.put(name, properties.getProperty(name));
740744
}
741745
return result;
746+
}
747+
748+
@Override
749+
public File getCustomTemplatesRoot() {
750+
return customTemplatesRoot == null ? project.getBasedir() : customTemplatesRoot;
742751
}
743752

744753
@Override

plugins/sbt/graphql-java-codegen-sbt-plugin/src/main/scala/io/github/dreamylost/graphql/codegen/GraphQLCodegenKeys.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ trait GraphQLCodegenKeys {
4343

4444
val customAnnotationsMapping = settingKey[util.Map[String, util.List[String]]]("customAnnotationsMapping")
4545

46+
val customTemplatesRoot = settingKey[File]("customTemplatesRoot")
47+
4648
val customTemplates = settingKey[util.Map[String, String]]("customTemplates")
4749

4850
val generateEqualsAndHashCode =

plugins/sbt/graphql-java-codegen-sbt-plugin/src/main/scala/io/github/dreamylost/graphql/codegen/GraphQLCodegenPlugin.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ class GraphQLCodegenPlugin(configuration: Configuration, private[codegen] val co
6767
generateJacksonTypeIdResolver := MappingConfigConstants.DEFAULT_GENERATE_JACKSON_TYPE_ID_RESOLVER,
6868
customTypesMapping := new JHashMap[String, String](), // TODO use scala Map, convert to java Map
6969
customAnnotationsMapping := new JHashMap[String, JList[String]](),
70+
customTemplatesRoot := file("."),
7071
customTemplates := new JHashMap[String, String](),
7172
directiveAnnotationsMapping := new JHashMap[String, JList[String]](),
7273
javaxValidationApiVersion := None,
@@ -150,6 +151,7 @@ class GraphQLCodegenPlugin(configuration: Configuration, private[codegen] val co
150151
mappingConfig.setTypeResolverPrefix((GraphQLCodegenConfig / typeResolverPrefix).value.orNull)
151152
mappingConfig.setModelValidationAnnotation((GraphQLCodegenConfig / modelValidationAnnotation).value)
152153
mappingConfig.setCustomAnnotationsMapping((GraphQLCodegenConfig / customAnnotationsMapping).value)
154+
mappingConfig.setCustomTemplatesRoot((GraphQLCodegenConfig / customTemplatesRoot).value)
153155
mappingConfig.setCustomTemplates((GraphQLCodegenConfig / customTemplates).value)
154156
mappingConfig.setGenerateEqualsAndHashCode((GraphQLCodegenConfig / generateEqualsAndHashCode).value)
155157
mappingConfig.setGenerateImmutableModels((GraphQLCodegenConfig / generateImmutableModels).value)

src/main/java/com/kobylynskyi/graphql/codegen/generators/FreeMarkerTemplateFilesCreator.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,9 @@ private static Template getTemplateForTypeAndLanguage(MappingContext mappingCont
6363
if (mappingContext.getCustomTemplates() != null) {
6464
templatePath = mappingContext.getCustomTemplates().get(templateType.name());
6565
}
66+
6667
if (templatePath != null) {
67-
return FreeMarkerTemplatesRegistry.getCustomTemplates(templatePath);
68+
return FreeMarkerTemplatesRegistry.getCustomTemplate(mappingContext.getCustomTemplatesRoot(), templatePath);
6869
} else {
6970
return FreeMarkerTemplatesRegistry.getTemplateWithLang(language, templateType);
7071
}

src/main/java/com/kobylynskyi/graphql/codegen/generators/FreeMarkerTemplatesRegistry.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import com.kobylynskyi.graphql.codegen.model.exception.UnableToLoadFreeMarkerTemplateException;
66
import freemarker.cache.ClassTemplateLoader;
77
import freemarker.cache.FileTemplateLoader;
8-
import freemarker.cache.MultiTemplateLoader;
98
import freemarker.cache.TemplateLoader;
109
import freemarker.core.PlainTextOutputFormat;
1110
import freemarker.ext.beans.BeansWrapper;
@@ -28,7 +27,7 @@ class FreeMarkerTemplatesRegistry {
2827
new EnumMap<>(GeneratedLanguage.class);
2928

3029
private static final Configuration configuration;
31-
30+
3231
static {
3332
try {
3433
configuration = buildFreeMarkerTemplateConfiguration();
@@ -64,24 +63,34 @@ private static String buildTemplatePath(FreeMarkerTemplateType templateType, Gen
6463
templateType.name().toLowerCase());
6564
}
6665

67-
private static Configuration buildFreeMarkerTemplateConfiguration() throws IOException {
68-
Configuration configuration = new Configuration(FREEMARKER_TEMPLATE_VERSION);
66+
private static Configuration buildFreeMarkerTemplateConfiguration() {
6967
ClassTemplateLoader classTemplateLoader = new ClassTemplateLoader(GraphQLCodegen.class.getClassLoader(), "");
70-
FileTemplateLoader fileTemplateLoader = new FileTemplateLoader(new File("."));
71-
configuration.setTemplateLoader(
72-
new MultiTemplateLoader(new TemplateLoader[] {classTemplateLoader, fileTemplateLoader}));
68+
69+
return buildFreeMarkerTemplateConfiguration(classTemplateLoader);
70+
}
71+
72+
private static Configuration buildFreeMarkerTemplateConfiguration(TemplateLoader templateLoader) {
73+
Configuration configuration = new Configuration(FREEMARKER_TEMPLATE_VERSION);
74+
configuration.setTemplateLoader(templateLoader);
7375
configuration.setDefaultEncoding(DEFAULT_ENCODING);
7476
configuration.setOutputFormat(PlainTextOutputFormat.INSTANCE);
7577
configuration.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
7678
configuration.setLogTemplateExceptions(false);
7779
configuration.setWrapUncheckedExceptions(true);
7880
configuration.setSharedVariable("statics", new BeansWrapper(FREEMARKER_TEMPLATE_VERSION).getStaticModels());
81+
7982
return configuration;
8083
}
8184

82-
public static Template getCustomTemplates(String templatePath) {
85+
private static Configuration buildFreeMarkerCustomTemplateConfiguration(File file) throws IOException {
86+
FileTemplateLoader fileTemplateLoader = new FileTemplateLoader(file);
87+
88+
return buildFreeMarkerTemplateConfiguration(fileTemplateLoader);
89+
}
90+
91+
public static Template getCustomTemplate(File templateRoot, String templatePath) {
8392
try {
84-
return configuration.getTemplate(templatePath);
93+
return buildFreeMarkerCustomTemplateConfiguration(templateRoot).getTemplate(templatePath);
8594
} catch (IOException e) {
8695
throw new UnableToLoadFreeMarkerTemplateException(e);
8796
}

src/main/java/com/kobylynskyi/graphql/codegen/model/GraphQLCodegenConfiguration.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.kobylynskyi.graphql.codegen.model;
22

33
import com.kobylynskyi.graphql.codegen.generators.FreeMarkerTemplateType;
4+
5+
import java.io.File;
46
import java.util.List;
57
import java.util.Map;
68
import java.util.Set;
@@ -29,10 +31,17 @@ public interface GraphQLCodegenConfiguration {
2931
*/
3032
Map<String, String> getCustomTypesMapping();
3133

34+
/**
35+
* Can be used to specify the root directory for the custom FreeMaker templates
36+
*
37+
* @return the directory source for the root directory
38+
*/
39+
File getCustomTemplatesRoot();
40+
3241
/**
3342
* Can be used to supply paths to custom FreeMarker templates for code generation.
3443
*
35-
* @return a map, where key is a tempalte type and a value is path to a FreeMarker template
44+
* @return a map, where key is a template type and a value is path to a FreeMarker template
3645
*/
3746
Map<String, String> getCustomTemplates();
3847

src/main/java/com/kobylynskyi/graphql/codegen/model/MappingConfig.java

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.kobylynskyi.graphql.codegen.model;
22

33
import com.kobylynskyi.graphql.codegen.generators.FreeMarkerTemplateType;
4+
5+
import java.io.File;
46
import java.util.HashMap;
57
import java.util.HashSet;
68
import java.util.List;
@@ -86,6 +88,8 @@ public class MappingConfig implements GraphQLCodegenConfiguration, Combinable<Ma
8688
private Set<String> parametrizedResolverAnnotations = new HashSet<>();
8789

8890
private Map<String, String> customTypesMapping = new HashMap<>();
91+
92+
private File customTemplatesRoot = new File(".");
8993
private Map<String, String> customTemplates = new HashMap<>();
9094

9195
private Set<String> typesAsInterfaces = new HashSet<>();
@@ -247,6 +251,24 @@ public void setCustomTypesMapping(Map<String, String> customTypesMapping) {
247251
this.customTypesMapping = customTypesMapping;
248252
}
249253

254+
@Override
255+
public File getCustomTemplatesRoot() {
256+
return customTemplatesRoot;
257+
}
258+
259+
public void setCustomTemplatesRoot(File customTemplatesRoot) {
260+
this.customTemplatesRoot = customTemplatesRoot;
261+
}
262+
263+
@Override
264+
public Map<String, String> getCustomTemplates() {
265+
return customTemplates;
266+
}
267+
268+
public void setCustomTemplates(Map<String, String> customTemplates) {
269+
this.customTemplates = customTemplates;
270+
}
271+
250272
/**
251273
* Provide a path to a custom template for the specific FreeMarker template type (if absent).
252274
*
@@ -260,15 +282,6 @@ public void putCustomTemplatesIfAbsent(String from, String to) {
260282
customTemplates.computeIfAbsent(from, k -> to);
261283
}
262284

263-
@Override
264-
public Map<String, String> getCustomTemplates() {
265-
return customTemplates;
266-
}
267-
268-
public void setCustomTemplates(Map<String, String> customTemplates) {
269-
this.customTemplates = customTemplates;
270-
}
271-
272285
@Override
273286
public Map<String, List<String>> getCustomAnnotationsMapping() {
274287
return customAnnotationsMapping;

src/main/java/com/kobylynskyi/graphql/codegen/model/MappingContext.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,12 @@ public Boolean isGenerateSealedInterfaces() {
8484
public Map<String, String> getCustomTypesMapping() {
8585
return config.getCustomTypesMapping();
8686
}
87-
87+
88+
@Override
89+
public File getCustomTemplatesRoot() {
90+
return config.getCustomTemplatesRoot();
91+
}
92+
8893
@Override
8994
public Map<String, String> getCustomTemplates() {
9095
return config.getCustomTemplates();

0 commit comments

Comments
 (0)