Skip to content

Commit

Permalink
WIp
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry committed Oct 10, 2023
1 parent 7ef21d3 commit 4ce0b17
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 192 deletions.
71 changes: 11 additions & 60 deletions src/Console/Command/Diff.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
use Fidry\Console\Input\IO;
use KevinGH\Box\Console\PharInfoRenderer;
use KevinGH\Box\Phar\PharDiff;
use KevinGH\Box\PharInfo\DiffMode;
use KevinGH\Box\Phar\PharInfo;
use KevinGH\Box\Phar\DiffMode;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\BufferedOutput;
Expand All @@ -30,8 +30,8 @@
use function array_map;
use function count;
// TODO: migrate to Safe API
use function implode;
use function explode;
use function implode;
use function is_string;
use function sprintf;
use const PHP_EOL;
Expand Down Expand Up @@ -120,10 +120,10 @@ public function execute(IO $io): int

$diff = new PharDiff(...$paths);

$this->showArchives($diff, $io);
$result1 = $this->compareArchives($diff, $io);
$result2 = $this->compareContents($diff, $io);

return $result2;
return $result1 + $result2;
}

/**
Expand All @@ -144,8 +144,10 @@ private static function getPaths(IO $io): array
);
}

private function showArchives(PharDiff $diff, IO $io): void
private function compareArchives(PharDiff $diff, IO $io): int
{
$io->comment('<info>Comparing the two archives... (do not check the signatures)</info>');

$pharInfoA = $diff->getPharInfoA();
$pharInfoB = $diff->getPharInfoB();

Expand All @@ -168,6 +170,8 @@ private function showArchives(PharDiff $diff, IO $io): void
$pharInfoB,
$io,
);

return ExitCode::FAILURE;
}

private static function getDiffMode(IO $io): DiffMode
Expand Down Expand Up @@ -216,6 +220,8 @@ private static function getDiffMode(IO $io): DiffMode

private function compareContents(PharDiff $diff, IO $io): int
{
$io->comment('<info>Comparing the two archives contents...</info>');

$checkSumAlgorithm = $io->getOption(self::CHECK_OPTION)->asNullableNonEmptyString() ?? self::DEFAULT_CHECKSUM_ALGO;

if ($io->hasOption('-c') || $io->hasOption('--check')) {
Expand All @@ -239,61 +245,6 @@ private function compareContents(PharDiff $diff, IO $io): int
return ExitCode::FAILURE;
}

$io->writeln(sprintf(
'--- Files present in "%s" but not in "%s"',
$diff->getPharA()->getFileName(),
$diff->getPharB()->getFileName(),
));
$io->writeln(sprintf(
'+++ Files present in "%s" but not in "%s"',
$diff->getPharB()->getFileName(),
$diff->getPharA()->getFileName(),
));

$io->newLine();

self::renderPaths('-', $diff->getPharA(), $diffResult[0], $io);
self::renderPaths('+', $diff->getPharB(), $diffResult[1], $io);

$io->error(sprintf(
'%d file(s) difference',
count($diffResult[0]) + count($diffResult[1]),
));

return ExitCode::FAILURE;
}

private function compareContentssS(PharDiff $diff, IO $io): int
{
$io->comment('<info>Comparing the two archives contents...</info>');

$checkSumAlgorithm = $io->getOption(self::CHECK_OPTION)->asNullableNonEmptyString() ?? self::DEFAULT_CHECKSUM_ALGO;

if ($io->hasOption('-c') || $io->hasOption('--check')) {
return $diff->listChecksums($checkSumAlgorithm);
}

if ($io->getOption(self::GNU_DIFF_OPTION)->asBoolean()) {
$diffResult = $diff->gnuDiff();
} elseif ($io->getOption(self::GIT_DIFF_OPTION)->asBoolean()) {
$diffResult = $diff->gitDiff();
} else {
$diffResult = $diff->listDiff();
}

if (null === $diffResult || [[], []] === $diffResult) {
$io->success('The contents are identical');

return ExitCode::SUCCESS;
}

if (is_string($diffResult)) {
// Git or GNU diff: we don't have much control on the format
$io->writeln($diffResult);

return ExitCode::FAILURE;
}

$io->writeln(sprintf(
'--- Files present in "%s" but not in "%s"',
$diff->getPharInfoA()->getFileName(),
Expand Down
14 changes: 11 additions & 3 deletions src/Phar/DiffMode.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@

declare(strict_types=1);

namespace KevinGH\Box\PharInfo;
/*
* This file is part of the box project.
*
* (c) Kevin Herrera <[email protected]>
* Théo Fidry <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace KevinGH\Box\Phar;

use BackedEnum;
use UnitEnum;
use function array_map;

enum DiffMode: string
Expand Down
28 changes: 7 additions & 21 deletions src/Phar/PharDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

use KevinGH\Box\Console\Command\Extract;
use KevinGH\Box\Pharaoh\PharDiff as ParagoniePharDiff;
use KevinGH\Box\Phar\DiffMode;
use SplFileInfo;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Process\Process;
Expand All @@ -26,6 +27,9 @@
use function str_replace;
use const DIRECTORY_SEPARATOR;

/**
* @internal
*/
final class PharDiff
{
private readonly ParagoniePharDiff $diff;
Expand Down Expand Up @@ -68,8 +72,8 @@ public function diff(DiffMode $mode): null|string|array
}

return self::getDiff(
$this->pharA,
$this->pharB,
$this->pharInfoA,
$this->pharInfoB,
self::getModeCommand($mode),
);
}
Expand All @@ -82,24 +86,6 @@ private static function getModeCommand(DiffMode $mode): string
};
}

public function gitDiff(): ?string
{
return self::getDiff(
$this->pharInfoA,
$this->pharInfoB,
'git diff --no-index',
);
}

public function gnuDiff(): ?string
{
return self::getDiff(
$this->pharInfoA,
$this->pharInfoB,
'diff --exclude='.Extract::PHAR_META_PATH,
);
}

/**
* @see ParagoniePharDiff::listChecksums()
*/
Expand All @@ -113,7 +99,7 @@ public function listChecksums(string $algo = 'sha384'): int
* which are not in the second and the second array all the files present in the second PHAR but
* not the first one.
*/
public function listDiff(): array
private function listDiff(): array
{
$pharAFiles = self::collectFiles($this->pharInfoA);
$pharBFiles = self::collectFiles($this->pharInfoB);
Expand Down
Loading

0 comments on commit 4ce0b17

Please sign in to comment.