gen method
void
gen()
Implementation
void gen() {
final pathSeparator = Platform.pathSeparator;
final annotation = '@ULoCDeclaration()';
File declaredFile = File(
['lib', 'routes', 'routes.dart'].join(pathSeparator),
);
File routesFile = File(
['lib', 'routes', 'routes.uloc.g.dart'].join(pathSeparator),
);
if (args.length >= 2 && args[1] != '') {
declaredFile = File(args[1]);
if (!declaredFile.existsSync()) {
throw Exception('${args[1]} is not found');
}
}
final content = declaredFile.readAsStringSync();
if (!content.contains(annotation)) {
throw Exception('${args[1]} Cannot find ULoCDeclaration');
}
final imports = content.split(annotation).first;
if (args.length >= 3 && args[2] != '') {
routesFile = File(args[2]);
if (!routesFile.existsSync()) {
routesFile.createSync(recursive: true);
}
}
final routesMap = parseULoCRoutesToJson(content);
List<String> result = [];
result.add('///');
result.add('/// **************************************************');
result.add('/// **** [GENERATED CODE - DO NOT MODIFY BY HAND] ****');
result.add('/// **************************************************');
result.add('///');
result.add(
'// ignore_for_file: constant_identifier_names, non_constant_identifier_names, dangling_library_doc_comments',
);
result.add(imports);
result.add('/// use this for [named navigation]');
result.add('class Routes {');
result.add(' Routes._();');
result.add('');
for (MapEntry<String, Map<String, String>> entry in routesMap.entries) {
result.add(buildRouteName(entry.key, entry.value['route'] ?? ''));
}
result.add('}');
result.add('');
result.add('/// use this to pass to [MaterialApp] Route setting');
result.add('final ULoC uloc = ULoC([');
for (MapEntry<String, Map<String, String>> entry in routesMap.entries) {
result.add(' RouteProperties<${entry.value['providerName']}>(');
result.add(
' routeName: Routes.${entry.key}${(entry.value['route']?.contains(':') ?? false) ? '()' : ''},',
);
result.add(' provider: ${entry.value['provider']},');
result.add(' child: ${entry.value['child']}(),');
result.add(' ),');
}
result.add(']);');
result.add('');
routesFile.writeAsStringSync(result.join('\n'));
const green = '\x1B[32m';
const reset = '\x1B[0m';
print('$green File generated: ${routesFile.absolute.path} $reset');
exit(0);
}