Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display files size and limit depth with the info command #236

Merged
merged 1 commit into from
May 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified fixtures/info/tree-phar.phar
Binary file not shown.
2 changes: 1 addition & 1 deletion src/Console/Command/Build.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use const E_USER_DEPRECATED;
use function trigger_error;
use const E_USER_DEPRECATED;

/**
* @deprecated
Expand Down
7 changes: 5 additions & 2 deletions src/Console/Command/Compile.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
use function count;
use function decoct;
use function explode;
use function filesize;
use function function_exists;
use function get_class;
use function implode;
Expand All @@ -56,7 +57,7 @@
use function KevinGH\Box\FileSystem\make_path_relative;
use function KevinGH\Box\FileSystem\remove;
use function KevinGH\Box\FileSystem\rename;
use function KevinGH\Box\formatted_filesize;
use function KevinGH\Box\format_size;
use function KevinGH\Box\get_phar_compression_algorithms;
use function posix_setrlimit;
use function putenv;
Expand Down Expand Up @@ -828,7 +829,9 @@ private function logEndBuilding(BuildLogger $logger, SymfonyStyle $io, Box $box,
sprintf(
'PHAR: %s (%s)',
$box->count() > 1 ? $box->count().' files' : $box->count().' file',
formatted_filesize($path)
format_size(
filesize($path)
)
)
.PHP_EOL
.'You can inspect the generated PHAR with the "<comment>info</comment>" command.'
Expand Down
62 changes: 54 additions & 8 deletions src/Console/Command/Info.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

namespace KevinGH\Box\Console\Command;

use Assert\Assertion;
use DateTimeImmutable;
use DirectoryIterator;
use Phar;
Expand All @@ -34,11 +35,12 @@
use function array_sum;
use function count;
use function end;
use function filesize;
use function is_array;
use function iterator_to_array;
use function KevinGH\Box\FileSystem\copy;
use function KevinGH\Box\FileSystem\remove;
use function KevinGH\Box\formatted_filesize;
use function KevinGH\Box\format_size;
use function key;
use function realpath;
use function sprintf;
Expand All @@ -56,6 +58,7 @@ final class Info extends Command
private const LIST_OPT = 'list';
private const METADATA_OPT = 'metadata';
private const MODE_OPT = 'mode';
private const DEPTH_OPT = 'depth';

/**
* The list of recognized compression algorithms.
Expand Down Expand Up @@ -121,10 +124,17 @@ protected function configure(): void
$this->addOption(
self::MODE_OPT,
'm',
InputOption::VALUE_OPTIONAL,
InputOption::VALUE_REQUIRED,
'The listing mode. (default: indent, options: indent, flat)',
'indent'
);
$this->addOption(
self::DEPTH_OPT,
'd',
InputOption::VALUE_REQUIRED,
'The depth of the tree displayed',
-1
);
}

/**
Expand Down Expand Up @@ -168,6 +178,10 @@ public function execute(InputInterface $input, OutputInterface $output): int

public function showInfo(string $file, string $originalFile, InputInterface $input, OutputInterface $output, SymfonyStyle $io): int
{
$depth = (int) $input->getOption(self::DEPTH_OPT);

Assertion::greaterOrEqualThan($depth, -1, 'Expected the depth to be a positive integer or -1, got "%d"');

try {
try {
$phar = new Phar($file);
Expand All @@ -178,11 +192,16 @@ public function showInfo(string $file, string $originalFile, InputInterface $inp
return $this->showPharInfo(
$phar,
$input->getOption(self::LIST_OPT),
$depth,
'indent' === $input->getOption(self::MODE_OPT),
$output,
$io
);
} catch (Throwable $throwable) {
if ($output->isDebug()) {
throw $throwable;
}

$io->error(
sprintf(
'Could not read the file "%s".',
Expand Down Expand Up @@ -214,8 +233,14 @@ private function showGlobalInfo(OutputInterface $output, SymfonyStyle $io): int
/**
* @param Phar|PharData $phar
*/
private function showPharInfo($phar, bool $content, bool $indent, OutputInterface $output, SymfonyStyle $io): int
{
private function showPharInfo(
$phar,
bool $content,
int $depth,
bool $indent,
OutputInterface $output,
SymfonyStyle $io
): int {
$signature = $phar->getSignature();

$this->showPharGlobalInfo($phar, $io, $signature);
Expand All @@ -226,6 +251,8 @@ private function showPharInfo($phar, bool $content, bool $indent, OutputInterfac
$this->renderContents(
$output,
$phar,
0,
$depth,
$indent ? 0 : false,
$root,
$phar,
Expand Down Expand Up @@ -320,7 +347,9 @@ private function showPharGlobalInfo($phar, SymfonyStyle $io, $signature): void
sprintf(
'<comment>Contents:</comment>%s (%s)',
1 === $totalCount ? ' 1 file' : " $totalCount files",
formatted_filesize($phar->getPath())
format_size(
filesize($phar->getPath())
)
)
);
}
Expand Down Expand Up @@ -361,11 +390,17 @@ private function render(OutputInterface $output, array $attributes): void
private function renderContents(
OutputInterface $output,
iterable $list,
int $depth,
int $maxDepth,
$indent,
string $base,
$phar,
string $root
): void {
if (-1 !== $maxDepth && $depth > $maxDepth) {
return;
}

foreach ($list as $item) {
$item = $phar[str_replace($root, '', $item->getPathname())];

Expand All @@ -386,22 +421,33 @@ private function renderContents(
$output->writeln("<info>$path</info>");
}
} else {
$compression = ' <fg=red>[NONE]</fg=red>';
$compression = '<fg=red>[NONE]</fg=red>';

foreach (self::FILE_ALGORITHMS as $code => $name) {
if ($item->isCompressed($code)) {
$compression = " <fg=cyan>[$name]</fg=cyan>";
$compression = "<fg=cyan>[$name]</fg=cyan>";
break;
}
}

$output->writeln($path.$compression);
$fileSize = format_size($item->getCompressedSize());

$output->writeln(
sprintf(
'%s %s - %s',
$path,
$compression,
$fileSize
)
);
}

if ($item->isDir()) {
$this->renderContents(
$output,
new DirectoryIterator($item->getPathname()),
$depth + 1,
$maxDepth,
(false === $indent) ? $indent : $indent + 2,
$base,
$phar,
Expand Down
2 changes: 1 addition & 1 deletion src/PhpSettingsHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
use Composer\XdebugHandler\Process;
use Composer\XdebugHandler\XdebugHandler;
use Psr\Log\LoggerInterface;
use const PHP_EOL;
use function function_exists;
use function getenv;
use function ini_get;
use function KevinGH\Box\FileSystem\append_to_file;
use function sprintf;
use function trim;
use const PHP_EOL;

/**
* @private
Expand Down
7 changes: 3 additions & 4 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,11 @@ function get_phar_compression_algorithm_extension(int $algorithm): ?string
/**
* @private
*/
function formatted_filesize(string $path): string
// TODO: add more tests for this
function format_size(int $size): string
{
Assertion::file($path);

$size = filesize($path);
$units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];

$power = $size > 0 ? (int) floor(log($size, 1024)) : 0;

return sprintf(
Expand Down
2 changes: 1 addition & 1 deletion tests/BoxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

use Exception;
use InvalidArgumentException;
use function iterator_to_array;
use KevinGH\Box\Compactor\FakeCompactor;
use KevinGH\Box\Console\DisplayNormalizer;
use KevinGH\Box\Test\FileSystemTestCase;
Expand All @@ -35,6 +34,7 @@
use function extension_loaded;
use function file_put_contents;
use function in_array;
use function iterator_to_array;
use function KevinGH\Box\FileSystem\canonicalize;
use function KevinGH\Box\FileSystem\dump_file;
use function KevinGH\Box\FileSystem\make_tmp_dir;
Expand Down
2 changes: 1 addition & 1 deletion tests/ConfigurationFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
use Generator;
use InvalidArgumentException;
use KevinGH\Box\Json\JsonValidationException;
use const DIRECTORY_SEPARATOR;
use function file_put_contents;
use function KevinGH\Box\FileSystem\dump_file;
use function KevinGH\Box\FileSystem\make_path_absolute;
use function KevinGH\Box\FileSystem\rename;
use function KevinGH\Box\FileSystem\symlink;
use const DIRECTORY_SEPARATOR;

/**
* @covers \KevinGH\Box\Configuration
Expand Down
2 changes: 1 addition & 1 deletion tests/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@
use Phar;
use Seld\JsonLint\ParsingException;
use stdClass;
use const DIRECTORY_SEPARATOR;
use function file_put_contents;
use function KevinGH\Box\FileSystem\dump_file;
use function KevinGH\Box\FileSystem\remove;
use function KevinGH\Box\FileSystem\rename;
use const DIRECTORY_SEPARATOR;

/**
* @covers \KevinGH\Box\Configuration
Expand Down
2 changes: 1 addition & 1 deletion tests/ConfigurationTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
use KevinGH\Box\Console\ConfigurationHelper;
use KevinGH\Box\Test\FileSystemTestCase;
use stdClass;
use const DIRECTORY_SEPARATOR;
use function file_put_contents;
use function KevinGH\Box\FileSystem\make_path_absolute;
use function natcasesort;
use const DIRECTORY_SEPARATOR;

abstract class ConfigurationTestCase extends FileSystemTestCase
{
Expand Down
1 change: 0 additions & 1 deletion tests/Console/Command/CompileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

namespace KevinGH\Box\Console\Command;

use const DIRECTORY_SEPARATOR;
use DirectoryIterator;
use Generator;
use InvalidArgumentException;
Expand Down
Loading