All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gabrielittner.github.diff.Main.kt Maven / Gradle / Ivy

There is a newer version: 0.8.0
Show newest version
package com.gabrielittner.github.diff

import com.google.common.collect.ImmutableSortedSet
import com.jakewharton.dex.DexParser
import com.xenomachina.argparser.ArgParser
import org.kohsuke.github.GitHub
import java.nio.file.Files
import java.nio.file.Paths
import com.gabrielittner.github.diff.analyzer.AndroidApplicationInfo
import com.android.ide.common.process.ProcessException
import com.android.annotations.NonNull
import com.gabrielittner.github.diff.analyzer.AaptInvoker
import com.android.utils.NullLogger
import com.android.sdklib.repository.AndroidSdkHandler
import com.google.common.collect.ImmutableSortedMap
import java.io.File
import java.nio.file.Path


private val HOME = Paths.get(System.getProperty("user.home"))

fun main(args: Array) {
    val parsedArgs = MainArgs(ArgParser(args))
    val config = parsedArgs.toConfig()
    val github = GitHub.connectUsingOAuth(parsedArgs.githubToken)
    val storage = DatastoreStorage(parsedArgs.googleCredentials)
    val apks = parsedArgs.apks.map { loadApk(config, it) }
    GitHubDiffPoster(config, github, storage).process(apks)
}

private fun loadApk(config: Config, file: String): Apk {
    val path = if (file.startsWith("/")) Paths.get(file) else HOME.resolve(file)
    val fileSize = Files.size(path).toInt()
    val dexParser = DexParser.fromPath(path)
    val methods = ImmutableSortedSet.copyOf(dexParser.listMethods().map { it.toString() })
    val fields = ImmutableSortedSet.copyOf(dexParser.listFields().map { it.toString() })
    // val manifest = AndroidXmlTranslator.parse(Files.newInputStream(path))
    val aaptDump = aaptDump(path)
    val permissions = ImmutableSortedSet.copyOf(aaptDump.permissions)
    val features = ImmutableSortedMap.copyOf(aaptDump.usesFeature)
    val featuresNR = ImmutableSortedSet.copyOf(aaptDump.usesFeatureNotRequired)
    return Apk(
            config.commit,
            path.fileName.toString(),
            fileSize,
            methods.size,
            fields.size,
            permissions,
            features,
            featuresNR,
            ImmutableSortedSet.of(), // methods,
            ImmutableSortedSet.of(), // fields,
            "" // manifest
    )
}

fun aaptDump(@NonNull apk: Path): AndroidApplicationInfo {
    val output: List
    try {
        val androidHome = System.getenv("ANDROID_HOME")
        val sdkHandler = AndroidSdkHandler.getInstance(File(androidHome))
        val aaptInvoker = AaptInvoker(sdkHandler, NullLogger())
        output = aaptInvoker.dumpBadging(apk.toFile())
    } catch (e: ProcessException) {
        throw RuntimeException(e)
    }
    return AndroidApplicationInfo.parseBadging(output)
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy