Skip to content

Commit 42f2b7f

Browse files
committed
Make the plugin output directory configurable
Make outputDir configurable, so that users can have full control over the output path. Additionally, the GenerateJavaTask lazy by converting inputs to gradle Properties. See: https://docs.gradle.org/current/userguide/lazy_configuration.html
1 parent a25d8ba commit 42f2b7f

File tree

4 files changed

+222
-108
lines changed

4 files changed

+222
-108
lines changed

graphql-dgs-codegen-gradle/src/main/kotlin/com/netflix/graphql/dgs/codegen/gradle/CodegenPlugin.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ class CodegenPlugin : Plugin<Project> {
4444
val javaConvention = project.convention.getPlugin(JavaPluginConvention::class.java)
4545
val sourceSets = javaConvention.sourceSets
4646
val mainSourceSet = sourceSets.getByName(SourceSet.MAIN_SOURCE_SET_NAME)
47-
val outputDir = generateJavaTaskProvider.map(GenerateJavaTask::getOutputDir)
47+
val outputDir = generateJavaTaskProvider.flatMap(GenerateJavaTask::outputDir)
48+
4849
mainSourceSet.java.srcDirs(project.files(outputDir).builtBy(generateJavaTaskProvider))
4950

5051
project.afterEvaluate { p ->

graphql-dgs-codegen-gradle/src/main/kotlin/com/netflix/graphql/dgs/codegen/gradle/GenerateJavaTask.kt

Lines changed: 136 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -22,136 +22,177 @@ import com.netflix.graphql.dgs.codegen.CodeGen
2222
import com.netflix.graphql.dgs.codegen.CodeGenConfig
2323
import com.netflix.graphql.dgs.codegen.Language
2424
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
2533
import org.gradle.api.tasks.Input
2634
import org.gradle.api.tasks.InputFiles
2735
import org.gradle.api.tasks.OutputDirectory
2836
import org.gradle.api.tasks.TaskAction
2937
import org.jetbrains.kotlin.gradle.plugin.KotlinPluginWrapper
30-
import java.io.File
3138
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+
)
6284

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)
6587

66-
@Input
67-
var generateBoxedTypes = false
88+
@get:Input
89+
val generateBoxedTypes: Property<Boolean> = objectFactory.property(Boolean::class.java)
90+
.convention(false)
6891

69-
@Input
70-
var generateClient = false
92+
@get:Input
93+
val generateClient: Property<Boolean> = objectFactory.property(Boolean::class.java)
94+
.convention(false)
7195

72-
@Input
73-
var generateDataTypes = true
96+
@get:Input
97+
val generateDataTypes: Property<Boolean> = objectFactory.property(Boolean::class.java)
98+
.convention(true)
7499

75-
@Input
76-
var generateInterfaces = false
100+
@get:Input
101+
val generateInterfaces: Property<Boolean> = objectFactory.property(Boolean::class.java)
102+
.convention(false)
77103

78-
@Input
79-
var generateInterfaceSetters = true
104+
@get:Input
105+
val generateInterfaceSetters: Property<Boolean> = objectFactory.property(Boolean::class.java)
106+
.convention(true)
80107

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+
)
85115

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+
)
90123

91-
@Input
92-
var includeQueries = mutableListOf<String>()
124+
@get:Input
125+
val includeQueries: SetProperty<String> = objectFactory.setProperty(String::class.java)
93126

94-
@Input
95-
var includeMutations = mutableListOf<String>()
127+
@get:Input
128+
val includeMutations: SetProperty<String> = objectFactory.setProperty(String::class.java)
96129

97-
@Input
98-
var includeSubscriptions = mutableListOf<String>()
130+
@get:Input
131+
val includeSubscriptions: SetProperty<String> = objectFactory.setProperty(String::class.java)
99132

100-
@Input
101-
var skipEntityQueries = false
133+
@get:Input
134+
val skipEntityQueries: Property<Boolean> = objectFactory.property(Boolean::class.java)
135+
.convention(false)
102136

103-
@Input
104-
var shortProjectionNames = false
137+
@get:Input
138+
val shortProjectionNames: Property<Boolean> = objectFactory.property(Boolean::class.java)
139+
.convention(false)
105140

106-
@Input
107-
var omitNullInputFields = false
141+
@get:Input
142+
val omitNullInputFields: Property<Boolean> = objectFactory.property(Boolean::class.java)
143+
.convention(false)
108144

109-
@Input
110-
var maxProjectionDepth = 10
145+
@get:Input
146+
val maxProjectionDepth: Property<Int> = objectFactory.property(Int::class.javaObjectType)
147+
.convention(10)
111148

112-
@Input
113-
var kotlinAllFieldsOptional = false
149+
@get:Input
150+
val kotlinAllFieldsOptional: Property<Boolean> = objectFactory.property(Boolean::class.java)
151+
.convention(false)
114152

115-
@Input
116-
var snakeCaseConstantNames = false
153+
@get:Input
154+
val snakeCaseConstantNames: Property<Boolean> = objectFactory.property(Boolean::class.java)
155+
.convention(false)
117156

118157
@TaskAction
119158
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)
123164
}
124165
logger.info("Processing schema files:")
125166
schemaPaths.forEach {
126-
logger.info("Processing $it")
167+
logger.info("Processing {}", it)
127168
}
128169

129170
val config = CodeGenConfig(
130171
schemas = emptySet(),
131172
schemaFiles = schemaPaths,
132-
outputDir = getOutputDir().toPath(),
133-
examplesOutputDir = getExampleOutputDir().toPath(),
173+
outputDir = outputDir.get().asFile.toPath(),
174+
examplesOutputDir = exampleOutputDir.get().asFile.toPath(),
134175
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()
155196
)
156197

157198
logger.info("Codegen config: {}", config)

0 commit comments

Comments
 (0)