forked from moezbhatti/qksms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate_synthetics.py
More file actions
279 lines (233 loc) · 10.4 KB
/
Copy pathmigrate_synthetics.py
File metadata and controls
279 lines (233 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#!/usr/bin/env python3
"""
Migrate Kotlin Android Synthetic view references to findViewById.
Reads a build error log, identifies unresolved view IDs, looks up their types
from layout XMLs, and adds lazy findViewById properties to each class.
"""
import xml.etree.ElementTree as ET
import os
import re
import sys
from collections import defaultdict
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
LAYOUT_DIR = os.path.join(PROJECT_ROOT, 'presentation/src/main/res/layout')
SRC_ROOT = os.path.join(PROJECT_ROOT, 'presentation/src')
ANDROID_NS = '{http://schemas.android.com/apk/res/android}'
# FQCN mapping for short class names used in XML
WIDGET_IMPORTS = {
'Toolbar': 'androidx.appcompat.widget.Toolbar',
'RecyclerView': 'androidx.recyclerview.widget.RecyclerView',
'AppBarLayout': 'com.google.android.material.appbar.AppBarLayout',
'CollapsingToolbarLayout': 'com.google.android.material.appbar.CollapsingToolbarLayout',
'TabLayout': 'com.google.android.material.tabs.TabLayout',
'ViewPager': 'androidx.viewpager.widget.ViewPager',
'ViewPager2': 'androidx.viewpager2.widget.ViewPager2',
'FloatingActionButton': 'com.google.android.material.floatingactionbutton.FloatingActionButton',
'SwipeRefreshLayout': 'androidx.swiperefreshlayout.widget.SwipeRefreshLayout',
'CoordinatorLayout': 'androidx.coordinatorlayout.widget.CoordinatorLayout',
'NestedScrollView': 'androidx.core.widget.NestedScrollView',
'CardView': 'androidx.cardview.widget.CardView',
'ConstraintLayout': 'androidx.constraintlayout.widget.ConstraintLayout',
'Group': 'androidx.constraintlayout.widget.Group',
'Guideline': 'androidx.constraintlayout.widget.Guideline',
'ChangeHandlerFrameLayout': 'com.bluelinelabs.conductor.ChangeHandlerFrameLayout',
# Default android.widget.* and android.view.*
'View': 'android.view.View',
'ViewGroup': 'android.view.ViewGroup',
'Button': 'android.widget.Button',
'TextView': 'android.widget.TextView',
'EditText': 'android.widget.EditText',
'ImageView': 'android.widget.ImageView',
'ImageButton': 'android.widget.ImageButton',
'LinearLayout': 'android.widget.LinearLayout',
'RelativeLayout': 'android.widget.RelativeLayout',
'FrameLayout': 'android.widget.FrameLayout',
'ProgressBar': 'android.widget.ProgressBar',
'SeekBar': 'android.widget.SeekBar',
'Spinner': 'android.widget.Spinner',
'Switch': 'android.widget.Switch',
'CheckBox': 'android.widget.CheckBox',
'ScrollView': 'android.widget.ScrollView',
'HorizontalScrollView': 'android.widget.HorizontalScrollView',
'Space': 'android.widget.Space',
}
def parse_layouts():
"""Parse all layout XML files and return view_id -> view_type mapping."""
view_map = {}
for f in os.listdir(LAYOUT_DIR):
if not f.endswith('.xml'):
continue
path = os.path.join(LAYOUT_DIR, f)
try:
tree = ET.parse(path)
for elem in tree.iter():
aid = elem.get(ANDROID_NS + 'id')
if aid and aid.startswith('@+id/'):
view_id = aid[5:]
tag = elem.tag
# Remove package prefix for known types
if '.' in tag:
short_name = tag.rsplit('.', 1)[1]
else:
short_name = tag
view_map[view_id] = (short_name, tag)
except Exception:
pass
return view_map
def parse_errors(error_file):
"""Parse build error log and return dict of file -> set of unresolved view IDs."""
file_refs = defaultdict(set)
with open(error_file) as f:
content = f.read()
# Join wrapped lines (terminal wrapping breaks lines)
lines = content.split('\n')
joined = []
for line in lines:
if line.startswith('e: ') or line.startswith('w: ') or line.startswith('FAILURE') or not joined:
joined.append(line)
elif joined and joined[-1].startswith('e: '):
joined[-1] += line
else:
joined.append(line)
pattern = re.compile(r"^e: file://(/.+?\.kt):\d+:\d+ Unresolved reference '(\w+)'")
for line in joined:
m = pattern.match(line.strip())
if m:
filepath = m.group(1)
ref = m.group(2)
file_refs[filepath].add(ref)
return file_refs
def get_import_for_type(short_name, full_tag):
"""Get the import statement needed for a view type."""
if short_name in WIDGET_IMPORTS:
return WIDGET_IMPORTS[short_name]
# If it's a full qualified name already
if '.' in full_tag:
return full_tag
# Custom project widgets - check if they're in the project
custom_prefix = 'com.moez.QKSMS.common.widget.'
return custom_prefix + short_name
def classify_file(filepath, content):
"""Classify a file as Activity, Controller, Adapter, Widget, or Other."""
if ': QkActivity()' in content or ': QkThemedActivity()' in content:
return 'activity'
if 'QkController' in content or 'LifecycleController' in content:
return 'controller'
if 'RecyclerView.Adapter' in content or 'QkRealmAdapter' in content or 'QkAdapter' in content:
return 'adapter'
if ': LinearLayout' in content or ': FrameLayout' in content or ': ConstraintLayout' in content or ': RelativeLayout' in content or ': View(' in content:
return 'widget'
if ': Dialog' in content or 'AlertDialog' in content or 'DialogFragment' in content:
return 'dialog'
return 'other'
def add_view_properties(filepath, content, view_ids, view_map, file_type):
"""Add lazy findViewById properties to a class for the given view IDs."""
# Filter to only IDs that exist in the view_map
valid_ids = {vid for vid in view_ids if vid in view_map}
if not valid_ids:
return content, False
# Determine the view lookup expression based on file type
if file_type == 'activity':
lookup_prefix = 'findViewById'
elif file_type == 'controller':
lookup_prefix = 'view!!.findViewById'
elif file_type == 'widget':
lookup_prefix = 'findViewById'
elif file_type == 'adapter':
# For adapters, view refs are usually in onBindViewHolder with holder.itemView
# These are complex - skip automated migration
return content, False
else:
lookup_prefix = 'findViewById'
# Build property declarations
properties = []
imports_needed = set()
for vid in sorted(valid_ids):
short_name, full_tag = view_map[vid]
import_path = get_import_for_type(short_name, full_tag)
if file_type in ('controller',):
prop = f' private val {vid}: {short_name}? get() = view?.findViewById(R.id.{vid})'
elif file_type == 'activity':
prop = f' private val {vid}: {short_name}? get() = findViewById(R.id.{vid})'
elif file_type == 'widget':
prop = f' private val {vid}: {short_name}? get() = findViewById(R.id.{vid})'
else:
prop = f' private val {vid}: {short_name}? get() = findViewById(R.id.{vid})'
properties.append(prop)
# Only add import if not android.view.View (usually already imported)
# and the type needs importing
if import_path and import_path not in ('android.view.View',):
imports_needed.add(import_path)
if not properties:
return content, False
# Add imports
lines = content.split('\n')
import_section_end = 0
for i, line in enumerate(lines):
if line.startswith('import '):
import_section_end = i + 1
existing_imports = set(line.strip() for line in lines if line.startswith('import '))
new_imports = []
for imp in sorted(imports_needed):
import_line = f'import {imp}'
if import_line not in existing_imports:
new_imports.append(import_line)
if new_imports:
for imp in reversed(new_imports):
lines.insert(import_section_end, imp)
# Find the class body start (first { after class declaration)
class_body_start = None
brace_count = 0
in_class_decl = False
for i, line in enumerate(lines):
stripped = line.strip()
if re.match(r'^(abstract\s+)?class\s+\w+', stripped) or \
re.match(r'^(open\s+)?class\s+\w+', stripped) or \
re.match(r'^(internal\s+)?class\s+\w+', stripped):
in_class_decl = True
if in_class_decl:
if '{' in line:
class_body_start = i
break
if class_body_start is not None:
# Insert properties after the class opening brace
insert_at = class_body_start + 1
properties_block = '\n'.join(properties)
lines.insert(insert_at, '')
lines.insert(insert_at + 1, ' // View references (migrated from synthetics)')
for j, prop in enumerate(properties):
lines.insert(insert_at + 2 + j, prop)
lines.insert(insert_at + 2 + len(properties), '')
return '\n'.join(lines), True
def main():
error_file = sys.argv[1] if len(sys.argv) > 1 else '/tmp/build_output2.txt'
print("Parsing layout XMLs...")
view_map = parse_layouts()
print(f"Found {len(view_map)} view IDs")
print("Parsing build errors...")
file_refs = parse_errors(error_file)
print(f"Found unresolved references in {len(file_refs)} files")
modified = 0
skipped = 0
for filepath, refs in sorted(file_refs.items()):
if not os.path.exists(filepath):
continue
with open(filepath) as f:
content = f.read()
file_type = classify_file(filepath, content)
# Filter refs to only those that are view IDs
view_refs = {r for r in refs if r in view_map}
if not view_refs:
continue
new_content, changed = add_view_properties(filepath, content, view_refs, view_map, file_type)
if changed:
with open(filepath, 'w') as f:
f.write(new_content)
modified += 1
print(f" Modified: {os.path.basename(filepath)} ({file_type}) - {len(view_refs)} view refs: {', '.join(sorted(view_refs))}")
else:
skipped += 1
print(f" Skipped: {os.path.basename(filepath)} ({file_type}) - {len(view_refs)} refs (adapter/other)")
print(f"\nDone! Modified {modified} files, skipped {skipped}")
if __name__ == '__main__':
main()