deflectPackage method Null safety

  1. @override
void deflectPackage(
  1. Directory destinationDirectory
)
override

Modifies a package on the filesystem in order to remove dart:mirrors from the package.

A copy of this compiler's package will be written to destinationDirectory. This method is overridden to modify the contents of that directory to remove all uses of dart:mirrors.

Packages should export their Compiler in their main library file and only import mirrors in files directly or transitively imported by the Compiler file. This method should remove that export statement and therefore remove all transitive mirror imports.

Implementation

@override
void deflectPackage(Directory destinationDirectory) {
  final libraryFile = File.fromUri(
    destinationDirectory.uri.resolve("lib/").resolve("runtime.dart"),
  );
  libraryFile.writeAsStringSync(
    "library runtime;\nexport 'src/context.dart';\nexport 'src/exceptions.dart';",
  );

  final contextFile = File.fromUri(
    destinationDirectory.uri
        .resolve("lib/")
        .resolve("src/")
        .resolve("context.dart"),
  );
  final contextFileContents = contextFile.readAsStringSync().replaceFirst(
        "import 'package:conduit_runtime/src/mirror_context.dart' as context;",
        "import 'package:generated_runtime/generated_runtime.dart' as context;",
      );
  contextFile.writeAsStringSync(contextFileContents);

  final pubspecFile =
      File.fromUri(destinationDirectory.uri.resolve("pubspec.yaml"));
  final pubspecContents = pubspecFile.readAsStringSync().replaceFirst(
        "\ndependencies:",
        "\ndependencies:\n  generated_runtime:\n    path: ../../generated_runtime/",
      );
  pubspecFile.writeAsStringSync(pubspecContents);
}