build method

Future build()

Implementation

Future build() async {
  if (!context.buildDirectory.existsSync()) {
    context.buildDirectory.createSync();
  }

  // Here is where we need to provide a temporary copy of the script file with the main function stripped;
  // this is because when the RuntimeGenerator loads, it needs Mirror access to any declarations in this file
  var scriptSource = context.source;
  final strippedScriptFile = File.fromUri(context.targetScriptFileUri)
    ..writeAsStringSync(scriptSource);
  final analyzer = CodeAnalyzer(strippedScriptFile.absolute.uri);
  final analyzerContext = analyzer.contexts.contextFor(analyzer.path);
  final parsedUnit = analyzerContext.currentSession
      .getParsedUnit(analyzer.path) as ParsedUnitResult;

  final mainFunctions = parsedUnit.unit.declarations
      .whereType<FunctionDeclaration>()
      .where((f) => f.name.value() == "main")
      .toList();

  for (final f in mainFunctions.reversed) {
    scriptSource = scriptSource.replaceRange(f.offset, f.end, "");
  }

  strippedScriptFile.writeAsStringSync(scriptSource);

  try {
    await copyPath(
        context.sourceApplicationDirectory.uri.resolve('test/not_tests').path,
        context.buildDirectoryUri.resolve('not_tests').path);
  } catch (_) {}

  await IsolateExecutor.run(
    BuildExecutable(context.safeMap),
    packageConfigURI:
        sourceDirectoryUri.resolve('.dart_tool/package_config.json'),
    imports: [
      "package:conduit_runtime/runtime.dart",
      context.targetScriptFileUri.toString()
    ],
    logHandler: (s) => print(s), //ignore: avoid_print
  );
}