11from argparse import ArgumentParser
22from os import path
33from shutil import copyfile
4- from typing import Optional
4+ from typing import Optional , TypeGuard
55from kopyt import Parser
66from kopyt .node import (
77 CallSuffix ,
1414from kopyt .position import Position
1515
1616
17- def is_postfix_unary_expression (node ) -> bool :
17+ def is_postfix_unary_expression (node ) -> TypeGuard [ PostfixUnaryExpression ] :
1818 return isinstance (node , PostfixUnaryExpression )
1919
2020
21- def is_simple_identifier (node , value : Optional [str ] = None ) -> bool :
21+ def is_simple_identifier (
22+ node , value : Optional [str ] = None
23+ ) -> TypeGuard [SimpleIdentifier ]:
2224 if not isinstance (node , SimpleIdentifier ):
2325 return False
2426 if value is not None and node .value != value :
2527 return False
2628 return True
2729
2830
29- def is_call_suffix (node ) -> bool :
31+ def is_call_suffix (node ) -> TypeGuard [ CallSuffix ] :
3032 return isinstance (node , CallSuffix )
3133
3234
@@ -36,6 +38,18 @@ def get_call_suffix_with_lambda(node) -> Optional[CallSuffix]:
3638 return None
3739
3840
41+ def _has_release_value_arg (arguments ) -> bool :
42+ """Check if any ValueArgument wraps a LineStringLiteral '"release"'."""
43+ for arg in arguments :
44+ if not isinstance (arg , ValueArgument ):
45+ continue
46+ if not isinstance (arg .value , LineStringLiteral ):
47+ continue
48+ if arg .value .value == '"release"' :
49+ return True
50+ return False
51+
52+
3953def find_suffixes_with_lambda (postfix_node : PostfixUnaryExpression ) -> list [CallSuffix ]:
4054 result = []
4155 for suffix in postfix_node .suffixes :
@@ -68,14 +82,8 @@ def find_call_suffix_with_release_arg(
6882 continue
6983 if suffix .arguments is None :
7084 continue
71-
72- for arg in suffix .arguments :
73- if not isinstance (arg , ValueArgument ):
74- continue
75- if not isinstance (arg .value , LineStringLiteral ):
76- continue
77- if arg .value .value == '"release"' :
78- return suffix
85+ if _has_release_value_arg (suffix .arguments ):
86+ return suffix
7987
8088 return None
8189
@@ -87,6 +95,7 @@ def find_release_buildtype_suffix(
8795 call_suffix = get_call_suffix_with_lambda (buildtypes_suffix )
8896 if not call_suffix :
8997 continue
98+ assert call_suffix .lambda_expression is not None
9099
91100 buildtypes_lambda = call_suffix .lambda_expression .value
92101 buildtypes_expr = find_expression_by_name (
@@ -138,10 +147,10 @@ def find_release_buildtype_suffix(
138147use_signing_configs_code = 'signingConfig = signingConfigs.getByName("release")'
139148
140149for root_statement in gradle_build_script .statements :
141- if not is_postfix_unary_expression (root_statement .statement ):
150+ android_expr = root_statement .statement
151+ if not is_postfix_unary_expression (android_expr ):
142152 continue
143153
144- android_expr = root_statement .statement
145154 if not is_simple_identifier (android_expr .expression , "android" ):
146155 continue
147156
@@ -150,11 +159,13 @@ def find_release_buildtype_suffix(
150159 continue
151160
152161 android_call = call_suffixes [0 ]
153- android_lambda = android_call .lambda_expression .value
162+ android_lambda = android_call .lambda_expression
163+ if android_lambda is None :
164+ continue
154165
155- android_lambda .statements = [Parser ( signing_configs_code ). parse_statement ()] + list (
156- android_lambda . statements
157- )
166+ android_lambda .value . statements = [
167+ Parser ( signing_configs_code ). parse_statement ()
168+ ] + list ( android_lambda . value . statements )
158169
159170 release_suffix = find_release_buildtype_suffix (android_expr )
160171 if release_suffix and release_suffix .lambda_expression :
0 commit comments