aboutsummaryrefslogtreecommitdiff
path: root/buildSrc/src/main/kotlin/org/jetbrains/ValidatePublications.kt
blob: 4659f6695fd501113d59dbfa81db33180e37a5df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package org.jetbrains

import com.jfrog.bintray.gradle.BintrayExtension
import org.gradle.api.DefaultTask
import org.gradle.api.GradleException
import org.gradle.api.Project
import org.gradle.api.artifacts.ProjectDependency
import org.gradle.api.publish.PublishingExtension
import org.gradle.api.publish.maven.MavenPublication
import org.gradle.api.tasks.TaskAction
import org.gradle.kotlin.dsl.findByType
import org.gradle.kotlin.dsl.provideDelegate

open class ValidatePublications : DefaultTask() {
    class MissingBintrayPublicationException(project: Project, publication: MavenPublication) : GradleException(
        "Project ${project.path} has publication ${publication.name} that is not configured for bintray publication"
    )

    class UnpublishedProjectDependencyException(
        project: Project, dependencyProject: Project
    ) : GradleException(
        "Published project ${project.path} cannot depend on unpublished projed ${dependencyProject.path}"
    )


    @TaskAction
    fun validatePublicationConfiguration() {
        @Suppress("LocalVariableName")
        project.subprojects.forEach { subProject ->
            val publishing = subProject.extensions.findByType<PublishingExtension>() ?: return@forEach
            publishing.publications
                .filterIsInstance<MavenPublication>()
                .filter { it.version == project.dokkaVersion }
                .forEach { publication ->
                    if (project.publicationChannels.any { it.isBintrayRepository }) {
                        checkPublicationIsConfiguredForBintray(subProject, publication)
                    }
                    checkProjectDependenciesArePublished(subProject)
                }
        }
    }

    private fun checkPublicationIsConfiguredForBintray(project: Project, publication: MavenPublication) {
        val bintrayExtension = project.extensions.findByType<BintrayExtension>()
            ?: throw MissingBintrayPublicationException(project, publication)

        val isPublicationConfiguredForBintray = bintrayExtension.publications.orEmpty()
            .any { publicationName -> publicationName == publication.name }

        if (!isPublicationConfiguredForBintray) {
            throw MissingBintrayPublicationException(project, publication)
        }
    }

    private fun checkProjectDependenciesArePublished(project: Project) {
        (project.configurations.findByName("implementation")?.allDependencies.orEmpty() +
                project.configurations.findByName("api")?.allDependencies.orEmpty())
            .filterIsInstance<ProjectDependency>()
            .forEach { projectDependency ->
                val publishing = projectDependency.dependencyProject.extensions.findByType<PublishingExtension>()
                    ?: throw UnpublishedProjectDependencyException(
                        project = project, dependencyProject = projectDependency.dependencyProject
                    )

                val isPublished = publishing.publications.filterIsInstance<MavenPublication>()
                    .filter { it.version == project.dokkaVersion }
                    .any()

                if (!isPublished) {
                    throw UnpublishedProjectDependencyException(project, projectDependency.dependencyProject)
                }
            }
    }

    init {
        group = "verification"
        project.tasks.named("check") {
            dependsOn(this@ValidatePublications)
        }
    }
}