-
-
Notifications
You must be signed in to change notification settings - Fork 3
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
Changes from 7 commits
27e00e9
570da6b
086d404
8c66e94
baceb0e
65998e2
801a406
6bfd05c
d76a921
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
|
||
|
@@ -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 | ||
String headCommitMessage = grgit.head().shortMessage | ||
while (headCommitMessage.contains("[gradle-release-task]")) { | ||
Clean = false | ||
println "Found git commit: $headCommitMessage" | ||
if (headCommitMessage.indexOf("vitabu-") > -1) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 Would require adjusting the string used in |
||
def tagName = headCommitMessage.split("vitabu-")[1] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Change to |
||
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!" | ||
} | ||
} |
There was a problem hiding this comment.
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