All Downloads are FREE. Search and download functionalities are using the official Maven repository.

name.remal.gradle_plugins.toolkit.build_logic.gradle-plugin-collect-api-dependencies.gradle Maven / Gradle / Ivy

There is a newer version: 0.68.0
Show newest version
if (project.isBuildSrcProject) return

allprojects {
    pluginManager.withPlugin('java-gradle-plugin') {
        project.ext.nonJavaApiDependencies = project.objects.setProperty(String)

        configurations.indirectApi.dependencies.addAllLater(
            project.provider {
                Dependency asmBomDependency = configurations.projectDependencyConstraints.allDependencies
                    .findAll { isPlatformDependency(it) }
                    .find { "${it.group}:${it.name}" == 'org.ow2.asm:asm-bom' }
                if (asmBomDependency == null) throw new GradleException("asm-bom platform dependency can't be found")
                return [project.dependencies.create("${asmBomDependency.group}:${asmBomDependency.name}:${asmBomDependency.version}:@pom")]
            }
        )

        Configuration gradlePluginApiDependenciesConf = configurations.create('gradlePluginApiDependencies') { Configuration conf ->
            conf.canBeResolved = true
            conf.canBeConsumed = false
            conf.extendsFrom(configurations.api)
            conf.extendsFrom(configurations.indirectApi)
        }

        tasks.register('collectGradlePluginApiDependencies') { Task task ->
            SetProperty nonJavaApiDependencies = project.nonJavaApiDependencies
            inputs.property('nonJavaApiDependencies', nonJavaApiDependencies)

            List inputConfs = [
                gradlePluginApiDependenciesConf,
            ]

            inputConfs.forEach { inputConf ->
                task.inputs.files(inputConf)
                    .optional()
                    .withNormalizer(ClasspathNormalizer)
                    .withPathSensitivity(PathSensitivity.RELATIVE)
                    .withPropertyName(inputConf.name)
            }

            SetProperty platformDependencyNotations = project.objects.setProperty(String)
            platformDependencyNotations.addAll(
                project.provider {
                    Collection notation = new TreeSet()
                    inputConfs.forEach { Configuration inputConf ->
                        inputConf.allDependencies.forEach { Dependency dep ->
                            notation.add("${dep.group}:${dep.name}:${dep.version}")
                        }
                    }
                    return notation
                }
            )
            task.inputs.property('platformDependencyNotations', platformDependencyNotations)

            File outputFile = project.file("${project.layout.buildDirectory.asFile.get()}/gradle-plugin-api-dependencies.txt")
            outputs.file(outputFile).withPropertyName('outputFile')
            task.ext.outputFile = outputFile
            task.doFirst { outputFile.delete() }

            task.doLast {
                outputFile.parentFile.mkdirs()

                Collection dependencies = new TreeSet()

                Collection resolvedArtifacts = gradlePluginApiDependenciesConf.resolvedConfiguration.resolvedArtifacts

                Closure isExcludedCategory = { ResolvedDependencyResult resolvedDependency ->
                    return isPlatformDependency(resolvedDependency) || isDocumentationDependency(resolvedDependency) || isVerificationDependency(resolvedDependency)
                }

                Closure processResolvedDependency = { ResolvedDependencyResult resolvedDependency ->
                    ModuleComponentIdentifier id = resolvedDependency.selected.id.with { id ->
                        if (id instanceof ModuleComponentIdentifier) {
                            return id
                        } else {
                            return null
                        }
                    }
                    if (id == null) return

                    if (isPlatformDependency(resolvedDependency)) {
                        String notation = "${id.group}:${id.module}:${id.version}"
                        dependencies.add(notation)
                        return
                    }

                    Collection dependencyArtifacts = resolvedArtifacts.findAll { it.id.componentIdentifier == id }
                    dependencyArtifacts.forEach { dependencyArtifact ->
                        String notation = "${id.group}:${id.module}:${id.version}:${dependencyArtifact.classifier ?: ''}@${dependencyArtifact.extension}"
                        notation = notation.replaceFirst(/:?@(jar)?$/, '')
                        dependencies.add(notation)
                    }
                }

                gradlePluginApiDependenciesConf.incoming.resolutionResult
                    .root
                    .dependencies
                    .findAll { it instanceof ResolvedDependencyResult }
                    .collect { (ResolvedDependencyResult) it }
                    .findAll { !isExcludedCategory(it) }
                    .forEach { processResolvedDependency(it) }

                gradlePluginApiDependenciesConf.incoming.resolutionResult
                    .allDependencies
                    .findAll { it instanceof ResolvedDependencyResult }
                    .collect { (ResolvedDependencyResult) it }
                    .findAll { !isExcludedCategory(it) }
                    .findAll { !it.selected.selectionReason.expected }
                    .forEach { processResolvedDependency(it) }

                nonJavaApiDependencies.get()
                    .findAll { it != null && !it.isEmpty() }
                    .forEach { dependencies.add(it) }

                outputFile.setText(dependencies.join('\n') + '\n', 'UTF-8')
            }
        }
    }
}

tasks.create('collectAllGradlePluginApiDependencies') { Task task ->
    Closure> getCollectGradlePluginApiDependenciesTasks = {
        project.allprojects
            .findAll { it.pluginManager.hasPlugin('java-gradle-plugin') }
            .collect { it.tasks.getByName('collectGradlePluginApiDependencies') }
    }.memoize()
    task.dependsOn(project.provider { getCollectGradlePluginApiDependenciesTasks() })

    Closure> getGradlePluginApiDependenciesFiles = {
        getCollectGradlePluginApiDependenciesTasks().collect { it.outputFile }
    }.memoize()
    task.inputs.files(project.provider { getGradlePluginApiDependenciesFiles() })
        .optional()
        .withPathSensitivity(PathSensitivity.RELATIVE)
        .withPropertyName('gradlePluginApiDependenciesFiles')

    File outputFile = project.file("gradle-plugin-api-dependencies.txt")
    task.outputs.file(outputFile).withPropertyName('outputFile')
    task.ext.outputFile = outputFile
    task.doFirst { outputFile.delete() }

    task.doLast {
        outputFile.parentFile.mkdirs()

        Collection dependencies = new TreeSet()
        getGradlePluginApiDependenciesFiles().forEach { file ->
            file.getText('UTF-8').split(/\n/)
                .collect { it.replaceFirst(/#.*/, '') }
                .collect { it.trim() }
                .findAll { !it.isEmpty() }
                .forEach { dependencies.add(it) }
        }
        outputFile.setText(
            [
                "# This file is generated automatically by `${it.name}` Gradle task.",
                '# Do not modify it yourself unless you know what you are doing.',
                '',
                dependencies.join('\n'),
                '',
            ].join('\n'),
            'UTF-8'
        )
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy