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

208 lines
7.4 KiB
Groovy
Raw Normal View History

// *************************
// * 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}"
}
ext.copyStringsXmlFiles = { 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}")
}
}
ext.copyNonStringsXmlFiles = { dir, tmpTargetDir ->
copy {
from(dir)
exclude '**/strings.xml'
into("${tmpTargetDir}")
}
}
// 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
copyStringsXmlFiles(dir, tmpTargetDir)
copyNonStringsXmlFiles(dir, tmpTargetDir)
return "${tmpTargetDir}"
}
ext.copyFiles = { dir, tmpTargetDir ->
copy {
from(dir)
into("${tmpTargetDir}")
}
}
// replace variables content for specific files
ext.alterFilesAndVerify = { targetDir, isTest ->
if (targetDir.contains('src/main/java') or isTest) { // only look into the 'main' variant
replaceAndVerify('s', true, targetDir,
/* filename: */ 'org/schabi/newpipe/error/ErrorActivity.java',
/* match: */ 'ERROR_EMAIL_ADDRESS = "crashreport@newpipe.schabi.org"',
/* replace: */ 'ERROR_EMAIL_ADDRESS = "crashreport@gmx.com"',
/* verify: */ 'crashreport@gmx.com')
replaceAndVerify('m', false, targetDir,
/* filename: */ 'org/schabi/newpipe/error/ErrorActivity.java',
/* match: */ '(public static final String ERROR_GITHUB_ISSUE_URL.*\n[^=]*=)[^;]*',
/* replace: */ '\\1 "https://github.com/bravenewpipe/NewPipeExtractor/issues"',
/* verify: */ 'https://github.com/bravenewpipe/NewPipeExtractor/issues')
replaceAndVerify('s', true, targetDir,
/* filename: */ 'org/schabi/newpipe/util/ReleaseVersionUtil.kt',
/* match: */ '"B0:2E:90:7C:1C:D6:FC:57:C3:35:F0:88:D0:8F:50:5F:94:E4:D2:15"',
/* replace: */ '"C3:96:13:CD:13:92:3F:37:EE:B6:9F:7A:0D:EA:7C:70:E0:7A:73:D8"',
/* verify: */ '"C3:96:13:CD:13:92:3F:37:EE:B6:9F:7A:0D:EA:7C:70:E0:7A:73:D8"')
replaceAndVerify('m', false, targetDir,
/* filename: */ 'org/schabi/newpipe/NewVersionWorker.kt',
/* match: */ '(private const val NEWPIPE_API_URL =).*',
/* replace: */ '\\1\n "https://raw.githubusercontent.com/bravenewpipe/bnp-r-mgr/master/api/data.json"',
/* verify: */ '"https://raw.githubusercontent.com/bravenewpipe/bnp-r-mgr/master/api/data.json"')
}
return "${targetDir}"
}
ext.replaceAndVerify = { flags, byline, dir, fileName, match, replace, verify ->
assert file(dir + '/' + fileName).exists()
// check if file is already changed
def lines2 = new File(dir + '/' + fileName).readLines()
def result2 = lines2.find { it.contains(verify) }
if (result2 != null) {
println "[BraveNewPipe] already changed $match in $fileName"
// already changed so return
return
}
println "[BraveNewPipe] string replacing in file: " + fileName + ' [What:]' + match
ant.replaceregexp(
match:match,
replace:replace,
flags:flags,
byline:byline) {
fileset(dir: dir, includes: fileName)
}
// verify that it really got changed
def lines = new File(dir + '/' + fileName).readLines()
def result = lines.find { it.contains(verify) }
//println result
assert result != null : "No match for '${match} in ${fileName}"
}
// 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 DEBUGGING of copyFilesAndReplaceStrings() or
// alterFilesAndVerify()
task testReplacingStrings() {
// -> comment the 'return' statement to actually start debugging
return
println "[TESTING BraveNewPipe string replacing]"
// modify .{xml} files
def relativeDirFile = 'src/main/res'
def sourceDir = "${rootDir}/app/${relativeDirFile}/"
def targetDir = "${buildDir}/tmp/${relativeDirFile}/test_output"
cleanupDirBefore(targetDir)
copyFilesAndReplaceStrings(sourceDir, targetDir)
}
ext.replaceNewPipeWithBraveNewPipeStrings = {
println "[BraveNewPipe string replacing]"
// modify .{xml} files
def relativeDirFile = 'src/main/res'
def sourceDir = "${rootDir}/app/${relativeDirFile}/"
def targetDir = "${buildDir}/tmp/${relativeDirFile}/test_output"
copyStringsXmlFiles(sourceDir, targetDir)
copyFiles(targetDir, sourceDir)
}
task bravify() {
group = 'brave'
description = 'replaces string NewPipe with BraveNewPipe'
doLast {
replaceNewPipeWithBraveNewPipeStrings()
}
}
// Patch NewPipe to use BraveNewPipe's:
// - support email address
// - the update json data URL
// - the apk signature
// This task (if enabled) will run on the current code base and
// does this job for you automatically. To be re-run if needed.
// -- evermind --
task testReplaceMailandJsonandSignature() {
return // enable only when you want it to run
// modify .{java,kt} files
def relativeDirFile = 'src/main/java'
def sourceDir = "${rootDir}/app/${relativeDirFile}/"
alterFilesAndVerify(sourceDir, true)
}