generateForAnnotatedElement method

  1. @override
FutureOr<String> generateForAnnotatedElement(
  1. Element element,
  2. ConstantReader annotation,
  3. BuildStep buildStep
)

Looking for top-level variable that marked with GoRouterGenerator and generate corresponding routerConfig and route

Implementation

@override
FutureOr<String> generateForAnnotatedElement(
    Element element, ConstantReader annotation, BuildStep buildStep) {
  final goRouterGen = StringBuffer();
  final classGen = StringBuffer();
  final helperGen = StringBuffer();
  if (element is TopLevelVariableElement) {
    final value = element.computeConstantValue();
    final root = _getIterableValue(value);
    if (root != null) {
      final goRootName = _getStringArgumentFromAnnotation(annotation, 'routerConfigVariableName');
      goRouterGen.write('final $goRootName = <RouteBase>');
      _writeGoRoutes(root, goRouterGen, childrenSep: ';', isTopLevel: true);
      //
      final rootName = _getStringArgumentFromAnnotation(annotation, 'routeVariableName');
      final defRoot = 'root';
      _writeClassRoutes(classGen, childrenNodes: root, path: defRoot);
      _writeStringBufferAtTop(
          classGen, 'final $rootName = ${_classNameBasedOnPaths([defRoot])}();');
      //
      _writeHelper(helperGen);
    } else {
      //TODO error handling
    }
  } else {
    //TODO error handling
  }

  // print('<-- Router builder end.');

  //! Never return empty string, or it wouldn't create part file
  return helperGen.toString() +
      '\n//-----------------------------------------\n' +
      goRouterGen.toString() +
      '\n//-----------------------------------------\n' +
      classGen.toString() +
      '\n//-----------------------------------------\n';
}