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
  String 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 unitResult = analyzerContext.currentSession
      .getParsedUnit2(analyzer.path) as ParsedUnitResult;
  final mainFunctions = unitResult.unit.declarations
      .whereType<FunctionDeclaration>()
      .where((f) => f.name.name == "main")
      .toList();

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

  strippedScriptFile.writeAsStringSync(scriptSource);

  await Loner.run(BuildExecutable(context.safeMap),
      packageConfigURI: sourceDirectoryUri.resolve(".packages"),
      imports: [
        "package:replica/replica.dart",
        context.targetScriptFileUri.toString()
      ],
      logHandler: (s) => print(s));
}