@@ -22,136 +22,177 @@ 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
58
- }
59
-
60
- @Input
61
- var language = if (hasKotlinPluginWrapperClass && project.plugins.hasPlugin(KotlinPluginWrapper ::class .java)) " KOTLIN" else " JAVA"
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
+ @get:Input
72
+ val language: Property <String > = objectFactory.property(String ::class .java)
73
+ .convention(
74
+ providerFactory.provider {
75
+ val hasKotlinPluginWrapperClass = try {
76
+ this .javaClass.classLoader.loadClass(" org.jetbrains.kotlin.gradle.plugin.KotlinPluginWrapper" )
77
+ true
78
+ } catch (ex: Exception ) {
79
+ false
80
+ }
81
+ if (hasKotlinPluginWrapperClass && project.plugins.hasPlugin(KotlinPluginWrapper ::class .java)) " KOTLIN" else " JAVA"
82
+ }
83
+ )
62
84
63
- @Input
64
- var typeMapping = mutableMapOf <String , String >( )
85
+ @get: Input
86
+ val typeMapping: MapProperty <String , String > = objectFactory.mapProperty( String :: class .java, String :: class .java )
65
87
66
- @Input
67
- var generateBoxedTypes = false
88
+ @get:Input
89
+ val generateBoxedTypes: Property <Boolean > = objectFactory.property(Boolean ::class .java)
90
+ .convention(false )
68
91
69
- @Input
70
- var generateClient = false
92
+ @get:Input
93
+ val generateClient: Property <Boolean > = objectFactory.property(Boolean ::class .java)
94
+ .convention(false )
71
95
72
- @Input
73
- var generateDataTypes = true
96
+ @get:Input
97
+ val generateDataTypes: Property <Boolean > = objectFactory.property(Boolean ::class .java)
98
+ .convention(true )
74
99
75
- @Input
76
- var generateInterfaces = false
100
+ @get:Input
101
+ val generateInterfaces: Property <Boolean > = objectFactory.property(Boolean ::class .java)
102
+ .convention(false )
77
103
78
- @Input
79
- var generateInterfaceSetters = true
104
+ @get:Input
105
+ val generateInterfaceSetters: Property <Boolean > = objectFactory.property(Boolean ::class .java)
106
+ .convention(true )
80
107
81
- @OutputDirectory
82
- fun getOutputDir (): File {
83
- return Paths .get(" $generatedSourcesDir /generated/sources/dgs-codegen" ).toFile()
84
- }
108
+ @get:OutputDirectory
109
+ val outputDir: DirectoryProperty = objectFactory.directoryProperty()
110
+ .convention(
111
+ generatedSourcesDir.flatMap { baseDir ->
112
+ projectLayout.dir(providerFactory.provider { project.file(" $baseDir /generated/sources/dgs-codegen" ) })
113
+ }
114
+ )
85
115
86
- @OutputDirectory
87
- fun getExampleOutputDir (): File {
88
- return Paths .get(" $generatedSourcesDir /generated/sources/dgs-codegen-generated-examples" ).toFile()
89
- }
116
+ @get:OutputDirectory
117
+ val exampleOutputDir: DirectoryProperty = objectFactory.directoryProperty()
118
+ .convention(
119
+ generatedSourcesDir.flatMap { baseDir ->
120
+ projectLayout.dir(providerFactory.provider { project.file(" $baseDir /generated/sources/dgs-codegen-generated-examples" ) })
121
+ }
122
+ )
90
123
91
- @Input
92
- var includeQueries = mutableListOf <String >( )
124
+ @get: Input
125
+ val includeQueries: SetProperty <String > = objectFactory.setProperty( String :: class .java )
93
126
94
- @Input
95
- var includeMutations = mutableListOf <String >( )
127
+ @get: Input
128
+ val includeMutations: SetProperty <String > = objectFactory.setProperty( String :: class .java )
96
129
97
- @Input
98
- var includeSubscriptions = mutableListOf <String >( )
130
+ @get: Input
131
+ val includeSubscriptions: SetProperty <String > = objectFactory.setProperty( String :: class .java )
99
132
100
- @Input
101
- var skipEntityQueries = false
133
+ @get:Input
134
+ val skipEntityQueries: Property <Boolean > = objectFactory.property(Boolean ::class .java)
135
+ .convention(false )
102
136
103
- @Input
104
- var shortProjectionNames = false
137
+ @get:Input
138
+ val shortProjectionNames: Property <Boolean > = objectFactory.property(Boolean ::class .java)
139
+ .convention(false )
105
140
106
- @Input
107
- var omitNullInputFields = false
141
+ @get:Input
142
+ val omitNullInputFields: Property <Boolean > = objectFactory.property(Boolean ::class .java)
143
+ .convention(false )
108
144
109
- @Input
110
- var maxProjectionDepth = 10
145
+ @get:Input
146
+ val maxProjectionDepth: Property <Int > = objectFactory.property(Int ::class .javaObjectType)
147
+ .convention(10 )
111
148
112
- @Input
113
- var kotlinAllFieldsOptional = false
149
+ @get:Input
150
+ val kotlinAllFieldsOptional: Property <Boolean > = objectFactory.property(Boolean ::class .java)
151
+ .convention(false )
114
152
115
- @Input
116
- var snakeCaseConstantNames = false
153
+ @get:Input
154
+ val snakeCaseConstantNames: Property <Boolean > = objectFactory.property(Boolean ::class .java)
155
+ .convention(false )
117
156
118
157
@TaskAction
119
158
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" )
159
+ val schemaPaths = schemaPaths.get().asSequence()
160
+ .map { Paths .get(it.toString()).toFile() }
161
+ .toSortedSet()
162
+ schemaPaths.asSequence().filter { ! it.exists() }.forEach {
163
+ logger.warn(" Schema location {} does not exist" , it.absolutePath)
123
164
}
124
165
logger.info(" Processing schema files:" )
125
166
schemaPaths.forEach {
126
- logger.info(" Processing $it " )
167
+ logger.info(" Processing {} " , it )
127
168
}
128
169
129
170
val config = CodeGenConfig (
130
171
schemas = emptySet(),
131
172
schemaFiles = schemaPaths,
132
- outputDir = getOutputDir() .toPath(),
133
- examplesOutputDir = getExampleOutputDir() .toPath(),
173
+ outputDir = outputDir.get().asFile .toPath(),
174
+ examplesOutputDir = exampleOutputDir.get().asFile .toPath(),
134
175
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
176
+ packageName = packageName.get() ,
177
+ subPackageNameClient = subPackageNameClient.get() ,
178
+ subPackageNameDatafetchers = subPackageNameDatafetchers.get() ,
179
+ subPackageNameTypes = subPackageNameTypes.get() ,
180
+ language = Language .valueOf(language.get(). uppercase(Locale .getDefault())),
181
+ generateBoxedTypes = generateBoxedTypes.get() ,
182
+ generateClientApi = generateClient.get() ,
183
+ generateInterfaces = generateInterfaces.get() ,
184
+ generateInterfaceSetters = generateInterfaceSetters.get() ,
185
+ typeMapping = typeMapping.get() ,
186
+ includeQueries = includeQueries.get (),
187
+ includeMutations = includeMutations.get (),
188
+ includeSubscriptions = includeSubscriptions.get (),
189
+ skipEntityQueries = skipEntityQueries.get() ,
190
+ shortProjectionNames = shortProjectionNames.get() ,
191
+ generateDataTypes = generateDataTypes.get() ,
192
+ omitNullInputFields = omitNullInputFields.get() ,
193
+ maxProjectionDepth = maxProjectionDepth.get() ,
194
+ kotlinAllFieldsOptional = kotlinAllFieldsOptional.get() ,
195
+ snakeCaseConstantNames = snakeCaseConstantNames.get()
155
196
)
156
197
157
198
logger.info(" Codegen config: {}" , config)
0 commit comments