Skip to content

Commit

Permalink
Support regex in ignoreList
Browse files Browse the repository at this point in the history
  • Loading branch information
arriolac committed Jan 21, 2021
1 parent 8be841a commit eca3a15
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
10 changes: 8 additions & 2 deletions plugin/src/main/java/com/google/secrets_plugin/SecretsPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<Project> {

Expand All @@ -25,10 +29,12 @@ class SecretsPlugin : Plugin<Project> {
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, "")
Expand Down
2 changes: 2 additions & 0 deletions sample-app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand All @@ -36,4 +37,5 @@ secrets {

// Add keys that the plugin should ignore from the properties file
ignoreList.add("keyToIgnore")
ignoreList.add("ignore*")
}
3 changes: 3 additions & 0 deletions secrets.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit eca3a15

Please sign in to comment.