From 8841d00ee1a4e8281256c567a3e501bfb1b551b6 Mon Sep 17 00:00:00 2001 From: Ilya Nekrasov Date: Mon, 11 Mar 2024 16:01:15 +0200 Subject: [PATCH 1/2] Add experimental logic to configure build tag pattern. But now it has restrictions: for example, in tag name should be only one - delimiter --- example/dimensions/build.gradle.kts | 5 +++ .../publish/plugin/BuildPublishPlugin.kt | 13 +++++-- .../plugin/command/GitCommandExecutor.kt | 11 +++--- .../android/build/publish/plugin/enity/Tag.kt | 2 ++ .../plugin/extension/config/OutputConfig.kt | 5 +++ .../task/changelog/GenerateChangelogTask.kt | 15 +++++--- .../task/changelog/git/ChangelogBuilder.kt | 8 +++-- .../task/changelog/git/GitRepository.kt | 36 +++++++++++-------- .../changelog/work/GenerateChangelogWork.kt | 7 ++-- .../publish/plugin/task/tag/GetLastTagTask.kt | 7 ++++ .../plugin/task/tag/work/GenerateTagWork.kt | 4 ++- 11 files changed, 79 insertions(+), 34 deletions(-) diff --git a/example/dimensions/build.gradle.kts b/example/dimensions/build.gradle.kts index 1bc9629..71073c6 100644 --- a/example/dimensions/build.gradle.kts +++ b/example/dimensions/build.gradle.kts @@ -45,6 +45,11 @@ buildPublish { register("default") { baseFileName.set("example-base-project-android") } + + register("x86MinApi21AlphaDebug") { + baseFileName.set("example-base-project-android") + buildTagPattern.set("cabinet\\+.+\\.(\\d+)-x86MinApi21AlphaDebug") + } } changelog { register("default") { diff --git a/plugin-build/plugin/src/main/java/ru/kode/android/build/publish/plugin/BuildPublishPlugin.kt b/plugin-build/plugin/src/main/java/ru/kode/android/build/publish/plugin/BuildPublishPlugin.kt index 9f4068f..c84ef3f 100644 --- a/plugin-build/plugin/src/main/java/ru/kode/android/build/publish/plugin/BuildPublishPlugin.kt +++ b/plugin-build/plugin/src/main/java/ru/kode/android/build/publish/plugin/BuildPublishPlugin.kt @@ -135,7 +135,12 @@ abstract class BuildPublishPlugin : Plugin { with(buildPublishExtension.output) { findByName(buildVariant.name) ?: getByName(DEFAULT_CONTAINER_NAME) } - val tagBuildProvider = registerGetLastTagTask(buildVariant, grgitService) + val tagBuildProvider = + registerGetLastTagTask( + buildVariant, + outputConfig.buildTagPattern, + grgitService, + ) val useVersionsFromTagProvider = outputConfig.useVersionsFromTag.orElse(true) val versionCodeProvider = useVersionsFromTagProvider.flatMap { useVersionsFromTag -> @@ -183,6 +188,7 @@ abstract class BuildPublishPlugin : Plugin { val generateChangelogFileProvider = tasks.registerGenerateChangelogTask( changelogConfig.commitMessageKey, + outputConfig.buildTagPattern, buildVariant, changelogFile, tagBuildProvider, @@ -333,6 +339,7 @@ abstract class BuildPublishPlugin : Plugin { private fun Project.registerGetLastTagTask( buildVariant: BuildVariant, + buildTagPattern: Provider, grgitService: Provider, ): Provider { val tagBuildFile = @@ -344,6 +351,7 @@ abstract class BuildPublishPlugin : Plugin { ) { task -> task.tagBuildFile.set(tagBuildFile) task.buildVariant.set(buildVariant.name) + task.buildTagPattern.set(buildTagPattern) task.getGrgitService().set(grgitService) }.flatMap { it.tagBuildFile } } @@ -362,6 +370,7 @@ abstract class BuildPublishPlugin : Plugin { private fun TaskContainer.registerGenerateChangelogTask( commitMessageKey: Provider, + buildTagPattern: Provider, buildVariant: BuildVariant, changelogFile: Provider, tagBuildProvider: Provider, @@ -372,7 +381,7 @@ abstract class BuildPublishPlugin : Plugin { GenerateChangelogTask::class.java, ) { it.commitMessageKey.set(commitMessageKey) - it.buildVariant.set(buildVariant.name) + it.buildTagPattern.set(buildTagPattern) it.changelogFile.set(changelogFile) it.tagBuildFile.set(tagBuildProvider) it.getGrgitService().set(grgitService) diff --git a/plugin-build/plugin/src/main/java/ru/kode/android/build/publish/plugin/command/GitCommandExecutor.kt b/plugin-build/plugin/src/main/java/ru/kode/android/build/publish/plugin/command/GitCommandExecutor.kt index 9cc6369..b5ad603 100644 --- a/plugin-build/plugin/src/main/java/ru/kode/android/build/publish/plugin/command/GitCommandExecutor.kt +++ b/plugin-build/plugin/src/main/java/ru/kode/android/build/publish/plugin/command/GitCommandExecutor.kt @@ -9,19 +9,19 @@ internal class GitCommandExecutor( private val grgitService: GrgitService, ) { /** - * Extracts lines which contain [tag] from all commit messages in commit range (inclusive). + * Extracts lines which contain [key] from all commit messages in commit range (inclusive). * For example calling this function with arguments `"CHANGELOG", CommitRange("someSHA1", "someSHA2")` * will run `git log` command and extract lines containing "CHANGELOG" from each commit in that range. */ - fun extractTagFromCommitMessages( - tag: String, + fun extractMarkedCommitMessages( + key: String, range: CommitRange?, ): List { return getCommitsByRange(range) .flatMap { commit -> commit.fullMessage .split('\n') - .filter { message -> message.contains(tag) } + .filter { message -> message.contains(key) } } } @@ -30,10 +30,9 @@ internal class GitCommandExecutor( * Resulting set will be limited to [limitResultCount] tags or `null` if no tags found */ fun findBuildTags( - buildVariant: String, + buildTagRegex: Regex, limitResultCount: Int, ): List? { - val buildTagRegex = Regex(".+\\.(\\d+)-$buildVariant") return grgitService.grgit.tag.list() .filter { tag -> tag.name.matches(buildTagRegex) } .sortedBy { tag -> diff --git a/plugin-build/plugin/src/main/java/ru/kode/android/build/publish/plugin/enity/Tag.kt b/plugin-build/plugin/src/main/java/ru/kode/android/build/publish/plugin/enity/Tag.kt index 5d943cf..593bbb6 100644 --- a/plugin-build/plugin/src/main/java/ru/kode/android/build/publish/plugin/enity/Tag.kt +++ b/plugin-build/plugin/src/main/java/ru/kode/android/build/publish/plugin/enity/Tag.kt @@ -46,12 +46,14 @@ private fun Tag.toBuildVariant(buildVariant: String): String { } } +// TODO: Add logic to configure logic to extract build version outside private fun Tag.toBuildVersion(): String { val tagFirstPart = name.split("-").first() val numbers = Regex("\\d+").findAll(tagFirstPart).toList() return numbers.dropLast(1).joinToString(separator = ".") { it.value } } +// TODO: Add logic to configure logic to extract build number outside private fun Tag.toBuildNumber(): Int { val tagFirstPart = name.split("-").first() return Regex("\\d+").findAll(tagFirstPart).last().value.toInt() diff --git a/plugin-build/plugin/src/main/java/ru/kode/android/build/publish/plugin/extension/config/OutputConfig.kt b/plugin-build/plugin/src/main/java/ru/kode/android/build/publish/plugin/extension/config/OutputConfig.kt index 43bd0aa..05ffc06 100644 --- a/plugin-build/plugin/src/main/java/ru/kode/android/build/publish/plugin/extension/config/OutputConfig.kt +++ b/plugin-build/plugin/src/main/java/ru/kode/android/build/publish/plugin/extension/config/OutputConfig.kt @@ -2,6 +2,7 @@ package ru.kode.android.build.publish.plugin.extension.config import org.gradle.api.provider.Property import org.gradle.api.tasks.Input +import org.gradle.api.tasks.Optional interface OutputConfig { val name: String @@ -25,4 +26,8 @@ interface OutputConfig { */ @get:Input val useVersionsFromTag: Property + + @get:Input + @get:Optional + val buildTagPattern: Property } diff --git a/plugin-build/plugin/src/main/java/ru/kode/android/build/publish/plugin/task/changelog/GenerateChangelogTask.kt b/plugin-build/plugin/src/main/java/ru/kode/android/build/publish/plugin/task/changelog/GenerateChangelogTask.kt index b6b7985..c10da08 100644 --- a/plugin-build/plugin/src/main/java/ru/kode/android/build/publish/plugin/task/changelog/GenerateChangelogTask.kt +++ b/plugin-build/plugin/src/main/java/ru/kode/android/build/publish/plugin/task/changelog/GenerateChangelogTask.kt @@ -9,6 +9,7 @@ import org.gradle.api.provider.Property import org.gradle.api.tasks.Input import org.gradle.api.tasks.InputFile import org.gradle.api.tasks.Internal +import org.gradle.api.tasks.Optional import org.gradle.api.tasks.OutputFile import org.gradle.api.tasks.TaskAction import org.gradle.api.tasks.options.Option @@ -43,10 +44,6 @@ abstract class GenerateChangelogTask @get:Option(option = "tagBuildFile", description = "Json contains info about tag build") abstract val tagBuildFile: RegularFileProperty - @get:Input - @get:Option(option = "buildVariant", description = "Current build variant") - abstract val buildVariant: Property - @get:Input @get:Option( option = "commitMessageKey", @@ -54,6 +51,14 @@ abstract class GenerateChangelogTask ) abstract val commitMessageKey: Property + @get:Input + @get:Option( + option = "buildTagPattern", + description = "Tag pattern to correctly search related tags", + ) + @get:Optional + abstract val buildTagPattern: Property + @get:OutputFile @get:Option( option = "changelogFile", @@ -66,7 +71,7 @@ abstract class GenerateChangelogTask val workQueue: WorkQueue = workerExecutor.noIsolation() workQueue.submit(GenerateChangelogWork::class.java) { parameters -> parameters.commitMessageKey.set(commitMessageKey) - parameters.buildVariant.set(buildVariant) + parameters.buildTagPattern.set(buildTagPattern) parameters.tagBuildFile.set(tagBuildFile) parameters.changelogFile.set(changelogFile) parameters.grgitService.set(grgitService) diff --git a/plugin-build/plugin/src/main/java/ru/kode/android/build/publish/plugin/task/changelog/git/ChangelogBuilder.kt b/plugin-build/plugin/src/main/java/ru/kode/android/build/publish/plugin/task/changelog/git/ChangelogBuilder.kt index a204ce5..0616499 100644 --- a/plugin-build/plugin/src/main/java/ru/kode/android/build/publish/plugin/task/changelog/git/ChangelogBuilder.kt +++ b/plugin-build/plugin/src/main/java/ru/kode/android/build/publish/plugin/task/changelog/git/ChangelogBuilder.kt @@ -14,18 +14,20 @@ internal class ChangelogBuilder( @Suppress("ReturnCount") fun buildForBuildTag( buildTag: Tag.Build, + buildTagPattern: String?, defaultValueSupplier: ((TagRange) -> String?)? = null, ): String? { val buildVariant = buildTag.buildVariant - return buildForBuildVariant(buildVariant, defaultValueSupplier) + return buildForBuildVariant(buildVariant, buildTagPattern, defaultValueSupplier) } private fun buildForBuildVariant( buildVariant: String, + buildTagPattern: String?, defaultValueSupplier: ((TagRange) -> String?)? = null, ): String? { val tagRange = - gitRepository.findTagRange(buildVariant) + gitRepository.findTagRange(buildVariant, buildTagPattern) .also { if (it == null) logger?.warn("failed to build a changelog: no build tags") } ?: return null return tagRange.buildChangelog() ?: defaultValueSupplier?.invoke(tagRange) @@ -44,7 +46,7 @@ internal class ChangelogBuilder( // (but remember, tags can be annotated - which is taken care of above) if (this.currentBuildTag.commitSha != this.previousBuildTag?.commitSha) { gitCommandExecutor - .extractTagFromCommitMessages(messageKey, this.asCommitRange()) + .extractMarkedCommitMessages(messageKey, this.asCommitRange()) .map { it.replace(Regex("\\s*$messageKey:?\\s*"), "• ") } .forEach { messageBuilder.appendLine(it) diff --git a/plugin-build/plugin/src/main/java/ru/kode/android/build/publish/plugin/task/changelog/git/GitRepository.kt b/plugin-build/plugin/src/main/java/ru/kode/android/build/publish/plugin/task/changelog/git/GitRepository.kt index 4d31f38..4ddd54b 100644 --- a/plugin-build/plugin/src/main/java/ru/kode/android/build/publish/plugin/task/changelog/git/GitRepository.kt +++ b/plugin-build/plugin/src/main/java/ru/kode/android/build/publish/plugin/task/changelog/git/GitRepository.kt @@ -6,26 +6,34 @@ import ru.kode.android.build.publish.plugin.enity.TagRange internal class GitRepository( private val gitCommandExecutor: GitCommandExecutor, - private val buildVariant: String, ) { /** * Finds a range of build tags, returning `null` if no build tags are present (happens on a new projects) */ - fun findTagRange(buildVariant: String): TagRange? { - val tags = gitCommandExecutor.findBuildTags(buildVariant, limitResultCount = 2) - return tags?.let { - val currentBuildTag = it.first() - val previousBuildTag = it.getOrNull(1) - TagRange( - // todo add Build types - currentBuildTag = Tag.Build(currentBuildTag, buildVariant), - previousBuildTag = previousBuildTag?.let { Tag.Build(it, buildVariant) }, - ) - } + fun findTagRange( + buildVariant: String, + buildTagPattern: String?, + ): TagRange? { + val buildTagRegex = Regex(buildTagPattern ?: DEFAULT_TAG_PATTERN.format(buildVariant)) + return gitCommandExecutor.findBuildTags(buildTagRegex, limitResultCount = 2) + ?.let { tags -> + val currentBuildTag = tags.first() + val previousBuildTag = tags.getOrNull(1) + TagRange( + currentBuildTag = Tag.Build(currentBuildTag, buildVariant), + previousBuildTag = previousBuildTag?.let { Tag.Build(it, buildVariant) }, + ) + } } - fun findRecentBuildTag(): Tag.Build? { - val tags = gitCommandExecutor.findBuildTags(buildVariant, limitResultCount = 1) + fun findRecentBuildTag( + buildVariant: String, + buildTagPattern: String?, + ): Tag.Build? { + val buildTagRegex = Regex(buildTagPattern ?: DEFAULT_TAG_PATTERN.format(buildVariant)) + val tags = gitCommandExecutor.findBuildTags(buildTagRegex, limitResultCount = 1) return tags?.first()?.let { Tag.Build(it, buildVariant) } } } + +private const val DEFAULT_TAG_PATTERN = ".+\\.(\\d+)-%s" diff --git a/plugin-build/plugin/src/main/java/ru/kode/android/build/publish/plugin/task/changelog/work/GenerateChangelogWork.kt b/plugin-build/plugin/src/main/java/ru/kode/android/build/publish/plugin/task/changelog/work/GenerateChangelogWork.kt index 4f30a83..62b4b16 100644 --- a/plugin-build/plugin/src/main/java/ru/kode/android/build/publish/plugin/task/changelog/work/GenerateChangelogWork.kt +++ b/plugin-build/plugin/src/main/java/ru/kode/android/build/publish/plugin/task/changelog/work/GenerateChangelogWork.kt @@ -14,7 +14,7 @@ import javax.inject.Inject interface GenerateChangelogParameters : WorkParameters { val commitMessageKey: Property - val buildVariant: Property + val buildTagPattern: Property val tagBuildFile: RegularFileProperty val changelogFile: RegularFileProperty val grgitService: Property @@ -27,14 +27,15 @@ abstract class GenerateChangelogWork override fun execute() { val messageKey = parameters.commitMessageKey.get() + val buildTagPattern = parameters.buildTagPattern.orNull val currentBuildTag = fromJson(parameters.tagBuildFile.asFile.get()) - val buildVariant = parameters.buildVariant.get() val gitCommandExecutor = GitCommandExecutor(parameters.grgitService.get()) - val gitRepository = GitRepository(gitCommandExecutor, buildVariant) + val gitRepository = GitRepository(gitCommandExecutor) val changelog = ChangelogBuilder(gitRepository, gitCommandExecutor, logger, messageKey) .buildForBuildTag( currentBuildTag, + buildTagPattern, defaultValueSupplier = { tagRange -> val previousBuildName = tagRange.previousBuildTag?.name?.let { "($it)" } "No changes compared to the previous build $previousBuildName" diff --git a/plugin-build/plugin/src/main/java/ru/kode/android/build/publish/plugin/task/tag/GetLastTagTask.kt b/plugin-build/plugin/src/main/java/ru/kode/android/build/publish/plugin/task/tag/GetLastTagTask.kt index a5f7944..6fb273f 100644 --- a/plugin-build/plugin/src/main/java/ru/kode/android/build/publish/plugin/task/tag/GetLastTagTask.kt +++ b/plugin-build/plugin/src/main/java/ru/kode/android/build/publish/plugin/task/tag/GetLastTagTask.kt @@ -8,6 +8,7 @@ import org.gradle.api.plugins.BasePlugin import org.gradle.api.provider.Property import org.gradle.api.tasks.Input import org.gradle.api.tasks.Internal +import org.gradle.api.tasks.Optional import org.gradle.api.tasks.OutputFile import org.gradle.api.tasks.TaskAction import org.gradle.api.tasks.options.Option @@ -37,6 +38,11 @@ abstract class GetLastTagTask @get:Option(option = "buildVariant", description = "Current build variant") abstract val buildVariant: Property + @get:Input + @get:Option(option = "buildTagPattern", description = "Tag pattern to correctly search related tags") + @get:Optional + abstract val buildTagPattern: Property + @get:OutputFile @get:Option(option = "tagBuildFile", description = "Json contains info about tag build") abstract val tagBuildFile: RegularFileProperty @@ -47,6 +53,7 @@ abstract class GetLastTagTask workQueue.submit(GenerateTagWork::class.java) { parameters -> parameters.tagBuildFile.set(tagBuildFile) parameters.buildVariant.set(buildVariant) + parameters.buildTagPattern.set(buildTagPattern) parameters.grgitService.set(grgitService) } workQueue.await() diff --git a/plugin-build/plugin/src/main/java/ru/kode/android/build/publish/plugin/task/tag/work/GenerateTagWork.kt b/plugin-build/plugin/src/main/java/ru/kode/android/build/publish/plugin/task/tag/work/GenerateTagWork.kt index e5bd576..29c7d89 100644 --- a/plugin-build/plugin/src/main/java/ru/kode/android/build/publish/plugin/task/tag/work/GenerateTagWork.kt +++ b/plugin-build/plugin/src/main/java/ru/kode/android/build/publish/plugin/task/tag/work/GenerateTagWork.kt @@ -13,6 +13,7 @@ import javax.inject.Inject interface GenerateTagParameters : WorkParameters { val buildVariant: Property + val buildTagPattern: Property val tagBuildFile: RegularFileProperty val grgitService: Property } @@ -24,8 +25,9 @@ abstract class GenerateTagWork override fun execute() { val buildVariant = parameters.buildVariant.get() + val buildTagPattern = parameters.buildTagPattern.orNull val gitCommandExecutor = GitCommandExecutor(parameters.grgitService.get()) - val buildTag = GitRepository(gitCommandExecutor, buildVariant).findRecentBuildTag() + val buildTag = GitRepository(gitCommandExecutor).findRecentBuildTag(buildVariant, buildTagPattern) val tagBuildOutput = parameters.tagBuildFile.asFile.get() if (buildTag != null) { From bf612b1fc590f5647d192608edaa5260199e3b74 Mon Sep 17 00:00:00 2001 From: Ilya Nekrasov Date: Mon, 11 Mar 2024 16:46:20 +0200 Subject: [PATCH 2/2] Increase version to 1.3.0-alpha01 --- CHANGELOG.md | 3 +++ plugin-build/build.gradle.kts | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5099378..a254e7e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Changelog +## 1.3.0-alpha01 +* Add experimental logic to configure build tag pattern (buildTagPattern). But now it has restrictions: for example, in tag name should be only one - delimiter + ## 1.2.0 * Hide firebase app distribution plugin inside, and add logic to apply it only if required * Add `useVersionsFromTag` property inside OutputConfig to disable logic when versions and file names is applied from tag diff --git a/plugin-build/build.gradle.kts b/plugin-build/build.gradle.kts index 5592cb2..f567fdc 100644 --- a/plugin-build/build.gradle.kts +++ b/plugin-build/build.gradle.kts @@ -7,7 +7,7 @@ plugins { allprojects { group = "ru.kode.android" - version = "1.2.0" + version = "1.3.0-alpha01" } val dependsOnRecursivelyByName = { task: Task, name: String ->