ScriptsConfig.fromJson constructor

ScriptsConfig.fromJson(
  1. Map json_
)

Implementation

factory ScriptsConfig.fromJson(Map json_) {
  final logger = Logger();

  final json = {...json_};
  final scripts = <String, Script>{};

  final parents = (json.remove(Keys.parents) as List?)?.cast<String>();

  final allowedKeys = RegExp(
    r'^_?([a-z][a-z0-9_.\-]*)?(?<=[a-z0-9_])$',
    caseSensitive: false,
  );

  for (final entry in json.entries) {
    final key = '${entry.key}'.trim();
    if (key.contains(' ')) {
      logger.err(
        'The script name "$key" contains spaces, '
        'which is not allowed.',
      );
      continue;
    }

    if (!allowedKeys.hasMatch(key) && !Keys.scriptParameters.contains(key)) {
      logger.err(
        'The script name "$key" uses forbidden characters, allowed: '
        '${allowedKeys.pattern} (case insensitive)',
      );
      continue;
    }

    scripts[key] = Script.fromJson(
      key,
      entry.value,
      parents: parents,
    );
  }

  return ScriptsConfig(
    scripts: scripts,
    parents: parents,
  );
}