1 /*
<lambda>null2  * Copyright 2021 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.testConfiguration
18 
19 import androidx.build.getDistributionDirectory
20 import androidx.build.getSupportRootFolder
21 import com.google.gson.GsonBuilder
22 import java.io.File
23 import org.gradle.api.DefaultTask
24 import org.gradle.api.Project
25 import org.gradle.api.file.RegularFileProperty
26 import org.gradle.api.tasks.CacheableTask
27 import org.gradle.api.tasks.Input
28 import org.gradle.api.tasks.Internal
29 import org.gradle.api.tasks.OutputFile
30 import org.gradle.api.tasks.TaskAction
31 import org.gradle.api.tasks.bundling.Zip
32 
33 @CacheableTask
34 abstract class ModuleInfoGenerator : DefaultTask() {
35     @get:OutputFile abstract val outputFile: RegularFileProperty
36 
37     @get:Internal val testModules: MutableList<TestModule> = mutableListOf()
38 
39     @Input
40     fun getSerialized(): String {
41         val gson = GsonBuilder().setPrettyPrinting().create()
42         val data = testModules.associateBy { it.name }
43         return gson.toJson(data)
44     }
45 
46     @TaskAction
47     fun writeModuleInfo() {
48         val file = outputFile.get().asFile
49         file.parentFile.mkdirs()
50         file.writeText(getSerialized())
51     }
52 }
53 
54 /**
55  * Register two tasks needed to generate information for Android test owners service. One task zips
56  * all the OWNERS files in frameworks/support, and second task creates a module-info.json that links
57  * test modules to paths.
58  */
registerOwnersServiceTasksnull59 internal fun Project.registerOwnersServiceTasks() {
60     tasks.register("zipOwnersFiles", Zip::class.java) { task ->
61         task.archiveFileName.set("owners.zip")
62         task.destinationDirectory.set(getDistributionDirectory())
63         task.from(layout.projectDirectory)
64         task.include("**/OWNERS")
65         task.exclude("buildSrc/.gradle/**")
66         task.exclude(".gradle/**")
67         task.exclude("build/reports/**")
68         task.includeEmptyDirs = false
69     }
70 
71     tasks.register(CREATE_MODULE_INFO, ModuleInfoGenerator::class.java) { task ->
72         task.outputFile.set(File(getDistributionDirectory(), "module-info.json"))
73     }
74 }
75 
addToModuleInfonull76 internal fun Project.addToModuleInfo(testName: String, projectIsolationEnabled: Boolean) {
77     if (!projectIsolationEnabled) {
78         rootProject.tasks.named(CREATE_MODULE_INFO).configure {
79             it as ModuleInfoGenerator
80             it.testModules.add(
81                 TestModule(
82                     name = testName,
83                     path = listOf(projectDir.toRelativeString(getSupportRootFolder()))
84                 )
85             )
86         }
87     }
88 }
89 
90 data class TestModule(val name: String, val path: List<String>)
91 
92 private const val CREATE_MODULE_INFO = "createModuleInfo"
93