package buildsrc.utils import java.io.File import org.gradle.api.NamedDomainObjectContainer import org.gradle.api.NamedDomainObjectFactory import org.gradle.api.Project import org.gradle.api.artifacts.Configuration import org.gradle.api.component.AdhocComponentWithVariants import org.gradle.api.file.RelativePath import org.gradle.api.model.ObjectFactory import org.gradle.api.plugins.ExtensionContainer import org.gradle.kotlin.dsl.* /** * Mark this [Configuration] as one that will be consumed by other subprojects. * * ``` * isCanBeResolved = false * isCanBeConsumed = true * ``` */ fun Configuration.asProvider( visible: Boolean = true ) { isVisible = visible isCanBeResolved = false isCanBeConsumed = true } /** * Mark this [Configuration] as one that will consume artifacts from other subprojects (also known as 'resolving') * * ``` * isCanBeResolved = true * isCanBeConsumed = false * ``` * */ fun Configuration.asConsumer( visible: Boolean = false ) { isVisible = visible isCanBeResolved = true isCanBeConsumed = false } /** Drop the first [count] directories from the path */ fun RelativePath.dropDirectories(count: Int): RelativePath = RelativePath(true, *segments.drop(count).toTypedArray()) /** Drop the first directory from the path */ fun RelativePath.dropDirectory(): RelativePath = dropDirectories(1) /** Drop the first directory from the path */ fun RelativePath.dropDirectoriesWhile( segmentPrediate: (segment: String) -> Boolean ): RelativePath = RelativePath( true, *segments.dropWhile(segmentPrediate).toTypedArray(), ) /** * Don't publish test fixtures (which causes warnings when publishing) * * https://docs.gradle.org/current/userguide/java_testing.html#publishing_test_fixtures */ fun Project.skipTestFixturesPublications() { val javaComponent = components["java"] as AdhocComponentWithVariants javaComponent.withVariantsFromConfiguration(configurations["testFixturesApiElements"]) { skip() } javaComponent.withVariantsFromConfiguration(configurations["testFixturesRuntimeElements"]) { skip() } } /** * Add an extension to the [ExtensionContainer], and return the value. * * Adding an extension is especially useful for improving the DSL in build scripts when [T] is a * [NamedDomainObjectContainer]. * Using an extension will allow Gradle to generate * [type-safe model accessors](https://docs.gradle.org/current/userguide/kotlin_dsl.html#kotdsl:accessor_applicability) * for added types. * * ([name] should match the property name. This has to be done manually. I tried using a * delegated-property provider but then Gradle can't introspect the types properly, so it fails to * create accessors). */ internal inline fun ExtensionContainer.adding( name: String, value: T, ): T { add(name, value) return value } /** * Create a new [NamedDomainObjectContainer], using * [org.gradle.kotlin.dsl.domainObjectContainer] * (but [T] is `reified`). * * @param[factory] an optional factory for creating elements * @see org.gradle.kotlin.dsl.domainObjectContainer */ internal inline fun ObjectFactory.domainObjectContainer( factory: NamedDomainObjectFactory? = null ): NamedDomainObjectContainer = if (factory == null) { domainObjectContainer(T::class) } else { domainObjectContainer(T::class, factory) } /** workaround for the overly verbose replacement for the deprecated [Project.getBuildDir] property */ val Project.buildDir_: File get() = layout.buildDirectory.get().asFile