1 /* 2 * 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.cli.common 18 19 import com.android.SdkConstants 20 import com.android.tools.metalava.model.PackageFilter 21 import com.github.ajalt.clikt.parameters.groups.OptionGroup 22 import com.github.ajalt.clikt.parameters.options.convert 23 import com.github.ajalt.clikt.parameters.options.option 24 import java.io.File 25 26 const val ARG_SOURCE_PATH = "--source-path" 27 28 const val ARG_STUB_PACKAGES = "--stub-packages" 29 30 /** The name of the group, can be used in help text to refer to the options in this group. */ 31 const val SOURCE_OPTIONS_GROUP = "Sources" 32 33 class SourceOptions : 34 OptionGroup( 35 name = SOURCE_OPTIONS_GROUP, 36 help = 37 """ 38 Options that control which source files will be processed. 39 """ 40 .trimIndent() 41 ) { 42 43 private val sourcePathString by 44 option( 45 ARG_SOURCE_PATH, 46 metavar = "<path>", 47 help = 48 """ 49 A ${File.pathSeparator} separated list of directories containing source 50 files (organized in a standard Java package hierarchy). 51 """ 52 .trimIndent(), 53 ) 54 55 internal val sourcePath by <lambda>null56 lazy(LazyThreadSafetyMode.NONE) { getSourcePath(ARG_SOURCE_PATH, sourcePathString) } 57 getSourcePathnull58 private fun getSourcePath(argName: String, path: String?) = 59 if (path == null) { 60 emptyList() 61 } else if (path.isBlank()) { 62 // Don't compute absolute path; we want to skip this file later on. 63 // For current directory one should use ".", not "". 64 listOf(File("")) 65 } else { <lambda>null66 path.split(File.pathSeparator).map { 67 if (it.endsWith(SdkConstants.DOT_JAVA)) { 68 cliError( 69 "$argName should point to a source root directory, not a source file ($it)" 70 ) 71 } 72 73 stringToExistingDir(it) 74 } 75 } 76 77 val apiPackages by 78 option( 79 ARG_STUB_PACKAGES, 80 metavar = "<package-list>", 81 help = 82 """ 83 List of packages (separated by ${File.pathSeparator}) which will be used to 84 filter out irrelevant classes. If specified, only classes in these packages 85 will be included in signature files, stubs, etc.. This is not limited to 86 just the stubs; the $ARG_STUB_PACKAGES name is historical. 87 88 See `metalava help package-filters` for more information. 89 """ 90 .trimIndent() 91 ) <lambda>null92 .convert { PackageFilter.parse(it) } 93 } 94