1 /*
<lambda>null2  * Copyright (C) 2024 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.MetalavaSubCommand
20 import com.android.tools.metalava.cli.common.executionEnvironment
21 import com.android.tools.metalava.cli.common.existingFile
22 import com.android.tools.metalava.cli.common.newFile
23 import com.android.tools.metalava.cli.common.progressTracker
24 import com.android.tools.metalava.cli.common.stderr
25 import com.android.tools.metalava.model.visitors.ApiVisitor
26 import com.android.tools.metalava.reporter.BasicReporter
27 import com.github.ajalt.clikt.parameters.arguments.argument
28 
29 /** Generates a JDiff XML file from a jar. */
30 class JarToJDiffCommand :
31     MetalavaSubCommand(
32         help =
33             """
34                 Convert a jar file into a file in the JDiff XML format.
35 
36                 This is intended for use by the coverage team to extract information needed to
37                 determine test coverage of the API from the stubs jars. Any other use is
38                 unsupported.
39             """
40                 .trimIndent()
41     ) {
42 
43     private val jarFile by
44         argument(
45                 name = "<jar-file>",
46                 help =
47                     """
48                         Jar file to convert to the JDiff XML format.
49                     """
50                         .trimIndent()
51             )
52             .existingFile()
53 
54     private val xmlFile by
55         argument(
56                 name = "<xml-file>",
57                 help =
58                     """
59                         Output JDiff XML format file.
60                     """
61                         .trimIndent()
62             )
63             .newFile()
64 
65     override fun run() {
66         // Make sure that none of the code called by this command accesses the global `options`
67         // property.
68         OptionsDelegate.disallowAccess()
69 
70         StandaloneJarCodebaseLoader.create(
71                 executionEnvironment,
72                 progressTracker,
73                 BasicReporter(stderr)
74             )
75             .use { jarCodebaseLoader ->
76                 val codebase = jarCodebaseLoader.loadFromJarFile(jarFile)
77 
78                 val apiType = ApiType.PUBLIC_API
79                 val apiPredicateConfig = ApiPredicate.Config()
80                 val apiEmit = apiType.getEmitFilter(apiPredicateConfig)
81                 val apiReference = apiType.getReferenceFilter(apiPredicateConfig)
82 
83                 createReportFile(progressTracker, codebase, xmlFile, "JDiff File") { printWriter ->
84                     JDiffXmlWriter(
85                         writer = printWriter,
86                         filterEmit = apiEmit,
87                         filterReference = apiReference,
88                         preFiltered = false,
89                         showUnannotated = false,
90                         config = ApiVisitor.Config(),
91                     )
92                 }
93             }
94     }
95 }
96