runStyleHost function

Future<void> runStyleHost(
  1. List<String> arguments,
  2. List<StyleRule> rules
)

Runs the custom rules of a package for Inspectra.

Inspectra generates a small program that imports the package's custom rule files and calls this function; it is not meant to be called by hand. arguments holds the path of the request Inspectra wrote: the package root, the files to check relative to it, the ids of the rules that are switched off, and where to write the answer. The answer lists every rule with its description and the violations of the rules that are on, or the reason why the rules cannot run.

Throws a FileSystemException when the request cannot be read.

Implementation

Future<void> runStyleHost(List<String> arguments, List<StyleRule> rules) async {
  final request = jsonDecode(
    await File(arguments.single).readAsString(),
  ) as Map<String, Object?>;
  final output = File(request['output']! as String);
  final String? error = _validate(rules);
  if (error != null) {
    await output.writeAsString(jsonEncode(<String, Object?>{'error': error}));
    return;
  }
  final disabled = <String>{
    for (final Object? id in request['disabled']! as List<Object?>) '$id',
  };
  final checker = StyleChecker(<StyleRule>[
    for (final rule in rules)
      if (!disabled.contains(rule.id)) rule,
  ]);
  final violations = <StyleViolation>[];
  final root = request['root']! as String;
  for (final path in request['files']! as List<Object?>) {
    final String content = await File(p.join(root, '$path')).readAsString();
    violations.addAll(checker.checkSource('$path', content));
  }
  await output.writeAsString(
    jsonEncode(<String, Object?>{
      'rules': <Map<String, Object?>>[
        for (final rule in rules)
          <String, Object?>{'id': rule.id, 'description': rule.description},
      ],
      'violations': <Map<String, Object?>>[
        for (final violation in violations) violation.toJson(),
      ],
    }),
  );
}