Skip to content

Commit

Permalink
separate custom gradle scripts into own files
Browse files Browse the repository at this point in the history
  • Loading branch information
leonlatsch committed Sep 9, 2021
1 parent 3eb74f0 commit 0213493
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 121 deletions.
123 changes: 2 additions & 121 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,124 +26,5 @@ allprojects {
}
}

tasks.register("updateVersion") {
val version: String? by project
version ?: throw GradleException("Not version provided")

val file = file("gradle.properties")
if (file.canRead()) {
val properties = Properties().apply {
load(file.inputStream())
}

val oldVersionCode: String = properties["appVersionCode"] as String
val newVersionCode: String = oldVersionCode.toInt().inc().toString()

properties["appVersionName"] = version!!
properties["appVersionCode"] = newVersionCode

properties.store(file.writer(), null)
} else {
throw GradleException("${file.name} not readable")
}
}

tasks.register("updateTranslations") {
val resPath = "app/src/main/res"
val bytes = java.io.FileInputStream(File("$resPath/values/strings.xml")).readBytes()
val enLines = String(bytes).split("\n")

var enStrings = 0

for (line in enLines) {
if (line.contains("<string")) {
enStrings++
}
}

val badges = arrayListOf<String>()
File(resPath).walk().forEach { dir ->
if (dir.isDirectory &&
dir.name.contains("values") &&
dir.name != "values"
) {
dir.walk().forEach { stringsFile ->
if (stringsFile.name == "strings.xml") {
var strings = 0
var author = "UNKNOWN"
val lines = String(java.io.FileInputStream(stringsFile).readBytes()).split("\n")
for (line in lines) {
if (line.contains("<string") && !line.contains("TODO")) {
strings++
} else if (line.contains("MAINTAINED BY")) {
author = line.substring(line.indexOf("(") + 1, line.indexOf(")"))
}
}
val localeName = dir.name.replace("values-", "")
val percentage = (strings.toDouble() / enStrings.toDouble()) * 100
val template =
"![{alt-locale}](https://img.shields.io/badge/{locale}-{percentage}{color})\n"
val color = when {
percentage > 99 -> "25-brightgreen"
percentage > 75 -> "25-yellow"
percentage > 50 -> "25-orange"
percentage > 0 -> "25-red"
else -> "lightgrey"
}
val localeDisplay = Locale.forLanguageTag(localeName.replace("-r", "-"))
.getDisplayName(Locale.US)
val badge = template
.replace("{locale}", localeDisplay.replace(" ", "%20"))
.replace("{alt-locale}", localeDisplay)
.replace("{percentage}", "${percentage.toInt()}%")
.replace("{color}", color)
badges.add(badge)
}
}
}
} // READ strings.xml

if (badges.isNotEmpty()) {
badges.sort()

val readmeString = String(java.io.FileInputStream(File("README.md")).readBytes())
val readmeLines = readmeString.split("\n")

var beginIndex = 0
var endIndex = 0
var i = 0
while (i < readmeLines.size) {
if (readmeLines[i].contains("BEGIN-TRANSLATIONS")) {
beginIndex = i + 1
}
if (readmeLines[i].contains("END-TRANSLATIONS")) {
endIndex = i
}
i++
}

val prefixStrings = readmeLines.subList(0, beginIndex)
val suffixStrings = readmeLines.subList(endIndex, readmeLines.size - 1)

var badgeString = ""
badgeString += "![English](https://img.shields.io/badge/English-100%25-brightgreen)\n" // Hard code add english
badges.forEach {
badgeString += it
}

var newReadmeString = ""
prefixStrings.forEach {
newReadmeString += "$it\n"
}

newReadmeString += badgeString

suffixStrings.forEach {
newReadmeString += "$it\n"
}

if (newReadmeString.isNotEmpty()) {
File("README.md").writeText(newReadmeString)
}
}
}
apply("./gradle/updateVersion.gradle.kts")
apply("./gradle/updateTranslations.gradle.kts")
117 changes: 117 additions & 0 deletions gradle/updateTranslations.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import java.util.*

/*
* Copyright 2020-2021 Leon Latsch
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

tasks.register("updateTranslations") {
val resPath = "app/src/main/res"
val bytes = java.io.FileInputStream(File("$resPath/values/strings.xml")).readBytes()
val enLines = String(bytes).split("\n")

var enStrings = 0

for (line in enLines) {
if (line.contains("<string")) {
enStrings++
}
}

val badges = arrayListOf<String>()
File(resPath).walk().forEach { dir ->
if (dir.isDirectory &&
dir.name.contains("values") &&
dir.name != "values"
) {
dir.walk().forEach { stringsFile ->
if (stringsFile.name == "strings.xml") {
var strings = 0
var author = "UNKNOWN"
val lines = String(java.io.FileInputStream(stringsFile).readBytes()).split("\n")
for (line in lines) {
if (line.contains("<string") && !line.contains("TODO")) {
strings++
} else if (line.contains("MAINTAINED BY")) {
author = line.substring(line.indexOf("(") + 1, line.indexOf(")"))
}
}
val localeName = dir.name.replace("values-", "")
val percentage = (strings.toDouble() / enStrings.toDouble()) * 100
val template =
"![{alt-locale}](https://img.shields.io/badge/{locale}-{percentage}{color})\n"
val color = when {
percentage > 99 -> "25-brightgreen"
percentage > 75 -> "25-yellow"
percentage > 50 -> "25-orange"
percentage > 0 -> "25-red"
else -> "lightgrey"
}
val localeDisplay = Locale.forLanguageTag(localeName.replace("-r", "-"))
.getDisplayName(Locale.US)
val badge = template
.replace("{locale}", localeDisplay.replace(" ", "%20"))
.replace("{alt-locale}", localeDisplay)
.replace("{percentage}", "${percentage.toInt()}%")
.replace("{color}", color)
badges.add(badge)
}
}
}
} // READ strings.xml

if (badges.isNotEmpty()) {
badges.sort()

val readmeString = String(java.io.FileInputStream(File("README.md")).readBytes())
val readmeLines = readmeString.split("\n")

var beginIndex = 0
var endIndex = 0
var i = 0
while (i < readmeLines.size) {
if (readmeLines[i].contains("BEGIN-TRANSLATIONS")) {
beginIndex = i + 1
}
if (readmeLines[i].contains("END-TRANSLATIONS")) {
endIndex = i
}
i++
}

val prefixStrings = readmeLines.subList(0, beginIndex)
val suffixStrings = readmeLines.subList(endIndex, readmeLines.size - 1)

var badgeString = ""
badgeString += "![English](https://img.shields.io/badge/English-100%25-brightgreen)\n" // Hard code add english
badges.forEach {
badgeString += it
}

var newReadmeString = ""
prefixStrings.forEach {
newReadmeString += "$it\n"
}

newReadmeString += badgeString

suffixStrings.forEach {
newReadmeString += "$it\n"
}

if (newReadmeString.isNotEmpty()) {
File("README.md").writeText(newReadmeString)
}
}
}
43 changes: 43 additions & 0 deletions gradle/updateVersion.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import java.util.*

/*
* Copyright 2020-2021 Leon Latsch
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

tasks.register("updateVersion") {
doLast {
val version: String? by project
if (version == null || version == "Unspecified") {
return@doLast
}

val file = file("gradle.properties")
if (file.canRead()) {
val properties = Properties().apply {
load(file.inputStream())
}

val oldVersionCode: String = properties["appVersionCode"] as String
val newVersionCode: String = oldVersionCode.toInt().inc().toString()

properties["appVersionName"] = version!!
properties["appVersionCode"] = newVersionCode

properties.store(file.writer(), null)
} else {
throw GradleException("${file.name} not readable")
}
}
}

0 comments on commit 0213493

Please sign in to comment.