Implementation
Future<void> refactorLibrary() async {
final pathTempLibrary = join(current, 'morpheme_library_temp');
if (exists(pathTempLibrary)) {
deleteDir(pathTempLibrary);
}
await 'git clone https://github.com/morphemedesign/morpheme-flutter-library.git morpheme_library_temp'
.run;
final pathOldLibrary =
join(current, 'core', 'packages', '${oldName.snakeCase}_library');
final pathNewLibrary =
join(current, 'core', 'packages', '${newName.snakeCase}_library');
if (exists(pathOldLibrary)) {
DirectoryHelper.createDir(join(pathOldLibrary, 'packages'));
}
final pubspecLibrary = Map.from(YamlHelper.loadFileYaml(
join(pathOldLibrary, 'pubspec.yaml'),
));
final packagesLibrary = find(
'*',
workingDirectory: join(pathTempLibrary, 'packages'),
recursive: false,
types: [Find.directory],
).toList();
final dependencies = pubspecLibrary['dependencies'];
if (dependencies is! Map) return;
dependencies.forEach((key, value) {
final nameLibrary = key
.toString()
.replaceAll(RegExp('morpheme|${oldName.snakeCase}'), '');
final isLibrary = packagesLibrary.firstWhereOrNull((element) {
return nameLibrary ==
element.replaceAll(
join(pathTempLibrary, 'packages', 'morpheme'), '');
}) ==
null;
final findPackage = find(
'*$nameLibrary',
workingDirectory: join(pathOldLibrary, 'packages'),
recursive: false,
types: [Find.directory],
).toList();
if (isLibrary || findPackage.isNotEmpty) return;
if (exists(join(pathTempLibrary, 'packages', 'morpheme$nameLibrary'))) {
moveDir(
join(pathTempLibrary, 'packages', 'morpheme$nameLibrary'),
join(pathOldLibrary, 'packages', 'morpheme$nameLibrary'),
);
}
final pubspec = Map.from(YamlHelper.loadFileYaml(
join(
pathOldLibrary, 'packages', 'morpheme$nameLibrary', 'pubspec.yaml'),
));
final dependencies = pubspec['dependencies'];
if (dependencies is! Map) return;
dependencies.forEach((key, value) {
final nameLibrary = key
.toString()
.replaceAll(RegExp('morpheme|${oldName.snakeCase}'), '');
final isLibrary = packagesLibrary.firstWhereOrNull((element) {
return nameLibrary ==
element.replaceAll(
join(pathTempLibrary, 'packages', 'morpheme'), '');
}) ==
null;
final findPackage = find(
'*$nameLibrary',
workingDirectory: join(pathOldLibrary, 'packages'),
recursive: false,
types: [Find.directory],
).toList();
if (isLibrary || findPackage.isNotEmpty) return;
if (exists(join(pathTempLibrary, 'packages', 'morpheme$nameLibrary'))) {
moveDir(
join(pathTempLibrary, 'packages', 'morpheme$nameLibrary'),
join(pathOldLibrary, 'packages', 'morpheme$nameLibrary'),
);
}
});
});
final oldLibrary = find(
'*',
workingDirectory: join(pathOldLibrary, 'packages'),
recursive: false,
types: [Find.directory],
).toList();
for (var element in oldLibrary) {
if (exists(join(pathOldLibrary, 'packages', element, 'example'))) {
deleteDir(join(pathOldLibrary, 'packages', element, 'example'));
}
if (exists(join(pathOldLibrary, 'packages', element, 'CHANGELOG.md'))) {
delete(join(pathOldLibrary, 'packages', element, 'CHANGELOG.md'));
}
if (exists(join(pathOldLibrary, 'packages', element, 'README.md'))) {
delete(join(pathOldLibrary, 'packages', element, 'README.md'));
}
if (exists(join(pathOldLibrary, 'packages', element, 'AUTHORS'))) {
delete(join(pathOldLibrary, 'packages', element, 'AUTHORS'));
}
final pubspec = Map.from(YamlHelper.loadFileYaml(
join(pathOldLibrary, 'packages', element, 'pubspec.yaml'),
));
pubspec['name'] = pubspec['name'].toString().replaceAll(
RegExp('morpheme|${oldName.snakeCase}'), newName.snakeCase);
pubspec['description'] = pubspec['description'].toString().replaceAll(
RegExp('morpheme|${oldName.snakeCase}'), newName.snakeCase);
pubspec['description'] = pubspec['description'].toString().replaceAll(
RegExp('Morpheme|${oldName.pascalCase}'), newName.pascalCase);
pubspec['publish_to'] = 'none';
pubspec.remove('homepage');
pubspec.remove('repository');
List<String> keysToRemove = [];
List<MapEntry> mapEntries = [];
final dependency = Map.from(pubspec['dependencies']);
dependency.forEach((key, value) {
if (key.toString().contains(RegExp('morpheme|${oldName.snakeCase}'))) {
final libraryName = key.toString().replaceAll(
RegExp('morpheme|${oldName.snakeCase}'), newName.snakeCase);
keysToRemove.add(key);
mapEntries.add(MapEntry(libraryName, {'path': '../$libraryName'}));
}
});
dependency.removeWhere((key, value) => keysToRemove.contains(key));
dependency.addEntries(mapEntries);
pubspec['dependencies'] = dependency;
YamlHelper.saveFileYaml(
join(pathOldLibrary, 'packages', element, 'pubspec.yaml'),
pubspec,
);
}
final pubspec = Map.from(YamlHelper.loadFileYaml(
join(pathOldLibrary, 'pubspec.yaml'),
));
pubspec['name'] = pubspec['name']
.toString()
.replaceAll(RegExp('morpheme|${oldName.snakeCase}'), newName.snakeCase);
List<String> keysToRemove = [];
List<MapEntry> mapEntries = [];
final dependency = Map.from(pubspec['dependencies']);
dependency.forEach((key, value) {
if (key.toString().contains(RegExp('morpheme|${oldName.snakeCase}'))) {
final libraryName = key.toString().replaceAll(
RegExp('morpheme|${oldName.snakeCase}'), newName.snakeCase);
keysToRemove.add(key);
mapEntries
.add(MapEntry(libraryName, {'path': './packages/$libraryName'}));
}
});
dependency.removeWhere((key, value) => keysToRemove.contains(key));
dependency.addEntries(mapEntries);
pubspec['dependencies'] = dependency;
YamlHelper.saveFileYaml(
join(pathOldLibrary, 'pubspec.yaml'),
pubspec,
);
for (var element in oldLibrary) {
final lastName = element.split(separator).last.replaceAll(
RegExp('morpheme|${oldName.snakeCase}'), newName.snakeCase);
if (exists(element)) {
moveDir(element, join(pathOldLibrary, 'packages', lastName));
}
}
if (exists(pathOldLibrary)) {
moveDir(pathOldLibrary, pathNewLibrary);
}
RefactorHelper.renameFileAndClassName(
pathDir: pathNewLibrary,
oldName: oldName,
newName: newName,
);
replace(
join(current, 'core', 'pubspec.yaml'),
RegExp('morpheme|${oldName.snakeCase}'),
newName.snakeCase,
);
if (exists(pathTempLibrary)) {
deleteDir(pathTempLibrary);
}
}