Keep a script's arguments and trailing comment when it moves between orchestrators
This commit is contained in:
@@ -25,9 +25,18 @@
|
||||
// of every orchestrator. That is a distinct intent from conf_toggle.php's commenting
|
||||
// out — this removes the line, that disables it in place.
|
||||
//
|
||||
// Indentation is normalised on re-insertion.
|
||||
// The moved line is rewritten as two spaces and the quoted path, so a script does not
|
||||
// carry its old array's formatting into its new one.
|
||||
// Entry text moves verbatim; only a fresh entry is written from the bare path.
|
||||
// An entry may carry inline arguments ("Media/media_cleaner.sh anime"), a trailing
|
||||
// comment and its own indentation. Regenerating the line from the script path drops
|
||||
// all three — six live entries in master.conf carry arguments, and stripping them
|
||||
// would leave media_cleaner.sh with no share and fallback.sh without --stop. The
|
||||
// matched line is therefore carried across untouched, which is the same rule
|
||||
// reorderarray.php follows. A script that was in no array is written fresh, indented
|
||||
// to match the entries already in the target.
|
||||
//
|
||||
// One path may hold several entries, and they move together.
|
||||
// "Media/media_cleaner.sh anime" and "… media" are two jobs sharing a path. Every
|
||||
// match is collected and re-inserted, rather than collapsing them into one.
|
||||
//
|
||||
// A move that finds nothing to move still succeeds.
|
||||
// The removal pass is best-effort; only a missing *target* is an error. A script that
|
||||
@@ -116,16 +125,28 @@ if (!$lines) {
|
||||
}
|
||||
|
||||
$scriptEsc = preg_quote($script, '/');
|
||||
$removedLine = null;
|
||||
$removedLines = [];
|
||||
$inArray = false;
|
||||
|
||||
// Step 1: find and remove the script line from whatever array it is currently in.
|
||||
// Step 1: find and remove the script's line(s) from whatever array they are in.
|
||||
//
|
||||
// The original text is carried across verbatim. An entry is not just a path — it may hold
|
||||
// inline arguments ("Media/media_cleaner.sh anime"), a trailing comment, and the file's
|
||||
// indentation, and regenerating the line from the bare path silently drops all three. Six
|
||||
// live entries in master.conf carry arguments; a move that strips them would leave
|
||||
// media_cleaner.sh with no share to clean and fallback.sh without --stop.
|
||||
//
|
||||
// reorderarray.php preserves entry text for exactly this reason. A move must not be the one
|
||||
// operation that loses it.
|
||||
//
|
||||
// A path can legitimately appear more than once in the same array with different arguments,
|
||||
// so every match is collected and re-inserted together rather than collapsing to one.
|
||||
$newLines = [];
|
||||
foreach ($lines as $line) {
|
||||
if (preg_match('/^\s*[A-Z_]+_SCRIPTS\s*=\s*\(/', $line)) $inArray = true;
|
||||
if ($inArray && preg_match('/^\s*\)\s*(?:#.*)?$/', $line) && !str_contains($line, '(')) $inArray = false;
|
||||
if ($inArray && preg_match('/^\s*(?:#\s*)?"' . $scriptEsc . '(?:\s[^"]*)?"/', $line)) {
|
||||
$removedLine = ' "' . $script . '"' . "\n"; // normalise indentation when re-inserting
|
||||
$removedLines[] = $line;
|
||||
continue; // drop from current location
|
||||
}
|
||||
$newLines[] = $line;
|
||||
@@ -136,10 +157,15 @@ if ($toArray) {
|
||||
$resultLines = [];
|
||||
$inTarget = false;
|
||||
$inserted = false;
|
||||
$indent = ' '; // master.conf indents array entries eight spaces
|
||||
foreach ($newLines as $line) {
|
||||
if (preg_match('/^\s*' . preg_quote($toArray, '/') . '\s*=\s*\(/', $line)) $inTarget = true;
|
||||
// Match the indentation the target array actually uses rather than assuming it.
|
||||
if ($inTarget && preg_match('/^(\s+)(?:#\s*)?"/', $line, $im)) $indent = $im[1];
|
||||
if ($inTarget && !$inserted && preg_match('/^\s*\)\s*(?:#.*)?$/', $line) && !str_contains($line, '(')) {
|
||||
$resultLines[] = $removedLine ?? (' "' . $script . '"' . "\n");
|
||||
foreach ($removedLines ?: [$indent . '"' . $script . '"' . "\n"] as $moved) {
|
||||
$resultLines[] = $moved;
|
||||
}
|
||||
$inTarget = false;
|
||||
$inserted = true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user