variables method

Future<Map<String, String?>?> variables()

Returns a map of all variables in the script file.

The keys of the map are the variable names, and the values are the variable values.

Returns null if the file does not exist or if no variables are found.

Implementation

Future<Map<String, String?>?> variables() async {
  final script = await contents();
  if (script == null) {
    return null;
  }
  final regex = RegExp('readonly (.*)="(.*)"');
  final matches = regex.allMatches(script);

  if (matches.isEmpty) {
    return null;
  }

  final variables = <String, String?>{};
  for (final match in matches) {
    final variableName = match.group(1)!;
    final variableValue = await get(variableName);
    variables[variableName] = variableValue;
  }

  return variables;
}