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.GradleException
20 import org.gradle.api.Project
21 import org.gradle.api.artifacts.MinimalExternalModuleDependency
22 import org.gradle.api.artifacts.VersionCatalog
23 import org.gradle.api.artifacts.VersionCatalogsExtension
24 
25 val Project.versionCatalog: VersionCatalog
26     get() = project.extensions.getByType(VersionCatalogsExtension::class.java).find("libs").get()
27 
Projectnull28 fun Project.getLibraryByName(name: String): MinimalExternalModuleDependency {
29     val library = versionCatalog.findLibrary(name)
30     return if (library.isPresent) {
31         library.get().get()
32     } else {
33         throw GradleException("Could not find a library for `$name`")
34     }
35 }
36 
Projectnull37 fun Project.getVersionByName(name: String): String {
38     val version = versionCatalog.findVersion(name)
39     return if (version.isPresent) {
40         version.get().requiredVersion
41     } else {
42         throw GradleException("Could not find a version for `$name`")
43     }
44 }
45