Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Depend on AGP API artifact only #94

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
[versions]
gradle = "7.0.4"
gradleVersion = "8.4.2"
junit = "4.13.2"
kotlin = "1.9.24"
Expand All @@ -10,7 +9,7 @@ mockitoKotlin = "2.2.0"
secretsGradlePlugin = "2.0.1"

[libraries]
gradle = { module = "com.android.tools.build:gradle", version.ref = "gradle" }
agp-api = "com.android.tools.build:gradle-api:7.0.4"
gradle-v842 = { module = "com.android.tools.build:gradle", version.ref = "gradleVersion" }
junit = { module = "junit:junit", version.ref = "junit" }
kotlin-gradle-plugin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlinGradlePlugin" }
Expand Down
4 changes: 2 additions & 2 deletions secrets-gradle-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ java {
}

dependencies {
compileOnly(libs.gradle)
compileOnly(libs.agp.api)
implementation(libs.kotlin.stdlib)
testImplementation(libs.gradle)
testImplementation(libs.agp.api)
testImplementation(libs.junit)
testImplementation(libs.mockito.kotlin)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ import com.android.build.api.variant.ApplicationAndroidComponentsExtension
import com.android.build.api.variant.BuildConfigField
import com.android.build.api.variant.LibraryAndroidComponentsExtension
import com.android.build.api.variant.Variant
import com.android.build.gradle.AppExtension
import com.android.build.gradle.LibraryExtension
import com.android.build.gradle.internal.core.InternalBaseVariant
import org.gradle.api.Project
import java.io.FileNotFoundException
import java.util.Properties
Expand All @@ -33,12 +30,6 @@ fun Project.androidAppComponent(): ApplicationAndroidComponentsExtension? =
fun Project.androidLibraryComponent(): LibraryAndroidComponentsExtension? =
extensions.findByType(LibraryAndroidComponentsExtension::class.java)

fun Project.androidProject(): AppExtension? =
extensions.findByType(AppExtension::class.java)

fun Project.libraryProject(): LibraryExtension? =
extensions.findByType(LibraryExtension::class.java)

fun Project.loadPropertiesFile(fileName: String): Properties {
// Load file
val propertiesFile = file(fileName)
Expand Down Expand Up @@ -73,20 +64,6 @@ fun Variant.inject(properties: Properties, ignore: List<String>) {
}
}

fun InternalBaseVariant.inject(properties: Properties, ignore: List<String>) {
val ignoreRegexs = ignore.map { Regex(pattern = it) }
properties.keys.map { key ->
key as String
}.filter { key ->
key.isNotEmpty() && !ignoreRegexs.any { it.containsMatchIn(key) }
}.forEach { key ->
val value = properties.getProperty(key).removeSurrounding("\"")
val translatedKey = key.replace(javaVarRegexp, "")
buildConfigField("String", translatedKey, value.addParenthesisIfNeeded())
mergedFlavor.manifestPlaceholders[translatedKey] = value
}
}

fun String.addParenthesisIfNeeded(): String {
if (isEmpty()) {
return this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package com.google.android.libraries.mapsplatform.secrets_gradle_plugin

import com.android.build.api.variant.Variant
import com.android.build.gradle.internal.core.InternalBaseVariant
import org.gradle.api.Plugin
import org.gradle.api.Project
import java.io.FileNotFoundException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@

package com.google.android.libraries.mapsplatform.secrets_gradle_plugin

import com.android.build.gradle.internal.core.InternalBaseVariant
import com.android.build.api.variant.BuildConfigField
import com.android.build.api.variant.Variant
import com.nhaarman.mockitokotlin2.doReturn
import com.nhaarman.mockitokotlin2.mock
import com.nhaarman.mockitokotlin2.verify
import org.gradle.api.Project
import org.gradle.api.ProjectConfigurationException
import org.gradle.api.internal.project.ProjectInternal
import org.gradle.testfixtures.ProjectBuilder
import org.junit.Assert
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Ignore
import org.junit.Rule
Expand All @@ -35,10 +36,9 @@ class SecretsPluginTest {
@JvmField
val tempFolder = TemporaryFolder()

lateinit var placeholders: MutableMap<String, Any>
lateinit var root: Project
lateinit var project: Project
lateinit var variant: InternalBaseVariant
lateinit var variant: Variant

@Before
fun setUp() {
Expand All @@ -51,12 +51,11 @@ class SecretsPluginTest {
.withName("project")
.withParent(root)
.build()
placeholders = mutableMapOf()
val flavor = mock<InternalBaseVariant.MergedFlavor> {
on { manifestPlaceholders } doReturn placeholders
}
variant = mock {
on { mergedFlavor } doReturn flavor
on { buildConfigFields } doReturn
project.objects.mapProperty(String::class.java, BuildConfigField::class.java)
on { manifestPlaceholders } doReturn
project.objects.mapProperty(String::class.java, String::class.java)
}
project.pluginManager.apply("com.google.android.libraries.mapsplatform.secrets-gradle-plugin")
}
Expand Down Expand Up @@ -157,14 +156,18 @@ class SecretsPluginTest {

private fun check(vararg keyValues: Pair<String, String>) {
keyValues.forEach { (key, value) ->
Assert.assertEquals(value, placeholders[key])
verify(variant).buildConfigField("String", key, value.addParenthesisIfNeeded())
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assertion step looks wrong. I guess you want to assert that the key-paired buildConfigField exists?

assertEquals(value, variant.manifestPlaceholders.getting(key).get())
variant.buildConfigFields.getting(key).get().let {
assertEquals("String", it.type)
assertEquals(value.addParenthesisIfNeeded(), it.value)
assertEquals(null, it.comment)
}
}
}

private fun checkKeysNotIn(vararg keys: String) {
keys.forEach {
Assert.assertFalse(placeholders.containsKey(it))
assertTrue(variant.manifestPlaceholders.getting(it).orNull == null)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can use assertFalse or assertNull here if you want.

}
}
}