00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 if (!class_exists('File')) {
00028 uses('file');
00029 }
00030
00031
00032
00033
00034
00035
00036 class ProjectTask extends Shell {
00037
00038
00039
00040
00041
00042
00043
00044 function execute($project = null) {
00045 if ($project === null) {
00046 if (isset($this->args[0])) {
00047 $project = $this->args[0];
00048 $this->Dispatch->shiftArgs();
00049 }
00050 }
00051
00052 if ($project) {
00053 $this->Dispatch->parseParams(array('-app', $project));
00054 $project = $this->params['working'];
00055 }
00056
00057 if (empty($this->params['skel'])) {
00058 $this->params['skel'] = '';
00059 if (is_dir(CAKE_CORE_INCLUDE_PATH.DS.'cake'.DS.'console'.DS.'libs'.DS.'templates'.DS.'skel') === true) {
00060 $this->params['skel'] = CAKE_CORE_INCLUDE_PATH.DS.'cake'.DS.'console'.DS.'libs'.DS.'templates'.DS.'skel';
00061 }
00062 }
00063
00064 while (!$project) {
00065 $project = $this->in("What is the full path for this app including the app directory name?\nExample: ".$this->params['working'] . DS . "myapp", null, $this->params['working'] . DS . 'myapp');
00066 }
00067
00068 if ($project) {
00069 $response = false;
00070 while ($response == false && is_dir($project) === true && file_exists($project . 'config' . 'core.php')) {
00071 $response = $this->in('A project already exists in this location: '.$project.' Overwrite?', array('y','n'), 'n');
00072 if (strtolower($response) === 'n') {
00073 $response = $project = false;
00074 }
00075 }
00076 }
00077
00078 if ($this->bake($project)) {
00079 $path = Folder::slashTerm($project);
00080 if ($this->createHome($path)) {
00081 $this->out(__('Welcome page created', true));
00082 } else {
00083 $this->out(__('The Welcome page was NOT created', true));
00084 }
00085
00086 if ($this->securitySalt($path) === true ) {
00087 $this->out(__('Random hash key created for \'Security.salt\'', true));
00088 } else {
00089 $this->err(sprintf(__('Unable to generate random hash for \'Security.salt\', you should change it in %s', true), CONFIGS . 'core.php'));
00090 }
00091
00092 $corePath = $this->corePath($path);
00093 if ($corePath === true ) {
00094 $this->out(sprintf(__('CAKE_CORE_INCLUDE_PATH set to %s in webroot/index.php', true), CAKE_CORE_INCLUDE_PATH));
00095 $this->out(sprintf(__('CAKE_CORE_INCLUDE_PATH set to %s in webroot/test.php', true), CAKE_CORE_INCLUDE_PATH));
00096 $this->out(__('Remember to check these value after moving to production server', true));
00097 } elseif ($corePath === false) {
00098 $this->err(sprintf(__('Unable to set CAKE_CORE_INCLUDE_PATH, you should change it in %s', true), $path . 'webroot' .DS .'index.php'));
00099 }
00100 $Folder = new Folder($path);
00101 if (!$Folder->chmod($path . 'tmp', 0777)) {
00102 $this->err(sprintf(__('Could not set permissions on %s', true), $path . DS .'tmp'));
00103 $this->out(sprintf(__('chmod -R 0777 %s', true), $path . DS .'tmp'));
00104 }
00105
00106 $this->params['working'] = $path;
00107 $this->params['app'] = basename($path);
00108 return true;
00109 }
00110 }
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122 function bake($path, $skel = null, $skip = array('empty')) {
00123 if (!$skel) {
00124 $skel = $this->params['skel'];
00125 }
00126
00127 while (!$skel) {
00128 $skel = $this->in(sprintf(__("What is the path to the directory layout you wish to copy?\nExample: %s"), APP, null, ROOT . DS . 'myapp' . DS));
00129 if ($skel == '') {
00130 $this->out(__('The directory path you supplied was empty. Please try again.', true));
00131 } else {
00132 while (is_dir($skel) === false) {
00133 $skel = $this->in(__('Directory path does not exist please choose another:', true));
00134 }
00135 }
00136 }
00137
00138 $app = basename($path);
00139
00140 $this->out('Bake Project');
00141 $this->out("Skel Directory: $skel");
00142 $this->out("Will be copied to: {$path}");
00143 $this->hr();
00144
00145 $looksGood = $this->in('Look okay?', array('y', 'n', 'q'), 'y');
00146
00147 if (strtolower($looksGood) == 'y' || strtolower($looksGood) == 'yes') {
00148 $verbose = $this->in(__('Do you want verbose output?', true), array('y', 'n'), 'n');
00149
00150 $Folder = new Folder($skel);
00151 if ($Folder->copy(array('to' => $path, 'skip' => $skip))) {
00152 $this->hr();
00153 $this->out(sprintf(__("Created: %s in %s", true), $app, $path));
00154 $this->hr();
00155 } else {
00156 $this->err(" '" . $app . "' could not be created properly");
00157 return false;
00158 }
00159
00160 if (strtolower($verbose) == 'y' || strtolower($verbose) == 'yes') {
00161 foreach ($Folder->messages() as $message) {
00162 $this->out($message);
00163 }
00164 }
00165
00166 return true;
00167 } elseif (strtolower($looksGood) == 'q' || strtolower($looksGood) == 'quit') {
00168 $this->out('Bake Aborted.');
00169 } else {
00170 $this->execute(false);
00171 return false;
00172 }
00173 }
00174
00175
00176
00177
00178
00179
00180
00181 function createHome($dir) {
00182 $app = basename($dir);
00183 $path = $dir . 'views' . DS . 'pages' . DS;
00184 include(CAKE_CORE_INCLUDE_PATH.DS.'cake'.DS.'console'.DS.'libs'.DS.'templates'.DS.'views'.DS.'home.ctp');
00185 return $this->createFile($path.'home.ctp', $output);
00186 }
00187
00188
00189
00190
00191
00192
00193
00194 function securitySalt($path) {
00195 $File =& new File($path . 'config' . DS . 'core.php');
00196 $contents = $File->read();
00197 if (preg_match('/([\\t\\x20]*Configure::write\\(\\\'Security.salt\\\',[\\t\\x20\'A-z0-9]*\\);)/', $contents, $match)) {
00198 if (!class_exists('Security')) {
00199 uses('Security');
00200 }
00201 $string = Security::generateAuthKey();
00202 $result = str_replace($match[0], "\t" . 'Configure::write(\'Security.salt\', \''.$string.'\');', $contents);
00203 if ($File->write($result)) {
00204 return true;
00205 }
00206 return false;
00207 }
00208 return false;
00209 }
00210
00211
00212
00213
00214
00215
00216
00217 function corePath($path) {
00218 if (dirname($path) !== CAKE_CORE_INCLUDE_PATH) {
00219 $File =& new File($path . 'webroot' . DS . 'index.php');
00220 $contents = $File->read();
00221 if (preg_match('/([\\t\\x20]*define\\(\\\'CAKE_CORE_INCLUDE_PATH\\\',[\\t\\x20\'A-z0-9]*\\);)/', $contents, $match)) {
00222 $result = str_replace($match[0], "\t\tdefine('CAKE_CORE_INCLUDE_PATH', '" . CAKE_CORE_INCLUDE_PATH . "');", $contents);
00223 if (!$File->write($result)) {
00224 return false;
00225 }
00226 } else {
00227 return false;
00228 }
00229
00230 $File =& new File($path . 'webroot' . DS . 'test.php');
00231 $contents = $File->read();
00232 if (preg_match('/([\\t\\x20]*define\\(\\\'CAKE_CORE_INCLUDE_PATH\\\',[\\t\\x20\'A-z0-9]*\\);)/', $contents, $match)) {
00233 $result = str_replace($match[0], "\t\tdefine('CAKE_CORE_INCLUDE_PATH', '" . CAKE_CORE_INCLUDE_PATH . "');", $contents);
00234 if (!$File->write($result)) {
00235 return false;
00236 }
00237 } else {
00238 return false;
00239 }
00240 return true;
00241 }
00242 }
00243
00244
00245
00246
00247
00248
00249
00250 function cakeAdmin($name) {
00251 $File =& new File(CONFIGS . 'core.php');
00252 $contents = $File->read();
00253 if (preg_match('%([/\\t\\x20]*Configure::write\(\'Routing.admin\',[\\t\\x20\'a-z]*\\);)%', $contents, $match)) {
00254 $result = str_replace($match[0], "\t" . 'Configure::write(\'Routing.admin\', \''.$name.'\');', $contents);
00255 if ($File->write($result)) {
00256 Configure::write('Routing.admin', $name);
00257 return true;
00258 } else {
00259 return false;
00260 }
00261 } else {
00262 return false;
00263 }
00264 }
00265
00266
00267
00268
00269
00270
00271 function help() {
00272 $this->hr();
00273 $this->out("Usage: cake bake project <arg1>");
00274 $this->hr();
00275 $this->out('Commands:');
00276 $this->out("\n\tproject <name>\n\t\tbakes app directory structure.\n\t\tif <name> begins with '/' path is absolute.");
00277 $this->out("");
00278 $this->_stop();
00279 }
00280
00281 }
00282 ?>