|
| 1 | +package com.google.firebase.gradle.plugins |
| 2 | + |
| 3 | +import com.google.firebase.gradle.plugins.semver.VersionDelta |
| 4 | +import org.gradle.api.DefaultTask |
| 5 | +import org.gradle.api.GradleException |
| 6 | +import org.gradle.api.file.RegularFileProperty |
| 7 | +import org.gradle.api.provider.Property |
| 8 | +import org.gradle.api.tasks.Input |
| 9 | +import org.gradle.api.tasks.InputFile |
| 10 | +import org.gradle.api.tasks.OutputFile |
| 11 | +import org.gradle.api.tasks.TaskAction |
| 12 | +import java.io.ByteArrayOutputStream |
| 13 | + |
| 14 | +abstract class SemVerTask : DefaultTask() { |
| 15 | + @get:InputFile |
| 16 | + abstract val apiTxtFile: RegularFileProperty |
| 17 | + @get:InputFile |
| 18 | + abstract val otherApiFile: RegularFileProperty |
| 19 | + @get:Input |
| 20 | + abstract val currentVersionString: Property<String> |
| 21 | + @get:Input |
| 22 | + abstract val previousVersionString: Property<String> |
| 23 | + |
| 24 | + @get:OutputFile |
| 25 | + abstract val outputApiFile: RegularFileProperty |
| 26 | + |
| 27 | + @TaskAction |
| 28 | + fun run() { |
| 29 | + val regex = Regex("\\d+\\.\\d+\\.\\d.*") |
| 30 | + if (!previousVersionString.get().matches(regex) || !currentVersionString.get().matches(regex)) { |
| 31 | + return // If these variables don't exist, no reason to check API |
| 32 | + } |
| 33 | + val (previousMajor, previousMinor, previousPatch) = previousVersionString.get().split(".") |
| 34 | + val (currentMajor, currentMinor, currentPatch) = currentVersionString.get().split(".") |
| 35 | + val bump = if (previousMajor != currentMajor) VersionDelta.MAJOR else if (previousMinor != currentMinor) VersionDelta.MINOR else VersionDelta.PATCH |
| 36 | + val stream = ByteArrayOutputStream() |
| 37 | + project.runMetalavaWithArgs( |
| 38 | + listOf( |
| 39 | + "--source-files", |
| 40 | + apiTxtFile.get().asFile.absolutePath, |
| 41 | + "--check-compatibility:api:released", |
| 42 | + otherApiFile.get().asFile.absolutePath, |
| 43 | + ) |
| 44 | + + MAJOR.flatMap{ m -> listOf("--error", m) } |
| 45 | + + MINOR.flatMap{ m -> listOf("--error", m) } |
| 46 | + + IGNORED.flatMap{ m -> listOf("--hide", m) } |
| 47 | + + listOf( |
| 48 | + "--format=v3", |
| 49 | + "--no-color", |
| 50 | + ), |
| 51 | + ignoreFailure = true, |
| 52 | + stdOut = stream |
| 53 | + ) |
| 54 | + |
| 55 | + val string = String(stream.toByteArray()) |
| 56 | + val reg = Regex("(.*)\\s+error:\\s+(.*\\s+\\[(.*)\\])") |
| 57 | + val minorChanges = mutableListOf<String>() |
| 58 | + val majorChanges = mutableListOf<String>() |
| 59 | + for (match in reg.findAll(string)) { |
| 60 | + val loc = match.groups[1]!!.value |
| 61 | + val message = match.groups[2]!!.value |
| 62 | + val type = match.groups[3]!!.value |
| 63 | + if (IGNORED.contains(type)) { |
| 64 | + continue // Shouldn't be possible |
| 65 | + } else if (MINOR.contains(type)) { |
| 66 | + minorChanges.add(message) |
| 67 | + } else { |
| 68 | + majorChanges.add(message) |
| 69 | + } |
| 70 | + } |
| 71 | + val allChanges = |
| 72 | + (majorChanges |
| 73 | + .joinToString(separator = "") { m -> " MAJOR: $m\n" }) + |
| 74 | + minorChanges |
| 75 | + .joinToString(separator = "") { m -> " MINOR: $m\n" } |
| 76 | + if (majorChanges.isNotEmpty()) { |
| 77 | + if (bump != VersionDelta.MAJOR) { |
| 78 | + throw GradleException("API has non-bumped breaking MAJOR changes\nCurrent version bump is ${bump}, update the gradle.properties or fix the changes\n$allChanges") |
| 79 | + } |
| 80 | + } else if (minorChanges.isNotEmpty()) { |
| 81 | + if (bump != VersionDelta.MAJOR && bump != VersionDelta.MINOR) { |
| 82 | + throw GradleException("API has non-bumped MINOR changes\nCurrent version bump is ${bump}, update the gradle.properties or fix the changes\n$allChanges") |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + companion object { |
| 87 | + private val MAJOR = setOf("AddedFinal") |
| 88 | + private val MINOR = setOf("AddedClass", "AddedMethod", "AddedField", "ChangedDeprecated") |
| 89 | + private val IGNORED = setOf("ReferencesDeprecated") |
| 90 | + } |
| 91 | +} |
0 commit comments