1
+ package com .github .michaeboyles .dgs ;
2
+
3
+ import com .netflix .graphql .dgs .codegen .CodeGen ;
4
+ import com .netflix .graphql .dgs .codegen .CodeGenConfig ;
5
+ import com .netflix .graphql .dgs .codegen .Language ;
6
+ import org .apache .maven .plugin .AbstractMojo ;
7
+ import org .apache .maven .plugins .annotations .LifecyclePhase ;
8
+ import org .apache .maven .plugins .annotations .Mojo ;
9
+ import org .apache .maven .plugins .annotations .Parameter ;
10
+ import org .apache .maven .project .MavenProject ;
11
+
12
+ import java .io .File ;
13
+ import java .io .IOException ;
14
+ import java .util .ArrayList ;
15
+ import java .util .Arrays ;
16
+ import java .util .Collections ;
17
+ import java .util .HashMap ;
18
+ import java .util .HashSet ;
19
+ import java .util .List ;
20
+ import java .util .Map ;
21
+ import java .util .Set ;
22
+ import java .util .stream .Collectors ;
23
+
24
+ @ Mojo (name = "generate" , defaultPhase = LifecyclePhase .GENERATE_SOURCES )
25
+ @ SuppressWarnings ({"FieldMayBeFinal" , "FieldCanBeLocal" , "MismatchedQueryAndUpdateOfCollection" , "unused" ,"MismatchedReadAndWriteOfArray" })
26
+ public class GenerateMojo extends AbstractMojo
27
+ {
28
+ @ Parameter (defaultValue = "${project.build.sourceDirectory}/../resources/schema" )
29
+ private File [] schemaPaths = {};
30
+
31
+ @ Parameter (required = true )
32
+ private String packageName ;
33
+
34
+ @ Parameter (defaultValue = "client" )
35
+ private String subPackageNameClient ;
36
+
37
+ @ Parameter (defaultValue = "datafetchers" )
38
+ private String subPackageNameDatafetchers ;
39
+
40
+ @ Parameter (defaultValue = "types" )
41
+ private String subPackageNameTypes ;
42
+
43
+ // Gradle plugin also checks this: project.plugins.hasPlugin(KotlinPluginWrapper::class.java)
44
+ // but I don't know the equivalent and it seems good enough
45
+ @ Parameter
46
+ private String language = hasKotlinPluginWrapperClass () ? "KOTLIN" : "JAVA" ;
47
+
48
+ @ Parameter
49
+ private Map <String , String > typeMapping = new HashMap <>();
50
+
51
+ @ Parameter
52
+ private boolean generateBoxedTypes = false ;
53
+
54
+ @ Parameter
55
+ private boolean generateClient = false ;
56
+
57
+ @ Parameter (defaultValue = "${project.build.directory}/generated-sources/annotations/" )
58
+ private File outputDir ;
59
+
60
+ @ Parameter (defaultValue = "${project.build.directory}/generated-examples" )
61
+ private File examplesOutputDir ;
62
+
63
+ @ Parameter
64
+ private List <String > includeQueries = new ArrayList <>();
65
+
66
+ @ Parameter
67
+ private List <String > includeMutations = new ArrayList <>();
68
+
69
+ @ Parameter
70
+ private boolean skipEntityQueries = false ;
71
+
72
+ @ Parameter
73
+ private boolean shortProjectionNames = false ;
74
+
75
+ /* These are available in an unreleased version
76
+ @Parameter
77
+ private boolean generateDataTypes = true;
78
+ @Parameter
79
+ private int maxProjectionDepth = 10;
80
+ */
81
+
82
+ @ Parameter (defaultValue = "${project}" )
83
+ private MavenProject project ;
84
+
85
+ @ Override
86
+ public void execute () {
87
+
88
+ Set <File > schemaPaths = Arrays .stream (this .schemaPaths )
89
+ .map (this ::getCanonicalFile )
90
+ .collect (Collectors .toSet ());
91
+
92
+ for (File schemaPath : schemaPaths ) {
93
+ if (!schemaPath .exists ()) {
94
+ getLog ().warn ("Schema location " + schemaPath + " does not exist" );
95
+ }
96
+ }
97
+
98
+ CodeGenConfig config = new CodeGenConfig (
99
+ Collections .emptySet (),
100
+ schemaPaths ,
101
+ getCanonicalFile (outputDir ).toPath (),
102
+ getCanonicalFile (examplesOutputDir ).toPath (),
103
+ true ,
104
+ packageName ,
105
+ subPackageNameClient ,
106
+ subPackageNameDatafetchers ,
107
+ subPackageNameTypes ,
108
+ Language .valueOf (language .toUpperCase ()),
109
+ generateBoxedTypes ,
110
+ generateClient ,
111
+ typeMapping ,
112
+ new HashSet <>(includeQueries ),
113
+ new HashSet <>(includeMutations ),
114
+ skipEntityQueries ,
115
+ shortProjectionNames
116
+ );
117
+
118
+ getLog ().info ("Codegen config: " + config );
119
+
120
+ new CodeGen (config ).generate ();
121
+ project .addCompileSourceRoot (getCanonicalFile (outputDir ).getAbsolutePath ());
122
+ }
123
+
124
+ private File getCanonicalFile (File file ) {
125
+ try {
126
+ return file .getCanonicalFile ();
127
+ } catch (IOException e ) {
128
+ throw new RuntimeException (e );
129
+ }
130
+ }
131
+
132
+ private boolean hasKotlinPluginWrapperClass () {
133
+ try {
134
+ getClass ().getClassLoader ().loadClass ("org.jetbrains.kotlin.gradle.plugin.KotlinPluginWrapper" );
135
+ return true ;
136
+ }
137
+ catch (Exception e ) {
138
+ return false ;
139
+ }
140
+ }
141
+ }
0 commit comments