Skip to content

Commit 392da5f

Browse files
authored
Connect PostReleasePlugin with PublishingPlugin (#5165)
* Provide util method to better remap lines in a File * Use the new util method to make task more readable * Move post release plugin to publishing plugin
1 parent c4b5a92 commit 392da5f

File tree

4 files changed

+46
-6
lines changed

4 files changed

+46
-6
lines changed

buildSrc/src/main/java/com/google/firebase/gradle/plugins/FirebaseLibraryPlugin.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ class FirebaseLibraryPlugin : BaseFirebaseLibraryPlugin() {
5454

5555
project.apply<DackkaPlugin>()
5656
project.apply<GitSubmodulePlugin>()
57-
project.apply<PostReleasePlugin>()
5857
project.tasks.getByName("preBuild").dependsOn("updateGitSubmodules")
5958
}
6059

buildSrc/src/main/java/com/google/firebase/gradle/plugins/GradleUtils.kt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,38 @@ fun Provider<File>.childFile(path: String) = map { File("${it.path}/$path") }
5757
*/
5858
fun File.childFile(childPath: String) = File("$path/$childPath")
5959

60+
/**
61+
* Rewrites the lines of a file.
62+
*
63+
* The lines of the file are first read and then transformed by the provided `block` function. The
64+
* transformed lines are then joined together with a newline character and written back to the file.
65+
*
66+
* If the `terminateWithNewline` parameter is set to `false`, the file will not be terminated with a
67+
* newline character.
68+
*
69+
* @param terminateWithNewline Whether to terminate the file with a newline character. Defaults to
70+
* `true`.
71+
* @param block A function that takes a string as input and returns a new string. This function is
72+
* used to transform the lines of the file before they are rewritten.
73+
*
74+
* ```
75+
* val file = File("my-file.txt")
76+
*
77+
* // Rewrite the lines of the file, replacing all spaces with tabs.
78+
* file.rewriteLines { it.replace(" ", "\t") }
79+
*
80+
* // Rewrite the lines of the file, capitalizing the first letter of each word.
81+
* file.rewriteLines { it.capitalizeWords() }
82+
* ```
83+
*
84+
* @see [readLines]
85+
* @see [writeText]
86+
*/
87+
fun File.rewriteLines(terminateWithNewline: Boolean = true, block: (String) -> String) {
88+
val newLines = readLines().map(block)
89+
writeText(newLines.joinToString("\n").let { if (terminateWithNewline) it + "\n" else it })
90+
}
91+
6092
/**
6193
* Provides a temporary file for use during the task.
6294
*

buildSrc/src/main/java/com/google/firebase/gradle/plugins/PublishingPlugin.kt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import org.gradle.api.Project
3030
import org.gradle.api.publish.maven.tasks.PublishToMavenRepository
3131
import org.gradle.api.tasks.Copy
3232
import org.gradle.api.tasks.bundling.Zip
33+
import org.gradle.kotlin.dsl.apply
3334
import org.gradle.kotlin.dsl.named
3435
import org.gradle.kotlin.dsl.register
3536

@@ -60,6 +61,9 @@ import org.gradle.kotlin.dsl.register
6061
* - [PUBLISH_RELEASING_LIBS_TO_LOCAL_TASK][registerPublishReleasingLibrariesToMavenLocalTask]
6162
* - [SEMVER_CHECK_TASK][registerSemverCheckForReleaseTask]
6263
* - [PUBLISH_ALL_TO_BUILD_TASK][registerPublishAllToBuildDir]
64+
*
65+
* Additionally, this plugin registers the [PostReleasePlugin] via [registerPostReleasePlugin] for
66+
* each releasing library.
6367
*/
6468
abstract class PublishingPlugin : Plugin<Project> {
6569
override fun apply(project: Project) {
@@ -91,6 +95,7 @@ abstract class PublishingPlugin : Plugin<Project> {
9195
registerPublishReleasingLibrariesToMavenLocalTask(project, releasingProjects)
9296
registerSemverCheckForReleaseTask(project, releasingProjects)
9397
registerPublishAllToBuildDir(project, allFirebaseLibraries)
98+
registerPostReleasePlugin(releasingProjects)
9499

95100
val buildMavenZip =
96101
project.tasks.register<Zip>(BUILD_MAVEN_ZIP_TASK) {
@@ -444,6 +449,13 @@ abstract class PublishingPlugin : Plugin<Project> {
444449
}
445450
}
446451

452+
/** Registers the [PostReleasePlugin] to each releaing project. */
453+
private fun registerPostReleasePlugin(releasingProjects: List<Project>) {
454+
for (releasingProject in releasingProjects) {
455+
releasingProject.apply<PostReleasePlugin>()
456+
}
457+
}
458+
447459
companion object {
448460
const val RELEASE_CONFIG_FILE = "release.json"
449461
const val RELEASE_REPORT_MD_FILE = "release_report.md"
@@ -476,7 +488,6 @@ abstract class PublishingPlugin : Plugin<Project> {
476488
*
477489
* @property releasingLibraries A list of libraries that should be released
478490
* @property name The name of the release (such as `m123`)
479-
*
480491
* @see computeReleaseMetadata
481492
*/
482493
data class ReleaseMetadata(

buildSrc/src/main/java/com/google/firebase/gradle/plugins/VersionBumpTask.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,8 @@ abstract class VersionBumpTask : DefaultTask() {
4949

5050
@TaskAction
5151
fun build() {
52-
with(versionFile.get()) {
53-
readLines()
54-
.map { it.takeUnless { it.startsWith("version=") } ?: "version=${newVersion.get()}" }
55-
.also { writeText(it.joinToString("\n") + '\n') }
52+
versionFile.get().rewriteLines {
53+
if (it.startsWith("version=")) "version=${newVersion.get()}" else it
5654
}
5755
}
5856

0 commit comments

Comments
 (0)