Browse Source

Adding cake core message extraction in cake extract

Jelle Henkens 14 years ago
parent
commit
c8e58b50ba

+ 27 - 0
lib/Cake/Console/Command/Task/ExtractTask.php

@@ -106,6 +106,13 @@ class ExtractTask extends AppShell {
 	protected $_validationDomain = 'default';
 
 /**
+ * Holds whether this call should extract the CakePHP Lib messages
+ *
+ * @var boolean
+ */
+	protected $_extractCore = false;
+
+/**
  * Method to interact with the User and get path selections.
  *
  * @return void
@@ -137,6 +144,7 @@ class ExtractTask extends AppShell {
 			$this->out();
 		}
 	}
+
 /**
  * Execution method always used for tasks
  *
@@ -162,6 +170,21 @@ class ExtractTask extends AppShell {
 			$this->_getPaths();
 		}
 
+		if (isset($this->params['extract-core'])) {
+			$this->_extractCore = !(strtolower($this->params['extract-core']) === 'no');
+		} else {
+			$response = $this->in(__d('cake_console', 'Would you like to extract the messages from the CakePHP core?'), array('y', 'n'), 'n');
+			$this->_extractCore = strtolower($response) === 'y';
+		}
+
+		if ($this->_extractCore) {
+			$this->_paths[] = CAKE;
+			$this->_exclude = array_merge($this->_exclude, array(
+				CAKE . 'Test',
+				CAKE . 'Console' . DS . 'Templates'
+			));
+		}
+
 		if (!empty($this->params['exclude-plugins']) && $this->_isExtractingApp()) {
 			$this->_exclude = array_merge($this->_exclude, App::path('plugins'));
 		}
@@ -307,6 +330,10 @@ class ExtractTask extends AppShell {
 				'boolean' => true,
 				'default' => false,
 				'help' => __d('cake_console', 'Always overwrite existing .pot files.')
+			))
+			->addOption('extract-core', array(
+				'help' => __d('cake_console', 'Extract messages from the CakePHP core libs.'),
+				'choices' => array('yes', 'no')
 			));
 	}
 

+ 37 - 0
lib/Cake/Test/Case/Console/Command/Task/ExtractTaskTest.php

@@ -76,6 +76,7 @@ class ExtractTaskTest extends CakeTestCase {
 
 		$this->Task->params['paths'] = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Pages';
 		$this->Task->params['output'] = $this->path . DS;
+		$this->Task->params['extract-core'] = 'no';
 		$this->Task->expects($this->never())->method('err');
 		$this->Task->expects($this->any())->method('in')
 			->will($this->returnValue('y'));
@@ -85,6 +86,8 @@ class ExtractTaskTest extends CakeTestCase {
 		$this->assertTrue(file_exists($this->path . DS . 'default.pot'));
 		$result = file_get_contents($this->path . DS . 'default.pot');
 
+		$this->assertFalse(file_exists($this->path . DS . 'cake.pot'));
+
 		$pattern = '/"Content-Type\: text\/plain; charset\=utf-8/';
 		$this->assertRegExp($pattern, $result);
 		$pattern = '/"Content-Transfer-Encoding\: 8bit/';
@@ -182,6 +185,7 @@ class ExtractTaskTest extends CakeTestCase {
 		$this->Task->params['paths'] = CAKE . 'Test' . DS . 'test_app' . DS . 'View';
 		$this->Task->params['output'] = $this->path . DS;
 		$this->Task->params['exclude'] = 'Pages,Layouts';
+		$this->Task->params['extract-core'] = 'no';
 
 		$this->Task->expects($this->any())->method('in')
 			->will($this->returnValue('y'));
@@ -210,6 +214,7 @@ class ExtractTaskTest extends CakeTestCase {
 			CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Posts';
 
 		$this->Task->params['output'] = $this->path . DS;
+		$this->Task->params['extract-core'] = 'no';
 		$this->Task->expects($this->never())->method('err');
 		$this->Task->expects($this->never())->method('_stop');
 		$this->Task->execute();
@@ -293,6 +298,7 @@ class ExtractTaskTest extends CakeTestCase {
 
 		$this->Task->params['paths'] = CAKE . 'Test' . DS . 'test_app' . DS;
 		$this->Task->params['output'] = $this->path . DS;
+		$this->Task->params['extract-core'] = 'no';
 		$this->Task->params['exclude-plugins'] = true;
 		$this->Task->params['ignore-model-validation'] = false;
 
@@ -338,6 +344,7 @@ class ExtractTaskTest extends CakeTestCase {
 
 		$this->Task->params['paths'] = CAKE . 'Test' . DS . 'test_app' . DS;
 		$this->Task->params['output'] = $this->path . DS;
+		$this->Task->params['extract-core'] = 'no';
 		$this->Task->params['exclude-plugins'] = true;
 		$this->Task->params['ignore-model-validation'] = false;
 
@@ -412,6 +419,7 @@ class ExtractTaskTest extends CakeTestCase {
 
 		$this->Task->params['paths'] = CAKE . 'Test' . DS . 'test_app' . DS;
 		$this->Task->params['output'] = $this->path . DS;
+		$this->Task->params['extract-core'] = 'no';
 		$this->Task->params['overwrite'] = true;
 
 		file_put_contents($this->path . DS . 'default.pot', 'will be overwritten');
@@ -422,4 +430,33 @@ class ExtractTaskTest extends CakeTestCase {
 		$result = file_get_contents($this->path . DS . 'default.pot');
 		$this->assertNotEquals($original, $result);
 	}
+
+/**
+ *  Test that the extract shell scans the core libs
+ *
+ * @return void
+ */
+	public function testExtractCore() {
+		$this->Task->interactive = false;
+
+		$this->Task->params['paths'] = CAKE . 'Test' . DS . 'test_app' . DS;
+		$this->Task->params['output'] = $this->path . DS;
+		$this->Task->params['extract-core'] = 'yes';
+
+		$this->Task->execute();
+		$this->assertTrue(file_exists($this->path . DS . 'cake.pot'));
+		$result = file_get_contents($this->path . DS . 'cake.pot');
+
+		$pattern = '/msgid "Yesterday, %s"\nmsgstr ""\n/';
+		$this->assertRegExp($pattern, $result);
+
+		$this->assertTrue(file_exists($this->path . DS . 'cake_dev.pot'));
+		$result = file_get_contents($this->path . DS . 'cake_dev.pot');
+
+		$pattern = '/#: Console\/Templates\//';
+		$this->assertNotRegExp($pattern, $result);
+
+		$pattern = '/#: Test\//';
+		$this->assertNotRegExp($pattern, $result);
+	}
 }