1 /*
<lambda>null2  * Copyright (C) 2023 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 com.android.tools.metalava
18 
19 import com.android.tools.metalava.cli.common.MetalavaCliException
20 import com.android.tools.metalava.cli.common.MetalavaSubCommand
21 import com.android.tools.metalava.cli.common.executionEnvironment
22 import com.android.tools.metalava.cli.common.existingDir
23 import com.android.tools.metalava.cli.common.progressTracker
24 import com.android.tools.metalava.cli.common.stderr
25 import com.android.tools.metalava.cli.common.stdout
26 import com.android.tools.metalava.cli.signature.SignatureFormatOptions
27 import com.android.tools.metalava.reporter.BasicReporter
28 import com.github.ajalt.clikt.parameters.arguments.argument
29 import com.github.ajalt.clikt.parameters.arguments.validate
30 import com.github.ajalt.clikt.parameters.groups.provideDelegate
31 
32 private const val ARG_ANDROID_ROOT_DIR = "<android-root-dir>"
33 
34 class AndroidJarsToSignaturesCommand :
35     MetalavaSubCommand(
36         help =
37             """
38     Rewrite the signature files in the `prebuilts/sdk` directory in the Android source tree.
39 
40     It does this by reading the API defined in the corresponding `android.jar` files.
41 """
42                 .trimIndent(),
43     ) {
44 
45     private val androidRootDir by
46         argument(
47                 ARG_ANDROID_ROOT_DIR,
48                 help =
49                     """
50         The root directory of the Android source tree. The new signature files will be generated in
51         the `prebuilts/sdk/<api>/public/api/android.txt` sub-directories.
52     """
53                         .trimIndent()
54             )
55             .existingDir()
56             .validate {
57                 require(it.resolve("prebuilts/sdk").isDirectory) {
58                     throw MetalavaCliException(
59                         "$ARG_ANDROID_ROOT_DIR does not point to an Android source tree"
60                     )
61                 }
62             }
63 
64     /** Add options for controlling the format of the generated files. */
65     private val signatureFormat by SignatureFormatOptions()
66 
67     override fun run() {
68         // Make sure that none of the code called by this command accesses the global `options`
69         // property.
70         OptionsDelegate.disallowAccess()
71 
72         StandaloneJarCodebaseLoader.create(
73                 executionEnvironment,
74                 progressTracker,
75                 BasicReporter(stderr),
76             )
77             .use { jarCodebaseLoader ->
78                 ConvertJarsToSignatureFiles(
79                         stderr,
80                         stdout,
81                         progressTracker,
82                         signatureFormat.fileFormat,
83                     )
84                     .convertJars(jarCodebaseLoader, androidRootDir)
85             }
86     }
87 }
88