Keep a script's arguments and trailing comment when it moves between orchestrators

This commit is contained in:
Gmer4Lfe
2026-08-24 18:22:59 -04:00
parent 28987240aa
commit 354992c9e7
+35 -9
View File
@@ -25,9 +25,18 @@
// of every orchestrator. That is a distinct intent from conf_toggle.php's commenting // of every orchestrator. That is a distinct intent from conf_toggle.php's commenting
// out — this removes the line, that disables it in place. // out — this removes the line, that disables it in place.
// //
// Indentation is normalised on re-insertion. // Entry text moves verbatim; only a fresh entry is written from the bare path.
// The moved line is rewritten as two spaces and the quoted path, so a script does not // An entry may carry inline arguments ("Media/media_cleaner.sh anime"), a trailing
// carry its old array's formatting into its new one. // 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. // 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 // The removal pass is best-effort; only a missing *target* is an error. A script that
@@ -115,17 +124,29 @@ if (!$lines) {
exit; exit;
} }
$scriptEsc = preg_quote($script, '/'); $scriptEsc = preg_quote($script, '/');
$removedLine = null; $removedLines = [];
$inArray = false; $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 = []; $newLines = [];
foreach ($lines as $line) { foreach ($lines as $line) {
if (preg_match('/^\s*[A-Z_]+_SCRIPTS\s*=\s*\(/', $line)) $inArray = true; 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*(?:#.*)?$/', $line) && !str_contains($line, '(')) $inArray = false;
if ($inArray && preg_match('/^\s*(?:#\s*)?"' . $scriptEsc . '(?:\s[^"]*)?"/', $line)) { if ($inArray && preg_match('/^\s*(?:#\s*)?"' . $scriptEsc . '(?:\s[^"]*)?"/', $line)) {
$removedLine = ' "' . $script . '"' . "\n"; // normalise indentation when re-inserting $removedLines[] = $line;
continue; // drop from current location continue; // drop from current location
} }
$newLines[] = $line; $newLines[] = $line;
@@ -136,10 +157,15 @@ if ($toArray) {
$resultLines = []; $resultLines = [];
$inTarget = false; $inTarget = false;
$inserted = false; $inserted = false;
$indent = ' '; // master.conf indents array entries eight spaces
foreach ($newLines as $line) { foreach ($newLines as $line) {
if (preg_match('/^\s*' . preg_quote($toArray, '/') . '\s*=\s*\(/', $line)) $inTarget = true; 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, '(')) { 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; $inTarget = false;
$inserted = true; $inserted = true;
} }