readPackagePom function

Future<String> readPackagePom(
  1. JavaRuntimeSpec spec
)

spec's POM, read from its package (JavaRuntimeSpec.pomPackageFile under the package root), or from the current directory when the package cannot be resolved — running from the package's own checkout.

The package root is found from its package: URI, which points into lib/; a POM lives beside lib/, out of package: reach.

Implementation

Future<String> readPackagePom(JavaRuntimeSpec spec) async {
  final searched = <String>[];
  final lib = await Isolate.resolvePackageUri(
    Uri.parse('package:${spec.name}/'),
  );
  if (lib != null && lib.scheme == 'file') {
    final root = Directory.fromUri(lib).parent;
    final pom = File(joinPath([root.path, spec.pomPackageFile]));
    searched.add(pom.path);
    if (pom.existsSync()) return pom.readAsStringSync();
  }
  final local = File(joinPath([Directory.current.path, spec.pomPackageFile]));
  searched.add(local.path);
  if (local.existsSync()) return local.readAsStringSync();

  throw StateError(
    '${spec.pomPackageFile} of ${spec.name} not found. Searched:\n'
    '${searched.map((s) => '  - $s').join('\n')}\n'
    'Run from a package that depends on ${spec.name}, after `dart pub get`.',
  );
}