Skip to content

Commit

Permalink
[Finder] Re-enable BsdFindAdapter for Darwin shells and fix it
Browse files Browse the repository at this point in the history
BSD find command needs "-E" switch to evaluate POSIX regex.
Added the ability to Command to insert bits at a given index.

On some systems (Mac OS X for example) php's function
sys_get_temp_dir() returns a directory that is a symlink.
This causes tests failures because expected paths are different
from path returned by the adapter. So, has been added a realpath.

The building of sorting command has been totally moved on the adapter so
the sorting command for BsdFindAdapter has been fixed.

The building of content filtering command has been moved on the adapter.
In BsdFindAdapter version, -r switch has been replaced with initial grep
that srips out blank lines, this way is compatible with BSD shell.
  • Loading branch information
mmenozzi committed Dec 20, 2012
1 parent 06c3222 commit ad6a6c8
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 52 deletions.
51 changes: 9 additions & 42 deletions src/Symfony/Component/Finder/Adapter/AbstractFindAdapter.php
Expand Up @@ -286,58 +286,25 @@ private function buildDatesFiltering(Command $command, array $dates)

/**
* @param Command $command
* @param array $contains
* @param Boolean $not
* @param string $sort
*
* @throws \InvalidArgumentException
*/
private function buildContentFiltering(Command $command, array $contains, $not = false)
private function buildSorting(Command $command, $sort)
{
foreach ($contains as $contain) {
$expr = Expression::create($contain);

// todo: avoid forking process for each $pattern by using multiple -e options
$command
->add('| xargs -r grep -I')
->add($expr->isCaseSensitive() ? null : '-i')
->add($not ? '-L' : '-l')
->add('-Ee')->arg($expr->renderPattern());
}
$this->buildFormatSorting($command, $sort);
}

/**
* @param Command $command
* @param string $sort
*
* @throws \InvalidArgumentException
*/
private function buildSorting(Command $command, $sort)
{
switch ($sort) {
case SortableIterator::SORT_BY_NAME:
$command->ins('sort')->add('| sort');

return;
case SortableIterator::SORT_BY_TYPE:
$format = '%y';
break;
case SortableIterator::SORT_BY_ACCESSED_TIME:
$format = '%A@';
break;
case SortableIterator::SORT_BY_CHANGED_TIME:
$format = '%C@';
break;
case SortableIterator::SORT_BY_MODIFIED_TIME:
$format = '%T@';
break;
default:
throw new \InvalidArgumentException('Unknown sort options: '.$sort.'.');
}

$this->buildFormatSorting($command, $format);
}
abstract protected function buildFormatSorting(Command $command, $sort);

/**
* @param Command $command
* @param string $format
* @param array $contains
* @param Boolean $not
*/
abstract protected function buildFormatSorting(Command $command, $format);
abstract protected function buildContentFiltering(Command $command, array $contains, $not = false);
}
62 changes: 54 additions & 8 deletions src/Symfony/Component/Finder/Adapter/BsdFindAdapter.php
Expand Up @@ -13,6 +13,8 @@

use Symfony\Component\Finder\Shell\Shell;
use Symfony\Component\Finder\Shell\Command;
use Symfony\Component\Finder\Iterator\SortableIterator;
use Symfony\Component\Finder\Expression\Expression;

/**
* Shell engine implementation using BSD find command.
Expand All @@ -34,21 +36,65 @@ public function getName()
*/
protected function canBeUsed()
{
// FIXME: this adapter does not work yet with Shell::TYPE_DARWIN
return in_array($this->shell->getType(), array(Shell::TYPE_BSD)) && parent::canBeUsed();
return in_array($this->shell->getType(), array(Shell::TYPE_BSD, Shell::TYPE_DARWIN)) && parent::canBeUsed();
}

/**
* {@inheritdoc}
*/
protected function buildFormatSorting(Command $command, $format)
protected function buildFormatSorting(Command $command, $sort)
{
switch ($sort) {
case SortableIterator::SORT_BY_NAME:
$command->ins('sort')->add('| sort');

return;
case SortableIterator::SORT_BY_TYPE:
$format = '%HT';
break;
case SortableIterator::SORT_BY_ACCESSED_TIME:
$format = '%a';
break;
case SortableIterator::SORT_BY_CHANGED_TIME:
$format = '%c';
break;
case SortableIterator::SORT_BY_MODIFIED_TIME:
$format = '%m';
break;
default:
throw new \InvalidArgumentException('Unknown sort options: '.$sort.'.');
}

$command
->get('find')
->add('-print0 | xargs -0 stat -f')
->arg($format.' %h/%f\\n')
->add('| sort | cut')
->arg('-d ')
->arg('-f2-');
->arg($format.'%t%N')
->add('| sort | cut -f 2');
}

/**
* {@inheritdoc}
*/
protected function buildFindCommand(Command $command, $dir)
{
parent::buildFindCommand($command, $dir)->addAtIndex('-E', 1);
return $command;
}

/**
* {@inheritdoc}
*/
protected function buildContentFiltering(Command $command, array $contains, $not = false)
{
foreach ($contains as $contain) {
$expr = Expression::create($contain);

// todo: avoid forking process for each $pattern by using multiple -e options
$command
->add('| grep -v \'^$\'')
->add('| xargs grep -I')
->add($expr->isCaseSensitive() ? null : '-i')
->add($not ? '-L' : '-l')
->add('-Ee')->arg($expr->renderPattern());
}
}
}
42 changes: 41 additions & 1 deletion src/Symfony/Component/Finder/Adapter/GnuFindAdapter.php
Expand Up @@ -13,6 +13,8 @@

use Symfony\Component\Finder\Shell\Shell;
use Symfony\Component\Finder\Shell\Command;
use Symfony\Component\Finder\Iterator\SortableIterator;
use Symfony\Component\Finder\Expression\Expression;

/**
* Shell engine implementation using GNU find command.
Expand All @@ -32,8 +34,29 @@ public function getName()
/**
* {@inheritdoc}
*/
protected function buildFormatSorting(Command $command, $format)
protected function buildFormatSorting(Command $command, $sort)
{
switch ($sort) {
case SortableIterator::SORT_BY_NAME:
$command->ins('sort')->add('| sort');

return;
case SortableIterator::SORT_BY_TYPE:
$format = '%y';
break;
case SortableIterator::SORT_BY_ACCESSED_TIME:
$format = '%A@';
break;
case SortableIterator::SORT_BY_CHANGED_TIME:
$format = '%C@';
break;
case SortableIterator::SORT_BY_MODIFIED_TIME:
$format = '%T@';
break;
default:
throw new \InvalidArgumentException('Unknown sort options: '.$sort.'.');
}

$command
->get('find')
->add('-printf')
Expand All @@ -59,4 +82,21 @@ protected function buildFindCommand(Command $command, $dir)
{
return parent::buildFindCommand($command, $dir)->add('-regextype posix-extended');
}

/**
* {@inheritdoc}
*/
protected function buildContentFiltering(Command $command, array $contains, $not = false)
{
foreach ($contains as $contain) {
$expr = Expression::create($contain);

// todo: avoid forking process for each $pattern by using multiple -e options
$command
->add('| xargs -r grep -I')
->add($expr->isCaseSensitive() ? null : '-i')
->add($not ? '-L' : '-l')
->add('-Ee')->arg($expr->renderPattern());
}
}
}
13 changes: 13 additions & 0 deletions src/Symfony/Component/Finder/Shell/Command.php
Expand Up @@ -246,4 +246,17 @@ public function join()
function($bit) { return null !== $bit; }
));
}

/**
* Insert a string or a Command instance before the bit at given position $index (index starts from 0).
*
* @param string|Command $bit
*
* @return Command The current Command instance
*/
public function addAtIndex($bit, $index)
{
array_splice($this->bits, $index, 0, $bit);
return $this;
}
}
2 changes: 1 addition & 1 deletion src/Symfony/Component/Finder/Tests/FinderTest.php
Expand Up @@ -23,7 +23,7 @@ public static function setUpBeforeClass()
{
parent::setUpBeforeClass();

self::$tmpDir = sys_get_temp_dir().'/symfony2_finder';
self::$tmpDir = realpath(sys_get_temp_dir().'/symfony2_finder');
}

public function testCreate()
Expand Down

0 comments on commit ad6a6c8

Please sign in to comment.