Add a resource directory to the main source set instead of adding

resources using processResources

This allows for IDEs to recognize the generated resources.

This also fixes an issue where sometimes running `clean build`
would cause 'java.io.IOException: Unable to delete directory',
although this fix may also be due to the new approach no longer
creating a temporary directory.
This commit is contained in:
Jason Penilla 2021-05-27 23:04:13 -07:00
parent e44b877174
commit 5e1f0bc577
No known key found for this signature in database
GPG key ID: 0E75A301420E48F8
2 changed files with 13 additions and 10 deletions

View file

@ -35,14 +35,13 @@ import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
import org.gradle.api.DefaultTask
import org.gradle.api.NamedDomainObjectCollection
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.provider.Property
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Nested
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.TaskAction
import org.gradle.kotlin.dsl.property
import java.io.File
open class GeneratePluginDescription : DefaultTask() {
@ -52,8 +51,8 @@ open class GeneratePluginDescription : DefaultTask() {
@Nested
val pluginDescription: Property<PluginDescription> = project.objects.property()
val outputFile: Provider<File>
@OutputFile get() = fileName.map { File(temporaryDir, it) }
@OutputDirectory
val outputDirectory: DirectoryProperty = project.objects.directoryProperty()
@TaskAction
fun generate() {
@ -72,7 +71,7 @@ open class GeneratePluginDescription : DefaultTask() {
.registerModule(module)
.setSerializationInclusion(JsonInclude.Include.NON_EMPTY)
mapper.writeValue(outputFile.get(), pluginDescription.get())
mapper.writeValue(outputDirectory.file(fileName).get().asFile, pluginDescription.get())
}
object NamedDomainObjectCollectionConverter : StdConverter<NamedDomainObjectCollection<Any>, Map<String, Any>>() {

View file

@ -27,9 +27,10 @@ package net.minecrell.pluginyml
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.plugins.JavaPlugin
import org.gradle.api.tasks.AbstractCopyTask
import org.gradle.kotlin.dsl.named
import org.gradle.kotlin.dsl.register
import org.gradle.api.tasks.SourceSet
import org.gradle.api.tasks.SourceSetContainer
import org.gradle.kotlin.dsl.getByType
import org.gradle.kotlin.dsl.withType
abstract class PlatformPlugin<T : PluginDescription>(private val platformName: String, private val fileName: String) : Plugin<Project> {
@ -43,9 +44,12 @@ abstract class PlatformPlugin<T : PluginDescription>(private val platformName: S
// Add extension
extensions.add(platformName.decapitalize(), description)
val generatedResourcesDirectory = layout.buildDirectory.dir("generated/plugin-yml/$platformName")
// Create task
val generateTask = tasks.register<GeneratePluginDescription>("generate${platformName}PluginDescription") {
fileName.set(this@PlatformPlugin.fileName)
outputDirectory.set(generatedResourcesDirectory)
pluginDescription.set(provider {
setDefaults(project, description)
description
@ -57,8 +61,8 @@ abstract class PlatformPlugin<T : PluginDescription>(private val platformName: S
}
plugins.withType<JavaPlugin> {
tasks.named<AbstractCopyTask>("processResources") {
from(generateTask)
extensions.getByType<SourceSetContainer>().named(SourceSet.MAIN_SOURCE_SET_NAME) {
resources.srcDir(generateTask)
}
}
}