Created
February 10, 2022 14:04
-
-
Save pedrohenriquebr/95323258d2b0dfe51d29cb140628becf to your computer and use it in GitHub Desktop.
migrate angular material 8 to angular material 9
This file contains hidden or 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
| import re | |
| regex = r"(import \{ )?((\w+)[, ]+)(?=.*?(\} from '(@angular\/material)'))" | |
| ''' | |
| Put this script in root dir of your angular project | |
| Execute from terminal: python migration.py | |
| ''' | |
| # dict MatModule to lib inside @angular/material/name | |
| dict = { | |
| 'MatAutocompleteModule': 'autocomplete', | |
| 'MatButtonModule': 'button', | |
| 'MatButtonToggleModule': 'button-toggle', | |
| 'MatCardModule': 'card', | |
| 'MatCheckboxModule': 'checkbox', | |
| 'MatChipsModule': 'chips', | |
| 'MatDatepickerModule': 'datepicker', | |
| 'MatDialogModule': 'dialog', | |
| 'MatExpansionModule': 'expansion', | |
| 'MatGridListModule': 'grid-list', | |
| 'MatIconModule': 'icon', | |
| 'MatInputModule': 'input', | |
| 'MatListModule': 'list', | |
| 'MatMenuModule': 'menu', | |
| 'MatNativeDateModule': 'native-date', | |
| 'MatPaginatorModule': 'paginator', | |
| 'MatProgressBarModule': 'progress-bar', | |
| 'MatProgressSpinnerModule': 'progress-spinner', | |
| 'MatRadioModule': 'radio', | |
| 'MatRippleModule': 'core', | |
| 'MatSelectModule': 'select', | |
| 'MatSidenavModule': 'sidenav', | |
| 'MatSliderModule': 'slider', | |
| 'MatSlideToggleModule': 'slide-toggle', | |
| 'MatSnackBarModule': 'snack-bar', | |
| 'MatSortModule': 'sort', | |
| 'MatSortable': 'sort', | |
| 'MatTableModule': 'table', | |
| 'MatTabsModule': 'tabs', | |
| 'MatToolbarModule': 'toolbar', | |
| 'MatTooltipModule': 'tooltip', | |
| 'MatStepperModule': 'stepper', | |
| 'MatDialog': 'dialog', | |
| 'MatDialogRef': 'dialog', | |
| 'MatSnackBar': 'snack-bar', | |
| 'MatTable': 'table', | |
| 'MatPaginator': 'paginator', | |
| 'MatSort': 'sort', | |
| 'MatFormField': 'form-field', | |
| 'MatInput': 'input', | |
| 'MatSelect': 'select', | |
| 'MatOption': 'option', | |
| 'MatAutocomplete': 'autocomplete', | |
| 'MatRadioChange': 'radio', | |
| 'MatSelectChange': 'select', | |
| 'MatSlideToggleChange': 'slide-toggle', | |
| 'MatCheckbox': 'checkbox', | |
| 'MatRadio': 'radio', | |
| 'MatDatepicker': 'datepicker', | |
| 'MatNativeDateModule': 'native-date', | |
| 'MatSlideToggle': 'slide-toggle', | |
| 'MatSlider': 'slider', | |
| 'MatTabs': 'tabs', | |
| 'MatToolbar': 'toolbar', | |
| 'MatTooltip': 'tooltip', | |
| 'MatSidenav': 'sidenav', | |
| 'MatList': 'list', | |
| 'MatExpansion': 'expansion', | |
| 'MatCard': 'card', | |
| 'MatButton': 'button', | |
| 'MatIcon': 'icon', | |
| 'MatMenu': 'menu', | |
| 'MatStepper': 'stepper', | |
| 'MatProgressSpinner': 'progress-spinner', | |
| 'MatProgressBar': 'progress-bar', | |
| 'MatRipple': 'ripple', | |
| 'MatChips': 'chips', | |
| 'MatGridList': 'grid-list', | |
| 'MatBadge': 'badge', | |
| 'MatBottomSheet': 'bottom-sheet', | |
| 'MatDivider': 'divider', | |
| 'MatDatepickerInputEvent': 'datepicker', | |
| 'MAT_DIALOG_DATA': 'dialog', | |
| 'MAT_DIALOG_DEFAULT_OPTIONS': 'dialog', | |
| 'MAT_DIALOG_SCROLL_STRATEGY': 'dialog', | |
| 'MAT_DIALOG_SCROLL_STRATEGY_PROVIDER': 'dialog', | |
| } | |
| import glob | |
| paths = glob.glob('./src/**/*.ts', recursive=True) | |
| count = 0 | |
| for path in paths: | |
| try: | |
| current_file = open(path) | |
| current_txt = ''.join(current_file.readlines()) | |
| # check that the file contains the string '@angular/material' | |
| if re.search(r"from '@angular/material'", current_txt): | |
| # print('found!') | |
| count += 1 | |
| matches = re.findall(regex, current_txt) | |
| d = re.findall(r"(import ?.+(\w.+)from '(@angular\/material))", current_txt) | |
| d = [x[0] for x in d] | |
| for _import in d: | |
| current_txt = current_txt.replace(_import, f'//{_import}') | |
| for i in matches: | |
| _import = i[0] | |
| _module_name = i[2] | |
| _angular_material = i[4] | |
| if _module_name not in dict: | |
| print(f'{_module_name} not found in dict {path}') | |
| continue | |
| current_txt = 'import { '+ _module_name+' } ' + f'from \'{ _angular_material }/{ dict[_module_name] }\'\n' + current_txt | |
| # replace the file content with the new content | |
| current_file = open(path, 'w') | |
| current_file.write(current_txt) | |
| except: | |
| pass | |
| print(count) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment