writeHypenRouter function

dynamic writeHypenRouter(
  1. String path,
  2. ResolvedLibraryResult libResolved
)

Implementation

writeHypenRouter(String path, final ResolvedLibraryResult libResolved) async {
  try {
    final libElement = libResolved.element;
    final classes =
        libElement.topLevelElements.whereType<ClassElement>().toList();

    final routePath = '/${p.relative(
      p.dirname(path),
      from: './lib/app',
    )}';

    final targetPath = changePathParameter(
      routePath.endsWith('.')
          ? routePath.substring(0, routePath.length - 1)
          : routePath,
    );

    final node = findNode(
      rootNode,
      targetPath,
    );

    if (path.isLayoutFile) {
      final layout =
          classes.where((c) => layoutWidget.isAssignableFrom(c)).firstOrNull;
      if (layout == null) {
        node.layoutWidget = '';
        node.layoutCustomTransition = '';
        return;
      }
      node.layoutWidget = layout.displayName;
      node.isFullScreenLayout = isFullScreenAnnotation.hasAnnotationOf(layout);

      final customPage = classes
          .where((c) => pageTransitionClass.isAssignableFrom(c))
          .firstOrNull;
      if (customPage != null) {
        node.layoutCustomTransition = customPage.displayName;
      } else {
        node.layoutCustomTransition = '';
      }
    } else if (path.isPageFile) {
      final page =
          classes.where((c) => pageWidget.isAssignableFrom(c)).firstOrNull;
      if (page == null) {
        node.pageWidget = '';
        node.pageCustomTransition = '';
        return;
      }
      if (initialLocationType.hasAnnotationOf(page)) {
        initialLocation = removeGroup(targetPath);
      }
      node.pageWidget = page.displayName;
      node.isFullScreenPage = isFullScreenAnnotation.hasAnnotationOf(page);

      final customPage = classes
          .where((c) => pageTransitionClass.isAssignableFrom(c))
          .firstOrNull;
      if (customPage != null) {
        node.pageCustomTransition = customPage.displayName;
      } else {
        node.pageCustomTransition = '';
      }
    } else {
      final template =
          classes.where((c) => templateWidget.isAssignableFrom(c)).firstOrNull;
      if (template == null) {
        node.templateWidget = '';
        return;
      }
      node.templateWidget = template.displayName;
    }

    await writeToFile();
  } catch (e) {
    print(e);
  }
}