Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Migrates files written in vue1 to vue2.
- # Based on recommandation of vue-migration helper.
- #
- # Only applies the 'easy' recommandations:
- # - suggested updates with the correct value
- # - argument order
- # - replace ready
- # - some custom filters with arguments (moment, padLeft, ...)
- #
- # How to :
- # - update `folder_to_migrate_path` and `migration_results_path`
- # - run vue-migration-helper tool
- # - run this script
- import fileinput
- import re
- def import_and_clean(file_path):
- lines = [line.rstrip('\n').strip(' ') for line in open(file_path)]
- # clean empty lines
- lines = [line for line in lines if line]
- # clean more info lines
- lines = [line for line in lines if not line.startswith('More info') and not line.startswith('Reason')]
- return lines
- def merge_lines(lines):
- # merge action and file location lines
- return [action + '@' + file_location for action, file_location in zip(lines[0::2], lines[1::2])]
- def get_file_and_line(line):
- file_name = re.search(r'[0-9]:\s(.*)$', line).group(1)
- line_number = int(re.search(r'\@Line\s([0-9]*)', line).group(1))
- return file_name, line_number
- def process_update_to_migrations(migration, line):
- original_code = re.search(r'Update\s(.*)\sto\s', migration)
- updated_code = re.search(r'^[0-9]*.*\sto\s(.*)\@', migration)
- if original_code:
- line = line.replace(original_code.group(1), updated_code.group(1))
- return line
- def process_switch_argument_orger(migration, line):
- original_code = re.search(r'Switch argument order.*\"(\(.*\)).*\"', migration)
- updated_code = re.search(r'^[0-9]*.*\sto\s(.*)\@', migration)
- if original_code:
- line = line.replace(original_code.group(1), updated_code.group(1))
- return line
- def process_replace_ready_with_mounted(migration, line):
- if re.match(r'^[0-9]*.*\sReplace ready with', migration):
- line = line.replace('ready', 'mounted')
- return line
- def process_replace_moment_format_filter(migration, line):
- original_code = re.search(r'Replace\smomentFormat(\s\'.*\')\s', migration)
- updated_code = re.search(r'Replace\smomentFormat\s.*momentFormat(\(.*\))', migration)
- if original_code:
- line = line.replace(original_code.group(1), updated_code.group(1))
- return line
- def process_replace_padleft(migration, line):
- original_code = re.search(r'Replace\spadLeft\s([0-9]*)', migration)
- updated_code = re.search(r'Replace\spadLeft\s.*padLeft(\(.*\))', migration)
- if original_code:
- line = line.replace(original_code.group(1), updated_code.group(1))
- return line
- def process_replace_prefix(migration, line):
- original_code = re.search(r'Replace\sprefix\s(\'.*\')\s', migration)
- updated_code = re.search(r'Replace\sprefix\s.*prefix(\(.*\))', migration)
- if original_code:
- line = line.replace(original_code.group(1), updated_code.group(1))
- return line
- def migrate_to_vue2():
- # The folder from where the vue-migration-helper tool was launched
- folder_to_migrate_path = '/Users/etienne/projects/admin-app/assets/main/js/vue/'
- # The file containing the results of the vue-migration-helper tool
- migration_results_path = '/Users/etienne/PycharmProjects/vue-migration-automation/migration-results'
- migrations = import_and_clean(migration_results_path)
- migrations = merge_lines(migrations)
- for migration in migrations:
- file_name, line_number = get_file_and_line(migration)
- full_file_path = folder_to_migrate_path + file_name
- file = open(full_file_path, "r")
- code_lines = file.readlines()
- file.close()
- for i, line in enumerate(code_lines):
- if i + 1 == line_number:
- line = process_update_to_migrations(migration, line)
- line = process_switch_argument_orger(migration, line)
- line = process_replace_ready_with_mounted(migration, line)
- line = process_replace_moment_format_filter(migration, line)
- line = process_replace_padleft(migration, line)
- line = process_replace_prefix(migration, line)
- code_lines[i] = line
- f = open(full_file_path, "w")
- f.write("".join(code_lines))
- f.close()
- migrate_to_vue2()
Add Comment
Please, Sign In to add comment