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
      .getParsedUnit2(analyzer.path) as ParsedUnitResult;

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

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

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