compile method

Future<File> compile({
  1. required Directory root,
})

Implementation

Future<File> compile({required Directory root}) async {
  final kernel = await root.getInternalRevaliFile(kernelFile);

  final toCompile = await root.getInternalRevaliFile(entrypointFile);

  var packageConfig = await root.getPackageConfig();

  if (!packageConfig.existsSync()) {
    logger.detail(
      'package_config missing before pub get: ${packageConfig.path} '
      '(project root: ${root.path})',
    );

    final progress = logger.progress('Running pub get');
    final result = await Process.run('dart', [
      'pub',
      'get',
      '--no-precompile',
    ], workingDirectory: root.path);
    progress.complete('Got dependencies');

    if (result.exitCode != 0) {
      throw Exception(
        'Failed to get dependencies in ${root.path}\n'
        'stdout: ${result.stdout}\n'
        'stderr: ${result.stderr}',
      );
    }

    packageConfig = await root.getPackageConfig();
    if (!packageConfig.existsSync()) {
      throw Exception(
        'package_config.json still missing after pub get at '
        '${packageConfig.path} (project root: ${root.path})',
      );
    }

    logger.detail(
      'package_config resolved after pub get: ${packageConfig.path}',
    );
  }

  final result = await Process.run('dart', [
    'compile',
    'kernel',
    toCompile.path,
    '-o',
    kernel.path,
  ], runInShell: true);

  if (result.exitCode != 0) {
    throw Exception('''
Failed to compile entrypoint
Error:
${result.stderr}''');
  }

  return kernel;
}