@@ -4,6 +4,7 @@ plugins {
44 id ' kotlin-kapt'
55 id ' dagger.hilt.android.plugin'
66 id ' maven-publish'
7+ id ' org.ajoberstar.grgit'
78}
89
910android {
@@ -74,4 +75,135 @@ publishing {
7475 credentials(PasswordCredentials )
7576 }
7677 }
78+ }
79+
80+ tasks. register(' ensureCleanRepo' ) {
81+ doLast {
82+ if (! grgit. repository. jgit. status(). call(). clean) {
83+ throw new GradleException (' Git status is not clean, please stash your changes!' )
84+ }
85+ }
86+ }
87+
88+ tasks. register(' releaseClean' ) {
89+ dependsOn ensureCleanRepo
90+ doLast {
91+ def clean = true
92+ def applicationId = android. defaultConfig. applicationId
93+
94+ String headCommitMessage = grgit. head(). shortMessage
95+ while (headCommitMessage. contains(" [gradle-release-task]" )) {
96+ clean = false
97+ println " Found git commit: $headCommitMessage "
98+ if (headCommitMessage. indexOf(" $applicationId -" ) > -1 ) {
99+ def tagName = headCommitMessage. split(" $applicationId -" )[1 ]
100+ println " Removing the git tag: $tagName "
101+ try {
102+ grgit. tag. remove {
103+ names = [tagName]
104+ }
105+ } catch (Exception e) {
106+ println " Error while removing git tag:\n $e "
107+ }
108+ }
109+ println " Resetting the git commit permanently!"
110+ grgit. reset(commit : " HEAD~1" , mode : " hard" )
111+ headCommitMessage = grgit. head(). shortMessage
112+
113+ }
114+ if (clean) {
115+ println " Repository is already clean"
116+ }
117+ println " Done!"
118+ }
119+ }
120+
121+ // Task parameters:
122+ // bumpVersion -> if available will specify new versionName directly and ignores the `bumpType` parameter.
123+ // bumpType[major|minor|patch] -> will specify how the version bumping occurs.
124+ tasks. register(' releasePrepare' ) {
125+ dependsOn ensureCleanRepo
126+ doLast {
127+ def applicationId = android. defaultConfig. applicationId
128+ def versionName = android. defaultConfig. versionName
129+
130+ if (versionName. indexOf(" -" ) > -1 ) {
131+ versionName = versionName. split(" -" )[0 ]
132+ }
133+
134+ // Prepare the release commit with the specific tag.
135+ String buildText = buildFile. getText()
136+ buildText = buildText. replaceFirst(/ versionName(\s +.*)/ , " versionName '$versionName '" )
137+ buildFile. setText(buildText) // replace the build file's text
138+ grgit. add(patterns : [' utils/build.gradle' ])
139+ try {
140+ grgit. commit(message : " [gradle-release-task] prepare release $applicationId -$versionName " )
141+ } catch (Exception e) {
142+ throw new GradleException (" Failed to commit, error:\n $e " )
143+ }
144+ try {
145+ grgit. tag. add {
146+ name = versionName
147+ message = " Release of $versionName "
148+ }
149+ } catch (Exception e) {
150+ throw new GradleException (" Failed to tag the repo, error:\n $e " )
151+ }
152+
153+ // Set new version name from input parameters.
154+ def newVersionName
155+ if (project. properties. containsKey(" bumpVersion" )) {
156+ newVersionName = project. properties[" bumpVersion" ]
157+ println " Bumping the version directly (bumpVersion=$newVersionName )"
158+ } else if (project. properties. containsKey(" bumpType" )) {
159+ def (major, minor, patch) = versionName. tokenize(' .' )
160+ switch (bumpType) {
161+ case " major" :
162+ major = major. toInteger() + 1
163+ minor = 0
164+ patch = 0
165+ break
166+ case " minor" :
167+ minor = minor. toInteger() + 1
168+ break
169+ case " patch" :
170+ patch = patch. toInteger() + 1
171+ break
172+ }
173+ newVersionName = " $major . $minor . $patch "
174+ } else {
175+ throw new GradleException (' Either bumpType or bumpVersion parameters should be provided' )
176+ }
177+
178+ // Prepare for next development iteration.
179+ def versionCode = android. defaultConfig. versionCode
180+ def newVersionCode = versionCode + 1
181+ println " Bumping versionName from $versionName to $newVersionName "
182+ println " Bumping versionCode from $versionCode to $newVersionCode "
183+ buildText = buildFile. getText()
184+ buildText = buildText. replaceFirst(/ versionName(\s +.*)/ , " versionName '$newVersionName -SNAPSHOT'" )
185+ buildText = buildText. replaceFirst(/ versionCode(\s +.*)/ , " versionCode $newVersionCode " )
186+ buildFile. setText(buildText) // replace the build file's text
187+ grgit. add(patterns : [' utils/build.gradle' ])
188+ grgit. commit(message : " [gradle-release-task] prepare for next development iteration" )
189+ println " Done!"
190+ }
191+ }
192+
193+ tasks. register(' releasePerform' ) {
194+ dependsOn ensureCleanRepo
195+ doLast {
196+ boolean force = false
197+ if (project. properties. containsKey(" force" )) {
198+ force = project. properties[" force" ]
199+ }
200+ println " Pushing the newest commits to the remote repository (force: $force )"
201+ try {
202+ grgit. push(force : force, tags : true )
203+ } catch (Exception e) {
204+ throw new GradleException (" Failed to push to the repo,\n " +
205+ " you can try using -Pforce=true parameter to force the push, error: \n $e " )
206+ }
207+ println " Done!"
208+ }
77209}
0 commit comments