|
| 1 | +// Copyright 2023 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package com.google.firebase.gradle.plugins |
| 16 | + |
| 17 | +import java.io.File |
| 18 | +import org.gradle.api.DefaultTask |
| 19 | +import org.gradle.api.provider.Property |
| 20 | +import org.gradle.api.tasks.InputFile |
| 21 | +import org.gradle.api.tasks.OutputFile |
| 22 | +import org.gradle.api.tasks.StopExecutionException |
| 23 | +import org.gradle.api.tasks.TaskAction |
| 24 | + |
| 25 | +/** |
| 26 | + * Changes the project level dependencies in a project's build file to pinned dependencies. |
| 27 | + * |
| 28 | + * Computes project level dependencies outside of the project's library group, looks for instances |
| 29 | + * of them in the `dependencies` block in the build file, and replaces them with pinned versions. |
| 30 | + * GMaven is used to figure out the latest version to use for a given dependency. |
| 31 | + * |
| 32 | + * For example, given the following input: |
| 33 | + * ``` |
| 34 | + * dependencies { |
| 35 | + * implementation(project(":appcheck:firebase-appcheck-interop")) |
| 36 | + * } |
| 37 | + * ``` |
| 38 | + * |
| 39 | + * The output would be: |
| 40 | + * ``` |
| 41 | + * dependencies { |
| 42 | + * implementation("com.google.firebase:firebase-appcheck-interop:17.0.1") |
| 43 | + * } |
| 44 | + * ``` |
| 45 | + * |
| 46 | + * *Assuming that `17.0.1` is the latest version of `firebase-appcheck-interop`* |
| 47 | + * |
| 48 | + * @property buildFile A [File] that should be used as the source to update from. Typically the |
| 49 | + * `build.gradle` or `build.gradle.kts` file for a given project. |
| 50 | + * @property outputFile A [File] that should be used to write the new text to. Typically the same as |
| 51 | + * the input file ([buildFile]). |
| 52 | + * @see PostReleasePlugin |
| 53 | + */ |
| 54 | +abstract class UpdatePinnedDependenciesTask : DefaultTask() { |
| 55 | + @get:[InputFile] |
| 56 | + abstract val buildFile: Property<File> |
| 57 | + |
| 58 | + @get:[OutputFile] |
| 59 | + abstract val outputFile: Property<File> |
| 60 | + |
| 61 | + @TaskAction |
| 62 | + fun updateBuildFileDependencies() { |
| 63 | + val dependenciesToChange = findProjectLevelDependenciesToChange() |
| 64 | + |
| 65 | + if (dependenciesToChange.isEmpty()) throw StopExecutionException("No libraries to change.") |
| 66 | + |
| 67 | + val buildFileContent = buildFile.get().readLines() |
| 68 | + val updatedContent = replaceProjectLevelDependencies(buildFileContent, dependenciesToChange) |
| 69 | + |
| 70 | + validateDependenciesHaveChanged(dependenciesToChange, buildFileContent, updatedContent) |
| 71 | + |
| 72 | + outputFile.get().writeText(updatedContent.joinToString("\n") + "\n") |
| 73 | + } |
| 74 | + |
| 75 | + private fun validateDependenciesHaveChanged( |
| 76 | + dependenciesToChange: List<FirebaseLibraryExtension>, |
| 77 | + oldContent: List<String>, |
| 78 | + updatedContent: List<String> |
| 79 | + ) { |
| 80 | + if (oldContent == updatedContent) |
| 81 | + throw RuntimeException( |
| 82 | + "Expected the following project level dependencies, but found none: " + |
| 83 | + "${dependenciesToChange.joinToString("\n") { it.mavenName }}" |
| 84 | + ) |
| 85 | + |
| 86 | + val diff = oldContent.diff(updatedContent) |
| 87 | + val changedLines = diff.mapNotNull { it.first ?: it.second } |
| 88 | + |
| 89 | + val (librariesCorrectlyChanged, linesChangedIncorrectly) = |
| 90 | + dependenciesToChange.partition { lib -> changedLines.any { it.contains(lib.path) } } |
| 91 | + |
| 92 | + val librariesNotChanged = dependenciesToChange - librariesCorrectlyChanged |
| 93 | + |
| 94 | + if (linesChangedIncorrectly.isNotEmpty()) |
| 95 | + throw RuntimeException( |
| 96 | + "The following lines were caught by our REGEX, but should not have been:\n ${linesChangedIncorrectly.joinToString("\n")}" |
| 97 | + ) |
| 98 | + |
| 99 | + if (librariesNotChanged.isNotEmpty()) |
| 100 | + throw RuntimeException( |
| 101 | + "The following libraries were not found, but should have been:\n ${librariesNotChanged.joinToString("\n") { it.mavenName }}" |
| 102 | + ) |
| 103 | + |
| 104 | + if (librariesCorrectlyChanged.size > dependenciesToChange.size) |
| 105 | + throw RuntimeException( |
| 106 | + "Too many libraries were caught by our change, possible REGEX false positive:\n ${changedLines.joinToString("\n")}" |
| 107 | + ) |
| 108 | + } |
| 109 | + |
| 110 | + private fun findProjectLevelDependenciesToChange(): List<FirebaseLibraryExtension> { |
| 111 | + val firebaseLibrary = project.firebaseLibrary |
| 112 | + |
| 113 | + return firebaseLibrary.projectLevelDependencies - firebaseLibrary.librariesToRelease |
| 114 | + } |
| 115 | + |
| 116 | + private val FirebaseLibraryExtension.projectLevelDependencies: List<FirebaseLibraryExtension> |
| 117 | + get() = resolveProjectLevelDependencies().filterNot { it.path in DEPENDENCIES_TO_IGNORE } |
| 118 | + |
| 119 | + private fun replaceProjectLevelDependencies( |
| 120 | + buildFileContent: List<String>, |
| 121 | + libraries: List<FirebaseLibraryExtension> |
| 122 | + ) = |
| 123 | + buildFileContent.replaceMatches(DEPENDENCY_REGEX) { |
| 124 | + val projectName = it.firstCapturedValue |
| 125 | + val projectToChange = libraries.find { it.path == projectName } |
| 126 | + val latestVersion = projectToChange?.latestVersion |
| 127 | + |
| 128 | + latestVersion?.let { "\"${projectToChange.mavenName}:$latestVersion\"" } |
| 129 | + } |
| 130 | + |
| 131 | + companion object { |
| 132 | + /** |
| 133 | + * Regex used in finding project level dependencies and their respective project. |
| 134 | + * |
| 135 | + * The regex can be broken down as such: |
| 136 | + * |
| 137 | + * `(N whitespace)(N letters)(a space or bracket)project((a single or double quote): (anything |
| 138 | + * besides whitespace)(a single or double quote))` |
| 139 | + * |
| 140 | + * For example, given the following input: |
| 141 | + * ``` |
| 142 | + * dependencies { |
| 143 | + * implementation 'com.google.firebase:firebase-annotations:16.2.0' |
| 144 | + * implementation 'com.google.firebase:firebase-common:20.3.1' |
| 145 | + * implementation project(':firebase-installations') |
| 146 | + * implementation 'com.google.firebase:firebase-database-collection:18.0.1' |
| 147 | + * ``` |
| 148 | + * |
| 149 | + * The following group would be captured: |
| 150 | + * ``` |
| 151 | + * :firebase-installations |
| 152 | + * ``` |
| 153 | + */ |
| 154 | + val DEPENDENCY_REGEX = |
| 155 | + Regex("(?<=\\s{1,20}\\w{1,20}(?:\\s|\\())project\\((?:'|\")(:\\S+)(?:'|\")\\)") |
| 156 | + |
| 157 | + val DEPENDENCIES_TO_IGNORE = listOf(":protolite-well-known-types") |
| 158 | + } |
| 159 | +} |
0 commit comments