Skip to content

Commit

Permalink
Merge pull request consolidation#196 from MAXakaWIZARD/pr-missing-pac…
Browse files Browse the repository at this point in the history
…kages-hints

Add missing packages error reporting to Minify and Less tasks
  • Loading branch information
DavertMik committed Aug 8, 2015
2 parents 994d877 + ba35662 commit fc9d584
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
8 changes: 8 additions & 0 deletions src/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ protected function printResult()
}
}

public static function errorMissingPackage(TaskInterface $task, $class, $package)
{
$messageTpl = 'Class %s not found. Please install %s Composer package';
$message = sprintf($messageTpl, $class, $package);

return self::error($task, $message);
}

static function error(TaskInterface $task, $message, $data = [])
{
return new self($task, 1, $message, $data);
Expand Down
14 changes: 12 additions & 2 deletions src/Task/Assets/Less.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ protected function compile($file)
*/
protected function lessphp($file)
{
if (!class_exists('\lessc')) {
return Result::errorMissingPackage($this, 'lessc', 'leafo/lessphp');
}

$lessCode = file_get_contents($file);
$less = new \lessc();
return $less->compile($lessCode);
Expand All @@ -141,6 +145,10 @@ protected function lessphp($file)
*/
protected function less($file)
{
if (!class_exists('\Less_Parser')) {
return Result::errorMissingPackage($this, 'Less_Parser', 'oyejorge/less.php');
}

$lessCode = file_get_contents($file);
$parser = new \Less_Parser();
$parser->parse($lessCode);
Expand All @@ -157,8 +165,10 @@ public function run()
foreach ($this->files as $in => $out) {
$css = $this->compile($in);

if (false === $css) {
return \Result::error($this, 'Less compilation failed for %s.', $in);
if ($css instanceof Result) {
return $css;
} elseif (false === $css) {
return Result::error($this, 'Less compilation failed for %s.', $in);
}

$dst = $out . '.part';
Expand Down
14 changes: 12 additions & 2 deletions src/Task/Assets/Minify.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,17 +149,25 @@ protected function getExtension($path)
protected function getMinifiedText()
{
switch ($this->type) {

case 'css':
if (!class_exists('\CssMin')) {
return Result::errorMissingPackage($this, 'CssMin', 'natxet/CssMin');
}

return \CssMin::minify($this->text);
break;

case 'js':
if (!class_exists('\JSqueeze') && !class_exists('\Patchwork\JSqueeze')) {
return Result::errorMissingPackage($this, 'Patchwork\JSqueeze', 'patchwork/jsqueeze');
}

if (class_exists('\JSqueeze')) {
$jsqueeze = new \JSqueeze();
} else {
$jsqueeze = new \Patchwork\JSqueeze();
}

return $jsqueeze->squeeze(
$this->text,
$this->squeezeOptions['singleLine'],
Expand Down Expand Up @@ -231,7 +239,9 @@ public function run()
$size_before = strlen($this->text);
$minified = $this->getMinifiedText();

if (false === $minified) {
if ($minified instanceof Result) {
return $minified;
} elseif (false === $minified) {
return Result::error($this, 'Minification failed.');
}

Expand Down

0 comments on commit fc9d584

Please sign in to comment.