Skip to content

Commit

Permalink
Displays the file size for the minified assets.
Browse files Browse the repository at this point in the history
It also fixes a division by zero error when an empty file was minified.
  • Loading branch information
Florian Krämer committed Mar 23, 2015
1 parent a01a370 commit 7eef70f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
12 changes: 11 additions & 1 deletion src/Common/TaskIO.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ protected function printTaskError($text, $task = null)
$this->writeln(" <fg=white;bg=red;options=bold>[$name]</fg=white;bg=red;options=bold> $text");
}

protected function formatBytes($size, $precision = 2)
{
if ($size === 0) {
return 0;
}
$base = log($size, 1024);
$suffixes = array('', 'k', 'M', 'G', 'T');
return round(pow(1024, $base - floor($base)), $precision) . $suffixes[floor($base)];
}

protected function getPrintedTaskName($task = null)
{
if (!$task) {
Expand All @@ -34,4 +44,4 @@ protected function getPrintedTaskName($task = null)
$name = str_replace('Robo\Task\\', '' , $name);
return $name;
}
}
}
18 changes: 14 additions & 4 deletions src/Task/Assets/Minify.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,15 +243,25 @@ public function run()
if (false === $write_result) {
return Result::error($this, 'File write failed.');
}

$minified_percent = number_format(100 - ($size_after / $size_before * 100), 1);
if ($size_before === 0) {
$minified_percent = 0;
} else {
$minified_percent = number_format(100 - ($size_after / $size_before * 100), 1);
}
$this->printTaskSuccess(
sprintf(
'Wrote <info>%s</info> (reduced by <info>%s%%</info>)', $this->dst,
'Wrote <info>%s</info>',
$this->dst
)
);
$this->printTaskSuccess(
sprintf(
'Wrote <info>%s</info> (reduced by <info>%s</info> / <info>%s%%</info>)',
$this->formatBytes($size_after),
$this->formatBytes(($size_before - $size_after)),
$minified_percent
)
);

return Result::success($this, 'Asset minified.');
}
}

0 comments on commit 7eef70f

Please sign in to comment.