From ae9403601eb78268b317635d137289f446c7e4d8 Mon Sep 17 00:00:00 2001 From: mark_story Date: Sun, 17 Oct 2010 22:38:32 -0400 Subject: [PATCH] Making the project task correctly replace the __CAKE_PATH__ constant when creating new projects. --- cake/console/shells/tasks/project.php | 128 ++++++++++++------ .../console/shells/tasks/project.test.php | 33 +++-- 2 files changed, 109 insertions(+), 52 deletions(-) diff --git a/cake/console/shells/tasks/project.php b/cake/console/shells/tasks/project.php index 12f9050d15b..310ac80166d 100644 --- a/cake/console/shells/tasks/project.php +++ b/cake/console/shells/tasks/project.php @@ -45,15 +45,15 @@ public function execute() { $project = $this->args[0]; } - if ($project) { - $this->Dispatch->parseParams(array('-app', $project)); - $project = $this->Dispatch->params['working']; + if ($project && isset($_SERVER['PWD'])) { + $project = $_SERVER['PWD'] . DS . $project; } if (empty($this->params['skel'])) { - $this->params['skel'] = ''; - if (is_dir(CAKE . 'console' . DS . 'templates' . DS . 'skel') === true) { - $this->params['skel'] = CAKE . 'console' . DS . 'templates' . DS . 'skel'; + $core = App::core('shells'); + $skelPath = dirname($core[0]) . DS . 'templates' . DS . 'skel'; + if (is_dir($skelPath) === true) { + $this->params['skel'] = $skelPath; } } @@ -74,38 +74,55 @@ public function execute() { } } + $success = true; if ($this->bake($project)) { $path = Folder::slashTerm($project); if ($this->createHome($path)) { - $this->out(__('Welcome page created')); + $this->out(__(' * Welcome page created')); } else { - $this->out(__('The Welcome page was NOT created')); + $this->err(__('The Welcome page was NOT created')); + $success = false; } - if ($this->securitySalt($path) === true ) { - $this->out(__('Random hash key created for \'Security.salt\'')); + if ($this->securitySalt($path) === true) { + $this->out(__(' * Random hash key created for \'Security.salt\'')); } else { $this->err(sprintf(__('Unable to generate random hash for \'Security.salt\', you should change it in %s'), CONFIGS . 'core.php')); + $success = false; } - if ($this->securityCipherSeed($path) === true ) { - $this->out(__('Random seed created for \'Security.cipherSeed\'')); + if ($this->securityCipherSeed($path) === true) { + $this->out(__(' * Random seed created for \'Security.cipherSeed\'')); } else { $this->err(sprintf(__('Unable to generate random seed for \'Security.cipherSeed\', you should change it in %s'), CONFIGS . 'core.php')); + $success = false; } - $corePath = $this->corePath($path); - if ($corePath === true ) { - $this->out(sprintf(__('CAKE_CORE_INCLUDE_PATH set to %s in webroot/index.php'), CAKE_CORE_INCLUDE_PATH)); - $this->out(sprintf(__('CAKE_CORE_INCLUDE_PATH set to %s in webroot/test.php'), CAKE_CORE_INCLUDE_PATH)); - $this->out(__('Remember to check these value after moving to production server')); - } elseif ($corePath === false) { + if ($this->corePath($path) === true) { + $this->out(sprintf(__(' * CAKE_CORE_INCLUDE_PATH set to %s in webroot/index.php'), CAKE_CORE_INCLUDE_PATH)); + $this->out(sprintf(__(' * CAKE_CORE_INCLUDE_PATH set to %s in webroot/test.php'), CAKE_CORE_INCLUDE_PATH)); + $this->out(__(' * Remember to check these value after moving to production server')); + } else { $this->err(sprintf(__('Unable to set CAKE_CORE_INCLUDE_PATH, you should change it in %s'), $path . 'webroot' .DS .'index.php')); + $success = false; } + if ($this->consolePath($path) === true) { + $this->out(__(' * app/console/cake.php path set.')); + } else { + $this->err(__('Unable to set console path for app/console.')); + $success = false; + } + $Folder = new Folder($path); if (!$Folder->chmod($path . 'tmp', 0777)) { $this->err(sprintf(__('Could not set permissions on %s'), $path . DS .'tmp')); $this->out(sprintf(__('chmod -R 0777 %s'), $path . DS .'tmp')); + $success = false; + } + if ($success) { + $this->out(__('Project baked successfully!')); + } else { + $this->out(__('Project baked but with some issues..')); } $this->Dispatch->params['working'] = $path; @@ -142,9 +159,8 @@ function bake($path, $skel = null, $skip = array('empty')) { $app = basename($path); - $this->out(__('Bake Project')); - $this->out(__('Skel Directory: ') . $skel); - $this->out(__('Will be copied to: ') . $path); + $this->out(__('Skel Directory: ') . $skel); + $this->out(__('Will be copied to: ') . $path); $this->hr(); $looksGood = $this->in(__('Look okay?'), array('y', 'n', 'q'), 'y'); @@ -165,7 +181,7 @@ function bake($path, $skel = null, $skip = array('empty')) { } foreach ($Folder->messages() as $message) { - $this->out($message, 1, Shell::VERBOSE); + $this->out(String::wrap(' * ' . $message), 1, Shell::VERBOSE); } return true; @@ -191,6 +207,28 @@ public function createHome($dir) { return $this->createFile($path.'home.ctp', $output); } +/** + * Generates the correct path to the CakePHP libs that are generating the project + * and points app/console/cake.php to the right place + * + * @param string $path Project path. + * @return boolean success + */ + public function consolePath($path) { + $File = new File($path . 'console' . DS . 'cake.php'); + $contents = $File->read(); + if (preg_match('/(__CAKE_PATH__)/', $contents, $match)) { + $path = CAKE_CORE_INCLUDE_PATH . DS . 'cake' . DS . 'console' . DS; + $replacement = "'" . str_replace(DS, "' . DIRECTORY_SEPARATOR . '", $path) . "'"; + $result = str_replace($match[0], $replacement, $contents); + if ($File->write($result)) { + return true; + } + return false; + } + return false; + } + /** * Generates and writes 'Security.salt' * @@ -214,28 +252,28 @@ public function securitySalt($path) { return false; } - /** - * Generates and writes 'Security.cipherSeed' - * - * @param string $path Project path - * @return boolean Success - */ - public function securityCipherSeed($path) { - $File = new File($path . 'config' . DS . 'core.php'); - $contents = $File->read(); - if (preg_match('/([\s]*Configure::write\(\'Security.cipherSeed\',[\s\'A-z0-9]*\);)/', $contents, $match)) { - if (!class_exists('Security')) { - require LIBS . 'security.php'; - } - $string = substr(bin2hex(Security::generateAuthKey()), 0, 30); - $result = str_replace($match[0], "\t" . 'Configure::write(\'Security.cipherSeed\', \''.$string.'\');', $contents); - if ($File->write($result)) { - return true; - } - return false; +/** + * Generates and writes 'Security.cipherSeed' + * + * @param string $path Project path + * @return boolean Success + */ + public function securityCipherSeed($path) { + $File = new File($path . 'config' . DS . 'core.php'); + $contents = $File->read(); + if (preg_match('/([\s]*Configure::write\(\'Security.cipherSeed\',[\s\'A-z0-9]*\);)/', $contents, $match)) { + if (!class_exists('Security')) { + require LIBS . 'security.php'; + } + $string = substr(bin2hex(Security::generateAuthKey()), 0, 30); + $result = str_replace($match[0], "\t" . 'Configure::write(\'Security.cipherSeed\', \''.$string.'\');', $contents); + if ($File->write($result)) { + return true; } return false; } + return false; + } /** * Generates and writes CAKE_CORE_INCLUDE_PATH @@ -245,7 +283,7 @@ public function securityCipherSeed($path) { */ public function corePath($path) { if (dirname($path) !== CAKE_CORE_INCLUDE_PATH) { - $File =& new File($path . 'webroot' . DS . 'index.php'); + $File = new File($path . 'webroot' . DS . 'index.php'); $contents = $File->read(); if (preg_match('/([\s]*define\(\'CAKE_CORE_INCLUDE_PATH\',[\s\'A-z0-9]*\);)/', $contents, $match)) { $root = strpos(CAKE_CORE_INCLUDE_PATH, '/') === 0 ? " DS . '" : "'"; @@ -257,7 +295,7 @@ public function corePath($path) { return false; } - $File =& new File($path . 'webroot' . DS . 'test.php'); + $File = new File($path . 'webroot' . DS . 'test.php'); $contents = $File->read(); if (preg_match('/([\s]*define\(\'CAKE_CORE_INCLUDE_PATH\',[\s\'A-z0-9]*\);)/', $contents, $match)) { $result = str_replace($match[0], "\t\tdefine('CAKE_CORE_INCLUDE_PATH', " . $root . str_replace(DS, "' . DS . '", trim(CAKE_CORE_INCLUDE_PATH, DS)) . "');", $contents); @@ -279,7 +317,7 @@ public function corePath($path) { */ public function cakeAdmin($name) { $path = (empty($this->configPath)) ? CONFIGS : $this->configPath; - $File =& new File($path . 'core.php'); + $File = new File($path . 'core.php'); $contents = $File->read(); if (preg_match('%([/\s]*Configure::write\(\'Routing.prefixes\',[\s\'a-z,\)\(]*\);)%', $contents, $match)) { $result = str_replace($match[0], "\t" . 'Configure::write(\'Routing.prefixes\', array(\''.$name.'\'));', $contents); @@ -351,6 +389,8 @@ public function getOptionParser() { 'help' => __('Application directory to make, if it starts with "/" the path is absolute.') ))->addOption('empty', array( 'help' => __('Create empty files in each of the directories. Good if you are using git') + ))->addOption('skel', array( + 'help' => __('The directory layout to use for the new application skeleton. Defaults to cake/console/templates/skel of CakePHP used to create the project.') )); } diff --git a/cake/tests/cases/console/shells/tasks/project.test.php b/cake/tests/cases/console/shells/tasks/project.test.php index b6518d86d14..e0aff9bd775 100644 --- a/cake/tests/cases/console/shells/tasks/project.test.php +++ b/cake/tests/cases/console/shells/tasks/project.test.php @@ -266,13 +266,30 @@ public function testExecute() { $this->Task->execute(); $this->assertTrue(is_dir($path), 'No project dir %s'); - $this->assertTrue(is_dir($path . DS . 'controllers'), 'No controllers dir %s'); - $this->assertTrue(is_dir($path . DS . 'controllers' . DS .'components'), 'No components dir %s'); - $this->assertTrue(is_dir($path . DS . 'models'), 'No models dir %s'); - $this->assertTrue(is_dir($path . DS . 'views'), 'No views dir %s'); - $this->assertTrue(is_dir($path . DS . 'views' . DS . 'helpers'), 'No helpers dir %s'); - $this->assertTrue(is_dir($path . DS . 'tests'), 'No tests dir %s'); - $this->assertTrue(is_dir($path . DS . 'tests' . DS . 'cases'), 'No cases dir %s'); - $this->assertTrue(is_dir($path . DS . 'tests' . DS . 'fixtures'), 'No fixtures dir %s'); + $this->assertTrue(is_dir($path . DS . 'controllers'), 'No controllers dir '); + $this->assertTrue(is_dir($path . DS . 'controllers' . DS .'components'), 'No components dir '); + $this->assertTrue(is_dir($path . DS . 'models'), 'No models dir'); + $this->assertTrue(is_dir($path . DS . 'views'), 'No views dir'); + $this->assertTrue(is_dir($path . DS . 'views' . DS . 'helpers'), 'No helpers dir'); + $this->assertTrue(is_dir($path . DS . 'tests'), 'No tests dir'); + $this->assertTrue(is_dir($path . DS . 'tests' . DS . 'cases'), 'No cases dir'); + $this->assertTrue(is_dir($path . DS . 'tests' . DS . 'fixtures'), 'No fixtures dir'); + } + +/** + * test console path + * + * @return void + */ + function testConsolePath() { + $this->_setupTestProject(); + + $path = $this->Task->path . 'bake_test_app' . DS; + $result = $this->Task->consolePath($path); + $this->assertTrue($result); + + $file = new File($path . 'console' . DS . 'cake.php'); + $contents = $file->read(); + $this->assertNoPattern('/__CAKE_PATH__/', $contents, 'Console path placeholder left behind.'); } }