1# Copyright 2022 Google LLC. All rights reserved. 2# 3# Licensed under the Apache License, Version 2.0 (the License); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15"""kt_analysis""" 16 17load("//:visibility.bzl", "RULES_KOTLIN") 18 19visibility(RULES_KOTLIN) 20 21def _get_action(actions, mnemonic): 22 """Get a specific action 23 24 Args: 25 actions: [List[Action]] 26 mnemonic: [string] Identify the action whose args to search 27 28 Returns: 29 [Action|None] The arg value, or None if it couldn't be found 30 """ 31 menmonic_actions = [a for a in actions if a.mnemonic == mnemonic] 32 if len(menmonic_actions) == 0: 33 return None 34 elif len(menmonic_actions) > 1: 35 fail("Expected a single '%s' action" % mnemonic) 36 37 return menmonic_actions[0] 38 39def _get_all_args(action, arg_name, style = "trim"): 40 """Gets values for all instances of an arg name from a specific action. 41 42 Args: 43 action: [Action|None] 44 arg_name: [string] 45 style: ["trim"|"next"|"list"] The style of commandline arg 46 47 Returns: 48 [list[string]|list[list[string]]|None] The list of matching arg values 49 """ 50 if not action: 51 return [] 52 53 args = action.argv 54 matches = [(i, a) for (i, a) in enumerate(args) if a.startswith(arg_name)] 55 56 result = [] 57 for index, arg in matches: 58 if style == "trim": 59 result.append(arg[len(arg_name):]) 60 elif style == "next": 61 result.append(args[index + 1]) 62 elif style == "list": 63 sub_result = [] 64 for i in range(index + 1, len(args)): 65 if args[i].startswith("--"): 66 break 67 sub_result.append(args[i]) 68 result.append(sub_result) 69 else: 70 fail("Unrecognized arg style '%s" % style) 71 72 return result 73 74def _get_arg(action, arg_name, style = "trim"): 75 """Gets values for exactly one instance of an arg name from a specific action. 76 77 Args: 78 action: [Action|None] 79 arg_name: [string] 80 style: ["trim"|"next"|"list"] The style of commandline arg 81 82 Returns: 83 [string|list[string]|None] The arg value, or None if it couldn't be found 84 """ 85 results = _get_all_args(action, arg_name, style) 86 87 if len(results) == 0: 88 return None 89 elif len(results) == 1: 90 return results[0] 91 else: 92 fail("Expected a single '%s' arg" % arg_name) 93 94def _check_endswith_test(ctx): 95 name = ctx.label.name 96 for i in range(0, 10): 97 # TODO: Remove support for suffix digits 98 if name.endswith(str(i)): 99 name = name.removesuffix(str(i)) 100 break 101 if name.endswith("_test"): 102 return 103 104 fail("Analysis test names must end in '_test'") 105 106kt_analysis = struct( 107 # go/keep-sorted start 108 DEFAULT_LIST = ["__default__"], 109 check_endswith_test = _check_endswith_test, 110 get_action = _get_action, 111 get_all_args = _get_all_args, 112 get_arg = _get_arg, 113 # go/keep-sorted end 114) 115