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.cli.signature
18 
19 import com.android.tools.metalava.ApiType
20 import com.android.tools.metalava.DexApiWriter
21 import com.android.tools.metalava.OptionsDelegate
22 import com.android.tools.metalava.cli.common.MetalavaSubCommand
23 import com.android.tools.metalava.cli.common.SignatureFileLoader
24 import com.android.tools.metalava.cli.common.existingFile
25 import com.android.tools.metalava.cli.common.newFile
26 import com.android.tools.metalava.cli.common.progressTracker
27 import com.android.tools.metalava.createReportFile
28 import com.android.tools.metalava.model.noOpAnnotationManager
29 import com.android.tools.metalava.model.text.SignatureFile
30 import com.android.tools.metalava.model.visitors.ApiVisitor
31 import com.github.ajalt.clikt.parameters.arguments.argument
32 
33 class SignatureToDexCommand :
34     MetalavaSubCommand(
35         help = "Convert an API signature file into a file containing a list of DEX signatures.",
36     ) {
37 
38     private val apiFile by
39         argument(
40                 name = "<api-file>",
41                 help = "API signature file to convert to DEX signatures.",
42             )
43             .existingFile()
44 
45     private val dexFile by
46         argument(
47                 name = "<dex-file>",
48                 help = "Output DEX signatures file.",
49             )
50             .newFile()
51 
52     override fun run() {
53         // Make sure that none of the code called by this command accesses the global `options`
54         // property.
55         OptionsDelegate.disallowAccess()
56 
57         val signatureFileLoader = SignatureFileLoader(annotationManager = noOpAnnotationManager)
58         val signatureApi = signatureFileLoader.load(SignatureFile.fromFile(apiFile))
59 
60         val apiVisitorConfig = ApiVisitor.Config()
61         val apiPredicateConfig = apiVisitorConfig.apiPredicateConfig
62         val apiType = ApiType.ALL
63         val apiEmit = apiType.getEmitFilter(apiPredicateConfig)
64         val apiReference = apiType.getReferenceFilter(apiPredicateConfig)
65 
66         createReportFile(progressTracker, signatureApi, dexFile, "DEX API") { printWriter ->
67             DexApiWriter(
68                 printWriter,
69                 apiEmit,
70                 apiReference,
71                 apiVisitorConfig,
72             )
73         }
74     }
75 }
76