1 /*
2  * Copyright 2022 The Android Open Source Project
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 package androidx.build
18 
19 import org.gradle.api.Project
20 import org.gradle.api.UnknownProjectException
21 
22 // Resolves the given project, and if it is not found,
23 // throws an exception that mentions the active project subset, if any (MAIN, COMPOSE, ...)
Projectnull24 fun Project.resolveProject(projectSpecification: String): Project {
25     try {
26         return project.project(projectSpecification)
27     } catch (e: UnknownProjectException) {
28         val subset = project.getProjectSubset()
29         val subsetDescription =
30             if (subset == null) {
31                 ""
32             } else {
33                 " in subset $subset"
34             }
35         throw UnknownProjectException(
36             "Project $projectSpecification not found$subsetDescription",
37             e
38         )
39     }
40 }
41 
42 /**
43  * Returns the name of the subset of projects participating in the build.
44  *
45  * Project subsets are defined in settings.gradle and allow including only a subset of projects in
46  * the build, to make project configuration run more quickly.
47  */
Projectnull48 fun Project.getProjectSubset(): String? {
49     val envProp = project.providers.environmentVariable("ANDROIDX_PROJECTS")
50     if (envProp.isPresent) {
51         return envProp.get().uppercase()
52     }
53     return null
54 }
55