Skip to content

Commit 1554b4d

Browse files
Adds support for diffs in the package manager
Signed-off-by: Jon Stovell <jonstovell@gmail.com>
1 parent 9e8ee65 commit 1554b4d

4 files changed

Lines changed: 553 additions & 12 deletions

File tree

Sources/Diff/FullDiff.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919

2020
/**
2121
* Represents a full, two-way diff between two strings.
22-
*
23-
* @todo Add ability to use diffs in the package manager.
2422
*/
2523
class FullDiff extends Diff
2624
{
@@ -519,6 +517,9 @@ public function formatContext(): string
519517
* This information is used to indicate the correct file operations to
520518
* perform when applying the diff to files.
521519
*
520+
* @see \SMF\PackageManager\PackageUtils::parseDiff() for more information
521+
* on how to generate raw diffs for use in SMF's package manager.
522+
*
522523
* @param string $raw_diff The raw diff.
523524
* @throws \ValueError if $raw_diff does not contain valid raw diff data.
524525
* @return array Instances of this class.

Sources/PackageManager/PackageManager.php

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,9 @@ public function installTest(): void
461461
'failed' => true,
462462
];
463463
} else {
464-
if ($action['boardmod']) {
464+
if ($action['diff']) {
465+
$mod_actions = PackageUtils::parseDiff(@file_get_contents(Config::$packagesdir . '/temp/' . Utils::$context['base_path'] . $action['filename']), true, $action['reverse'], $theme_paths);
466+
} elseif ($action['boardmod']) {
465467
$mod_actions = PackageUtils::parseBoardMod(@file_get_contents(Config::$packagesdir . '/temp/' . Utils::$context['base_path'] . $action['filename']), true, $action['reverse'], $theme_paths);
466468
} else {
467469
$mod_actions = PackageUtils::parseModification(@file_get_contents(Config::$packagesdir . '/temp/' . Utils::$context['base_path'] . $action['filename']), true, $action['reverse'], $theme_paths);
@@ -540,6 +542,27 @@ public function installTest(): void
540542
'description' => Lang::getTxt('package_action_error', file: 'Packages'),
541543
'failed' => true,
542544
];
545+
} elseif ($mod_action['type'] == 'create-file') {
546+
Utils::$context['actions'][$actual_filename] = [
547+
'type' => Lang::$txt['package_create_file'],
548+
'action' => Utils::htmlspecialchars(strtr($mod_action['filename'], [Config::$boarddir => '.'])),
549+
'description' => $failed ? Lang::$txt['package_action_failure'] : '',
550+
'failed' => $failed,
551+
];
552+
} elseif ($mod_action['type'] == 'move-file') {
553+
Utils::$context['actions'][$actual_filename] = [
554+
'type' => Lang::$txt['package_move_file'],
555+
'action' => Utils::htmlspecialchars(strtr($mod_action['source'], [Config::$boarddir => '.'])) . ' => ' . Utils::htmlspecialchars(strtr($mod_action['destination'], [Config::$boarddir => '.'])),
556+
'description' => $failed ? Lang::$txt['package_action_missing'] : '',
557+
'failed' => $failed,
558+
];
559+
} elseif ($mod_action['type'] == 'remove-file') {
560+
Utils::$context['actions'][$actual_filename] = [
561+
'type' => Lang::$txt['package_delete_file'],
562+
'action' => Utils::htmlspecialchars(strtr($mod_action['filename'], [Config::$boarddir => '.'])),
563+
'description' => $failed ? Lang::$txt['package_action_missing'] : '',
564+
'failed' => $failed,
565+
];
543566
}
544567
}
545568

@@ -555,7 +578,16 @@ public function installTest(): void
555578
}
556579

557580
// We just need it for actual parse changes.
558-
if (!in_array($mod_action['type'], ['error', 'result', 'opened', 'saved', 'end', 'missing', 'skipping', 'chmod'])) {
581+
if (!in_array($mod_action['type'], ['error', 'result', 'opened', 'saved', 'end', 'missing', 'skipping', 'chmod', 'create-file', 'move-file', 'remove-file'])) {
582+
if (!isset(Utils::$context['actions'][$actual_filename])) {
583+
Utils::$context['actions'][$actual_filename] = [
584+
'type' => Lang::$txt['execute_modification'],
585+
'action' => Utils::htmlspecialchars(strtr($mod_action['filename'], [Config::$boarddir => '.'])),
586+
'description' => $failed ? Lang::$txt['package_action_failure'] : Lang::$txt['package_action_success'],
587+
'failed' => $failed,
588+
];
589+
}
590+
559591
if (empty($mod_action['is_custom'])) {
560592
Utils::$context['actions'][$actual_filename]['operations'][] = [
561593
'type' => Lang::getTxt('execute_modification', file: 'Packages'),
@@ -565,6 +597,7 @@ public function installTest(): void
565597
'operation_key' => $operation_key,
566598
'filename' => $action['filename'],
567599
'is_boardmod' => $action['boardmod'],
600+
'is_diff' => $action['diff'],
568601
'failed' => $mod_action['failed'],
569602
'ignore_failure' => !empty($mod_action['ignore_failure']),
570603
];
@@ -580,6 +613,7 @@ public function installTest(): void
580613
'operation_key' => $operation_key,
581614
'filename' => $action['filename'],
582615
'is_boardmod' => $action['boardmod'],
616+
'is_diff' => $action['diff'],
583617
'failed' => $mod_action['failed'],
584618
'ignore_failure' => !empty($mod_action['ignore_failure']),
585619
];
@@ -1081,7 +1115,9 @@ public function install(): void
10811115
$failed_count++;
10821116

10831117
if ($action['type'] == 'modification' && !empty($action['filename'])) {
1084-
if ($action['boardmod']) {
1118+
if ($action['diff']) {
1119+
$mod_actions = PackageUtils::parseDiff(file_get_contents(Config::$packagesdir . '/temp/' . Utils::$context['base_path'] . $action['filename']), false, $action['reverse'], $theme_paths);
1120+
} elseif ($action['boardmod']) {
10851121
$mod_actions = PackageUtils::parseBoardMod(file_get_contents(Config::$packagesdir . '/temp/' . Utils::$context['base_path'] . $action['filename']), false, $action['reverse'], $theme_paths);
10861122
} else {
10871123
$mod_actions = PackageUtils::parseModification(file_get_contents(Config::$packagesdir . '/temp/' . Utils::$context['base_path'] . $action['filename']), false, $action['reverse'], $theme_paths);
@@ -1850,8 +1886,9 @@ public function showOperations(): void
18501886
}
18511887
}
18521888

1853-
// Boardmod?
1854-
if (isset($_REQUEST['boardmod'])) {
1889+
if (isset($_REQUEST['diff'])) {
1890+
$mod_actions = PackageUtils::parseDiff(@file_get_contents(Config::$packagesdir . '/temp/' . Utils::$context['base_path'] . $_REQUEST['filename']), true, $reverse, $theme_paths);
1891+
} elseif (isset($_REQUEST['boardmod'])) {
18551892
$mod_actions = PackageUtils::parseBoardMod(@file_get_contents(Config::$packagesdir . '/temp/' . Utils::$context['base_path'] . $_REQUEST['filename']), true, $reverse, $theme_paths);
18561893
} else {
18571894
$mod_actions = PackageUtils::parseModification(@file_get_contents(Config::$packagesdir . '/temp/' . Utils::$context['base_path'] . $_REQUEST['filename']), true, $reverse, $theme_paths);
@@ -2635,7 +2672,7 @@ public function serverBrowse(): void
26352672
}
26362673

26372674
// Might take some time.
2638-
@set_time_limit(600);
2675+
Sapi::setTimeLimit();
26392676

26402677
// Read packages.xml and parse into XmlArray. (the true tells it to trim things ;).)
26412678
$listing = new XmlArray(WebFetchApi::fetch($_GET['package']), true);

0 commit comments

Comments
 (0)