Created
December 5, 2019 07:24
-
-
Save nirlanka/ce13d45c58d8530cc437b672801823c6 to your computer and use it in GitHub Desktop.
An example script for replacing code in a C# project
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## // e.g. | |
## MBOverlay mbOverlay = UIUtil.GetBusyOverlay(AppStrings.LoadRevision); | |
## --> VML.LoaderViewModel.Add($"DoorSideBar-{AppStrings.LoadRevision}", Models.ActivityType.API, message: AppStrings.LoadRevision, activitySeverity: Models.ActivitySeverity.High); | |
## mbOverlay.Hide(); | |
## --> VML.LoaderViewModel.Remove($"DoorSideBar-{AppStrings.LoadRevision}"); | |
## // AppStrings.LoadRevision, Properties..., "", messages: List<string> | |
import os, re | |
# files = [f for f in os.listdir('.') if os.path.isfile(f)] | |
path = 'D:\\dev\\MBApp.Wpf' | |
files = [] | |
def list_files_recursively(): | |
# r=root, d=directories, f = files | |
for r, d, f in os.walk(path): | |
for file in f: | |
if file[-3:] == '.cs': | |
files.append(os.path.join(r, file)) | |
list_files_recursively() | |
for f in files: | |
if f[-3:] == '.cs': | |
print(f) | |
class_name = None | |
with open(f, 'r+') as lines: | |
txt = None | |
try: | |
for l in lines: | |
if not class_name and 'class' in l: | |
tokens = l.split() | |
idx = None | |
try: | |
idx = tokens.index('class') + 1 | |
class_name = tokens[idx] | |
except ValueError: | |
pass | |
# print(class_name) | |
break | |
except UnicodeDecodeError: | |
continue | |
if not class_name: | |
continue | |
with open(f, 'r+') as file: | |
txt = file.read() | |
cases = re.findall(r'\.GetBusyOverlay\(\s*[\"\s\.\w.]*', txt) | |
if (len(cases) > 0): | |
print('cases = ' + str(cases)) | |
for c in cases: | |
appstr = re.findall(r'\([\"\s\.\w.]*', c)[0].replace('(', '') | |
# print(appstr) | |
begin_newpart = 'VML.LoaderViewModel.Add($"' | |
begin_newpart_closer = 'VML.LoaderViewModel.Remove($"' | |
end_newpart = '' | |
end_newpart_closer = '' | |
if appstr == 'messages': | |
begin_newpart = 'messages.ForEach(message => ' + begin_newpart | |
begin_newpart_closer = 'messages.ForEach(message => ' + begin_newpart_closer | |
end_newpart = end_newpart + ')' | |
end_newpart_closer = end_newpart_closer + ')' | |
appstr = 'message' | |
newpart = begin_newpart + class_name + '-{' + appstr + '}", Models.ActivityType.API, message: ' + appstr + ', activitySeverity: Models.ActivitySeverity.High)' + end_newpart | |
case_txt = txt.split(c)[1].split('.GetBusyOverlay')[0] | |
closers_count = case_txt.count('mbOverlay.Hide()') | |
txt = re.sub(r'((MBOverlay)|(var))\smbOverlay\s\=\sUIUtil\.GetBusyOverlay\(\s*.*\s*\)', newpart, txt, 1) | |
for i in range(closers_count): | |
close_str = begin_newpart_closer + class_name + '-{' + appstr + '}")' + end_newpart_closer | |
txt = re.sub(r'mbOverlay\.Hide\(\)', close_str, txt, 1) | |
with open(f, 'w+') as writefile: | |
writefile.write(txt) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment