1 /*
<lambda>null2  * Copyright 2018 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 androidx.build.gradle.isRoot
20 import java.io.File
21 import org.gradle.api.Project
22 import org.gradle.api.file.Directory
23 import org.gradle.api.file.RegularFile
24 import org.gradle.api.provider.Provider
25 
26 /**
27  * @return build id string for current build
28  *
29  * The build server does not pass the build id so we infer it from the last folder of the
30  * distribution directory name.
31  */
32 fun getBuildId(): String {
33     return if (System.getenv("BUILD_NUMBER") != null) {
34         System.getenv("BUILD_NUMBER")
35     } else {
36         "0"
37     }
38 }
39 
40 /**
41  * Gets set to true when the build id is prefixed with P.
42  *
43  * In AffectedModuleDetector, we return a different ProjectSubset in presubmit vs. postsubmit, to
44  * get the desired test behaviors.
45  */
isPresubmitBuildnull46 fun isPresubmitBuild(): Boolean {
47     return if (System.getenv("BUILD_NUMBER") != null) {
48         System.getenv("BUILD_NUMBER").startsWith("P")
49     } else {
50         false
51     }
52 }
53 
54 /**
55  * The DIST_DIR is where you want to save things from the build. The build server will copy the
56  * contents of DIST_DIR to somewhere and make it available.
57  */
Projectnull58 fun Project.getDistributionDirectory(): File {
59     val envVar = project.providers.environmentVariable("DIST_DIR").getOrElse("")
60     return if (envVar != "") {
61             File(envVar)
62         } else {
63             // Subdirectory of out directory (an ancestor of all files generated by the build)
64             File(getOutDirectory(), "dist")
65         }
66         .also { distDir -> distDir.mkdirs() }
67 }
68 
Projectnull69 fun Project.getOutDirectory(): File = extensions.extraProperties.get("outDir") as File
70 
71 /** Directory to put build info files for release service dependency files. */
72 fun Project.getBuildInfoDirectory(): File = File(getDistributionDirectory(), "build-info")
73 
74 /**
75  * Directory for android test configuration files that get consumed by Tradefed in CI. These configs
76  * cause all the tests to be run, except in cases where buildSrc changes.
77  */
78 fun Project.getTestConfigDirectory(): Provider<Directory> =
79     rootProject.layout.buildDirectory.dir("test-xml-configs")
80 
81 /** Directory for App APKs (from ApkOutputProviders) used in device tests. */
82 fun Project.getAppApksFilesDirectory(): Provider<Directory> =
83     rootProject.layout.buildDirectory.dir("app-apks-files")
84 
85 /** A file within [getTestConfigDirectory] */
86 fun Project.getFileInTestConfigDirectory(name: String): Provider<RegularFile> =
87     getTestConfigDirectory().map { it.file(name) }
88 
89 /** Directory to put host test results so they can be consumed by the testing dashboard. */
Projectnull90 fun Project.getHostTestResultDirectory(): File =
91     File(getDistributionDirectory(), "host-test-reports")
92 
93 /** Whether the build should force all versions to be snapshots. */
94 fun isSnapshotBuild() = System.getenv("SNAPSHOT") != null
95 
96 /** Directory in a maven format to put all the publishing libraries. */
97 fun Project.getRepositoryDirectory(): File {
98     val actualRootProject = if (project.isRoot) project else project.rootProject
99     val directory =
100         if (isSnapshotBuild()) {
101             // For snapshot builds we put artifacts directly where downstream users can find them.
102             File(actualRootProject.getDistributionDirectory(), "repository")
103         } else {
104             File(getOutDirectory(), "repository")
105         }
106     directory.mkdirs()
107     return directory
108 }
109 
110 /** Directory in a maven format to put per project publishing artifacts. */
Projectnull111 fun Project.getPerProjectRepositoryDirectory(): Provider<Directory> =
112     project.layout.buildDirectory.dir("repository")
113