Skip to content

Instantly share code, notes, and snippets.

@guid-empty
Created April 10, 2018 09:37
Show Gist options
  • Save guid-empty/dd8e14ba866e49f87f5b5131d7b3cc62 to your computer and use it in GitHub Desktop.
Save guid-empty/dd8e14ba866e49f87f5b5131d7b3cc62 to your computer and use it in GitHub Desktop.
replace Watcher.toSymbol usings
import 'dart:async';
import 'dart:io';
import 'package:path/path.dart';
main(List<String> arguments) async {
Stream<FileSystemEntity> entityList = Directory.current.list(recursive: true, followLinks: false);
Set types = new Set();
await for (FileSystemEntity entity in entityList) {
FileSystemEntityType type = await FileSystemEntity.type(entity.path);
switch (type) {
case FileSystemEntityType.FILE:
String fileExtension = extension(entity.path);
if (fileExtension == '.dart') {
String content = await new File(entity.path).readAsString();
if (content.contains('Watcher.toSymbol(')) {
print('\nFile to process: ${entity.path}');
List<String> lines = await new File(entity.path).readAsLines();
lines.forEach((String line) {
if (line.contains('Watcher.toSymbol(')) {
int watcherExpressionStarted = line.indexOf('Watcher.toSymbol(');
int typeDeclarationStarted = line.indexOf('(', watcherExpressionStarted) + 1;
int typeDeclarationFinished = line.indexOf('=>', typeDeclarationStarted);
if (typeDeclarationFinished > 0) {
int watcherExpressionFinished = line.indexOf(')', typeDeclarationFinished) + 1;
if (typeDeclarationStarted > 0 && typeDeclarationFinished > 0 && watcherExpressionStarted > 0 &&
watcherExpressionFinished > 0) {
String watcherExpression = line.substring(watcherExpressionStarted, watcherExpressionFinished).trim();
String typeDeclaration = line.substring(typeDeclarationStarted, typeDeclarationFinished).replaceAll(
'(', '').replaceAll(')', '');
print('watcher: $watcherExpression');
List<String> typeDeclarationParts = typeDeclaration.split(' ');
String type = typeDeclarationParts[0].trim();
int fieldDeclarationStarted = line.indexOf('=>', typeDeclarationFinished) + 2;
int fieldDeclarationFinished = line.indexOf(')', fieldDeclarationStarted) + 1;
String fieldDeclaration = line.substring(fieldDeclarationStarted, fieldDeclarationFinished)
.replaceAll('(', '')
.replaceAll(')', '').trim();
List<String> fieldDeclarationParts = fieldDeclaration.split('.');
String field = fieldDeclarationParts[1].trim();
print('type: $typeDeclaration, $type, fieldDeclaration: $field');
types.add(type);
String newExpression = '\$${type}Metadata.$field.symbol';
print('expression to replace: $newExpression');
content = content.replaceAll(watcherExpression, newExpression);
}
}
}
});
await new File(entity.path).writeAsString(content);
}
}
break;
default:
}
}
types.forEach(print);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment