yubioath-flutter/android/app/collect_licenses.gradle

91 lines
3.0 KiB
Groovy
Raw Normal View History

2022-08-31 16:49:43 +03:00
import groovy.json.JsonOutput
import java.util.regex.Pattern
def collectLicenses(File rootDir, File ossPluginResDir, File outDir) {
2022-08-31 16:49:43 +03:00
def pattern = Pattern.compile("^(\\d+:\\d+) (.*)\$")
// third_party_licenses file generated by oss-licenses-plugin
def licenses = new File(ossPluginResDir, "third_party_licenses")
// third_party_license_metadata file generated by oss-licenses-plugin
def metadata = new File(ossPluginResDir, "third_party_license_metadata")
// check that files exist, are readable and output directory is writable
if (!licenses.canRead()) {
throw new GradleException("Cannot find/read ${licenses.absolutePath}")
}
if (!metadata.canRead()) {
throw new GradleException("Cannot find/read ${metadata.absolutePath}")
}
if (!outDir.exists() || !outDir.canExecute()) {
if (!outDir.mkdir()) {
throw new GradleException("Failed to create ${outDir.absolutePath}")
}
2022-08-31 16:49:43 +03:00
}
def outFile = new File(outDir, "android.json")
2022-08-31 16:49:43 +03:00
if (outFile.exists()) {
outFile.delete()
}
println "Used licenses:"
var licenseMap = [:]
var index = 0
licenses.eachLine { line ->
def indices = "$index:${line.length()}"
println line
2022-08-31 16:49:43 +03:00
licenseMap[indices] = line
index += line.length() + 1
}
println "Modules:"
2022-08-31 16:49:43 +03:00
def licenseList = []
metadata.eachLine { line ->
def matcher = pattern.matcher(line)
if (matcher.find()) {
def indices = matcher.group(1)
def packageName = matcher.group(2)
if (licenseMap.get(indices) == null) {
throw new GradleException("Unexpected license indexes: $indices for $packageName")
}
println packageName + " license: ${licenseMap[indices]}"
licenseList.add(PackageName: packageName, PackageLicense: licenseMap[indices])
} else {
throw new GradleException("Unexpected project license line: $line")
}
}
// add zxing_licenses which are not detected
println "adding zxing licenses"
2022-08-31 16:49:43 +03:00
licenseList.add(PackageName: "ZXing Core (3.3.0)", PackageLicense: "https://www.apache.org/licenses/LICENSE-2.0.txt")
licenseList.add(PackageName: "ZXing Android Core (3.3.0)", PackageLicense: "https://www.apache.org/licenses/LICENSE-2.0.txt")
outFile.write(new JsonOutput().toJson(licenseList))
println "Created ${outFile.absolutePath}"
// copy license assets to flutter resources
def licensesDir = new File(rootDir, "licenses/");
copy {
from(licensesDir.absolutePath) {
include "**/*txt"
include "**/*json"
}
into outDir
}
// remove not needed oss-licenses-plugin files
licenses.delete()
metadata.delete()
2022-08-31 16:49:43 +03:00
}
task collectLicenses() {
dependsOn(":app:releaseOssLicensesTask")
doLast {
def ossPluginResDir = new File(project.buildDir, "generated/third_party_licenses/release/res/raw/")
collectLicenses(project.rootDir, ossPluginResDir, new File(project.rootDir.parent, "assets/licenses/raw/"))
2022-08-31 16:49:43 +03:00
}
}