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

Automatic version bumping #96

Merged
merged 9 commits into from
Sep 19, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 123 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
apply plugin: 'com.android.application'

apply plugin: 'org.ajoberstar.grgit'
android {
compileSdkVersion 32

Expand Down Expand Up @@ -60,3 +60,125 @@ dependencies {
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

task ensureCleanRepo {
doLast {
if (!grgit.repository.jgit.status().call().clean) {
throw new GradleException('Git status is not clean, please stash your changes!')
}
}
}
task releaseClean(dependsOn: ensureCleanRepo) {
doLast {
def Clean = true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make Clean lower-case: clean

String headCommitMessage = grgit.head().shortMessage
while (headCommitMessage.contains("[gradle-release-task]")) {
Clean = false
println "Found git commit: $headCommitMessage"
if (headCommitMessage.indexOf("vitabu-") > -1) {
Copy link
Member

@nya-elimu nya-elimu Jun 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To make the solution scalable to other Android apps (with different names, can you change "vitabu-" into a variable? For example by using the applicationId variable instead?

Would require adjusting the string used in grgit.commit(): "prepare release ${applicationId}-"

def tagName = headCommitMessage.split("vitabu-")[1]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment here as above.

println "Removing the git tag: $tagName"
try {
grgit.tag.remove {
names = [tagName]
}
} catch (Exception e) {
println "Error while removing git tag:\n $e"
}
}
println "Resetting the git commit permanently!"
grgit.reset(commit: "HEAD~1", mode: "hard")
headCommitMessage = grgit.head().shortMessage

}
if (Clean){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change to if (clean) {

println "Repository is already clean"
}
println "Done!"
}
}

// Task parameters:
// bumpVersion -> if available will specify new versionName directly and ignores the `bumpType` parameter.
// bumpType[major|minor|patch] -> will specify how the version bumping occurs.
task releasePrepare(dependsOn: ensureCleanRepo) {
doLast {
def versionName = android.defaultConfig.versionName

if (versionName.indexOf("-") > -1) {
versionName = versionName.split("-")[0]
}

// Prepare the release commit with the specific tag.
String buildText = buildFile.getText()
buildText = buildText.replaceFirst(/versionName(\s+.*)/, "versionName '$versionName'")
buildFile.setText(buildText) //replace the build file's text
grgit.add(patterns: ['app/build.gradle'])
grgit.commit(message: "[gradle-release-task] prepare release vitabu-$versionName")
try {
grgit.tag.add {
name = versionName
message = "Release of $versionName"
}
} catch (Exception e) {
throw new GradleException("Failed to tag the repo, error:\n $e")
}


// Set new version name from input parameters.
def newVersionName
if (project.properties.containsKey("bumpVersion")) {
newVersionName = project.properties["bumpVersion"]
println "Bumping the version directly (bumpVersion=$newVersionName)"
} else if (project.properties.containsKey("bumpType")) {
def (major, minor, patch) = versionName.tokenize('.')
switch (bumpType) {
case "major":
major = major.toInteger() + 1
minor = 0
patch = 0
break
case "minor":
minor = minor.toInteger() + 1
break
case "patch":
patch = patch.toInteger() + 1
break
}
newVersionName = "$major.$minor.$patch"
} else {
throw new GradleException('Either bumpType or bumpVersion parameters should be provided')
}

// Prepare for next development iteration.
def versionCode = android.defaultConfig.versionCode
def newVersionCode = versionCode + 1
println "Bumping versionName from $versionName to $newVersionName"
println "Bumping versionCode from $versionCode to $newVersionCode"
buildText = buildFile.getText()
buildText = buildText.replaceFirst(/versionName(\s+.*)/, "versionName '$newVersionName-SNAPSHOT'")
buildText = buildText.replaceFirst(/versionCode(\s+.*)/, "versionCode $newVersionCode")
buildFile.setText(buildText) //replace the build file's text
grgit.add(patterns: ['app/build.gradle'])
grgit.commit(message: "[gradle-release-task] prepare for next development iteration")
println "Done!"
}

}

task releasePerform(dependsOn: ensureCleanRepo) {
doLast {
boolean force = false
if (project.properties.containsKey("force")) {
force = project.properties["force"]
}
println "Pushing the newest commits to the remote repository (force: $force)"
try {
grgit.push(force: force, tags: true)
} catch (Exception e) {
throw new GradleException("Failed to push to the repo,\n" +
" you can try using -Pforce=true parameter to force the push, error: \n$e")
}
println "Done!"
}
}
5 changes: 4 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ buildscript {
repositories {
google()
jcenter()
mavenCentral()
maven { url 'https://jitpack.io' }
}
dependencies {
classpath 'com.android.tools.build:gradle:7.1.2'

classpath 'org.ajoberstar.grgit:grgit-gradle:5.0.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
Expand All @@ -26,3 +28,4 @@ allprojects {
task clean(type: Delete) {
delete rootProject.buildDir
}