Skip to content

Commit

Permalink
Use version constraints for openrewrite and openrewrite-remote Py…
Browse files Browse the repository at this point in the history
…thon packages

The goal is to ensure that only compatible versions (with incremented patch version) get installed.
  • Loading branch information
knutwannheden committed Sep 4, 2024
1 parent f221ce1 commit a01e9b5
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,41 @@ tasks.register("appendOpenRewriteRequirements") {
dependsOn("exportPoetryRequirements")
doLast {
val file = requirementsFile.get().asFile
file.appendText("cbor2\nopenrewrite\nopenrewrite-remote\n")
file.appendText("cbor2\n")
file.appendText("openrewrite${generatePipVersionConstraint(project.version.toString())}\n")
file.appendText("openrewrite-remote${generatePipVersionConstraint(getDirectDependencyVersion("org.openrewrite:rewrite-remote-java"))}\n")
}
}

tasks.named("processResources") {
dependsOn("appendOpenRewriteRequirements")
}

fun generatePipVersionConstraint(version: String): String {
if (version.endsWith("-SNAPSHOT")) {
return "<${version.replace("-SNAPSHOT", "")}"
}
val versionParts = version.split(".")
if (versionParts.size != 3) {
throw IllegalArgumentException("Invalid version format: $version")
}

val major = versionParts[0]
val minor = versionParts[1].toInt()
val patch = versionParts[2]

val nextMinor = minor + 1

return ">=${major}.${minor}.${patch},<${major}.${nextMinor}.0"
}

fun Task.getDirectDependencyVersion(dependencyName: String): String {
val compileClasspathConfig = project.configurations.getByName("compileClasspath")
var version = ""
compileClasspathConfig.resolvedConfiguration.firstLevelModuleDependencies.forEach { dependency ->
if ((dependency.module.id.group + ':' + dependency.module.id.name) == dependencyName) {
version = dependency.module.id.version
}
}
return version
}

0 comments on commit a01e9b5

Please sign in to comment.