Guest User

Untitled

a guest
Jan 19th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.17 KB | None | 0 0
  1. # Migrates files written in vue1 to vue2.
  2. # Based on recommandation of vue-migration helper.
  3. #
  4. # Only applies the 'easy' recommandations:
  5. # - suggested updates with the correct value
  6. # - argument order
  7. # - replace ready
  8. # - some custom filters with arguments (moment, padLeft, ...)
  9. #
  10. # How to :
  11. # - update `folder_to_migrate_path` and `migration_results_path`
  12. # - run vue-migration-helper tool
  13. # - run this script
  14.  
  15. import fileinput
  16. import re
  17.  
  18.  
  19. def import_and_clean(file_path):
  20. lines = [line.rstrip('\n').strip(' ') for line in open(file_path)]
  21. # clean empty lines
  22. lines = [line for line in lines if line]
  23. # clean more info lines
  24. lines = [line for line in lines if not line.startswith('More info') and not line.startswith('Reason')]
  25.  
  26. return lines
  27.  
  28.  
  29. def merge_lines(lines):
  30. # merge action and file location lines
  31. return [action + '@' + file_location for action, file_location in zip(lines[0::2], lines[1::2])]
  32.  
  33.  
  34. def get_file_and_line(line):
  35. file_name = re.search(r'[0-9]:\s(.*)$', line).group(1)
  36. line_number = int(re.search(r'\@Line\s([0-9]*)', line).group(1))
  37. return file_name, line_number
  38.  
  39.  
  40. def process_update_to_migrations(migration, line):
  41. original_code = re.search(r'Update\s(.*)\sto\s', migration)
  42. updated_code = re.search(r'^[0-9]*.*\sto\s(.*)\@', migration)
  43. if original_code:
  44. line = line.replace(original_code.group(1), updated_code.group(1))
  45. return line
  46.  
  47.  
  48. def process_switch_argument_orger(migration, line):
  49. original_code = re.search(r'Switch argument order.*\"(\(.*\)).*\"', migration)
  50. updated_code = re.search(r'^[0-9]*.*\sto\s(.*)\@', migration)
  51. if original_code:
  52. line = line.replace(original_code.group(1), updated_code.group(1))
  53. return line
  54.  
  55.  
  56. def process_replace_ready_with_mounted(migration, line):
  57. if re.match(r'^[0-9]*.*\sReplace ready with', migration):
  58. line = line.replace('ready', 'mounted')
  59. return line
  60.  
  61.  
  62. def process_replace_moment_format_filter(migration, line):
  63. original_code = re.search(r'Replace\smomentFormat(\s\'.*\')\s', migration)
  64. updated_code = re.search(r'Replace\smomentFormat\s.*momentFormat(\(.*\))', migration)
  65. if original_code:
  66. line = line.replace(original_code.group(1), updated_code.group(1))
  67. return line
  68.  
  69.  
  70. def process_replace_padleft(migration, line):
  71. original_code = re.search(r'Replace\spadLeft\s([0-9]*)', migration)
  72. updated_code = re.search(r'Replace\spadLeft\s.*padLeft(\(.*\))', migration)
  73. if original_code:
  74. line = line.replace(original_code.group(1), updated_code.group(1))
  75. return line
  76.  
  77.  
  78. def process_replace_prefix(migration, line):
  79. original_code = re.search(r'Replace\sprefix\s(\'.*\')\s', migration)
  80. updated_code = re.search(r'Replace\sprefix\s.*prefix(\(.*\))', migration)
  81. if original_code:
  82. line = line.replace(original_code.group(1), updated_code.group(1))
  83. return line
  84.  
  85.  
  86. def migrate_to_vue2():
  87. # The folder from where the vue-migration-helper tool was launched
  88. folder_to_migrate_path = '/Users/etienne/projects/admin-app/assets/main/js/vue/'
  89. # The file containing the results of the vue-migration-helper tool
  90. migration_results_path = '/Users/etienne/PycharmProjects/vue-migration-automation/migration-results'
  91.  
  92. migrations = import_and_clean(migration_results_path)
  93. migrations = merge_lines(migrations)
  94.  
  95. for migration in migrations:
  96.  
  97. file_name, line_number = get_file_and_line(migration)
  98. full_file_path = folder_to_migrate_path + file_name
  99.  
  100. file = open(full_file_path, "r")
  101. code_lines = file.readlines()
  102. file.close()
  103.  
  104. for i, line in enumerate(code_lines):
  105. if i + 1 == line_number:
  106. line = process_update_to_migrations(migration, line)
  107. line = process_switch_argument_orger(migration, line)
  108. line = process_replace_ready_with_mounted(migration, line)
  109. line = process_replace_moment_format_filter(migration, line)
  110. line = process_replace_padleft(migration, line)
  111. line = process_replace_prefix(migration, line)
  112.  
  113. code_lines[i] = line
  114. f = open(full_file_path, "w")
  115. f.write("".join(code_lines))
  116. f.close()
  117.  
  118.  
  119. migrate_to_vue2()
Add Comment
Please, Sign In to add comment