build method

  1. @override
Future<void> build(
  1. BuildStep buildStep
)

Generates the outputs for a given BuildStep.

Implementation

@override
Future<void> build(BuildStep buildStep) async {
  var id = buildStep.inputId;
  var contents = await buildStep.readAsString(id);
  var assetPath = id.pathSegments.first == 'lib'
      ? p.url.join('packages', id.package, id.path)
      : id.path;

  var vmRuntimes = [Runtime.vm];
  var browserRuntimes =
      Runtime.builtIn.where((r) => r.isBrowser == true).toList();
  var nodeRuntimes = [Runtime.nodeJS];
  var config = await _ConfigLoader.instance.load(id.package, buildStep);
  if (config != null) {
    for (var customRuntime in config.defineRuntimes.values) {
      var parent = customRuntime.parent;
      if (vmRuntimes.any((r) => r.identifier == parent)) {
        var runtime = vmRuntimes.firstWhere((r) => r.identifier == parent);
        vmRuntimes.add(
            runtime.extend(customRuntime.name, customRuntime.identifier));
      } else if (browserRuntimes.any((r) => r.identifier == parent)) {
        var runtime =
            browserRuntimes.firstWhere((r) => r.identifier == parent);
        browserRuntimes.add(
            runtime.extend(customRuntime.name, customRuntime.identifier));
      } else if (nodeRuntimes.any((r) => r.identifier == parent)) {
        var runtime = nodeRuntimes.firstWhere((r) => r.identifier == parent);
        nodeRuntimes.add(
            runtime.extend(customRuntime.name, customRuntime.identifier));
      }
    }
  }
  var metadata = parseMetadata(
      assetPath,
      contents,
      vmRuntimes
          .followedBy(browserRuntimes)
          .followedBy(nodeRuntimes)
          .map((r) => r.identifier)
          .toSet());

  if (vmRuntimes.any((r) => metadata.testOn.evaluate(SuitePlatform(r)))) {
    await buildStep.writeAsString(id.addExtension('.vm_test.dart'), '''
        ${metadata.languageVersionComment ?? ''}
        import "dart:isolate";

        import "package:test/bootstrap/vm.dart";

        import "${p.url.basename(id.path)}" as test;

        void main(_, SendPort message) {
          internalBootstrapVmTest(() => test.main, message);
        }
      ''');
  }

  if (browserRuntimes
      .any((r) => metadata.testOn.evaluate(SuitePlatform(r)))) {
    await buildStep.writeAsString(id.addExtension('.browser_test.dart'), '''
        ${metadata.languageVersionComment ?? ''}
        import "package:test/bootstrap/browser.dart";

        import "${p.url.basename(id.path)}" as test;

        void main() {
          if (Uri.base.queryParameters['directRun'] == 'true') {
            test.main();
          } else {
            internalBootstrapBrowserTest(() => test.main);
          }
        }
      ''');
  }

  if (nodeRuntimes.any((r) => metadata.testOn.evaluate(SuitePlatform(r)))) {
    await buildStep.writeAsString(id.addExtension('.node_test.dart'), '''
        ${metadata.languageVersionComment ?? ''}
        import "package:test/bootstrap/node.dart";

        import "${p.url.basename(id.path)}" as test;

        void main() {
          internalBootstrapNodeTest(() => test.main);
        }
      ''');
  }
}