spawnStateGenerator function

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

Implementation

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

      ports.initRefreshPort.send(refreshPort.sendPort);

      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);

      await for (final path in refreshPort) {
        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);

        final affectedFiles = <String>[
          path,
          ...(await ctx.applyPendingFileChanges()),
        ]
            .where((p) => p.endsWith('.dart') && !p.endsWith('.hypen.dart'))
            .toSet();

        final futures = <Future>[];
        for (final path in affectedFiles) {
          futures.add(() async {
            final result = await ctx.currentSession.getResolvedLibrary(path);
            if (result is! ResolvedLibraryResult) {
              return;
            }
            await writeHypenState(path, result);
          }());
        }
        await futures.wait;
      }
    },
    (initRefreshPort: refreshPortSender,),
  );
}