Skip to content

Commit

Permalink
Using patch from 'wals' to add generic exclude option to the ExtractT…
Browse files Browse the repository at this point in the history
…ask. Allows you to exclude path segments from the generated PO files.

Fixes #1138
  • Loading branch information
markstory committed Oct 29, 2010
1 parent efac10d commit fff8279
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 18 deletions.
33 changes: 29 additions & 4 deletions cake/console/shells/tasks/extract.php
Expand Up @@ -90,13 +90,23 @@ class ExtractTask extends Shell {
*/
private $__output = null;

/**
* An array of directories to exclude.
*
* @var array
*/
protected $_exclude = array();

/**
* Execution method always used for tasks
*
* @return void
* @access private
*/
function execute() {
if (!empty($this->params['exclude'])) {
$this->_exclude = explode(',', $this->params['exclude']);
}
if (isset($this->params['files']) && !is_array($this->params['files'])) {
$this->__files = explode(',', $this->params['files']);
}
Expand Down Expand Up @@ -190,14 +200,17 @@ function __extract() {
public function getOptionParser() {
$parser = parent::getOptionParser();
return $parser->description(__('CakePHP Language String Extraction:'))
->addOption('app', array('help' => __('directory where your application is located.')))
->addOption('paths', array('help' => __('comma separted list of paths, full paths are needed.')))
->addOption('app', array('help' => __('Directory where your application is located.')))
->addOption('paths', array('help' => __('Comma separted list of paths, full paths are needed.')))
->addOption('merge', array(
'help' => __('[yes|no] Merge all domain strings into the default.po file.'),
'help' => __('Merge all domain strings into the default.po file.'),
'choices' => array('yes', 'no')
))
->addOption('output', array('help' => __('Full path to output directory.')))
->addOption('files', array('help' => __('comma separated list of files, full paths are needed.')));
->addOption('files', array('help' => __('Comma separated list of files, full paths are needed.')))
->addOption('exclude', array(
'help' => __('Comma separated list of directories to exclude. Any path containing a path segment with the provided values will be skipped. E.g. test,vendors')
));
}

/**
Expand Down Expand Up @@ -502,9 +515,21 @@ function __markerError($file, $line, $marker, $count) {
* @access private
*/
function __searchFiles() {
$pattern = false;
if (!empty($this->_exclude)) {
$pattern = '/[\/\\\\]' . implode('|', $this->_exclude) . '[\/\\\\]/';
}
foreach ($this->__paths as $path) {
$Folder = new Folder($path);
$files = $Folder->findRecursive('.*\.(php|ctp|thtml|inc|tpl)', true);
if (!empty($pattern)) {
foreach ($files as $i => $file) {
if (preg_match($pattern, $file)) {
unset($files[$i]);
}
}
$files = array_values($files);
}
$this->__files = array_merge($this->__files, $files);
}
}
Expand Down
50 changes: 36 additions & 14 deletions cake/tests/cases/console/shells/tasks/extract.test.php
Expand Up @@ -47,6 +47,8 @@ public function setUp() {
array('in', 'out', 'err', '_stop'),
array($out, $out, $in)
);
$this->path = TMP . 'tests' . DS . 'extract_task_test';
$Folder = new Folder($this->path . DS . 'locale', true);
}

/**
Expand All @@ -57,6 +59,9 @@ public function setUp() {
public function tearDown() {
parent::tearDown();
unset($this->Task);

$Folder = new Folder($this->path);
$Folder->delete();
}

/**
Expand All @@ -65,21 +70,18 @@ public function tearDown() {
* @return void
*/
public function testExecute() {
$path = TMP . 'tests' . DS . 'extract_task_test';
new Folder($path . DS . 'locale', true);

$this->Task->interactive = false;

$this->Task->params['paths'] = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS . 'pages';
$this->Task->params['output'] = $path . DS;
$this->Task->params['output'] = $this->path . DS;
$this->Task->expects($this->never())->method('err');
$this->Task->expects($this->any())->method('in')
->will($this->returnValue('y'));
$this->Task->expects($this->never())->method('_stop');

$this->Task->execute();
$this->assertTrue(file_exists($path . DS . 'default.pot'));
$result = file_get_contents($path . DS . 'default.pot');
$this->assertTrue(file_exists($this->path . DS . 'default.pot'));
$result = file_get_contents($this->path . DS . 'default.pot');

$pattern = '/"Content-Type\: text\/plain; charset\=utf-8/';
$this->assertPattern($pattern, $result);
Expand Down Expand Up @@ -131,7 +133,7 @@ public function testExecute() {
$this->assertPattern($pattern, $result);

// extract.ctp - reading the domain.pot
$result = file_get_contents($path . DS . 'domain.pot');
$result = file_get_contents($this->path . DS . 'domain.pot');

$pattern = '/msgid "You have %d new message."\nmsgid_plural "You have %d new messages."/';
$this->assertNoPattern($pattern, $result);
Expand All @@ -142,9 +144,32 @@ public function testExecute() {
$this->assertPattern($pattern, $result);
$pattern = '/msgid "You deleted %d message \(domain\)."\nmsgid_plural "You deleted %d messages \(domain\)."/';
$this->assertPattern($pattern, $result);
}

$Folder = new Folder($path);
$Folder->delete();
/**
* test exclusions
*
* @return void
*/
function testExtractWithExclude() {
$this->Task->interactive = false;

$this->Task->params['paths'] = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views';
$this->Task->params['output'] = $this->path . DS;
$this->Task->params['exclude'] = 'pages,layouts';

$this->Task->expects($this->any())->method('in')
->will($this->returnValue('y'));

$this->Task->execute();
$this->assertTrue(file_exists($this->path . DS . 'default.pot'));
$result = file_get_contents($this->path . DS . 'default.pot');

$pattern = '/\#: .*extract\.ctp:6\n/';
$this->assertNotRegExp($pattern, $result);

$pattern = '/\#: .*default\.ctp:26\n/';
$this->assertNotRegExp($pattern, $result);
}

/**
Expand All @@ -153,21 +178,18 @@ public function testExecute() {
* @return void
*/
function testExtractMultiplePaths() {
$path = TMP . 'tests' . DS . 'extract_task_test';
new Folder($path . DS . 'locale', true);

$this->Task->interactive = false;

$this->Task->params['paths'] =
TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS . 'pages,' .
TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS . 'posts';

$this->Task->params['output'] = $path . DS;
$this->Task->params['output'] = $this->path . DS;
$this->Task->expects($this->never())->method('err');
$this->Task->expects($this->never())->method('_stop');
$this->Task->execute();

$result = file_get_contents($path . DS . 'default.pot');
$result = file_get_contents($this->path . DS . 'default.pot');

$pattern = '/msgid "Add User"/';
$this->assertPattern($pattern, $result);
Expand Down

0 comments on commit fff8279

Please sign in to comment.