Skip to content
This repository has been archived by the owner on Dec 1, 2024. It is now read-only.

Move resource validation to its own plugin/extension #183

Merged
merged 2 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions build-logic/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@
**/
plugins {
`kotlin-dsl`
`java-gradle-plugin`
}

gradlePlugin {
plugins {
create("resource-validation") {
id = "resource-validation"
implementationClass = "io.matthewnelson.kmp.tor.binary.resource.validation.ResourceValidationPlugin"
}
}
}

dependencies {
Expand Down
1 change: 0 additions & 1 deletion build-logic/src/main/kotlin/build_logic.gradle.kts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/package resources
**/package io.matthewnelson.kmp.tor.binary.resource.validation

import com.android.build.api.dsl.LibraryExtension
import io.matthewnelson.encoding.base16.Base16
Expand All @@ -29,29 +29,6 @@ sealed class ResourceValidation(
protected val modulePackageName: String,
) {

companion object {

@JvmStatic
fun Project.resourceValidation(
block: Builder.() -> Unit
) { Builder(this).apply(block).build() }
}

@ResourceDsl
class Builder internal constructor(
private val project: Project
) {

@ResourceDsl
fun torResources(block: TorResources.() -> Unit) {
TorResources(project).apply(block).build()
}

internal fun build() {
// TODO: publication task dependencies
}
}

protected abstract val androidLibHashes: Set<AndroidLibHash>
protected abstract val jvmLibHashes: Set<JvmLibHash>
protected abstract val nativeResourceHashes: Set<NativeResourceHash>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/package resources
**/
package io.matthewnelson.kmp.tor.binary.resource.validation

/**
* A marker annotations for DSLs.
*/
@DslMarker
@Target(
AnnotationTarget.CLASS,
AnnotationTarget.FUNCTION,
AnnotationTarget.TYPEALIAS,
AnnotationTarget.TYPE,
)
annotation class ResourceDsl
import org.gradle.api.Plugin
import org.gradle.api.Project

open class ResourceValidationPlugin internal constructor(): Plugin<Project> {
override fun apply(target: Project) {
target.extensions.create(
TorResourceValidationExtension.NAME,
TorResourceValidationExtension::class.java,
target,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,28 @@
* See the License for the specific language governing permissions and
* limitations under the License.
**/
package resources
package io.matthewnelson.kmp.tor.binary.resource.validation

import com.android.build.api.dsl.LibraryExtension
import org.gradle.api.GradleException
import org.gradle.api.Project
import org.gradle.kotlin.dsl.getByName
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
import java.io.File
import javax.inject.Inject

@ResourceDsl
class TorResources internal constructor(
/**
* Validates packaged resources within the `external/build/pacakge` directory
* using [androidLibHashes], [jvmGeoipHashes], [jvmLibHashes], [nativeResourceHashes]
* and configures those platform source sets to utilize either mock resources
* from `library/binary/mock_resources`, or the actual built products from
* `external/build/package`. This is to maintain runtime referecnes and
* mitigate checking resources into version control.
*
* Any errors are written to the project's `build/reports/resource-validation/binary`
* directory for the respective files.
* */
abstract class TorResourceValidationExtension @Inject internal constructor(
project: Project
): ResourceValidation(
project = project,
Expand All @@ -32,21 +45,31 @@ class TorResources internal constructor(
private val geoipErrors = mutableSetOf<String>()
private var isGeoipConfigured = false

@ResourceDsl
fun LibraryExtension.configureTorJniResources() {
configureAndroidJniResources()
@Throws(IllegalStateException::class)
fun configureTorAndroidJniResources() {
check(project.plugins.hasPlugin("com.android.library")) {
"The 'com.android.library' plugin is required to utilize this function"
}

project.extensions.getByName<LibraryExtension>("android").apply {
configureAndroidJniResources()
}
}

@ResourceDsl
fun jvmTorLibResourcesSrcDir(): File = jvmLibResourcesSrcDir()
val jvmTorLibResourcesSrcDir: File get() = jvmLibResourcesSrcDir()

@Throws(IllegalStateException::class)
fun configureTorNativeResources() {
check(project.plugins.hasPlugin("org.jetbrains.kotlin.multiplatform")) {
"The 'org.jetbrains.kotlin.multiplatform' plugin is required to utilize this function"
}

@ResourceDsl
fun KotlinMultiplatformExtension.configureTorNativeResources() {
configureNativeResources()
project.extensions.getByName<KotlinMultiplatformExtension>("kotlin").apply {
configureNativeResources()
}
}

@ResourceDsl
fun jvmGeoipResourcesSrcDir(): File {
val jvmGeoipResourcesSrcDir: File get() {
val mockResourcesSrc = rootProjectDir
.resolve("library")
.resolve(moduleName)
Expand Down Expand Up @@ -99,8 +122,6 @@ class TorResources internal constructor(
}
}

internal fun build(): Set<String> = geoipErrors

private val jvmGeoipHashes = setOf(
"geoip.gz" to "a18e0233ccc5e97579d96dba6b498c9e1bb2597db1f9f355b4d95fa718b793d3",
"geoip6.gz" to "1ae4187df1b2c47331b07422f9de6404adee2b394dc48d984dfa50dd753833bc",
Expand Down Expand Up @@ -249,4 +270,8 @@ class TorResources internal constructor(
// hash = "TODO",
// ),
)

internal companion object {
internal const val NAME = "torResourceValidation"
}
}
35 changes: 15 additions & 20 deletions library/binary-android-unit-test/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import resources.ResourceValidation.Companion.resourceValidation

/*
* Copyright (c) 2023 Matthew Nelson
*
Expand All @@ -17,30 +15,27 @@ import resources.ResourceValidation.Companion.resourceValidation
**/
plugins {
id("configuration")
id("resource-validation")
}

kmpConfiguration {
resourceValidation {
torResources {
configure {
androidLibrary(namespace = "io.matthewnelson.kmp.tor.binary.android.unit.test") {
target { publishLibraryVariants("release") }
configure {
androidLibrary(namespace = "io.matthewnelson.kmp.tor.binary.android.unit.test") {
target { publishLibraryVariants("release") }

android {
// Only want to include binary resources from jvmMain
// and not geoip files which are positioned at
// jvmAndroidMain/resources.
//
// Doing so would cause a conflict for anyone depending
// on both :library:binary and :library:binary-android-unit-test
sourceSets.getByName("main").resources {
srcDir(jvmTorLibResourcesSrcDir())
}
}
android {
sourceSets.getByName("main").resources {
// Only want to include binary resources from jvmMain
// and not geoip files which are positioned at
// jvmAndroidMain/resources.
//
// Doing so would cause a conflict for anyone depending
// on both :library:binary and :library:binary-android-unit-test
srcDir(torResourceValidation.jvmTorLibResourcesSrcDir)
}

common { pluginIds("publication") }
}
}

common { pluginIds("publication") }
}
}
Loading
Loading