readScenarioRunIndex function

Future<ScenarioRunIndex> readScenarioRunIndex(
  1. String directory
)

Reads the index.json a matrix run wrote at the root of its output tree.

Each entry's ScenarioRunIndexEntry.output is relative to directory; the directory it names holds that point's own run.json, which ScenarioRunReport.read takes from there.

Throws FormatException when the directory holds no index, or holds one this version cannot read.

Implementation

Future<ScenarioRunIndex> readScenarioRunIndex(String directory) async {
  var file = File('$directory${Platform.pathSeparator}$scenarioRunIndexFile');
  if (!file.existsSync()) {
    throw FormatException(
      'No $scenarioRunIndexFile in "$directory". Only a matrix run '
      '(`devices=` / `languages=`) writes one, at the root of its output '
      'tree.',
    );
  }
  return ScenarioRunIndex.fromJson(switch (jsonDecode(
    await file.readAsString(),
  )) {
    Map json => json.cast<String, Object?>(),
    var other => throw FormatException(
      '${file.path} is ${other.runtimeType}, not an object.',
    ),
  });
}