LastPipeBender/app/replace-newpipe-with-bravenewpipe-strings.gradle

93 lines
3.3 KiB
Groovy

// *************************
// * This gradle script should be included in the build.gradle by
// * using: apply from: 'replace-newpipe-with-bravenewpipe-strings.gradle'
// *************************
// * It will replace all NewPipe occurrences with BraveNewPipe in any
// * strings.xml file. But some string references we do not want to change.
// * Therefore they are listed in the 'doNotReplaceLinesContaining' array.
// *************************
// begin -- vars and helper function
// array of strings that contain NewPipe but should not be replaced
def doNotReplaceLinesContaining = [
'name="donation_encouragement"',
'name="contribution_encouragement"',
'name="website_encouragement"',
'name="brave_about_fork"'
]
def shouldWeReplaceStuffInThisLine = { line ->
for (pattern in doNotReplaceLinesContaining) {
if (line.contains(pattern)) {
return false
}
}
return true
}
def cleanupDirBefore = { dir ->
delete "${dir}"
}
// the return value points to the new srcDir containing the modified files
ext.copyFilesAndReplaceStrings = { dir, tmpTargetDir ->
println "[BraveNewPipe string replacing] source dir: " + dir
println "[BraveNewPipe string replacing] target dir: " + tmpTargetDir
copy {
from(dir)
include '**/strings.xml'
filteringCharset = 'UTF-8'
filter {
line ->
if (shouldWeReplaceStuffInThisLine(line)) {
line
.replace('NewPipe', 'BraveNewPipe')
.replace('Newpipe', 'BraveNewPipe')
.replace('নিউপাইপ', 'সাহসী নিউপাইপ') // bn (bengali)
.replace('نیوپایپ', 'لوله جدید شجاع') // fa, czk (farsi)
} else {
line
}
}
into("${tmpTargetDir}")
}
copy {
from(dir)
exclude '**/strings.xml'
into("${tmpTargetDir}")
}
return "${tmpTargetDir}"
}
// end -- vars and helper function
// * * * * * * * * * * * *
// Do the actual replacing.
// * * * * * * * * * * * *
// source: https://stackoverflow.com/questions/40843740/replace-word-in-strings-xml-with-gradle-for-a-buildtype/57533688#57533688
// https://michd.me/jottings/gradle-variant.getx-is-obsolete/
android.applicationVariants.all { variant ->
variant.mergeResourcesProvider.getOrNull()?.doFirst {
variant.sourceSets.each { sourceSet ->
sourceSet.res.srcDirs = sourceSet.res.srcDirs.collect { dir ->
def relDir = relativePath(dir)
def tmpTargetDir = "${buildDir}/tmp/${variant.dirName}/${relDir}"
cleanupDirBefore(tmpTargetDir)
return copyFilesAndReplaceStrings(dir, tmpTargetDir)
}
}
}
}
// only for testing copyFilesAndReplaceStrings()
task testReplacingStrings() {
return // comment complete line if you want to run this task
println "[TESTING BraveNewPipe string replacing]"
def relativeDirFile = 'src/main/res'
def sourceDir = "${rootDir}/app/${relativeDirFile}/"
def targetDir = "${buildDir}/tmp/${relativeDirFile}/test_output"
cleanupDirBefore(targetDir)
copyFilesAndReplaceStrings(sourceDir, targetDir)
}