pathPubspecGetDependencyLines function

Future<List<String>?> pathPubspecGetDependencyLines(
  1. String dir,
  2. String dependency
)

Null if not a dependency, formatted on a single line with depencency prefix or multiple lines

Implementation

Future<List<String>?> pathPubspecGetDependencyLines(
    String dir, String dependency) async {
  var map = await pathGetPubspecYamlMap(dir);

  var lines = <String>[];
  if (pubspecYamlHasAnyDependencies(map, [dependency])) {
    var readLines = _loadPubspecContentLines(dir).toList();
    String? headerLine;
    var foundHeader = false;
    for (var i = 0; i < readLines.length; i++) {
      var line = readLines[i];
      if (foundHeader) {
        if (line.startsWith('    ')) {
          lines.add(line.substring(4));
        } else {
          break;
        }
      } else if (line.startsWith('  $dependency:')) {
        headerLine = line..trim();
        foundHeader = true;
      }
    }
    if (lines.isEmpty && headerLine != null) {
      lines.add(headerLine);
    }
    return lines;
  }
  return null;
}