Skip to content

Commit

Permalink
Add the option to pass Bloaty path to RulerCli
Browse files Browse the repository at this point in the history
  • Loading branch information
ViktorPetrovski committed Jan 4, 2024
1 parent 70fbdbe commit a8b4849
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 6 deletions.
3 changes: 3 additions & 0 deletions ruler-cli/src/main/java/com/spotify/ruler/cli/RulerCli.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class RulerCli : CliktCommand(), BaseRulerTask {
private val resourceMappingFile: File? by option().file()
private val unstrippedNativeFiles: List<File> by option().file().multiple()
private val aapt2Tool: File? by option().file()
private val bloatyTool: File? by option().file()

override fun print(content: String) = echo(content)

Expand All @@ -62,6 +63,7 @@ class RulerCli : CliktCommand(), BaseRulerTask {

override fun rulerConfig(): RulerConfig = config
override fun provideUnstrippedLibraryFiles() = unstrippedNativeFiles
override fun provideBloatyPath() = bloatyTool?.path

private val config: RulerConfig by lazy {
val json = Json.decodeFromStream<JsonRulerConfig>(rulerConfigJson.inputStream())
Expand Down Expand Up @@ -115,6 +117,7 @@ class RulerCli : CliktCommand(), BaseRulerTask {
Using Proguard Mapping File: ${mappingFile?.path}
Using Resource Mapping File: ${resourceMappingFile?.path}
Using AAPT2: ${aapt2Tool?.path}
Using Bloaty: ${bloatyTool?.path}
Writing reports to: ${reportDir.path}
""".trimIndent())
super.run()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ interface BaseRulerTask {
fun provideResourceMappingFile(): File?
fun rulerConfig(): RulerConfig
fun provideUnstrippedLibraryFiles(): List<File>
fun provideBloatyPath(): String?

private val rulerConfig: RulerConfig
get() = rulerConfig()

Expand Down Expand Up @@ -85,7 +87,7 @@ interface BaseRulerTask {
}

private fun getFilesFromBundle(): Map<String, List<AppFile>> {
val apkParser = ApkParser(provideUnstrippedLibraryFiles())
val apkParser = ApkParser(provideUnstrippedLibraryFiles(), provideBloatyPath())
val classNameSanitizer = ClassNameSanitizer(provideMappingFile())
val resourceNameSanitizer = ResourceNameSanitizer(provideResourceMappingFile())
val apkSanitizer = ApkSanitizer(classNameSanitizer, resourceNameSanitizer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ import java.util.zip.ZipFile

/** Responsible for parsing and extracting entries from APK files. */
class ApkParser(
private val unstrippedNativeLibraryPaths: List<File> = emptyList()
private val unstrippedNativeLibraryPaths: List<File> = emptyList(),
private val bloatyPath: String? = null
) {

/** Parses and returns the list of entries contained in the given [apkFile]. */
Expand All @@ -33,7 +34,7 @@ class ApkParser(
val sizeCalculator = ApkSizeCalculator.getDefault()
val downloadSizePerFile = sizeCalculator.getDownloadSizePerFile(apkFile.toPath())
val installSizePerFile = sizeCalculator.getRawSizePerFile(apkFile.toPath())

val bloaty = Bloaty(bloatyPath)
val apkEntries = mutableListOf<ApkEntry>()
ZipFile(apkFile).use { zipFile ->
zipFile.entries().iterator().forEach { zipEntry ->
Expand All @@ -52,7 +53,7 @@ class ApkParser(
name,
downloadSize,
installSize,
Bloaty.parseNativeLibraryEntry(
bloaty.parseNativeLibraryEntry(
bytes,
debugFileForNativeLibrary(entryName = name)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ private const val COLUMN_SIZE = 3
*
* Bloaty Source Code Repository: [https://github.com/google/bloaty](https://github.com/google/bloaty)
*/
object Bloaty {
class Bloaty(val path: String? = null) {

private val bloatyPath: String? by lazy { findBloatyPath() }

private fun findBloatyPath(): String? {
val path = executeCommandAndGetOutput("which bloaty").singleOrNull()
val path = path ?: executeCommandAndGetOutput("which bloaty").singleOrNull()
return if (path.isNullOrEmpty()) {
println("Could not find Bloaty. Install Bloaty for more information about native libraries.")
null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ abstract class RulerTask : DefaultTask(), BaseRulerTask {
override fun provideUnstrippedLibraryFiles(): List<File> = unstrippedNativeFiles.get().map {
it.asFile
}

override fun provideBloatyPath() = null

private fun createApkFile(): Map<String, List<File>> {
val apkCreator = ApkCreator(project.rootDir)

Expand Down

0 comments on commit a8b4849

Please sign in to comment.