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