Skip to content

Commit

Permalink
make IvyRemoveInvalidDependenciesPlugin and IvyRemovePlatformDependen…
Browse files Browse the repository at this point in the history
…ciesPlugin config cache compatible
  • Loading branch information
rpalcolea committed Nov 16, 2023
1 parent b629334 commit 60e860f
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import org.gradle.api.publish.ivy.IvyPublication
* Removes from descriptor dependencies that are invalid:
* 1) No revision available
*/
@CompileDynamic
class IvyRemoveInvalidDependenciesPlugin implements Plugin<Project> {
@Override
void apply(Project project) {
Expand All @@ -45,7 +46,12 @@ class IvyRemoveInvalidDependenciesPlugin implements Plugin<Project> {
ivyModuleDescriptorSpec.withXml(new Action<XmlProvider>() {
@Override
void execute(XmlProvider xml) {
configureXml(xml)
xml.asNode().dependencies.dependency.findAll() { Node dep ->
String revision = dep.@rev
if(!revision) {
dep.parent().remove(dep)
}
}
}
})
}
Expand All @@ -54,14 +60,4 @@ class IvyRemoveInvalidDependenciesPlugin implements Plugin<Project> {
}
})
}

@CompileDynamic
private void configureXml(XmlProvider xml) {
xml.asNode().dependencies.dependency.findAll() { Node dep ->
String revision = dep.@rev
if(!revision) {
dep.parent().remove(dep)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import org.gradle.api.Action
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.XmlProvider
import org.gradle.api.plugins.JavaPlugin
import org.gradle.api.publish.PublicationContainer
import org.gradle.api.publish.PublishingExtension
import org.gradle.api.publish.ivy.IvyModuleDescriptorSpec
Expand All @@ -30,58 +31,63 @@ import org.gradle.api.publish.ivy.IvyPublication
/**
* Removes from descriptor dependencies that have category status of platform or enhanced-platform
*/
@CompileDynamic
class IvyRemovePlatformDependenciesPlugin implements Plugin<Project> {
@Override
void apply(Project project) {
PublishingExtension publishing = project.extensions.getByType(PublishingExtension)
publishing.publications(new Action<PublicationContainer>() {
project.afterEvaluate(new Action<Project>() {
@Override
void execute(PublicationContainer publications) {
publications.withType(IvyPublication) { IvyPublication publication ->
publication.descriptor(new Action<IvyModuleDescriptorSpec>() {
void execute(Project p) {
p.plugins.withType(JavaPlugin) {
def platformDependencies = PlatformDependencyVerifier.findPlatformDependencies(p)
publishing.publications(new Action<PublicationContainer>() {
@Override
void execute(IvyModuleDescriptorSpec ivyModuleDescriptorSpec) {
ivyModuleDescriptorSpec.withXml(new Action<XmlProvider>() {
@Override
void execute(XmlProvider xml) {
configureXml(project, xml)
}
})
}
})
}
}
})
}
void execute(PublicationContainer publications) {
publications.withType(IvyPublication) { IvyPublication publication ->
publication.descriptor(new Action<IvyModuleDescriptorSpec>() {
@Override
void execute(IvyModuleDescriptorSpec ivyModuleDescriptorSpec) {
ivyModuleDescriptorSpec.withXml(new Action<XmlProvider>() {
@Override
void execute(XmlProvider xml) {
xml.asNode().dependencies.dependency.findAll() { Node dep ->
String scope = dep.@conf
String group = dep.@org
String name = dep.@name

@CompileDynamic
private void configureXml(Project project, XmlProvider xml) {
xml.asNode().dependencies.dependency.findAll() { Node dep ->
String scope = dep.@conf
String group = dep.@org
String name = dep.@name
if (scope == 'compile->default') {
scope = 'compile'
}

if (scope == 'compile->default') {
scope = 'compile'
}
if (scope == 'provided->default' || scope == 'runtime->default') {
scope = 'runtime'
}

if (scope == 'provided->default' || scope == 'runtime->default') {
scope = 'runtime'
}
if (scope == 'test->default') {
scope = 'test'
}

if (scope == 'test->default') {
scope = 'test'
}
if (scope.contains('->')) {
scope = scope.split('->')[0]
}

if (scope.contains('->')) {
scope = scope.split('->')[0]
}
boolean isPlatformDependency = PlatformDependencyVerifier.isPlatformDependency(platformDependencies, scope, group, name)

boolean isPlatformDependency = PlatformDependencyVerifier.isPlatformDependency(project, scope, group, name)
if(isPlatformDependency) {
dep.parent().remove(dep)
}
}
}
})
}
})
}
}
})
}

if(isPlatformDependency) {
dep.parent().remove(dep)
}
}
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import org.gradle.api.publish.PublishingExtension
import org.gradle.api.publish.ivy.IvyModuleDescriptorSpec
import org.gradle.api.publish.ivy.IvyPublication

@CompileDynamic
class IvyVerifyUnspecifiedVersionDependenciesPlugin implements Plugin<Project> {
@Override
void apply(Project project) {
Expand All @@ -41,7 +42,14 @@ class IvyVerifyUnspecifiedVersionDependenciesPlugin implements Plugin<Project> {
ivyModuleDescriptorSpec.withXml(new Action<XmlProvider>() {
@Override
void execute(XmlProvider xml) {
verifyVersionsInDependencies(xml)
xml.asNode().dependencies.dependency.findAll() { Node dep ->
String revision = dep.@rev
if(revision == 'unspecified') {
String group = dep.@org
String name = dep.@name
throw new GradleException("Dependency $group:$name has an invalid version: $revision. This publication is invalid")
}
}
}
})
}
Expand All @@ -51,15 +59,5 @@ class IvyVerifyUnspecifiedVersionDependenciesPlugin implements Plugin<Project> {
})
}

@CompileDynamic
private void verifyVersionsInDependencies(XmlProvider xml) {
xml.asNode().dependencies.dependency.findAll() { Node dep ->
String revision = dep.@rev
if(revision == 'unspecified') {
String group = dep.@org
String name = dep.@name
throw new GradleException("Dependency $group:$name has an invalid version: $revision. This publication is invalid")
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,17 @@ class PlatformDependencyVerifier {
private static final String REGULAR_PLATFORM = "platform"
private static final String ENFORCED_PLATFORM = "enforced-platform"

static boolean isPlatformDependency(Project project, String scope, String group, String name) {
Boolean result = checkIfPlatformDependency(project, scope, group, name)
static boolean isPlatformDependency(def platformDependencies, String scope, String group, String name) {
Boolean result = checkIfPlatformDependency(platformDependencies, scope, group, name)
Map<String, String> scoping = [compile: 'runtime', provided: 'compileOnly']
if (!result && scoping[scope]) {
result = checkIfPlatformDependency(project, scoping[scope], group, name)
result = checkIfPlatformDependency(platformDependencies, scoping[scope], group, name)
}
result
}

private static boolean checkIfPlatformDependency(Project project, String scope, String group, String name) {
def platformDependencies = findPlatformDependencies(project)[scope]
return platformDependencies.find { ComponentSelector componentSelector ->
private static boolean checkIfPlatformDependency(def platformDependencies, String scope, String group, String name) {
return platformDependencies.values().flatten().find { ComponentSelector componentSelector ->
if(componentSelector instanceof ModuleComponentSelector) {
return componentSelector.moduleIdentifier.group == group && componentSelector.moduleIdentifier.name == name
} else if(componentSelector instanceof ProjectComponentSelector) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import org.gradle.testkit.runner.TaskOutcome

class IvyNebulaPublishPluginSpec extends BaseIntegrationTestKitSpec {

def 'should successful publish with stable publishing feature flag'() {
def 'should successful publish'() {
given:
buildFile << """
plugins {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,15 @@ class IvyRemovePlatformDependenciesPluginSpec extends BaseIntegrationTestKitSpec
apply plugin: 'java'
repositories {
${generator.ivyRepositoryBlock}
mavenCentral()
${generator.ivyRepositoryBlock}
maven {
url = 'https://repo.jenkins-ci.org/releases/'
}
}
dependencies {
implementation platform('com.github.sghill.jenkins:jenkins-bom:latest.release')
implementation platform('org.jenkins-ci.main:jenkins-bom:latest.release')
implementation 'test.resolved:a:1.+'
}
""".stripIndent()
Expand Down Expand Up @@ -87,6 +90,12 @@ class IvyRemovePlatformDependenciesPluginSpec extends BaseIntegrationTestKitSpec
version = '0.1.0'
group = 'test.nebula'
repositories {
mavenCentral()
maven {
url = 'https://repo.jenkins-ci.org/releases/'
}
}
publishing {
repositories {
ivy {
Expand Down Expand Up @@ -140,7 +149,7 @@ repositories {
}
dependencies {
implementation platform('com.github.sghill.jenkins:jenkins-bom:latest.release')
implementation platform('org.jenkins-ci.main:jenkins-bom:latest.release')
implementation platform(project(":platform"))
implementation 'test.resolved:a:1.+'
}
Expand Down Expand Up @@ -192,14 +201,16 @@ dependencies {

buildFile << """\
apply plugin: 'java'
repositories {
${generator.ivyRepositoryBlock}
mavenCentral()
maven {
url = 'https://repo.jenkins-ci.org/releases/'
}
}
dependencies {
implementation enforcedPlatform('com.github.sghill.jenkins:jenkins-bom:latest.release')
implementation enforcedPlatform('org.jenkins-ci.main:jenkins-bom:latest.release')
implementation 'test.resolved:a:1.+'
}
Expand Down Expand Up @@ -254,10 +265,13 @@ dependencies {
repositories {
${generator.ivyRepositoryBlock}
mavenCentral()
maven {
url = 'https://repo.jenkins-ci.org/releases/'
}
}
dependencies {
implementation enforcedPlatform('com.github.sghill.jenkins:jenkins-bom:latest.release')
implementation enforcedPlatform('org.jenkins-ci.main:jenkins-bom:latest.release')
implementation 'test.resolved:a:1.+'
}
Expand Down Expand Up @@ -312,10 +326,13 @@ dependencies {
repositories {
${generator.ivyRepositoryBlock}
mavenCentral()
maven {
url = 'https://repo.jenkins-ci.org/releases/'
}
}
dependencies {
implementation enforcedPlatform('com.github.sghill.jenkins:jenkins-bom:latest.release')
implementation enforcedPlatform('org.jenkins-ci.main:jenkins-bom:latest.release')
implementation 'test.resolved:a:1.+'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ import nebula.test.dependencies.GradleDependencyGenerator
import nebula.test.dependencies.ModuleBuilder
import nebula.test.functional.ExecutionResult
import netflix.nebula.dependency.recommender.DependencyRecommendationsPlugin
import spock.lang.Ignore
import spock.lang.IgnoreIf

@Ignore
class PublishVerificationPluginIntegrationSpec extends IntegrationSpec {

def setup() {
Expand Down

0 comments on commit 60e860f

Please sign in to comment.