@@ -22,136 +22,186 @@ import com.netflix.graphql.dgs.codegen.CodeGen
22
22
import com.netflix.graphql.dgs.codegen.CodeGenConfig
23
23
import com.netflix.graphql.dgs.codegen.Language
24
24
import org.gradle.api.DefaultTask
25
+ import org.gradle.api.file.DirectoryProperty
26
+ import org.gradle.api.file.ProjectLayout
27
+ import org.gradle.api.model.ObjectFactory
28
+ import org.gradle.api.provider.ListProperty
29
+ import org.gradle.api.provider.MapProperty
30
+ import org.gradle.api.provider.Property
31
+ import org.gradle.api.provider.ProviderFactory
32
+ import org.gradle.api.provider.SetProperty
25
33
import org.gradle.api.tasks.Input
26
34
import org.gradle.api.tasks.InputFiles
27
35
import org.gradle.api.tasks.OutputDirectory
28
36
import org.gradle.api.tasks.TaskAction
29
37
import org.jetbrains.kotlin.gradle.plugin.KotlinPluginWrapper
30
- import java.io.File
31
38
import java.nio.file.Paths
32
- import java.util.*
33
-
34
- open class GenerateJavaTask : DefaultTask () {
35
- @Input
36
- var generatedSourcesDir: String = project.buildDir.absolutePath
37
-
38
- @InputFiles
39
- var schemaPaths = mutableListOf<Any >(" ${project.projectDir} /src/main/resources/schema" )
40
-
41
- @Input
42
- var packageName = " com.netflix.dgs.codegen.generated"
43
-
44
- @Input
45
- var subPackageNameClient = " client"
46
-
47
- @Input
48
- var subPackageNameDatafetchers = " datafetchers"
49
-
50
- @Input
51
- var subPackageNameTypes = " types"
52
-
53
- private val hasKotlinPluginWrapperClass = try {
54
- this .javaClass.classLoader.loadClass(" org.jetbrains.kotlin.gradle.plugin.KotlinPluginWrapper" )
55
- true
56
- } catch (ex: Exception ) {
57
- false
39
+ import java.util.Locale
40
+ import javax.inject.Inject
41
+
42
+ abstract class GenerateJavaTask @Inject constructor(
43
+ projectLayout : ProjectLayout ,
44
+ providerFactory : ProviderFactory ,
45
+ objectFactory : ObjectFactory
46
+ ) : DefaultTask() {
47
+ @get:Input
48
+ val generatedSourcesDir: Property <String > = objectFactory.property(String ::class .java)
49
+ .convention(projectLayout.buildDirectory.map { it.asFile.absolutePath })
50
+
51
+ @get:InputFiles
52
+ val schemaPaths: ListProperty <Any > = objectFactory.listProperty(Any ::class .java)
53
+ .convention(listOf (projectLayout.projectDirectory.dir(" src/main/resources/schema" ).toString()))
54
+
55
+ @get:Input
56
+ val packageName: Property <String > = objectFactory.property(String ::class .java)
57
+ .convention(" com.netflix.dgs.codegen.generated" )
58
+
59
+ @get:Input
60
+ val subPackageNameClient: Property <String > = objectFactory.property(String ::class .java)
61
+ .convention(" client" )
62
+
63
+ @get:Input
64
+ val subPackageNameDatafetchers: Property <String > = objectFactory.property(String ::class .java)
65
+ .convention(" datafetchers" )
66
+
67
+ @get:Input
68
+ val subPackageNameTypes: Property <String > = objectFactory.property(String ::class .java)
69
+ .convention(" types" )
70
+
71
+ private val hasKotlinPluginWrapperClass: Boolean by lazy(LazyThreadSafetyMode .PUBLICATION ) {
72
+ try {
73
+ this .javaClass.classLoader.loadClass(" org.jetbrains.kotlin.gradle.plugin.KotlinPluginWrapper" )
74
+ true
75
+ } catch (ex: Exception ) {
76
+ false
77
+ }
58
78
}
59
79
60
- @Input
61
- var language = if (hasKotlinPluginWrapperClass && project.plugins.hasPlugin(KotlinPluginWrapper ::class .java)) " KOTLIN" else " JAVA"
80
+ @get:Input
81
+ val language: Property <String > = objectFactory.property(String ::class .java)
82
+ .convention(
83
+ providerFactory.provider {
84
+ val hasKotlinPluginWrapperClass = try {
85
+ this .javaClass.classLoader.loadClass(" org.jetbrains.kotlin.gradle.plugin.KotlinPluginWrapper" )
86
+ true
87
+ } catch (ex: Exception ) {
88
+ false
89
+ }
90
+ if (hasKotlinPluginWrapperClass && project.plugins.hasPlugin(KotlinPluginWrapper ::class .java)) " KOTLIN" else " JAVA"
91
+ }
92
+ )
62
93
63
- @Input
64
- var typeMapping = mutableMapOf <String , String >( )
94
+ @get: Input
95
+ val typeMapping: MapProperty <String , String > = objectFactory.mapProperty( String :: class .java, String :: class .java )
65
96
66
- @Input
67
- var generateBoxedTypes = false
97
+ @get:Input
98
+ val generateBoxedTypes: Property <Boolean > = objectFactory.property(Boolean ::class .java)
99
+ .convention(false )
68
100
69
- @Input
70
- var generateClient = false
101
+ @get:Input
102
+ val generateClient: Property <Boolean > = objectFactory.property(Boolean ::class .java)
103
+ .convention(false )
71
104
72
- @Input
73
- var generateDataTypes = true
105
+ @get:Input
106
+ val generateDataTypes: Property <Boolean > = objectFactory.property(Boolean ::class .java)
107
+ .convention(true )
74
108
75
- @Input
76
- var generateInterfaces = false
109
+ @get:Input
110
+ val generateInterfaces: Property <Boolean > = objectFactory.property(Boolean ::class .java)
111
+ .convention(false )
77
112
78
- @Input
79
- var generateInterfaceSetters = true
113
+ @get:Input
114
+ val generateInterfaceSetters: Property <Boolean > = objectFactory.property(Boolean ::class .java)
115
+ .convention(true )
80
116
81
- @OutputDirectory
82
- fun getOutputDir (): File {
83
- return Paths .get(" $generatedSourcesDir /generated/sources/dgs-codegen" ).toFile()
84
- }
117
+ @get:OutputDirectory
118
+ val outputDir: DirectoryProperty = objectFactory.directoryProperty()
119
+ .convention(
120
+ generatedSourcesDir.flatMap { baseDir ->
121
+ projectLayout.dir(providerFactory.provider { project.file(" $baseDir /generated/sources/dgs-codegen" ) })
122
+ }
123
+ )
85
124
86
- @OutputDirectory
87
- fun getExampleOutputDir (): File {
88
- return Paths .get(" $generatedSourcesDir /generated/sources/dgs-codegen-generated-examples" ).toFile()
89
- }
125
+ @get:OutputDirectory
126
+ val exampleOutputDir: DirectoryProperty = objectFactory.directoryProperty()
127
+ .convention(
128
+ generatedSourcesDir.flatMap { baseDir ->
129
+ projectLayout.dir(providerFactory.provider { project.file(" $baseDir /generated/sources/dgs-codegen-generated-examples" ) })
130
+ }
131
+ )
90
132
91
- @Input
92
- var includeQueries = mutableListOf <String >( )
133
+ @get: Input
134
+ val includeQueries: SetProperty <String > = objectFactory.setProperty( String :: class .java )
93
135
94
- @Input
95
- var includeMutations = mutableListOf <String >( )
136
+ @get: Input
137
+ val includeMutations: SetProperty <String > = objectFactory.setProperty( String :: class .java )
96
138
97
- @Input
98
- var includeSubscriptions = mutableListOf <String >( )
139
+ @get: Input
140
+ val includeSubscriptions: SetProperty <String > = objectFactory.setProperty( String :: class .java )
99
141
100
- @Input
101
- var skipEntityQueries = false
142
+ @get:Input
143
+ val skipEntityQueries: Property <Boolean > = objectFactory.property(Boolean ::class .java)
144
+ .convention(false )
102
145
103
- @Input
104
- var shortProjectionNames = false
146
+ @get:Input
147
+ val shortProjectionNames: Property <Boolean > = objectFactory.property(Boolean ::class .java)
148
+ .convention(false )
105
149
106
- @Input
107
- var omitNullInputFields = false
150
+ @get:Input
151
+ val omitNullInputFields: Property <Boolean > = objectFactory.property(Boolean ::class .java)
152
+ .convention(false )
108
153
109
- @Input
110
- var maxProjectionDepth = 10
154
+ @get:Input
155
+ val maxProjectionDepth: Property <Int > = objectFactory.property(Int ::class .javaObjectType)
156
+ .convention(10 )
111
157
112
- @Input
113
- var kotlinAllFieldsOptional = false
158
+ @get:Input
159
+ val kotlinAllFieldsOptional: Property <Boolean > = objectFactory.property(Boolean ::class .java)
160
+ .convention(false )
114
161
115
- @Input
116
- var snakeCaseConstantNames = false
162
+ @get:Input
163
+ val snakeCaseConstantNames: Property <Boolean > = objectFactory.property(Boolean ::class .java)
164
+ .convention(false )
117
165
118
166
@TaskAction
119
167
fun generate () {
120
- val schemaPaths = schemaPaths.map { Paths .get(it.toString()).toFile() }.sorted().toSet()
121
- schemaPaths.filter { ! it.exists() }.forEach {
122
- logger.warn(" Schema location ${it.absolutePath} does not exist" )
168
+ val schemaPaths = schemaPaths.get().asSequence()
169
+ .map { Paths .get(it.toString()).toFile() }
170
+ .toSortedSet()
171
+ schemaPaths.asSequence().filter { ! it.exists() }.forEach {
172
+ logger.warn(" Schema location {} does not exist" , it.absolutePath)
123
173
}
124
174
logger.info(" Processing schema files:" )
125
175
schemaPaths.forEach {
126
- logger.info(" Processing $it " )
176
+ logger.info(" Processing {} " , it )
127
177
}
128
178
129
179
val config = CodeGenConfig (
130
180
schemas = emptySet(),
131
181
schemaFiles = schemaPaths,
132
- outputDir = getOutputDir() .toPath(),
133
- examplesOutputDir = getExampleOutputDir() .toPath(),
182
+ outputDir = outputDir.get().asFile .toPath(),
183
+ examplesOutputDir = exampleOutputDir.get().asFile .toPath(),
134
184
writeToFiles = true ,
135
- packageName = packageName,
136
- subPackageNameClient = subPackageNameClient,
137
- subPackageNameDatafetchers = subPackageNameDatafetchers,
138
- subPackageNameTypes = subPackageNameTypes,
139
- language = Language .valueOf(language.uppercase(Locale .getDefault())),
140
- generateBoxedTypes = generateBoxedTypes,
141
- generateClientApi = generateClient,
142
- generateInterfaces = generateInterfaces,
143
- generateInterfaceSetters = generateInterfaceSetters,
144
- typeMapping = typeMapping,
145
- includeQueries = includeQueries.toSet (),
146
- includeMutations = includeMutations.toSet (),
147
- includeSubscriptions = includeSubscriptions.toSet (),
148
- skipEntityQueries = skipEntityQueries,
149
- shortProjectionNames = shortProjectionNames,
150
- generateDataTypes = generateDataTypes,
151
- omitNullInputFields = omitNullInputFields,
152
- maxProjectionDepth = maxProjectionDepth,
153
- kotlinAllFieldsOptional = kotlinAllFieldsOptional,
154
- snakeCaseConstantNames = snakeCaseConstantNames
185
+ packageName = packageName.get() ,
186
+ subPackageNameClient = subPackageNameClient.get() ,
187
+ subPackageNameDatafetchers = subPackageNameDatafetchers.get() ,
188
+ subPackageNameTypes = subPackageNameTypes.get() ,
189
+ language = Language .valueOf(language.get(). uppercase(Locale .getDefault())),
190
+ generateBoxedTypes = generateBoxedTypes.get() ,
191
+ generateClientApi = generateClient.get() ,
192
+ generateInterfaces = generateInterfaces.get() ,
193
+ generateInterfaceSetters = generateInterfaceSetters.get() ,
194
+ typeMapping = typeMapping.get() ,
195
+ includeQueries = includeQueries.get (),
196
+ includeMutations = includeMutations.get (),
197
+ includeSubscriptions = includeSubscriptions.get (),
198
+ skipEntityQueries = skipEntityQueries.get() ,
199
+ shortProjectionNames = shortProjectionNames.get() ,
200
+ generateDataTypes = generateDataTypes.get() ,
201
+ omitNullInputFields = omitNullInputFields.get() ,
202
+ maxProjectionDepth = maxProjectionDepth.get() ,
203
+ kotlinAllFieldsOptional = kotlinAllFieldsOptional.get() ,
204
+ snakeCaseConstantNames = snakeCaseConstantNames.get()
155
205
)
156
206
157
207
logger.info(" Codegen config: {}" , config)
0 commit comments