syncTo method
Match the package dependencies versions from _versionsFile
and override
it the pubspecFile
.
If the matched versions is different, it will print the changes to the console.
Implementation
void syncTo(File pubspecFile) {
YamlMap? versionsMap = loadYaml(_versionsFile.readAsStringSync());
final pubspecMap = loadYaml(pubspecFile.readAsStringSync());
StringBuffer newContent = StringBuffer();
final pubspecFileLines = pubspecFile.readAsLinesSync();
int lineIndex = 0;
for (; lineIndex < pubspecFileLines.length;) {
final line = pubspecFileLines[lineIndex];
final trimLine = line.trim();
// Skip comment the yaml list
if (trimLine.startsWith("#") || !trimLine.contains(":")) {
newContent.writeln(line);
++lineIndex;
continue;
}
final key = trimLine.split(":").first.trim();
if (versionsMap!.containsKey(key)) {
if (trimLine.endsWith(":")) {
YamlMap? versionsValue = versionsMap[key];
YamlMap? pubspecValue = _findMapByKey(key, pubspecMap);
if (versionsValue.toString() != pubspecValue.toString()) {
final versionsValueLines =
map2Lines(key, line.indexOf(key), 0, versionsValue!);
newContent.write(versionsValueLines);
_stdout.write(
_createStdoutMessage(key, pubspecValue!, key, versionsValue));
final pubspecValueLineLength = _map2LinesCount(pubspecValue);
lineIndex += pubspecValueLineLength + 1;
continue;
}
} else {
final newLine =
line.substring(0, line.indexOf(":")) + ": " + versionsMap[key];
final trimNewLine = newLine.trim();
if (trimNewLine != trimLine) {
newContent.writeln(newLine);
_stdout.writeln(
"${_textWithRemovedColor(trimLine)} -> ${_textWithAddedColor(trimNewLine)}");
++lineIndex;
continue;
}
}
}
newContent.writeln(line);
++lineIndex;
}
pubspecFile.writeAsStringSync(newContent.toString());
_stdout.writeln("Complete!");
}