diff --git a/app/build.gradle b/app/build.gradle index 1082874041..9826c55ad5 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -226,20 +226,8 @@ android { } } -task ipToCode { - def inputFile = file("${projectDir}/geolite2_country_blocks_ipv4.csv") - - def outputDir = "${buildDir}/generated/binary" - def outputFile = new File(outputDir, "geolite2_country_blocks_ipv4.bin") - - outputs.file outputFile - - doLast { - exec { - commandLine "kotlin", "ipToCode.kts", inputFile.absolutePath, outputFile.absolutePath - println "Generated binary file at: ${outputFile.absolutePath}" - } - } +apply { + from("ipToCode.gradle.kts") } preBuild.dependsOn ipToCode diff --git a/app/ipToCode.gradle.kts b/app/ipToCode.gradle.kts new file mode 100644 index 0000000000..9ec2b29806 --- /dev/null +++ b/app/ipToCode.gradle.kts @@ -0,0 +1,41 @@ +import java.io.File +import java.io.DataOutputStream +import java.io.FileOutputStream + +task("ipToCode") { + val inputFile = File("${projectDir}/geolite2_country_blocks_ipv4.csv") + + val outputDir = "${buildDir}/generated/binary" + val outputFile = File(outputDir, "geolite2_country_blocks_ipv4.bin").apply { parentFile.mkdirs() } + + outputs.file(outputFile) + + doLast { + + // Ensure the input file exists + if (!inputFile.exists()) { + throw IllegalArgumentException("Input file does not exist: ${inputFile.absolutePath}") + } + + // Create a DataOutputStream to write binary data + DataOutputStream(FileOutputStream(outputFile)).use { out -> + inputFile.useLines { lines -> + var prevCode = -1 + lines.drop(1).forEach { line -> + runCatching { + val ints = line.split(".", "/", ",") + val code = ints[5].toInt().also { if (it == prevCode) return@forEach } + val ip = ints.take(4).fold(0) { acc, s -> acc shl 8 or s.toInt() } + + out.writeInt(ip) + out.writeInt(code) + + prevCode = code + } + } + } + } + + println("Processed data written to: ${outputFile.absolutePath}") + } +} diff --git a/app/ipToCode.kts b/app/ipToCode.kts deleted file mode 100644 index f73fec1cb1..0000000000 --- a/app/ipToCode.kts +++ /dev/null @@ -1,38 +0,0 @@ -import java.io.File -import java.io.DataOutputStream -import java.io.FileOutputStream - -// Check that the correct number of arguments is provided -if (args.size < 2) { - throw IllegalArgumentException("Please provide both input and output file paths.") -} - -// Get the input and output file paths from the command line arguments -val inputFile = File(args[0]) -val outputFile = File(args[1]).apply { parentFile.mkdirs() } - -// Ensure the input file exists -if (!inputFile.exists()) { - throw IllegalArgumentException("Input file does not exist: ${inputFile.absolutePath}") -} - -// Create a DataOutputStream to write binary data -DataOutputStream(FileOutputStream(outputFile)).use { out -> - inputFile.useLines { lines -> - var prevCode = -1 - lines.drop(1).forEach { line -> - runCatching { - val ints = line.split(".", "/", ",") - val code = ints[5].toInt().also { if (it == prevCode) return@forEach } - val ip = ints.take(4).fold(0) { acc, s -> acc shl 8 or s.toInt() } - - out.writeInt(ip) - out.writeInt(code) - - prevCode = code - } - } - } -} - -println("Processed data written to: ${outputFile.absolutePath}")