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.metalava
18 
19 import androidx.build.Version
20 import androidx.build.checkapi.ApiLocation
21 import java.io.File
22 import javax.inject.Inject
23 import org.gradle.api.file.Directory
24 import org.gradle.api.provider.Property
25 import org.gradle.api.tasks.CacheableTask
26 import org.gradle.api.tasks.Input
27 import org.gradle.api.tasks.InputFiles
28 import org.gradle.api.tasks.Internal
29 import org.gradle.api.tasks.OutputFiles
30 import org.gradle.api.tasks.PathSensitive
31 import org.gradle.api.tasks.PathSensitivity
32 import org.gradle.api.tasks.TaskAction
33 import org.gradle.workers.WorkerExecutor
34 
35 /**
36  * Generate API signature text files from a set of source files, and an API version history JSON
37  * file from the previous API signature files.
38  */
39 @CacheableTask
40 internal abstract class GenerateApiTask @Inject constructor(workerExecutor: WorkerExecutor) :
41     SourceMetalavaTask(workerExecutor) {
42 
43     @get:Input var generateRestrictToLibraryGroupAPIs = true
44 
45     /** Collection of text files to which API signatures will be written. */
46     @get:Internal // already expressed by getTaskOutputs()
47     abstract val apiLocation: Property<ApiLocation>
48 
49     @OutputFiles
50     fun getTaskOutputs(): List<File> {
51         val prop = apiLocation.get()
52         return listOf(prop.publicApiFile, prop.restrictedApiFile, prop.apiLevelsFile)
53     }
54 
55     @get:Internal abstract val currentVersion: Property<Version>
56 
57     /**
58      * The directory where past API files are stored. Not all files in the directory are used, they
59      * are filtered in [getPastApiFiles].
60      */
61     @get:Internal abstract var projectApiDirectory: Directory
62 
63     /** An ordered list of the API files to use in generating the API level metadata JSON. */
64     @InputFiles
65     @PathSensitive(PathSensitivity.NONE)
66     fun getPastApiFiles(): List<File> {
67         return getFilesForApiLevels(projectApiDirectory.asFileTree.files, currentVersion.get())
68     }
69 
70     @TaskAction
71     fun exec() {
72         check(bootClasspath.files.isNotEmpty()) { "Android boot classpath not set." }
73         check(sourcePaths.files.isNotEmpty()) { "Source paths not set." }
74         check(compiledSources.files.isNotEmpty()) {
75             "Compiled sources " + compiledSources + " is empty!"
76         }
77         compiledSources.files.forEach { compiled ->
78             check(compiled.exists()) { "File " + compiled + " does not exist" }
79         }
80 
81         val levelsArgs =
82             getGenerateApiLevelsArgs(
83                 projectApiDirectory.asFile,
84                 getPastApiFiles(),
85                 currentVersion.get(),
86                 apiLocation.get().apiLevelsFile
87             )
88 
89         generateApi(
90             metalavaClasspath,
91             createProjectXmlFile(),
92             sourcePaths.files,
93             apiLocation.get(),
94             ApiLintMode.CheckBaseline(baselines.get().apiLintFile, targetsJavaConsumers.get()),
95             generateRestrictToLibraryGroupAPIs,
96             levelsArgs,
97             k2UastEnabled.get(),
98             kotlinSourceLevel.get(),
99             workerExecutor,
100             manifestPath.orNull?.asFile?.absolutePath
101         )
102     }
103 }
104