|
| 1 | +/* |
| 2 | + * Copyright 2026 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import java.util.Properties |
| 18 | + |
| 19 | +/** |
| 20 | + * This script enforces that required API keys are present in a `secrets.properties` file |
| 21 | + * before allowing build or install tasks to proceed. It ignores syncs and test tasks. |
| 22 | + * |
| 23 | + * Usage in build.gradle.kts: |
| 24 | + * |
| 25 | + * extra["requiredSecrets"] = listOf("MAPS3D_API_KEY", "PLACES_API_KEY") |
| 26 | + * apply(from = "gradle/secrets-enforcement.gradle.kts") |
| 27 | + */ |
| 28 | + |
| 29 | +// Use the provided isCI flag if available, otherwise detect it from environment variables |
| 30 | +val isCI = if (project.extra.has("isCI")) { |
| 31 | + project.extra["isCI"] as? Boolean ?: false |
| 32 | +} else { |
| 33 | + System.getenv("CI")?.toBoolean() ?: false |
| 34 | +} |
| 35 | + |
| 36 | +if (!isCI) { |
| 37 | + val secretsFile = rootProject.file("secrets.properties") |
| 38 | + val requestedTasks = gradle.startParameter.taskNames |
| 39 | + @Suppress("UNCHECKED_CAST") |
| 40 | + val requiredKeys = (project.extra["requiredSecrets"] as? List<String>) ?: emptyList() |
| 41 | + |
| 42 | + if (requestedTasks.isEmpty() && !secretsFile.exists()) { |
| 43 | + // It's likely an IDE sync if no tasks are specified, so just issue a warning. |
| 44 | + println("Warning: secrets.properties not found. Gradle sync may succeed, but building/running the app will fail.") |
| 45 | + } else if (requestedTasks.isNotEmpty()) { |
| 46 | + val buildTaskKeywords = setOf("build", "install", "assemble") |
| 47 | + val testTaskKeywords = setOf("test", "report", "lint") |
| 48 | + |
| 49 | + val isBuildTask = requestedTasks.any { name -> |
| 50 | + buildTaskKeywords.any { kw -> name.contains(kw, ignoreCase = true) } |
| 51 | + } |
| 52 | + val isTestTask = requestedTasks.any { name -> |
| 53 | + testTaskKeywords.any { kw -> name.contains(kw, ignoreCase = true) } |
| 54 | + } |
| 55 | + val isDebugTask = requestedTasks.any { task -> |
| 56 | + task.contains("Debug", ignoreCase = true) || task.contains("installAndLaunch", ignoreCase = true) |
| 57 | + } |
| 58 | + |
| 59 | + if (isBuildTask && !isTestTask && isDebugTask) { |
| 60 | + val defaultsFile = rootProject.file("local.defaults.properties") |
| 61 | + val requiredKeysMessage = if (defaultsFile.exists()) { |
| 62 | + defaultsFile.readText() |
| 63 | + } else { |
| 64 | + requiredKeys.joinToString("\n") { "$it=<YOUR_API_KEY>" } |
| 65 | + } |
| 66 | + |
| 67 | + if (!secretsFile.exists()) { |
| 68 | + throw GradleException("secrets.properties file not found. Please create a 'secrets.properties' file in the root project directory with the following content:\n\n$requiredKeysMessage") |
| 69 | + } |
| 70 | + |
| 71 | + val secrets = Properties() |
| 72 | + secretsFile.inputStream().use { secrets.load(it) } |
| 73 | + |
| 74 | + requiredKeys.forEach { key -> |
| 75 | + val value = secrets.getProperty(key) |
| 76 | + if (value.isNullOrBlank() || !value.matches(Regex("^AIza[a-zA-Z0-9_-]{35}$"))) { |
| 77 | + throw GradleException("Invalid or missing $key in secrets.properties. Please provide a valid Google API key (starts with 'AIza').") |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments