sortPubspec function
Alphabetically sorts the dependency sections of a pubspec.yaml.
Sorts dependencies, dev_dependencies, and dependency_overrides by key
(case-insensitive). This is a line-based sort: each dependency keeps its
nested value lines (e.g. a git:/version: block) and any comment lines
directly above it, so comments and formatting are preserved. See issue
import_sorter#89.
Returns the rewritten YAML. If nothing needs reordering, the content is returned unchanged — byte for byte, so a file without a final newline is not reported as unsorted for the lack of one. A CRLF file stays CRLF on every line, including the one that used to be last.
Implementation
String sortPubspec(String pubspecContent) {
final crlf = pubspecContent.contains('\r\n');
final lf = crlf ? pubspecContent.replaceAll('\r\n', '\n') : pubspecContent;
final sorted = _sortLines(lf);
if (sorted == lf) return pubspecContent;
return crlf ? sorted.replaceAll('\n', '\r\n') : sorted;
}