execute method

Future execute()

Implementation

Future execute() async {
  final compilers = context.context.compilers;

  print("Resolving ASTs...");
  final astsToResolve =
      Set.from(compilers.expand((c) => c.getUrisToResolve(context)));
  await Future.forEach(astsToResolve, (astUri) async {
    await context.analyzer!
        .resolveUnitAt(context.resolveUri((astUri as Uri?) ?? Uri.parse('')));
  });

  print("Generating replica...");

  final replicaGenerator = RuntimeGenerator();
  context.context.replicas!.map.forEach((typeName, replica) {
    if (replica is SourceCompiler) {
      replicaGenerator.addRuntime(
          name: typeName, source: replica.compile(context));
    }
  });

  await replicaGenerator.writeTo(context.buildRuntimeDirectory.uri);
  print("Generated replica at '${context.buildRuntimeDirectory.uri}'.");

  final nameOfPackageBeingCompiled = context.sourceApplicationPubspec.name;
  final pubspecMap = {
    'name': 'replica_target',
    'version': '1.0.0',
    'environment': {'sdk': '>=2.7.0 <3.0.0'},
    'dependency_overrides': {}
  };
  Map overrides = pubspecMap['dependency_overrides'] as dynamic;
  var sourcePackageIsCompiled = false;

  compilers.forEach((compiler) {
    final packageInfo = _getPackageInfoForCompiler(compiler);
    final sourceDirUri = packageInfo.uri;
    final targetDirUri =
        context.buildPackagesDirectory.uri.resolve("${packageInfo.name}/");

    print("Compiling package '${packageInfo.name}'...");
    copyPackage(sourceDirUri, targetDirUri);
    compiler.deflectPackage(Directory.fromUri(targetDirUri));

    if (packageInfo.name != nameOfPackageBeingCompiled) {
      overrides[packageInfo.name] = {
        "path": targetDirUri.toFilePath(windows: Platform.isWindows)
      };
    } else {
      sourcePackageIsCompiled = true;
    }
    print("Package '${packageInfo.name} compiled to '${targetDirUri}'.");
  });

  final appDst = context.buildApplicationDirectory.uri;
  if (!sourcePackageIsCompiled) {
    print(
        "Copying application package (from '${context.sourceApplicationDirectory.uri}')...");
    copyPackage(context.sourceApplicationDirectory.uri, appDst);
    print("Application packaged copied to '${appDst}'.");
  }
  pubspecMap['dependencies'] = {
    nameOfPackageBeingCompiled: {
      "path": appDst.toFilePath(windows: Platform.isWindows)
    }
  };

  if (context.forTests) {
    final devDeps = context.sourceApplicationPubspecMap['dev_dependencies'];
    if (devDeps != null) {
      pubspecMap['dev_dependencies'] = devDeps;
    }
  }

  File.fromUri(context.buildDirectoryUri.resolve("pubspec.yaml"))
      .writeAsStringSync(json.encode(pubspecMap));

  context
      .getFile(context.targetScriptFileUri)
      .writeAsStringSync(context.source);

  context.context.compilers.forEach((compiler) {
    compiler.didFinishPackageGeneration(context);
  });

  print("Fetching dependencies (--offline --no-precompile)...");
  await getDependencies();
  print("Finished fetching dependencies.");

  if (!context.forTests) {
    print("Compiling...");
    await compile(context.targetScriptFileUri, context.executableUri);
    print("Success. Executable is located at '${context.executableUri}'.");
  }
}