61 lines
2.6 KiB
Python
61 lines
2.6 KiB
Python
import os
|
|
import re
|
|
|
|
def process_file(filepath, app_name):
|
|
# Skip translations and currency.dart
|
|
if 'translations.dart' in filepath or 'currency.dart' in filepath or 'translations_en.json' in filepath:
|
|
return
|
|
|
|
with open(filepath, 'r') as f:
|
|
content = f.read()
|
|
|
|
original_content = content
|
|
|
|
# We only want to replace instances that represent the currency string.
|
|
# Be careful not to replace it inside other words or imports.
|
|
# We'll use regex word boundaries or quotes.
|
|
|
|
# Pattern to match 'SYP'.tr or "SYP".tr
|
|
content = re.sub(r"(['\"]SYP['\"])\.tr", "CurrencyHelper.currency", content)
|
|
|
|
# Pattern to match 'SYP' or "SYP" (standalone, not followed by .tr since we did that)
|
|
content = re.sub(r"['\"]SYP['\"]", "CurrencyHelper.currency", content)
|
|
|
|
# Sometimes it's inside string interpolation: '100 SYP' -> '100 ${CurrencyHelper.currency}'
|
|
# But this is tricky. Let's look for " SYP" or " SYP " or "SYP " inside strings.
|
|
# A safer way is to find exactly ' SYP' or 'SYP ' inside dart strings and replace with ${CurrencyHelper.currency}
|
|
# For simplicity, let's just do a pass on common patterns:
|
|
content = re.sub(r" SYP['\"]", " ${CurrencyHelper.currency}'", content)
|
|
content = re.sub(r" SYP ", " ${CurrencyHelper.currency} ", content)
|
|
content = re.sub(r"SYP ", "${CurrencyHelper.currency} ", content)
|
|
|
|
# Fix double curly braces if it was already interpolated like '100 SYP' inside something
|
|
# Actually just simple regex
|
|
content = content.replace(" ${CurrencyHelper.currency}'", " ${CurrencyHelper.currency}'")
|
|
content = content.replace(' ${CurrencyHelper.currency}"', ' ${CurrencyHelper.currency}"')
|
|
|
|
if content != original_content:
|
|
# Need to add import
|
|
import_stmt = f"import 'package:{app_name}/constant/currency.dart';\n"
|
|
if import_stmt not in content:
|
|
# find first import
|
|
import_idx = content.find('import ')
|
|
if import_idx != -1:
|
|
content = content[:import_idx] + import_stmt + content[import_idx:]
|
|
else:
|
|
content = import_stmt + content
|
|
|
|
with open(filepath, 'w') as f:
|
|
f.write(content)
|
|
print(f"Updated {filepath}")
|
|
|
|
def process_dir(directory, app_name):
|
|
for root, dirs, files in os.walk(directory):
|
|
for file in files:
|
|
if file.endswith('.dart'):
|
|
process_file(os.path.join(root, file), app_name)
|
|
|
|
if __name__ == '__main__':
|
|
process_dir('siro_rider/lib', 'siro_rider')
|
|
process_dir('siro_driver/lib', 'siro_driver')
|