get static method

Future<ScriptRunnerConfig> get([
  1. FileSystem? fileSystem
])

Loads the script runner configuration from the current directory. You may give it a FileSystem to use, or it will use the default local one.

The configuration is loaded in the following priority:

  1. pubspec.yaml -> from 'script_runner' key
  2. script_runner.yaml -> from '.' key (root)

If none are found, an Exception is thrown.

Implementation

static Future<ScriptRunnerConfig> get([FileSystem? fileSystem]) async {
  final fs = fileSystem ?? LocalFileSystem();
  final startDir = fs.currentDirectory.path;

  final sourceMap = await _tryFindConfig(fs, startDir);

  if (sourceMap.isEmpty) {
    throw StateError(
        'Must provide scripts in either pubspec.yaml or script_runner.yaml');
  }

  final source = sourceMap.values.first;
  final configSource = sourceMap.keys.first;

  final env = <String, String>{}..addAll(
      (source['env'] as Map?)?.cast<String, String>() ?? {},
    );

  return ScriptRunnerConfig(
    shell: ScriptRunnerShellConfig.parse(source['shell']),
    scripts: _parseScriptsList(source['scripts'], fileSystem: fs),
    env: env,
    workingDir: source['cwd'],
    fileSystem: fileSystem,
    lineLength: source['line_length'] ?? 80,
    configSource: configSource,
  );
}