Making the project task correctly replace the __CAKE_PATH__ constant when creating new projects.

This commit is contained in:
mark_story 2010-10-17 22:38:32 -04:00
parent 8f0a96d4bb
commit ae9403601e
2 changed files with 109 additions and 52 deletions

View file

@ -45,15 +45,15 @@ class ProjectTask extends Shell {
$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 @@ class ProjectTask extends Shell {
}
}
$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 <error>NOT</error> created'));
$this->err(__('The Welcome page was <error>NOT</error> created'));
$success = false;
}
if ($this->securitySalt($path) === true) {
$this->out(__('Random hash key created for \'Security.salt\''));
$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\''));
$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(__(' * <warning>Remember to check these value after moving to production server</warning>'));
} 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(__('<success>Project baked successfully!</success>'));
} else {
$this->out(__('Project baked but with <warning>some issues.</warning>.'));
}
$this->Dispatch->params['working'] = $path;
@ -142,9 +159,8 @@ class ProjectTask extends Shell {
$app = basename($path);
$this->out(__('Bake Project'));
$this->out(__('Skel Directory: ') . $skel);
$this->out(__('Will be copied to: ') . $path);
$this->out(__('<info>Skel Directory</info>: ') . $skel);
$this->out(__('<info>Will be copied to</info>: ') . $path);
$this->hr();
$looksGood = $this->in(__('Look okay?'), array('y', 'n', 'q'), 'y');
@ -165,7 +181,7 @@ class ProjectTask extends Shell {
}
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 @@ class ProjectTask extends Shell {
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'
*
@ -245,7 +283,7 @@ class ProjectTask extends Shell {
*/
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 @@ class ProjectTask extends Shell {
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 @@ class ProjectTask extends Shell {
*/
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 @@ class ProjectTask extends Shell {
'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.')
));
}

View file

@ -266,13 +266,30 @@ class ProjectTaskTest extends CakeTestCase {
$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.');
}
}