diff --git a/cake/console/shells/tasks/extract.php b/cake/console/shells/tasks/extract.php index 03e1424e8..78a95fb3d 100644 --- a/cake/console/shells/tasks/extract.php +++ b/cake/console/shells/tasks/extract.php @@ -90,6 +90,13 @@ class ExtractTask extends Shell { */ private $__output = null; +/** + * An array of directories to exclude. + * + * @var array + */ + protected $_exclude = array(); + /** * Execution method always used for tasks * @@ -97,6 +104,9 @@ class ExtractTask extends Shell { * @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']); } @@ -190,14 +200,17 @@ class ExtractTask extends Shell { 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') + )); } /** @@ -502,9 +515,21 @@ class ExtractTask extends Shell { * @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); } } diff --git a/cake/tests/cases/console/shells/tasks/extract.test.php b/cake/tests/cases/console/shells/tasks/extract.test.php index b2c802a5d..21993e0d4 100644 --- a/cake/tests/cases/console/shells/tasks/extract.test.php +++ b/cake/tests/cases/console/shells/tasks/extract.test.php @@ -47,6 +47,8 @@ class ExtractTaskTest extends CakeTestCase { 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); } /** @@ -57,6 +59,9 @@ class ExtractTaskTest extends CakeTestCase { public function tearDown() { parent::tearDown(); unset($this->Task); + + $Folder = new Folder($this->path); + $Folder->delete(); } /** @@ -65,21 +70,18 @@ class ExtractTaskTest extends CakeTestCase { * @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); @@ -131,7 +133,7 @@ class ExtractTaskTest extends CakeTestCase { $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); @@ -142,9 +144,32 @@ class ExtractTaskTest extends CakeTestCase { $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); } /** @@ -153,21 +178,18 @@ class ExtractTaskTest extends CakeTestCase { * @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);