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

The newest version!
if (project.isBuildSrcProject) {
    return
}

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

        configurations.indirectApi.withDependencies { deps ->
            Collection mainConfNames = project.getSourceSetConfigurationNames(sourceSets.main)
            configurations.matching { mainConfNames.contains(it.name) }.forEach { Configuration conf ->
                conf.allDependencies
                    .findAll { it.group == 'org.ow2.asm' }
                    .collect { Dependency dep ->
                        if (isPlatformDependency(dep)) {
                            return "${dep.group}:${dep.name}:${dep.version}:@pom"
                        } else {
                            return dep.copy()
                        }
                    }
                    .forEach { Object notation ->
                        deps.add(project.dependencies.create(notation))
                    }
            }
        }

        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).value(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
            }).with { it.finalizeValueOnRead(); it }
            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 - 2025 Weber Informatics LLC | Privacy Policy