Improve ExtractTask when CakePHP installed via Composer

This commit is contained in:
Edgaras Janušauskas 2016-02-03 14:52:15 +02:00
parent fc57dee72f
commit f389231058
2 changed files with 21 additions and 18 deletions

View file

@ -151,7 +151,7 @@ class ExtractTask extends AppShell {
*/ */
public function execute() { public function execute() {
if (!empty($this->params['exclude'])) { if (!empty($this->params['exclude'])) {
$this->_exclude = explode(',', $this->params['exclude']); $this->_exclude = explode(',', str_replace('/', DS, $this->params['exclude']));
} }
if (isset($this->params['files']) && !is_array($this->params['files'])) { if (isset($this->params['files']) && !is_array($this->params['files'])) {
$this->_files = explode(',', $this->params['files']); $this->_files = explode(',', $this->params['files']);
@ -567,6 +567,11 @@ class ExtractTask extends AppShell {
protected function _buildFiles() { protected function _buildFiles() {
$paths = $this->_paths; $paths = $this->_paths;
$paths[] = realpath(APP) . DS; $paths[] = realpath(APP) . DS;
usort($paths, function ($a, $b) {
return strlen($b) - strlen($a);
});
foreach ($this->_translations as $category => $domains) { foreach ($this->_translations as $category => $domains) {
foreach ($domains as $domain => $translations) { foreach ($domains as $domain => $translations) {
foreach ($translations as $msgid => $contexts) { foreach ($translations as $msgid => $contexts) {
@ -799,19 +804,17 @@ class ExtractTask extends AppShell {
} }
$pattern = '/' . implode('|', $exclude) . '/'; $pattern = '/' . implode('|', $exclude) . '/';
} }
foreach ($this->_paths as $path) { foreach ($this->_paths as $i => $path) {
$Folder = new Folder($path); $this->_paths[$i] = realpath($path) . DS;
$Folder = new Folder($this->_paths[$i]);
$files = $Folder->findRecursive('.*\.(php|ctp|thtml|inc|tpl)', true); $files = $Folder->findRecursive('.*\.(php|ctp|thtml|inc|tpl)', true);
if (!empty($pattern)) { if (!empty($pattern)) {
foreach ($files as $i => $file) { $files = preg_grep($pattern, $files, PREG_GREP_INVERT);
if (preg_match($pattern, $file)) {
unset($files[$i]);
}
}
$files = array_values($files); $files = array_values($files);
} }
$this->_files = array_merge($this->_files, $files); $this->_files = array_merge($this->_files, $files);
} }
$this->_files = array_unique($this->_files);
} }
/** /**

View file

@ -135,23 +135,23 @@ class ExtractTaskTest extends CakeTestCase {
$this->assertRegExp($pattern, $result); $this->assertRegExp($pattern, $result);
// extract.ctp // extract.ctp
$pattern = '/\#: (\\\\|\/)extract\.ctp:15;6\n'; $pattern = '/\#: extract\.ctp:15;6\n';
$pattern .= 'msgid "You have %d new message."\nmsgid_plural "You have %d new messages."/'; $pattern .= 'msgid "You have %d new message."\nmsgid_plural "You have %d new messages."/';
$this->assertRegExp($pattern, $result); $this->assertRegExp($pattern, $result);
$pattern = '/msgid "You have %d new message."\nmsgstr ""/'; $pattern = '/msgid "You have %d new message."\nmsgstr ""/';
$this->assertNotRegExp($pattern, $result, 'No duplicate msgid'); $this->assertNotRegExp($pattern, $result, 'No duplicate msgid');
$pattern = '/\#: (\\\\|\/)extract\.ctp:7\n'; $pattern = '/\#: extract\.ctp:7\n';
$pattern .= 'msgid "You deleted %d message."\nmsgid_plural "You deleted %d messages."/'; $pattern .= 'msgid "You deleted %d message."\nmsgid_plural "You deleted %d messages."/';
$this->assertRegExp($pattern, $result); $this->assertRegExp($pattern, $result);
$pattern = '/\#: (\\\\|\/)extract\.ctp:14\n'; $pattern = '/\#: extract\.ctp:14\n';
$pattern .= '\#: (\\\\|\/)home\.ctp:68\n'; $pattern .= '\#: home\.ctp:68\n';
$pattern .= 'msgid "Editing this Page"\nmsgstr ""/'; $pattern .= 'msgid "Editing this Page"\nmsgstr ""/';
$this->assertRegExp($pattern, $result); $this->assertRegExp($pattern, $result);
$pattern = '/\#: (\\\\|\/)extract\.ctp:22\nmsgid "'; $pattern = '/\#: extract\.ctp:22\nmsgid "';
$pattern .= 'Hot features!'; $pattern .= 'Hot features!';
$pattern .= '\\\n - No Configuration: Set-up the database and let the magic begin'; $pattern .= '\\\n - No Configuration: Set-up the database and let the magic begin';
$pattern .= '\\\n - Extremely Simple: Just look at the name...It\'s Cake'; $pattern .= '\\\n - Extremely Simple: Just look at the name...It\'s Cake';
@ -162,19 +162,19 @@ class ExtractTaskTest extends CakeTestCase {
$this->assertContains('msgid "double \\"quoted\\""', $result, 'Strings with quotes not handled correctly'); $this->assertContains('msgid "double \\"quoted\\""', $result, 'Strings with quotes not handled correctly');
$this->assertContains("msgid \"single 'quoted'\"", $result, 'Strings with quotes not handled correctly'); $this->assertContains("msgid \"single 'quoted'\"", $result, 'Strings with quotes not handled correctly');
$pattern = '/\#: (\\\\|\/)extract\.ctp:36\nmsgid "letter"/'; $pattern = '/\#: extract\.ctp:36\nmsgid "letter"/';
$this->assertRegExp($pattern, $result, 'Strings with context should not overwrite strings without context'); $this->assertRegExp($pattern, $result, 'Strings with context should not overwrite strings without context');
$pattern = '/\#: (\\\\|\/)extract\.ctp:37;39\nmsgctxt "A"\nmsgid "letter"/'; $pattern = '/\#: extract\.ctp:37;39\nmsgctxt "A"\nmsgid "letter"/';
$this->assertRegExp($pattern, $result, 'Should contain string with context "A"'); $this->assertRegExp($pattern, $result, 'Should contain string with context "A"');
$pattern = '/\#: (\\\\|\/)extract\.ctp:38\nmsgctxt "B"\nmsgid "letter"/'; $pattern = '/\#: extract\.ctp:38\nmsgctxt "B"\nmsgid "letter"/';
$this->assertRegExp($pattern, $result, 'Should contain string with context "B"'); $this->assertRegExp($pattern, $result, 'Should contain string with context "B"');
$pattern = '/\#: (\\\\|\/)extract\.ctp:40\nmsgid "%d letter"\nmsgid_plural "%d letters"/'; $pattern = '/\#: extract\.ctp:40\nmsgid "%d letter"\nmsgid_plural "%d letters"/';
$this->assertRegExp($pattern, $result, 'Plural strings with context should not overwrite strings without context'); $this->assertRegExp($pattern, $result, 'Plural strings with context should not overwrite strings without context');
$pattern = '/\#: (\\\\|\/)extract\.ctp:41\nmsgctxt "A"\nmsgid "%d letter"\nmsgid_plural "%d letters"/'; $pattern = '/\#: extract\.ctp:41\nmsgctxt "A"\nmsgid "%d letter"\nmsgid_plural "%d letters"/';
$this->assertRegExp($pattern, $result, 'Should contain plural string with context "A"'); $this->assertRegExp($pattern, $result, 'Should contain plural string with context "A"');
// extract.ctp - reading the domain.pot // extract.ctp - reading the domain.pot