Skip to content

Commit

Permalink
Enable verification configuration through the plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
MrHadiSatrio authored and nathan3d committed Nov 21, 2024
1 parent 00b6a7e commit f805d92
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@

package com.spotify.ruler.common.veritication

import java.io.Serializable
import kotlinx.serialization.Serializable as KSerializable

@KSerializable
data class VerificationConfig(
val downloadSizeThreshold: Long = Long.MAX_VALUE,
val installSizeThreshold: Long = Long.MAX_VALUE
)
) : Serializable {
companion object {
private const val serialVersionUID = 1L
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ import com.android.build.api.variant.ApplicationAndroidComponentsExtension
import com.android.build.api.variant.ApplicationVariant
import com.spotify.ruler.common.models.AppInfo
import com.spotify.ruler.common.models.DeviceSpec
import com.spotify.ruler.common.veritication.VerificationConfig
import org.codehaus.groovy.runtime.StringGroovyMethods
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.file.RegularFile
import org.gradle.api.plugins.ExtensionAware
import org.gradle.api.provider.Provider

class RulerPlugin : Plugin<Project> {
Expand All @@ -34,6 +36,10 @@ class RulerPlugin : Plugin<Project> {
@Suppress("UnstableApiUsage")
override fun apply(project: Project) {
val rulerExtension = project.extensions.create(name, RulerExtension::class.java)
val rulerVerificationExtension = (rulerExtension as ExtensionAware).extensions.create(
"verification",
RulerVerificationExtension::class.java
)

project.plugins.withId("com.android.application") {
val androidComponents =
Expand Down Expand Up @@ -61,6 +67,8 @@ class RulerPlugin : Plugin<Project> {
task.omitFileBreakdown.set(rulerExtension.omitFileBreakdown)
task.unstrippedNativeFiles.set(rulerExtension.unstrippedNativeFiles)

task.verificationConfig.set(getVerificationConfig(rulerVerificationExtension))

// Add explicit dependency to support DexGuard
task.dependsOn("bundle$variantName")
}
Expand Down Expand Up @@ -161,6 +169,13 @@ class RulerPlugin : Plugin<Project> {
}
}

private fun getVerificationConfig(extension: RulerVerificationExtension): VerificationConfig {
return VerificationConfig(
downloadSizeThreshold = extension.downloadSizeThreshold.get(),
installSizeThreshold = extension.installSizeThreshold.get()
)
}

/** Checks if the given [project] is using DexGuard for obfuscation, instead of R8. */
private fun hasDexGuard(project: Project): Boolean {
return project.pluginManager.hasPlugin("dexguard")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import com.spotify.ruler.common.models.AppInfo
import com.spotify.ruler.common.models.DeviceSpec
import com.spotify.ruler.common.models.RulerConfig
import com.spotify.ruler.common.sanitizer.ClassNameSanitizer
import com.spotify.ruler.common.veritication.VerificationConfig
import com.spotify.ruler.plugin.dependency.EntryParser
import org.gradle.api.DefaultTask
import org.gradle.api.file.DirectoryProperty
Expand Down Expand Up @@ -77,6 +78,9 @@ abstract class RulerTask : DefaultTask(), BaseRulerTask {
@get:InputFile
abstract val staticDependenciesFile: RegularFileProperty

@get:Input
abstract val verificationConfig: Property<VerificationConfig>

@get:OutputDirectory
abstract val workingDir: DirectoryProperty

Expand All @@ -101,7 +105,7 @@ abstract class RulerTask : DefaultTask(), BaseRulerTask {
omitFileBreakdown = omitFileBreakdown.get(),
additionalEntries = emptyList(),
ignoredFiles = emptyList(),
verificationConfig = null
verificationConfig = verificationConfig.get()
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2024 Spotify AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.spotify.ruler.plugin

import org.gradle.api.model.ObjectFactory
import org.gradle.api.provider.Property

open class RulerVerificationExtension(objects: ObjectFactory) {
val downloadSizeThreshold: Property<Long> = objects.property(Long::class.java)
val installSizeThreshold: Property<Long> = objects.property(Long::class.java)

init {
downloadSizeThreshold.convention(Long.MAX_VALUE)
installSizeThreshold.convention(Long.MAX_VALUE)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,30 @@ class RulerIntegrationTest {
gradlew(task, expectFailure = true)
}

@ParameterizedTest
@ValueSource(strings = [":app:analyzeDebugBundle", ":app:analyzeReleaseBundle"])
fun `Bundle analysis fails if the download size threshold is set and breached`(task: String) {
val buildGradle = projectDir.resolve("app/build.gradle")
buildGradle.writeText(
buildGradle.readText()
.substringBeforeLast("}") + "verification { downloadSizeThreshold = 100L } }"
)

gradlew(task, expectFailure = true)
}

@ParameterizedTest
@ValueSource(strings = [":app:analyzeDebugBundle", ":app:analyzeReleaseBundle"])
fun `Bundle analysis fails if the install size threshold is set and breached`(task: String) {
val buildGradle = projectDir.resolve("app/build.gradle")
buildGradle.writeText(
buildGradle.readText()
.substringBeforeLast("}") + "verification { installSizeThreshold = 100L } }"
)

gradlew(task, expectFailure = false)
}

private fun gradlew(vararg arguments: String, expectFailure: Boolean = false): BuildResult {
val runner = GradleRunner.create()
.withProjectDir(projectDir)
Expand Down
5 changes: 5 additions & 0 deletions sample/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ ruler {

ownershipFile.set(project.layout.projectDirectory.file("ownership.yaml"))
defaultOwner.set("default-team")

verification {
downloadSizeThreshold = 20 * 1000 * 1000
installSizeThreshold = 20 * 1000 * 1000
}
}

// Include Ruler tasks in checks
Expand Down

0 comments on commit f805d92

Please sign in to comment.