mirror of
https://github.com/MaintainTeam/LastPipeBender.git
synced 2025-03-01 22:08:22 +03:00
299 lines
11 KiB
Groovy
299 lines
11 KiB
Groovy
import static groovy.io.FileType.FILES
|
|
|
|
// *************************
|
|
// * 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 =).*\n.*',
|
|
/* 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() {
|
|
group = "braveTest"
|
|
|
|
doLast {
|
|
// modify .{java,kt} files
|
|
def relativeDirFile = 'src/main/java'
|
|
def sourceDir = "${rootDir}/app/${relativeDirFile}/"
|
|
alterFilesAndVerify(sourceDir, true)
|
|
}
|
|
}
|
|
|
|
//-----------------------------------------------------------------------------------------
|
|
// ############ begin -- section only relevant for braveLegacy flavor building#############
|
|
//-----------------------------------------------------------------------------------------
|
|
// We have some same sourcecode files in 'main' and in 'braveLegacy' flavor.
|
|
// To make it compile we need to remove the 'main' files first. Before calling:
|
|
// # gradle assembleBraveLegacy{Debug,Release,Whatever}
|
|
// you have to call
|
|
// # gradle prepareLegacyFlavor
|
|
// and afterwards to restore the files (in case you want to build another flavor:
|
|
// # gradle unPrepareLegacyFlavor
|
|
ext.prepareFilesForLegacy = { doRestore ->
|
|
|
|
def relativeSourceDir = 'src/braveLegacy/java'
|
|
def sourceDir = "${rootDir}/app/${relativeSourceDir}/"
|
|
def tempDir = "${rootDir}/tmp-legacy"
|
|
|
|
new File(sourceDir).eachFileRecurse(FILES) {
|
|
if ((!it.name.startsWith('Brave')) && (it.name.endsWith(".kt") || it.name.endsWith(".java"))) {
|
|
def targetParentDir = it.parent.replace("braveLegacy", "main")
|
|
def fileName = it.name
|
|
def originFilePath = targetParentDir + "/" + fileName
|
|
def targetFileName = tempDir + "/" + fileName
|
|
|
|
if (doRestore) {
|
|
println "[BraveLegacy] move: " + targetFileName + " -> " + originFilePath
|
|
ant.move(file: targetFileName, tofile: originFilePath)
|
|
} else { // hide the files from the compiler
|
|
println "[BraveLegacy] move " + originFilePath + " -> " + tempDir
|
|
ant.move(file: originFilePath, toDir: tempDir)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
ext.alterNewVersionWorkerForLegacy = { targetDir, isTest, infix ->
|
|
|
|
if (targetDir.contains('src/main/java') or isTest) { // only look into the 'main' variant
|
|
replaceAndVerify('m', false, targetDir,
|
|
/* filename: */ 'org/schabi/newpipe/NewVersionWorker.kt',
|
|
/* match: */ '(private const val NEWPIPE_API_URL =).*\n.*',
|
|
/* replace: */ '\\1\n "https://raw.githubusercontent.com/bravenewpipe/bnp-r-mgr/' + infix + '/api/data.json"',
|
|
/* verify: */ '"https://raw.githubusercontent.com/bravenewpipe/bnp-r-mgr/' + infix + '/api/data.json"')
|
|
}
|
|
|
|
return "${targetDir}"
|
|
}
|
|
|
|
ext.replaceNewVersionJsonUrl = { infix ->
|
|
def relativeDirFile = 'src/main/java'
|
|
def sourceDir = "${rootDir}/app/${relativeDirFile}/"
|
|
alterNewVersionWorkerForLegacy(sourceDir, false, infix)
|
|
}
|
|
|
|
task prepareLegacyFlavor() {
|
|
group = 'brave'
|
|
description = "move duplicated files from 'main' to tmp dir"
|
|
doLast {
|
|
prepareFilesForLegacy(false)
|
|
replaceNewVersionJsonUrl("kitkat")
|
|
}
|
|
}
|
|
|
|
task unprepareLegacyFlavor() {
|
|
group = 'brave'
|
|
description = "move duplicated files from tmp dir 'main'"
|
|
doLast {
|
|
prepareFilesForLegacy(true)
|
|
replaceNewVersionJsonUrl("master")
|
|
}
|
|
}
|
|
|
|
task testReplaceNewVersionUrl() {
|
|
group = "braveTest"
|
|
description = "change to legacy new version URL"
|
|
doLast {
|
|
replaceNewVersionJsonUrl("kitkat")
|
|
}
|
|
}
|
|
|
|
task testUnReplaceNewVersionUrl() {
|
|
group = "braveTest"
|
|
description = "change to master new version URL"
|
|
doLast {
|
|
replaceNewVersionJsonUrl("master")
|
|
}
|
|
}
|
|
// ############ end -- section only relevant for braveLegacy flavor building#############
|