Skip to content

Commit

Permalink
✨ Update KotoolsSamplesJvmPlugin.apply(Project) method
Browse files Browse the repository at this point in the history
This method now creates the `backupMainSourcesTask` Gradle task.
  • Loading branch information
LVMVRQUXL committed Oct 31, 2024
1 parent f116622 commit b6ff70b
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 4 deletions.
28 changes: 24 additions & 4 deletions jvm/src/main/kotlin/org/kotools/samples/KotoolsSamplesJvmPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.gradle.api.Project
import org.gradle.api.file.Directory
import org.gradle.api.plugins.JavaPluginExtension
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.Copy
import org.gradle.api.tasks.TaskProvider
import org.gradle.kotlin.dsl.findByType
import org.gradle.kotlin.dsl.getByType
Expand Down Expand Up @@ -43,7 +44,9 @@ public class KotoolsSamplesJvmPlugin : Plugin<Project> {
this.checkSampleSourcesTask(project)
val extractSamples: TaskProvider<ExtractSamples> =
this.extractSamplesTask(project, checkSampleSources)
this.checkSampleReferencesTask(project, extractSamples)
val checkSampleReferences: TaskProvider<CheckSampleReferences> =
this.checkSampleReferencesTask(project, extractSamples)
this.backupMainSourcesTask(project, checkSampleReferences)
}

private fun kotlinSourceSets(project: Project) {
Expand Down Expand Up @@ -100,19 +103,36 @@ public class KotoolsSamplesJvmPlugin : Plugin<Project> {
private fun checkSampleReferencesTask(
project: Project,
extractSamples: TaskProvider<ExtractSamples>
) {
): TaskProvider<CheckSampleReferences> {
val sourceDirectory: Directory = this.sourceDirectory(project)
val extractedSamplesDirectory: Provider<Directory> =
extractSamples.flatMap(ExtractSamples::outputDirectory)
project.tasks.register<CheckSampleReferences>("checkSampleReferences")
.configure {
return project.tasks
.register<CheckSampleReferences>("checkSampleReferences") {
this.description = "Checks sample references from KDoc."
this.dependsOn += extractSamples
this.sourceDirectory.set(sourceDirectory)
this.extractedSamplesDirectory.set(extractedSamplesDirectory)
}
}

private fun backupMainSourcesTask(
project: Project,
checkSampleReferences: TaskProvider<CheckSampleReferences>
) {
val source: Directory = this.sourceDirectory(project)
val destination: Provider<Directory> =
project.layout.buildDirectory.dir("samples/sources-backup")
project.tasks.register<Copy>("backupMainSources")
.configure {
this.description =
"Copies main sources into the build directory."
this.dependsOn(checkSampleReferences)
this.from(source) { exclude("api", "sample", "test") }
this.into(destination)
}
}

private fun sourceDirectory(project: Project): Directory =
project.layout.projectDirectory.dir("src")

Expand Down
51 changes: 51 additions & 0 deletions jvm/src/test/kotlin/org/kotools/samples/KotoolsSamplesJvmPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.gradle.api.file.Directory
import org.gradle.api.file.FileCollection
import org.gradle.api.internal.plugins.PluginApplicationException
import org.gradle.api.plugins.JavaPluginExtension
import org.gradle.api.tasks.Copy
import org.gradle.api.tasks.SourceSet
import org.gradle.api.tasks.TaskProvider
import org.gradle.kotlin.dsl.apply
Expand All @@ -16,6 +17,7 @@ import org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension
import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformJvmPlugin
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
import org.kotools.samples.internal.KotlinJvmPluginNotFound
import java.io.File
import java.util.Objects
import kotlin.reflect.KClass
import kotlin.test.Test
Expand Down Expand Up @@ -199,6 +201,51 @@ class KotoolsSamplesJvmPluginTest {
)
}

@Test
fun `apply should create 'backupMainSources' task`() {
val project = Project()
.applyKotlinAndKotoolsSamplesJvmPlugins()
val task: Copy = project.assertTask("backupMainSources")
task.assertDescription("Copies main sources into the build directory.")
val checkSampleReferences: TaskProvider<CheckSampleReferences> =
project.tasks.named<CheckSampleReferences>("checkSampleReferences")
val expectedDependencies: List<TaskProvider<CheckSampleReferences>> =
listOf(checkSampleReferences)
val actualDependencies: List<Any> = task.dependsOn.toList()
assertContentEquals(expectedDependencies, actualDependencies)
val source: Directory = project.layout.projectDirectory.dir("src")
val areSourceFilesInSourceDirectory: Boolean =
task.source.all { source.asFile.path in it.path }
assertTrue(
actual = areSourceFilesInSourceDirectory,
message = "Source directory should be ${source}."
)
val areApiFilesExcluded: Boolean =
task.source.files.none { "api" in it.path }
assertTrue(
actual = areApiFilesExcluded,
message = "API files should be excluded from source directory."
)
val areSamplesExcluded: Boolean =
task.source.files.none { "sample" in it.path }
assertTrue(
actual = areSamplesExcluded,
message = "Samples should be excluded from source directory."
)
val areTestsExcluded: Boolean =
task.source.files.none { "test" in it.path }
assertTrue(
actual = areTestsExcluded,
message = "Tests should be excluded from source directory."
)
val expectedDestinationDirectory: File = project.layout.buildDirectory
.dir("samples/sources-backup")
.get()
.asFile
val actualDestinationDirectory: File = task.destinationDir
assertEquals(expectedDestinationDirectory, actualDestinationDirectory)
}

// ------------------------------ Conversions ------------------------------

@Test
Expand Down Expand Up @@ -227,6 +274,10 @@ private inline fun <reified T : Task> Project.assertTask(): T {
val kClass: KClass<T> = T::class
val name: String = kClass.simpleName?.replaceFirstChar(Char::lowercaseChar)
?: fail("The $kClass has no name.")
return this.assertTask(name)
}

private inline fun <reified T : Task> Project.assertTask(name: String): T {
val actual: T? = this.tasks.findByName(name) as? T
val message = "The '$name' Gradle task is not found."
return assertNotNull(actual, message)
Expand Down

0 comments on commit b6ff70b

Please sign in to comment.