|
| 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 | +// Evaluate if we are in a CI environment |
| 20 | +val isCI = System.getenv("CI")?.toBoolean() ?: false |
| 21 | + |
| 22 | +// Share the isCI flag with all subprojects via Gradle's extra properties |
| 23 | +extra["isCI"] = isCI |
| 24 | + |
| 25 | +if (!isCI) { |
| 26 | + val secretsFile = file("secrets.properties") |
| 27 | + val requestedTasks = gradle.startParameter.taskNames |
| 28 | + |
| 29 | + if (requestedTasks.isEmpty() && !secretsFile.exists()) { |
| 30 | + // It's likely an IDE sync if no tasks are specified, so just issue a warning. |
| 31 | + println("Warning: secrets.properties not found. Gradle sync may succeed, but building/running the app will fail.") |
| 32 | + } else if (requestedTasks.isNotEmpty()) { |
| 33 | + val buildTaskKeywords = setOf("build", "install", "assemble") |
| 34 | + val testTaskKeywords = setOf("test", "report", "lint") |
| 35 | + |
| 36 | + val isBuildTask = requestedTasks.any { name -> |
| 37 | + buildTaskKeywords.any { kw -> name.contains(kw, ignoreCase = true) } |
| 38 | + } |
| 39 | + val isTestTask = requestedTasks.any { name -> |
| 40 | + testTaskKeywords.any { kw -> name.contains(kw, ignoreCase = true) } |
| 41 | + } |
| 42 | + val isDebugTask = requestedTasks.any { task -> |
| 43 | + task.contains("Debug", ignoreCase = true) || task.contains("installAndLaunch", ignoreCase = true) |
| 44 | + } |
| 45 | + |
| 46 | + if (isBuildTask && !isTestTask && isDebugTask) { |
| 47 | + val defaultsFile = file("local.defaults.properties") |
| 48 | + val requiredKeysMessage = if (defaultsFile.exists()) { |
| 49 | + defaultsFile.readText() |
| 50 | + } else { |
| 51 | + "MAPS3D_API_KEY=<YOUR_API_KEY>\nPLACES_API_KEY=<YOUR_API_KEY>" |
| 52 | + } |
| 53 | + |
| 54 | + if (!secretsFile.exists()) { |
| 55 | + 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") |
| 56 | + } |
| 57 | + |
| 58 | + val secrets = Properties() |
| 59 | + secretsFile.inputStream().use { secrets.load(it) } |
| 60 | + val mapsApiKey = secrets.getProperty("MAPS3D_API_KEY") |
| 61 | + val placesApiKey = secrets.getProperty("PLACES_API_KEY") |
| 62 | + |
| 63 | + if (mapsApiKey.isNullOrBlank() || !mapsApiKey.matches(Regex("^AIza[a-zA-Z0-9_-]{35}$"))) { |
| 64 | + throw GradleException("Invalid or missing MAPS3D_API_KEY in secrets.properties. Please provide a valid Google Maps API key (starts with 'AIza').") |
| 65 | + } |
| 66 | + |
| 67 | + if (placesApiKey.isNullOrBlank() || !placesApiKey.matches(Regex("^AIza[a-zA-Z0-9_-]{35}$"))) { |
| 68 | + throw GradleException("Invalid or missing PLACES_API_KEY in secrets.properties. Please provide a valid Google Places API key (starts with 'AIza').") |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | +} |
0 commit comments