Skip to content

Commit

Permalink
move to the cli command
Browse files Browse the repository at this point in the history
  • Loading branch information
martinbonnin committed Jul 15, 2024
1 parent 06c9461 commit 229760f
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,57 @@ package com.gradleup.librarian.cli.command
import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.parameters.arguments.argument
import com.github.ajalt.clikt.parameters.arguments.optional
import com.github.ajalt.clikt.parameters.options.flag
import com.github.ajalt.clikt.parameters.options.option
import com.github.kinquirer.KInquirer
import com.github.kinquirer.components.promptConfirm
import com.gradleup.librarian.core.tooling.BumpMajor
import com.gradleup.librarian.core.tooling.DowngradeVersion
import com.gradleup.librarian.core.tooling.UseDefaultVersion
import com.gradleup.librarian.core.tooling.getCurrentVersion
import com.gradleup.librarian.core.tooling.tagAndBump
import com.gradleup.librarian.core.tooling.toVersionOrNull
import kotlin.system.exitProcess

internal class TagAndBump: CliktCommand() {
val versionToRelease by argument().optional()
val patch by option().flag()
val minor by option().flag()

private fun confirmOrExit(message: String) {
if (!KInquirer.promptConfirm(message, true)) {
exitProcess(1)
}
}

override fun run() {
tagAndBump(versionToRelease) {
val ret = when(it) {
is BumpMajor -> KInquirer.promptConfirm("Bump major version to '${it.tagVersion}' and bump? (current version is ${it.currentVersion})", true)
is DowngradeVersion -> KInquirer.promptConfirm("Downgrade version to '${it.tagVersion}' and bump? (current version is ${it.currentVersion})", true)
is UseDefaultVersion -> KInquirer.promptConfirm("Tag version '${it.expectedVersion}' and bump?", true)
}

if (!ret) {
exitProcess(1)
}
val currentVersion = getCurrentVersion()
val currentVersionParsed = currentVersion.toVersionOrNull()
check(currentVersionParsed != null) {
"Cannot parse current version: '${currentVersion}'"
}
check(currentVersionParsed.isSnapshot) {
"Version '${currentVersion} is not a -SNAPSHOT, check your working directory"
}

val expectedVersion = currentVersion.removeSuffix("-SNAPSHOT")

var versionToRelease = this.versionToRelease
if (versionToRelease == null) {
confirmOrExit("Tag version '${expectedVersion}' and bump?")
versionToRelease = expectedVersion
}

val tagVersionParsed = versionToRelease.toVersionOrNull()
check(tagVersionParsed != null) {
"Version must start with 'major.minor.patch' (found '$versionToRelease')"
}
if (tagVersionParsed < currentVersionParsed) {
confirmOrExit("Downgrade version to '${versionToRelease}' and bump? (current version is ${currentVersion})")
} else if (tagVersionParsed.major > currentVersionParsed.major) {
confirmOrExit("Bump major version to '${versionToRelease}' and bump? (current version is ${currentVersion})")
}

tagAndBump(versionToRelease)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,10 @@ internal fun getNextSnapshot(version: String): String {
return getNextPatch(version) + "-SNAPSHOT"
}

internal class PreRelease(
class PreRelease(
val name: String,
val version: Int,
) {

}
)

internal fun PreRelease?.compareTo(other: PreRelease?): Int {
return if (this == null && other == null) {
Expand All @@ -67,7 +65,7 @@ internal fun PreRelease?.compareTo(other: PreRelease?): Int {
}
}

internal class Version(
class Version(
val major: Int,
val minor: Int,
val patch: Int,
Expand All @@ -94,7 +92,7 @@ internal class Version(
}
}

internal fun String.toVersionOrNull(): Version? {
fun String.toVersionOrNull(): Version? {
val regex1 = Regex("([0-9]+)\\.([0-9]+)\\.([0-9]+)(.*)")

val result1 = regex1.matchEntire(this) ?: return null
Expand All @@ -114,7 +112,7 @@ internal fun String.toVersionOrNull(): Version? {
if (snapshot) {
rem = rem.removeSuffix("-SNAPSHOT")
}
if (!rem.isEmpty()) {
if (rem.isNotEmpty()) {
if (!rem.startsWith("-")) {
return null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@ package com.gradleup.librarian.core.tooling

import java.io.File

fun tagAndBump(versionToRelease: String) {
return tagAndBump(versionToRelease) {
error("versionToRelease must not be null")
}
}

sealed interface Confirmation
class UseDefaultVersion(
val expectedVersion: String
Expand All @@ -23,36 +17,9 @@ class BumpMajor(
val currentVersion: String
): Confirmation

fun tagAndBump(versionToRelease: String?, confirm: (Confirmation) -> Unit) {
fun tagAndBump(versionToRelease: String) {
checkCwd()

var tagVersion = versionToRelease
val currentVersion = getCurrentVersion()
val currentVersionParsed = currentVersion.toVersionOrNull()
check(currentVersionParsed != null) {
"Cannot parse current version: '${currentVersion}'"
}
check(currentVersionParsed.isSnapshot) {
"Version '${currentVersion} is not a -SNAPSHOT, check your working directory"
}

val expectedVersion = currentVersion.removeSuffix("-SNAPSHOT")

if (tagVersion == null) {
confirm(UseDefaultVersion(expectedVersion))
tagVersion = expectedVersion
}

val tagVersionParsed = tagVersion.toVersionOrNull()
check(tagVersionParsed != null) {
"Version must start with 'major.minor.patch' (found '$tagVersion')"
}
if (tagVersionParsed < currentVersionParsed) {
confirm(DowngradeVersion(tagVersion, currentVersion))
} else if (tagVersionParsed.major > currentVersionParsed.major) {
confirm(BumpMajor(tagVersion, currentVersion))
}

check(runCommand("git", "status", "--porcelain").isEmpty()) {
"Your git repo is not clean. Make sure to stash or commit your changes before making a release"
}
Expand All @@ -62,14 +29,14 @@ fun tagAndBump(versionToRelease: String?, confirm: (Confirmation) -> Unit) {
"You must be on the main branch or a release branch to make a release"
}

val markdown = processChangelog(tagVersion)
val markdown = processChangelog(versionToRelease)

// 'De-snapshot' the version, open a PR, and merge it
val releaseBranchName = "tag-$tagVersion"
val releaseBranchName = "tag-$versionToRelease"
runCommand("git", "checkout", "-b", releaseBranchName)
setCurrentVersion(tagVersion)
setVersionInDocs(tagVersion)
runCommand("git", "commit", "-a", "-m", "release $tagVersion")
setCurrentVersion(versionToRelease)
setVersionInDocs(versionToRelease)
runCommand("git", "commit", "-a", "-m", "release $versionToRelease")
runCommand("git", "push", "origin", releaseBranchName)
runCommand("gh", "pr", "create", "--base", startBranch, "--fill")

Expand All @@ -79,17 +46,17 @@ fun tagAndBump(versionToRelease: String?, confirm: (Confirmation) -> Unit) {
// Tag the release, and push the tag
runCommand("git", "checkout", startBranch)
runCommand("git", "pull", "origin", startBranch)
val tagName = "v$tagVersion"
val tagName = "v$versionToRelease"
runCommand("git", "tag", tagName, "-m", markdown)

runCommand("git", "push", "origin", tagName)
println("Tag pushed.")

// Bump the version to the next snapshot
val bumpVersionBranchName = "bump-$tagVersion"
val bumpVersionBranchName = "bump-$versionToRelease"
runCommand("git", "checkout", "-b", bumpVersionBranchName)

val nextSnapshot = getNextSnapshot(tagVersion)
val nextSnapshot = getNextSnapshot(versionToRelease)
setCurrentVersion(nextSnapshot)
runCommand("git", "commit", "-a", "-m", "version is now $nextSnapshot")
runCommand("git", "push", "origin", bumpVersionBranchName)
Expand Down Expand Up @@ -130,7 +97,7 @@ internal fun setCurrentVersion(version: String) {
file.writeText(newContent)
}

internal fun getCurrentVersion(): String {
fun getCurrentVersion(): String {
val file = File("librarian.properties")
require(file.exists()) {
"Cannot find file ${file.absolutePath}"
Expand Down

0 comments on commit 229760f

Please sign in to comment.