From eca3a151ffbde47ad546771eac32e46277cd4afd Mon Sep 17 00:00:00 2001 From: Chris Arriola Date: Thu, 21 Jan 2021 14:41:45 -0800 Subject: [PATCH] Support regex in ignoreList --- .../java/com/google/secrets_plugin/SecretsPlugin.kt | 10 ++++++++-- sample-app/build.gradle.kts | 2 ++ secrets.properties | 3 +++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/plugin/src/main/java/com/google/secrets_plugin/SecretsPlugin.kt b/plugin/src/main/java/com/google/secrets_plugin/SecretsPlugin.kt index fc357ac..764efe0 100644 --- a/plugin/src/main/java/com/google/secrets_plugin/SecretsPlugin.kt +++ b/plugin/src/main/java/com/google/secrets_plugin/SecretsPlugin.kt @@ -5,7 +5,11 @@ import org.gradle.api.Project /** * Plugin that reads secrets from a properties file and injects manifest build and BuildConfig - * variables into an Android project. + * variables into an Android project. Since property keys are turned into Java variables, + * invalid variable characters from the property key are removed. + * + * e.g. + * A key defined as "sdk.dir" in the properties file will be converted to "sdkdir". */ class SecretsPlugin : Plugin { @@ -25,10 +29,12 @@ class SecretsPlugin : Plugin { extension.propertiesFileName ) + val ignoreRegexs = extension.ignoreList.map { Regex(pattern = it) } + properties.keys.map { key -> key as String }.filter { key -> - key.isNotEmpty() && !extension.ignoreList.contains(key) + key.isNotEmpty() && !ignoreRegexs.any { it.containsMatchIn(key) } }.forEach { key -> val value = properties.getProperty(key) val translatedKey = key.replace(javaVarRegexp, "") diff --git a/sample-app/build.gradle.kts b/sample-app/build.gradle.kts index 9b42196..0360a9a 100644 --- a/sample-app/build.gradle.kts +++ b/sample-app/build.gradle.kts @@ -25,6 +25,7 @@ android { dependencies { implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.4.10") + implementation("com.google.android.material:material:1.2.1") implementation("androidx.appcompat:appcompat:1.2.0") } @@ -36,4 +37,5 @@ secrets { // Add keys that the plugin should ignore from the properties file ignoreList.add("keyToIgnore") + ignoreList.add("ignore*") } diff --git a/secrets.properties b/secrets.properties index c19b261..2b16363 100644 --- a/secrets.properties +++ b/secrets.properties @@ -2,3 +2,6 @@ # This file is checked into version control for the sake of demonstrating sample contents. # In practice, your properties file that contains secrets *should not* be under version control. gmpApiKey="API_KEY_VALUE" +keyToIgnore="This key is ignored" +ignoreKey1="This key is ignored" +ignoreKey2="This key is ignored, too"