Line data Source code
1 : part of rx_bloc_generator; 2 : 3 : /// The generator. 4 : class RxBlocGeneratorForAnnotation extends GeneratorForAnnotation<RxBloc> { 5 : /// Allows creating via `const` as well as enforces immutability here. 6 1 : const RxBlocGeneratorForAnnotation(); 7 : 8 : /// Generates the bloc based on the class with the @RxBloc() annotation. 9 : /// If either the states or events class is missing the file is not generated. 10 : @override 11 1 : Future<String> generateForAnnotatedElement( 12 : Element element, 13 : ConstantReader annotation, 14 : BuildStep buildStep, 15 : ) async { 16 : final classElement = element as ClassElement; 17 : 18 2 : final libraryReader = LibraryReader(classElement.library); 19 : 20 : try { 21 1 : return _BuildController( 22 : rxBlocClass: classElement, 23 : 24 : /// Provides the events class as [ClassElement] 25 : /// 26 : /// abstract class {RxBlocName}BlocEvents { 27 : // void fetch(); 28 : // } 29 2 : eventClass: libraryReader.classes.firstWhereOrNull( 30 2 : (ClassElement classElement) => classElement.displayName 31 3 : .contains(annotation.read('eventsClassName').stringValue), 32 : ), 33 : 34 : /// Provides the states class as [ClassElement] 35 : /// 36 : /// abstract class {RxBlocName}BlocEvents { 37 : // void fetch(); 38 : // } 39 2 : stateClass: libraryReader.classes.firstWhereOrNull( 40 2 : (classElement) => classElement.displayName 41 3 : .contains(annotation.read('statesClassName').stringValue), 42 : ), 43 1 : ).generate(); 44 1 : } on _RxBlocGeneratorException catch (e) { 45 : // User error 46 2 : _logError(e.message); 47 2 : return '/**\n${e.message}\n*/'; 48 0 : } on FormatterException catch (e) { 49 0 : var message = e.errors.map((AnalysisError e) => e.message).join('\n'); 50 : // Format error 51 0 : _reportIssue( 52 0 : 'FormatterException \n $message', 53 0 : libraryReader.allElements.first.source?.contents.data.toString() ?? '', 54 : ); 55 0 : return '/**\n${e.message}\n*/'; 56 0 : } on Exception catch (e) { 57 : // System error 58 0 : _reportIssue( 59 0 : e.toString(), 60 0 : libraryReader.allElements.first.source?.contents.data.toString() ?? '', 61 : ); 62 0 : return '/**\n$e\n*/'; 63 : } 64 : } 65 : }