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