Skip to content

Commit

Permalink
Implementing a new option 'exclude-plugins' from the ExtractTask
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed May 27, 2011
1 parent e669a81 commit 991501d
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 30 deletions.
66 changes: 36 additions & 30 deletions lib/Cake/Console/Command/Task/ExtractTask.php
Expand Up @@ -113,7 +113,7 @@ public function execute() {
if (isset($this->params['paths'])) {
$this->_paths = explode(',', $this->params['paths']);
} else {
$defaultPath = APP_PATH;
$defaultPath = APP;
$message = __d('cake_console', "What is the path you would like to extract?\n[Q]uit [D]one");
while (true) {
$response = $this->in($message, null, $defaultPath);
Expand All @@ -133,6 +133,10 @@ public function execute() {
}
}

if (!empty($this->params['exclude-plugins']) && $this->_isExtractingApp()) {
$this->_exclude = array_merge($this->_exclude, App::path('plugins'));
}

if (isset($this->params['output'])) {
$this->_output = $this->params['output'];
} else {
Expand Down Expand Up @@ -208,39 +212,24 @@ public function getOptionParser() {
))
->addOption('output', array('help' => __d('cake_console', 'Full path to output directory.')))
->addOption('files', array('help' => __d('cake_console', 'Comma separated list of files.')))
->addOption('exclude-plugins', array(
'boolean' => true,
'default' => true,
'help' => __d('cake_console', 'Ignores all files in plugins.')
))
->addOption('ignore-model-validation', array(
'boolean' => true,
'default' => false,
'help' => __d('cake_console', 'Ignores validation messages in the $validate property. Needs to be run in from the same app directory')
))
->addOption('validation-domain', array(
'help' => __d('cake_console', 'If set to a value, the localization domain to be used for model validation messages')
))
->addOption('exclude', array(
'help' => __d('cake_console', 'Comma separated list of directories to exclude. Any path containing a path segment with the provided values will be skipped. E.g. test,vendors')
));
}

/**
* Show help options
*
* @return void
*/
public function help() {
$this->out(__d('cake_console', 'CakePHP Language String Extraction:'));
$this->hr();
$this->out(__d('cake_console', 'The Extract script generates .pot file(s) with translations'));
$this->out(__d('cake_console', 'By default the .pot file(s) will be place in the locale directory of -app'));
$this->out(__d('cake_console', 'By default -app is ROOT/app'));
$this->hr();
$this->out(__d('cake_console', 'Usage: cake i18n extract <command> <param1> <param2>...'));
$this->out();
$this->out(__d('cake_console', 'Params:'));
$this->out(__d('cake_console', ' -app [path...]: directory where your application is located'));
$this->out(__d('cake_console', ' -root [path...]: path to install'));
$this->out(__d('cake_console', ' -core [path...]: path to cake directory'));
$this->out(__d('cake_console', ' -paths [comma separated list of paths]'));
$this->out(__d('cake_console', ' -merge [yes|no]: Merge all domains strings into the default.pot file'));
$this->out(__d('cake_console', ' -output [path...]: Full path to output directory'));
$this->out(__d('cake_console', ' -files: [comma separated list of files]'));
$this->out();
$this->out(__d('cake_console', 'Commands:'));
$this->out(__d('cake_console', ' cake i18n extract help: Shows this help message.'));
$this->out();
}

/**
* Extract tokens out of all files to be processed
*
Expand Down Expand Up @@ -524,7 +513,14 @@ protected function _markerError($file, $line, $marker, $count) {
protected function _searchFiles() {
$pattern = false;
if (!empty($this->_exclude)) {
$pattern = '/[\/\\\\]' . implode('|', $this->_exclude) . '[\/\\\\]/';
$exclude = array();
foreach ($this->_exclude as $e) {
if ($e[0] !== DS) {
$e = DS . $e;
}
$exclude[] = preg_quote($e, '/');
}
$pattern = '/' . implode('|', $exclude) . '/';
}
foreach ($this->_paths as $path) {
$Folder = new Folder($path);
Expand All @@ -540,4 +536,14 @@ protected function _searchFiles() {
$this->_files = array_merge($this->_files, $files);
}
}

/**
* Returns whether this execution is meant to extract string only from directories in folder represented by the
* APP constant, i.e. this task is extracting strings from same application.
*
* @return boolean
*/
protected function _isExtractingApp() {
return $this->_paths === array(APP);
}

This comment has been minimized.

Copy link
@desolat

desolat Jul 21, 2017

This is a poor criterium as it fails if trailing slashes are different or _paths includes more than the app. A more eleborate solution is needed, as this leads to silently disabling validation error message extraction.

This comment has been minimized.

Copy link
@ADmad

ADmad Jul 21, 2017

Member

@desolat Please don't comment on old commits. If you are facing a problem open a new issue.

}
29 changes: 29 additions & 0 deletions lib/Cake/Test/Case/Console/Command/Task/ExtractTaskTest.php
Expand Up @@ -202,4 +202,33 @@ function testExtractMultiplePaths() {
$pattern = '/msgid "Add User"/';
$this->assertPattern($pattern, $result);
}

/**
* Tests that it is possible to exclude plugin paths by enabling the param option for the ExtractTask
*
* @return void
*/
public function testExtractExcludePlugins() {
$this->Task->params['paths'] = array(
CAKE . 'Test' . DS . 'test_app'
);
App::build(array(
'plugins' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
));
$this->out = $this->getMock('ConsoleOutput', array(), array(), '', false);
$this->in = $this->getMock('ConsoleInput', array(), array(), '', false);
$this->Task = $this->getMock('ExtractTask',
array('_isExtractingApp', 'in', 'out', 'err', 'clear', '_stop'),
array($this->out, $this->out, $this->in)
);
$this->Task->expects($this->once())->method('_isExtractingApp')->will($this->returnValue(true));

$this->Task->params['paths'] = CAKE . 'Test' . DS . 'test_app' . DS;
$this->Task->params['output'] = $this->path . DS;
$this->Task->params['exclude-plugins'] = true;

$this->Task->execute();
$result = file_get_contents($this->path . DS . 'default.pot');
$this->assertNoPattern('#TesPlugin#', $result);
}
}
@@ -0,0 +1 @@
<?php __('This is a translatable string'); ?>

0 comments on commit 991501d

Please sign in to comment.