-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #75 from simonhauck/69-check-for-pre-release-versi…
…ons-before-the-release #69 add task to check for snapshot versions
- Loading branch information
Showing
10 changed files
with
661 additions
and
194 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
.../src/main/kotlin/io/github/simonhauck/release/tasks/CheckForPreReleaseDependenciesTask.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package io.github.simonhauck.release.tasks | ||
|
||
import org.gradle.api.GradleException | ||
import org.gradle.api.file.RegularFileProperty | ||
import org.gradle.api.logging.Logging | ||
import org.gradle.api.provider.ListProperty | ||
import org.gradle.api.provider.SetProperty | ||
import org.gradle.api.tasks.Input | ||
import org.gradle.api.tasks.InputFile | ||
import org.gradle.api.tasks.Optional | ||
import org.gradle.api.tasks.TaskAction | ||
|
||
abstract class CheckForPreReleaseDependenciesTask : BaseReleaseTask() { | ||
|
||
private val log = Logging.getLogger(CheckForPreReleaseDependenciesTask::class.java) | ||
|
||
@get:InputFile @get:Optional abstract val ignorePreReleaseDependenciesFile: RegularFileProperty | ||
@get:Input @get:Optional abstract val ignorePreReleaseDependencies: ListProperty<String> | ||
|
||
@get:Input abstract val usedDependencies: SetProperty<String> | ||
|
||
init { | ||
description = | ||
"Checks that no pre-release dependencies are used to ensure reproducible builds." | ||
outputs.upToDateWhen { true } | ||
} | ||
|
||
@TaskAction | ||
fun action() { | ||
val ignoredDependencies = combineIgnoredDependencies().toSet() | ||
|
||
val preReleaseDependencies = | ||
usedDependencies.get().filter { isPreReleaseVersion(it) }.sorted() | ||
|
||
val notAllowedDependencies = | ||
preReleaseDependencies.filterNot { it.startsWithAnyOf(ignoredDependencies) } | ||
|
||
if (notAllowedDependencies.isEmpty()) return | ||
|
||
val errorMessage = buildErrorMessage(notAllowedDependencies) | ||
throw GradleException(errorMessage) | ||
} | ||
|
||
private fun isPreReleaseVersion(it: String): Boolean { | ||
val split = it.split(":") | ||
if (split.size > 3) | ||
throw GradleException( | ||
"Something went wrong. Expected a dependency in form of group:name:version but got $it") | ||
|
||
if (split.size < 3) return false | ||
val version = split[2] | ||
|
||
val allChecks = | ||
listOf( | ||
version.contains("alpha", ignoreCase = true), | ||
version.contains("beta", ignoreCase = true), | ||
version.contains("beta", ignoreCase = true), | ||
version.contains("rc", ignoreCase = true), | ||
version.contains("pre", ignoreCase = true), | ||
version.contains("snapshot", ignoreCase = true), | ||
version.contains(".*[.-]M[0-9]+.*".toRegex(RegexOption.IGNORE_CASE)), | ||
) | ||
|
||
return allChecks.any { it } | ||
} | ||
|
||
private fun buildErrorMessage(notAllowedDependencies: List<String>): String { | ||
val header = | ||
"Found ${notAllowedDependencies.size} dependencies with a pre-release version that are not allowed\n" | ||
|
||
val list = notAllowedDependencies.joinToString("\n") { " - $it" } | ||
|
||
val hint = "\nChange the versions or add them to the ignore list." | ||
return header + list + hint | ||
} | ||
|
||
private fun String.startsWithAnyOf(list: Set<String>): Boolean { | ||
return list.any { this.startsWith(it) } | ||
} | ||
|
||
private fun combineIgnoredDependencies(): List<String> { | ||
val ignoreFromFile = | ||
ignorePreReleaseDependenciesFile.map { it.asFile.readLines() }.getOrElse(emptyList()) | ||
|
||
val ignoredFromList = ignorePreReleaseDependencies.getOrElse(emptyList()) | ||
|
||
return ignoreFromFile + ignoredFromList | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.