shellScriptSplitLines function
Convert a script to multiple commands (single line) or comments
Comments lines start with # or //
Implementation
List<String> shellScriptSplitLines(String script, {bool? skipComments}) {
skipComments ??= false;
var commands = <String>[];
// non null when previous line ended with ^ or \
String? currentCommand;
for (var line in LineSplitter.split(script)) {
line = line.trim();
void addAndClearCurrent(String command) {
commands.add(command);
currentCommand = null;
}
if (line.isNotEmpty) {
if (shellScriptLineIsComment(line)) {
if (!skipComments) {
commands.add(line);
}
} else {
// append to previous
if (currentCommand != null) {
line = '$currentCommand $line';
}
if (isLineToBeContinued(line)) {
// remove ending character
currentCommand = line.substring(0, line.length - 1).trim();
} else {
addAndClearCurrent(line);
}
}
} else {
// terminate current
if (currentCommand != null) {
addAndClearCurrent(currentCommand!);
}
}
}
return commands;
}