@@ -8,15 +8,30 @@ plugins {
88 id " kotlinx-serialization"
99}
1010
11+ def getVersionCodeFromProperties () {
12+ def propsFile = file(' version.properties' )
13+ if (propsFile. exists()) {
14+ def props = new Properties ()
15+ props. load(new FileInputStream (propsFile))
16+
17+ if (props[' versionCode' ] == null ) {
18+ throw new GradleException (" versionCode not found in the version.properties" )
19+ }
20+
21+ return props[' versionCode' ]. toInteger()
22+ }
23+ throw new GradleException (" The version.properties file was not found." )
24+ }
25+
1126android {
12- compileSdkVersion 33
27+ compileSdkVersion 34
1328 buildToolsVersion " 30.0.3"
1429 ndkVersion " 23.1.7779620"
1530 defaultConfig {
1631 applicationId " com.lcl.lclmeasurementtool"
1732 minSdkVersion 24
18- targetSdkVersion 31
19- versionCode 2
33+ targetSdkVersion 34
34+ versionCode getVersionCodeFromProperties()
2035 versionName " 1.0.3"
2136 testInstrumentationRunner " androidx.test.runner.AndroidJUnitRunner"
2237 javaCompileOptions {
@@ -176,3 +191,100 @@ protobuf {
176191kapt {
177192 correctErrorTypes true
178193}
194+
195+ def sortKeys (Properties properties ) {
196+ Map<String , String > sortedMap = new TreeMap (properties)
197+
198+ // Create a new Properties object and populate it from the sorted map.
199+ Properties sortedProps = new Properties ()
200+ sortedProps. putAll(sortedMap)
201+
202+ return sortedProps
203+ }
204+
205+ task bumpVersionCode {
206+ doLast {
207+ def versionFile = file(" version.properties" )
208+ if (! versionFile. exists()) {
209+ throw new GradleException (" version.properties file not found." )
210+ }
211+
212+ Properties properties = new Properties ()
213+ versionFile. withInputStream { stream -> properties. load(stream) }
214+
215+ if (! properties. containsKey(" versionCode" )) {
216+ throw new GradleException (" versionCode not found in the version.properties" )
217+ }
218+
219+ def versionCode = (properties[" versionCode" ]?. toInteger() ?: 0 ) + 1
220+
221+ properties[" versionCode" ] = versionCode. toString()
222+
223+ def sortedProperties = sortKeys(properties)
224+ versionFile. withOutputStream { stream ->
225+ sortedProperties. store(stream, null )
226+ }
227+
228+ println " ✅ Updated versionCode to: $versionCode "
229+ }
230+ }
231+
232+ def updateVersionProperties (closure ) {
233+ def propsFile = file(' version.properties' )
234+ def props = new Properties ()
235+ if (propsFile. exists()) {
236+ props. load(new FileInputStream (propsFile))
237+ } else {
238+ throw new GradleException (" version.properties file not found." )
239+ }
240+
241+ if (! props. containsKey(" versionMajor" ) || ! props. containsKey(" versionMinor" ) || ! props. containsKey(" versionPatch" )) {
242+ throw new GradleException (" version.properties is corrupted." )
243+ }
244+
245+ // Pass the properties to the closure to be modified
246+ closure(props)
247+
248+ // Save the updated properties
249+ def sortedProps = sortKeys(props)
250+ sortedProps. store(propsFile. newWriter(), null )
251+ }
252+
253+ task bumpPatch {
254+ doLast {
255+ updateVersionProperties { props ->
256+ def patch = props[' versionPatch' ]. toInteger() + 1
257+ def code = props[' versionCode' ]. toInteger() + 1
258+ props[' versionPatch' ] = patch. toString()
259+ props[' versionCode' ] = code. toString()
260+ println " ✅ Version updated to: ${ props['versionMajor']} .${ props['versionMinor']} .$patch (Code $code )"
261+ }
262+ }
263+ }
264+
265+ task bumpMinor {
266+ doLast {
267+ updateVersionProperties { props ->
268+ def minor = props[' versionMinor' ]. toInteger() + 1
269+ def code = props[' versionCode' ]. toInteger() + 1
270+ props[' versionMinor' ] = minor. toString()
271+ props[' versionPatch' ] = " 0" // Reset patch version
272+ props[' versionCode' ] = code. toString()
273+ println " ✅ Version updated to: ${ props['versionMajor']} .$minor . 0 (Code $code )"
274+ }
275+ }
276+ }
277+
278+ task bumpMajor {
279+ doLast {
280+ updateVersionProperties { props ->
281+ def major = props[' versionMajor' ]. toInteger() + 1
282+ def code = props[' versionCode' ]. toInteger() + 1
283+ props[' versionMajor' ] = major. toString()
284+ props[' versionMinor' ] = " 0" // Reset minor version
285+ props[' versionPatch' ] = " 0" // Reset patch version
286+ props[' versionCode' ] = code. toString()
287+ println " ✅ Version updated to: $major . 0 . 0 (Code $code )"
288+ }
289+ }
290+ }
0 commit comments