Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapt version in beforeReleaseBuild #350

Open
christiangroth opened this issue Jun 24, 2021 · 1 comment
Open

Adapt version in beforeReleaseBuild #350

christiangroth opened this issue Jun 24, 2021 · 1 comment

Comments

@christiangroth
Copy link

Hey guys,

first of all thanks for the great plugin, so far I solved a lot of scenarios using it! :)

Right now I'm trying to derive the new project version depending on our release notes. What I want to achieve is the following:

  • If a breaking release note snippet exists, inc project version by major bump before release
  • If a feature release note snippet exists, inc project version by minor bump before release

So what I did was quite simple:

  • added a new task updateProjectVersion implementing the logic described above. Setting the new project version means two things though:
    • update new version in gradle.properties
    • set project.version = newVersion
  • set beforeReleaseBuild.dependsOn(updateProjectVersion)
  • of course you need to set failOnCommitNeeded = false due to changes to gradle.properties by updateProjectVersion

What I see is that the tast is executed correctly and gradle.properties file is also correct and committed correctly. But lets look at an example with current version 0.1.1-SNAPSHOT and creating a feature release.

  • pre tag commit is [Gradle Release Plugin] - pre tag commit: '0.1.1' and contains changes in gradle.properties to version=0.2.0
  • new version commit is [Gradle Release Plugin] - new version commit: '0.1.2-SNAPSHOT' and contains changes in gradle.properties to version=0.1.2-SNAPSHOT

So I assume that the release plugin does not get the updated gradle project version. Any ideas/hints/help on this? I did not quite find the code pointer in the release plugin for a possible fix. I would have assumed that setting the project.version value before the release task will be reflected during the release task.

@christiangroth
Copy link
Author

for the sake of completeness I'll also provide the sources of my task. It is included in a custom plugin, so not all private methods may be available, but hopefully all you need in this case.

    private fun Project.registerUpdateProjectVersion() {
        tasks.register(updateProjectVersion) {
            it.group = releasenotesGroupName

            it.doLast {
                val extensions = listOf(extension.de, extension.en)
                val updateNotices = extensions.flatMap { extension ->
                    extension.collectSnippets(project.projectDir, ReleasenoteSnippetType.UPDATENOTICE)
                }
                if (updateNotices.isNotEmpty()) {
                    logger.info("detected update notice release notes snippets, configuring major version bump")
                    project.updateVersion(project.versionAsSemver().incMajor())
                    return@doLast
                }

                val features = extensions.flatMap { extension ->
                    extension.collectSnippets(project.projectDir, ReleasenoteSnippetType.FEATURE)
                }
                if (features.isNotEmpty()) {
                    logger.info("detected feature release notes snippets, configuring minor version bump")
                    project.updateVersion(project.versionAsSemver().incMinor())
                    return@doLast
                }

                // we do not need to do anything for bugfixes / patch level changes
                // (we also did never commit changes to gradle.properties in these cases)
                logger.info("all existing release note snippets (if any) lead to patch level bump only")
            }
        }.let { updateProjectVersionTask ->
            afterEvaluate {
                tasks.findByPath(beforeReleaseBuildTaskName)?.let { beforeReleaseBuildTask ->
                    logger.info("Task '${beforeReleaseBuildTask.path}' found, will depend on ${updateProjectVersionTask.name}")
                    beforeReleaseBuildTask.dependsOn(updateProjectVersionTask)
                }
            }
        }
    }

    private fun Project.versionAsSemver() = Semver(project.version.toString())

    private fun Project.updateVersion(newVersion: Semver) {
        project.version = newVersion

        val properties = Properties()
        properties.load(project.propertiesFile().inputStream())
        properties.setProperty("version", newVersion.toString())
        properties.store(project.propertiesFile().outputStream(), null);
    }

    private fun Project.propertiesFile() = rootDir.resolve("gradle.properties")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant