execute method

Future execute()

Implementation

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

  print("Resolving ASTs...");
  final astsToResolve = <Uri>{
    ...compilers.expand((c) => c.getUrisToResolve(context))
  };
  await Future.forEach<Uri>(
    astsToResolve,
    (astUri) async {
      final package = await context.getPackageFromUri(astUri);
      final Uri packageUri =
          package?.packageUriRoot.resolve(package.name) ?? astUri;
      return context.analyzer.resolveUnitOrLibraryAt(packageUri);
    },
  );

  print("Generating runtime...");

  final runtimeGenerator = RuntimeGenerator();
  for (final MapEntry<String, dynamic> entry
      in context.context.runtimes.map.entries) {
    if (entry.value is SourceCompiler) {
      await (entry.value as SourceCompiler).compile(context).then(
            (source) =>
                runtimeGenerator.addRuntime(name: entry.key, source: source),
          );
    }
  }

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

  final nameOfPackageBeingCompiled = context.sourceApplicationPubspec.name;
  final pubspecMap = <String, Object>{
    'name': 'runtime_target',
    'version': '1.0.0',
    'environment': {'sdk': '>=3.4.0 <4.0.0'},
    'dependency_overrides': {}
  };
  final overrides = pubspecMap['dependency_overrides'] as Map;
  var sourcePackageIsCompiled = false;

  for (final compiler in compilers) {
    final packageInfo = await _getPackageInfoForCompiler(compiler);
    final sourceDirUri = packageInfo.root;
    final targetDirUri =
        context.buildPackagesDirectory.uri.resolve("${packageInfo.name}/");
    print("Compiling package '${packageInfo.name}'...");
    await 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}')...",
    );
    await 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;
    }

    overrides['conduit_core'] = {
      'path': appDst.toFilePath(windows: Platform.isWindows)
    };
  }

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

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

  for (final compiler in context.context.compilers) {
    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}'.");
  }
}