2017-09-27 18:19:16 +00:00
|
|
|
/*
|
2018-07-24 12:16:14 +00:00
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2017-09-27 18:19:16 +00:00
|
|
|
* Copyright (c) 2017 Minecrell <https://github.com/Minecrell>
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package net.minecrell.pluginyml
|
|
|
|
|
|
|
|
import org.gradle.api.Plugin
|
|
|
|
import org.gradle.api.Project
|
2021-07-16 13:52:00 +00:00
|
|
|
import org.gradle.api.artifacts.Configuration
|
2017-10-07 12:50:02 +00:00
|
|
|
import org.gradle.api.plugins.JavaPlugin
|
2021-04-30 08:25:25 +00:00
|
|
|
import org.gradle.kotlin.dsl.register
|
2021-05-28 06:04:13 +00:00
|
|
|
import org.gradle.api.tasks.SourceSet
|
|
|
|
import org.gradle.api.tasks.SourceSetContainer
|
|
|
|
import org.gradle.kotlin.dsl.getByType
|
2017-09-27 18:19:16 +00:00
|
|
|
import org.gradle.kotlin.dsl.withType
|
|
|
|
|
2021-04-30 16:43:17 +00:00
|
|
|
abstract class PlatformPlugin<T : PluginDescription>(private val platformName: String, private val fileName: String) : Plugin<Project> {
|
2017-09-27 18:19:16 +00:00
|
|
|
|
|
|
|
protected abstract fun createExtension(project: Project): T
|
|
|
|
|
2021-07-16 13:52:00 +00:00
|
|
|
protected open fun createConfiguration(project: Project): Configuration? {
|
|
|
|
val library = project.configurations.maybeCreate("library")
|
|
|
|
return project.configurations.create("${platformName.decapitalize()}Library").extendsFrom(library)
|
|
|
|
}
|
|
|
|
|
2018-07-24 12:08:00 +00:00
|
|
|
final override fun apply(project: Project) {
|
2017-09-27 18:19:16 +00:00
|
|
|
project.run {
|
|
|
|
val description = createExtension(this)
|
|
|
|
|
|
|
|
// Add extension
|
|
|
|
extensions.add(platformName.decapitalize(), description)
|
|
|
|
|
2021-05-28 06:04:13 +00:00
|
|
|
val generatedResourcesDirectory = layout.buildDirectory.dir("generated/plugin-yml/$platformName")
|
|
|
|
|
2021-07-16 13:52:00 +00:00
|
|
|
// Add library configuration
|
|
|
|
val libraries = createConfiguration(this)
|
|
|
|
|
2017-09-27 18:19:16 +00:00
|
|
|
// Create task
|
2021-04-30 08:25:25 +00:00
|
|
|
val generateTask = tasks.register<GeneratePluginDescription>("generate${platformName}PluginDescription") {
|
2021-04-30 16:43:17 +00:00
|
|
|
fileName.set(this@PlatformPlugin.fileName)
|
2021-05-28 06:04:13 +00:00
|
|
|
outputDirectory.set(generatedResourcesDirectory)
|
2021-04-30 16:43:17 +00:00
|
|
|
pluginDescription.set(provider {
|
2021-07-16 13:52:00 +00:00
|
|
|
setDefaults(project, libraries, description)
|
2021-04-30 16:43:17 +00:00
|
|
|
description
|
|
|
|
})
|
2017-09-27 18:19:16 +00:00
|
|
|
|
2021-04-30 08:25:25 +00:00
|
|
|
doFirst {
|
2021-04-30 16:43:17 +00:00
|
|
|
validate(description)
|
2021-04-30 08:25:25 +00:00
|
|
|
}
|
2017-09-27 18:19:16 +00:00
|
|
|
}
|
|
|
|
|
2017-10-07 12:50:02 +00:00
|
|
|
plugins.withType<JavaPlugin> {
|
2021-05-28 06:04:13 +00:00
|
|
|
extensions.getByType<SourceSetContainer>().named(SourceSet.MAIN_SOURCE_SET_NAME) {
|
|
|
|
resources.srcDir(generateTask)
|
2021-07-16 13:52:00 +00:00
|
|
|
if (libraries != null) {
|
|
|
|
configurations.getByName(compileClasspathConfigurationName).extendsFrom(libraries)
|
|
|
|
}
|
2021-04-30 08:25:25 +00:00
|
|
|
}
|
2017-09-27 18:19:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-16 13:52:00 +00:00
|
|
|
protected abstract fun setDefaults(project: Project, libraries: Configuration?, description: T)
|
2017-09-27 18:19:16 +00:00
|
|
|
protected abstract fun validate(description: T)
|
|
|
|
|
|
|
|
}
|