spawnRouterGenerator function

Future<Isolate> spawnRouterGenerator(
  1. String normProjectPath,
  2. SendPort refreshPortSender
)

Implementation

Future<Isolate> spawnRouterGenerator(
  String normProjectPath,
  SendPort refreshPortSender,
) {
  return Isolate.spawn(
    (ports) async {
      final refreshPort = ReceivePort();

      final libPath = p.join(normProjectPath, 'lib');
      final binPath = p.join(normProjectPath, 'bin');
      final testPath = p.join(normProjectPath, 'test');
      final ctxCollection = AnalysisContextCollection(
        includedPaths: [
          libPath,
          binPath,
          testPath,
        ],
      );

      final libCtx = ctxCollection.contextFor(libPath);
      final binCtx = ctxCollection.contextFor(binPath);
      final testCtx = ctxCollection.contextFor(testPath);

      ports.initRefreshPort.send(refreshPort.sendPort);

      await for (final path in refreshPort) {
        if (!(await File(path).exists())) {
          await removeHypenRouter(path);
          continue;
        }
        late AnalysisContext ctx;

        if (p.isWithin(libPath, path)) {
          ctx = libCtx;
        } else if (p.isWithin(binPath, path)) {
          ctx = binCtx;
        } else if (p.isWithin(testPath, path)) {
          ctx = testCtx;
        } else {
          continue;
        }
        ctx.changeFile(path);

        await ctx.applyPendingFileChanges();

        final result = await ctx.currentSession.getResolvedLibrary(path);
        if (result is! ResolvedLibraryResult) {
          return;
        }
        await writeHypenRouter(path, result);
      }
    },
    (initRefreshPort: refreshPortSender,),
  );
}