Skip to content

Commit

Permalink
More configuration option
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszkwiecinski committed Mar 30, 2024
1 parent f792678 commit 6a7a73d
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# maven-sympathy

[![Build Status](https://github.com/usefulness/maven-sympathy/workflows/Build%20Project/badge.svg)](https://github.com/usefulness/maven-sympathy/actions)
[![Build Project](https://github.com/usefulness/maven-sympathy/actions/workflows/default.yml/badge.svg?branch=master&event=push)](https://github.com/usefulness/maven-sympathy/actions/workflows/default.yml)
[![Latest Version](https://img.shields.io/maven-metadata/v/https/plugins.gradle.org/m2/io/github/usefulness/maven-sympathy/maven-metadata.xml?label=gradle)](https://plugins.gradle.org/plugin/io.github.usefulness.maven-sympathy)
![Maven Central](https://img.shields.io/maven-central/v/io.github.usefulness/maven-sympathy)

Expand Down
11 changes: 11 additions & 0 deletions maven-sympathy/api/maven-sympathy.api
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
public final class io/github/usefulness/mavensympathy/BehaviorOnFailure : java/lang/Enum {
public static final field Fail Lio/github/usefulness/mavensympathy/BehaviorOnFailure;
public static final field Warn Lio/github/usefulness/mavensympathy/BehaviorOnFailure;
public static fun getEntries ()Lkotlin/enums/EnumEntries;
public static fun valueOf (Ljava/lang/String;)Lio/github/usefulness/mavensympathy/BehaviorOnFailure;
public static fun values ()[Lio/github/usefulness/mavensympathy/BehaviorOnFailure;
}

public final class io/github/usefulness/mavensympathy/MavenSympathyPlugin : org/gradle/api/Plugin {
public fun <init> ()V
public synthetic fun apply (Ljava/lang/Object;)V
Expand All @@ -6,7 +14,10 @@ public final class io/github/usefulness/mavensympathy/MavenSympathyPlugin : org/

public class io/github/usefulness/mavensympathy/SympathyForMrMavenTask : org/gradle/api/DefaultTask {
public fun <init> (Lorg/gradle/api/model/ObjectFactory;)V
public final fun behaviorOnFailure (Ljava/lang/String;)V
public final fun getBehaviorOnFailure ()Lorg/gradle/api/provider/Property;
public final fun getConfigurationWithDependencies ()Lorg/gradle/api/provider/MapProperty;
public final fun getExcludedConfigurations ()Lorg/gradle/api/provider/ListProperty;
public final fun getOutputFile ()Lorg/gradle/api/file/RegularFileProperty;
public final fun run ()V
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package io.github.usefulness.mavensympathy

public enum class BehaviorOnFailure {
Warn,
Fail,
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import org.gradle.api.artifacts.result.ResolvedComponentResult
import org.gradle.api.artifacts.result.ResolvedDependencyResult
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.model.ObjectFactory
import org.gradle.api.provider.ListProperty
import org.gradle.api.provider.MapProperty
import org.gradle.api.provider.Property
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.TaskAction
Expand All @@ -23,10 +25,24 @@ public open class SympathyForMrMavenTask @Inject constructor(objectFactory: Obje
@OutputFile
public val outputFile: RegularFileProperty = objectFactory.fileProperty()

@Input
public val excludedConfigurations: ListProperty<String> = objectFactory.listProperty(String::class.java)

@Input
public val behaviorOnFailure: Property<BehaviorOnFailure> = objectFactory.property(BehaviorOnFailure::class.java)
.value(BehaviorOnFailure.Fail)

public fun behaviorOnFailure(value: String) {
behaviorOnFailure.set(BehaviorOnFailure.entries.first { it.name.equals(value, ignoreCase = true) })
}

@TaskAction
public fun run() {
var fail = false
configurationWithDependencies.get().forEach { (name, root) ->
val errorMessages = mutableListOf<String>()
val excludedNames = excludedConfigurations.orElse(emptyList()).get()

configurationWithDependencies.get().filterNot { (name, _) -> name in excludedNames }.forEach { (name, root) ->
root.dependencies.asSequence()
.filterIsInstance<ResolvedDependencyResult>()
.filter { it.requested is ModuleComponentSelector }
Expand All @@ -36,15 +52,22 @@ public open class SympathyForMrMavenTask @Inject constructor(objectFactory: Obje
val requestedVersion = requested?.version
val selectedVersion = selected.moduleVersion?.version
if (!requestedVersion.isNullOrBlank() && requestedVersion != selectedVersion) {
logger.error("[$name] dependency $requested version changed $requestedVersion -> $selectedVersion")
val errorMessage = "[$name] dependency $requested version changed $requestedVersion -> $selectedVersion"
errorMessages.add(errorMessage)
logger.error(errorMessage)
fail = true
}
}
}
val report = outputFile.get().asFile
if (fail) {
report.writeText("NOT OK")
error("Declared dependencies were upgraded transitively. See task output above. Please update their versions.")
report.writeText(errorMessages.joinToString(separator = "\n"))
val failureMessage = "Declared dependencies were upgraded transitively. See task output above. Please update their versions."

when (behaviorOnFailure.get()) {
BehaviorOnFailure.Warn -> logger.error(failureMessage)
BehaviorOnFailure.Fail, null -> error(failureMessage)
}
} else {
report.writeText("OK")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,5 +113,80 @@ class IntegrationTest {
val result = runGradle(projectDir = rootDirectory, shouldFail = true)

assertThat(result.output).contains("com.squareup.okhttp3:okhttp:3.14.8 version changed 3.14.8 -> 3.14.9")
assertThat(rootDirectory.resolve("build/reports/sympathyForMrMaven/output.txt")).content()
.isEqualToIgnoringWhitespace(
"""
[compileClasspath] dependency com.squareup.okhttp3:okhttp:3.14.8 version changed 3.14.8 -> 3.14.9
[runtimeClasspath] dependency com.squareup.okhttp3:okhttp:3.14.8 version changed 3.14.8 -> 3.14.9
[testCompileClasspath] dependency com.squareup.okhttp3:okhttp:3.14.8 version changed 3.14.8 -> 3.14.9
[testRuntimeClasspath] dependency com.squareup.okhttp3:okhttp:3.14.8 version changed 3.14.8 -> 3.14.9
""".trimIndent(),
)
}

@Test
fun warningOnly() {
rootDirectory.resolve("build.gradle") {
// language=groovy
writeText(
"""
import io.github.usefulness.mavensympathy.BehaviorOnFailure
plugins {
id("java-library")
id("io.github.usefulness.maven-sympathy")
}
tasks.named("sympathyForMrMaven") {
behaviorOnFailure = BehaviorOnFailure.Warn
}
dependencies {
implementation("com.squareup.retrofit2:retrofit:2.11.0")
implementation("com.squareup.okhttp3:okhttp:3.14.8")
}
""".trimIndent(),
)
}
val result = runGradle(projectDir = rootDirectory)

assertThat(result.output).contains("com.squareup.okhttp3:okhttp:3.14.8 version changed 3.14.8 -> 3.14.9")
assertThat(rootDirectory.resolve("build/reports/sympathyForMrMaven/output.txt")).content()
.isEqualToIgnoringWhitespace(
"""
[compileClasspath] dependency com.squareup.okhttp3:okhttp:3.14.8 version changed 3.14.8 -> 3.14.9
[runtimeClasspath] dependency com.squareup.okhttp3:okhttp:3.14.8 version changed 3.14.8 -> 3.14.9
[testCompileClasspath] dependency com.squareup.okhttp3:okhttp:3.14.8 version changed 3.14.8 -> 3.14.9
[testRuntimeClasspath] dependency com.squareup.okhttp3:okhttp:3.14.8 version changed 3.14.8 -> 3.14.9
""".trimIndent(),
)
}

@Test
fun exclude() {
rootDirectory.resolve("build.gradle") {
// language=groovy
writeText(
"""
plugins {
id("java-library")
id("io.github.usefulness.maven-sympathy")
}
tasks.named("sympathyForMrMaven") {
excludedConfigurations = ["compileClasspath", "runtimeClasspath", "testCompileClasspath", "testRuntimeClasspath"]
behaviorOnFailure "fail"
}
dependencies {
implementation("com.squareup.retrofit2:retrofit:2.11.0")
implementation("com.squareup.okhttp3:okhttp:3.14.8")
}
""".trimIndent(),
)
}
val result = runGradle(projectDir = rootDirectory)

assertThat(result.output).doesNotContain("changed to")
}
}

0 comments on commit 6a7a73d

Please sign in to comment.