Skip to content

Commit

Permalink
feat: #17 Plugin Refactoring
Browse files Browse the repository at this point in the history
closes #17
  • Loading branch information
vbaidak committed Nov 14, 2024
1 parent 4637d10 commit 7797ae6
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 114 deletions.
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[versions]
project = "3.1.0"
project = "3.2.0"

gradle-publish = "1.3.0"

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* MIT License
*
* Copyright (c) 2019 Scalified
*
* 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 com.scalified.plugins.gradle.it

import org.gradle.api.Project
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.SourceSet
import org.gradle.api.tasks.SourceSetContainer
import org.gradle.api.tasks.TaskContainer
import org.gradle.kotlin.dsl.getByType
import org.gradle.kotlin.dsl.getPlugin
import org.gradle.plugins.ide.idea.IdeaPlugin

/**
* @author shell
* @since 2019-10-08
*/
internal val Project.ideaPlugin
get() = plugins.getPlugin(IdeaPlugin::class)

internal val Project.sourceSets: SourceSetContainer
get() = extensions.getByType<SourceSetContainer>()

internal val TaskContainer.itClasses
get() = named("itClasses")

internal fun Project.sourceSet(name: String): Provider<SourceSet> = project.sourceSets.named(name)

internal fun Project.createMissingDirectories(paths: Set<String>) {
paths.map(this::file).forEach { file ->
if (!file.exists()) {
file.mkdirs()
logger.debug("Created '${file.absolutePath}' directory")
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2019 Scalified
* Copyright (c) 2024 Scalified
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -24,16 +24,16 @@

package com.scalified.plugins.gradle.it

import org.gradle.api.Project
import org.gradle.api.plugins.ExtensionAware
import org.gradle.api.tasks.SourceSet
import org.gradle.api.tasks.SourceSetContainer
internal const val IT = "it"

/**
* @author shell
* @since 2019-10-08
*/
internal val Project.sourceSets: SourceSetContainer
get() = (this as ExtensionAware).extensions.getByName("sourceSets") as SourceSetContainer
internal const val GROUP = "verification"

internal const val DESCRIPTION = "Runs the integration tests"

internal const val MAX_HEAP_SIZE = "256m"

internal const val MAX_PARALLEL_FORKS = 4

internal const val SRC_DIR = "src/$IT/java"

internal fun Project.sourceSet(name: String): SourceSet = project.sourceSets.getByName(name)
internal const val RESOURCES_DIR = "src/$IT/resources"
140 changes: 56 additions & 84 deletions it/src/main/kotlin/com/scalified/plugins/gradle/it/ItPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,97 +28,69 @@ import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.plugins.JavaPlugin
import org.gradle.api.tasks.SourceSet
import org.gradle.kotlin.dsl.apply
import org.gradle.kotlin.dsl.hasPlugin
import org.gradle.kotlin.dsl.named
import org.gradle.kotlin.dsl.register
import org.gradle.plugins.ide.idea.IdeaPlugin
import org.slf4j.LoggerFactory

/**
* @author shell
* @since 2019-10-08
*/
internal const val IT_PLUGIN_NAME = "it"

internal const val IT_PLUGIN_GROUP = "verification"

internal const val IT_PLUGIN_DESCRIPTION = "Runs the integration tests"

private const val SOURCE_SET_NAME = "it"

open class ItPlugin : Plugin<Project> {

private val logger = LoggerFactory.getLogger(ItPlugin::class.java)

override fun apply(project: Project) {

if (!project.plugins.hasPlugin(JavaPlugin::class.java)) {
project.plugins.apply(JavaPlugin::class.java)
logger.debug("Applied Java Plugin")
}

val task = project.tasks.create(IT_TASK_NAME, ItTask::class.java)
logger.debug("Created $IT_TASK_NAME task")

project.sourceSets.create(SOURCE_SET_NAME)
logger.debug("Created $SOURCE_SET_NAME source set")

createDirectories(project, task)
configureSourceSet(project, task)
configureConfiguration(project)
configureTask(project)
}

private fun createDirectories(project: Project, task: ItTask) {
fun create(path: String) {
val dir = project.file(path)
if (!dir.exists()) {
dir.mkdirs()
logger.debug("Created ${dir.absolutePath} directory")
}
}
create(task.srcDir)
create(task.resourcesDir)
}

private fun configureSourceSet(project: Project, task: ItTask) {
val sourceSet = project.sourceSet(SOURCE_SET_NAME)

val mainSourceSet = project.sourceSet(SourceSet.MAIN_SOURCE_SET_NAME)
val testSourceSet = project.sourceSet(SourceSet.TEST_SOURCE_SET_NAME)

sourceSet.compileClasspath = sourceSet.compileClasspath.plus(mainSourceSet.output)
.plus(testSourceSet.compileClasspath)
.plus(testSourceSet.output)
sourceSet.runtimeClasspath = sourceSet.runtimeClasspath.plus(mainSourceSet.output)
.plus(testSourceSet.runtimeClasspath)
.plus(testSourceSet.output)

sourceSet.java.setSrcDirs(project.files(task.srcDir))
sourceSet.resources.setSrcDirs(project.files(task.resourcesDir))
logger.debug("Configured $SOURCE_SET_NAME source set")
}

private fun configureConfiguration(project: Project) {
val sourceSet = project.sourceSet(SOURCE_SET_NAME)
val testSourceSet = project.sourceSet(SourceSet.TEST_SOURCE_SET_NAME)

project.configurations.getByName(sourceSet.implementationConfigurationName)
.extendsFrom(project.configurations.getByName(testSourceSet.implementationConfigurationName))
logger.debug("Configured ${sourceSet.implementationConfigurationName} configuration")

project.configurations.getByName(sourceSet.runtimeOnlyConfigurationName)
.extendsFrom(project.configurations.getByName(testSourceSet.runtimeOnlyConfigurationName))
logger.debug("Configured ${sourceSet.runtimeOnlyConfigurationName} configuration")
}

private fun configureTask(project: Project) {
val task = project.tasks.getByName(IT_TASK_NAME) as ItTask

task.dependsOn(project.tasks.findByName("itClasses"))

val sourceSet = project.sourceSet(SOURCE_SET_NAME)
task.classpath = sourceSet.runtimeClasspath
task.testClassesDirs = sourceSet.output.classesDirs
task.maxHeapSize = MAX_HEAP_SIZE
task.maxParallelForks = MAX_PARALLEL_FORKS
logger.debug("Configured $IT_TASK_NAME task")
}
private val logger = LoggerFactory.getLogger(ItPlugin::class.java)

override fun apply(project: Project) {

listOf(JavaPlugin::class, IdeaPlugin::class).filterNot(project.plugins::hasPlugin).forEach { type ->
project.plugins.apply(type)
logger.info("Applied '${type.simpleName}' plugin")
}

project.tasks.register<ItTask>(IT)
project.sourceSets.register(IT)

project.afterEvaluate {
tasks.named<ItTask>(IT) {
createMissingDirectories(setOf(srcDir.get(), resourcesDir.get()))

sourceSets.named(IT) {
val mainSourceSet = project.sourceSet(SourceSet.MAIN_SOURCE_SET_NAME).get()
val testSourceSet = project.sourceSet(SourceSet.TEST_SOURCE_SET_NAME).get()

compileClasspath = compileClasspath.plus(mainSourceSet.output)
.plus(testSourceSet.compileClasspath)
.plus(testSourceSet.output)
runtimeClasspath = runtimeClasspath.plus(mainSourceSet.output)
.plus(testSourceSet.runtimeClasspath)
.plus(testSourceSet.output)

project.configurations.named(implementationConfigurationName) {
extendsFrom(project.configurations.named(testSourceSet.implementationConfigurationName).get())
}
project.configurations.named(runtimeOnlyConfigurationName) {
extendsFrom(project.configurations.named(testSourceSet.runtimeOnlyConfigurationName).get())
}

java.setSrcDirs(files(srcDir.get()))
resources.setSrcDirs(files(resourcesDir.get()))
classpath = runtimeClasspath
testClassesDirs = output.classesDirs
maxHeapSize = MAX_HEAP_SIZE
maxParallelForks = MAX_PARALLEL_FORKS

logger.debug("SourceSet '$IT' configured")
}

dependsOn(tasks.itClasses)

project.ideaPlugin.model.module.testSources.from(srcDir)
logger.debug("Task '$IT' configured")
}
}
}

}
27 changes: 10 additions & 17 deletions it/src/main/kotlin/com/scalified/plugins/gradle/it/ItTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,20 @@ import org.gradle.api.tasks.testing.Test
* @author shell
* @since 2019-10-08
*/
internal const val IT_TASK_NAME = "it"
abstract class ItTask : Test() {

internal const val MAX_HEAP_SIZE = "256m"
@get:Input
abstract val srcDir: Property<String>

internal const val MAX_PARALLEL_FORKS = 4

open class ItTask : Test() {
@get:Input
abstract val resourcesDir: Property<String>

init {
group = IT_PLUGIN_GROUP
description = IT_PLUGIN_DESCRIPTION
}

@Input
var srcDir = "src/$IT_PLUGIN_NAME/java"
group = GROUP
description = DESCRIPTION

@Input
var resourcesDir = "src/$IT_PLUGIN_NAME/resources"

@Suppress("UnstableApiUsage")
override fun getDryRun(): Property<Boolean> =
project.objects.property(Boolean::class.java).apply { convention(false) }
srcDir.convention(SRC_DIR)
resourcesDir.convention(RESOURCES_DIR)
}

}

0 comments on commit 7797ae6

Please sign in to comment.