1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26: if (!class_exists('File')) {
27: uses('file');
28: }
29: 30: 31: 32: 33: 34:
35: class ProjectTask extends Shell {
36: 37: 38: 39: 40: 41: 42:
43: function execute($project = null) {
44: if ($project === null) {
45: if (isset($this->args[0])) {
46: $project = $this->args[0];
47: $this->Dispatch->shiftArgs();
48: }
49: }
50:
51: if ($project) {
52: $this->Dispatch->parseParams(array('-app', $project));
53: $project = $this->params['working'];
54: }
55:
56: if (empty($this->params['skel'])) {
57: $this->params['skel'] = '';
58: if (is_dir(CAKE_CORE_INCLUDE_PATH.DS.'cake'.DS.'console'.DS.'libs'.DS.'templates'.DS.'skel') === true) {
59: $this->params['skel'] = CAKE_CORE_INCLUDE_PATH.DS.'cake'.DS.'console'.DS.'libs'.DS.'templates'.DS.'skel';
60: }
61: }
62:
63: while (!$project) {
64: $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');
65: }
66:
67: if ($project) {
68: $response = false;
69: while ($response == false && is_dir($project) === true && file_exists($project . 'config' . 'core.php')) {
70: $response = $this->in('A project already exists in this location: '.$project.' Overwrite?', array('y','n'), 'n');
71: if (strtolower($response) === 'n') {
72: $response = $project = false;
73: }
74: }
75: }
76:
77: if ($this->bake($project)) {
78: $path = Folder::slashTerm($project);
79: if ($this->createHome($path)) {
80: $this->out(__('Welcome page created', true));
81: } else {
82: $this->out(__('The Welcome page was NOT created', true));
83: }
84:
85: if ($this->securitySalt($path) === true ) {
86: $this->out(__('Random hash key created for \'Security.salt\'', true));
87: } else {
88: $this->err(sprintf(__('Unable to generate random hash for \'Security.salt\', you should change it in %s', true), CONFIGS . 'core.php'));
89: }
90:
91: $corePath = $this->corePath($path);
92: if ($corePath === true ) {
93: $this->out(sprintf(__('CAKE_CORE_INCLUDE_PATH set to %s in webroot/index.php', true), CAKE_CORE_INCLUDE_PATH));
94: $this->out(sprintf(__('CAKE_CORE_INCLUDE_PATH set to %s in webroot/test.php', true), CAKE_CORE_INCLUDE_PATH));
95: $this->out(__('Remember to check these value after moving to production server', true));
96: } elseif ($corePath === false) {
97: $this->err(sprintf(__('Unable to set CAKE_CORE_INCLUDE_PATH, you should change it in %s', true), $path . 'webroot' .DS .'index.php'));
98: }
99: $Folder = new Folder($path);
100: if (!$Folder->chmod($path . 'tmp', 0777)) {
101: $this->err(sprintf(__('Could not set permissions on %s', true), $path . DS .'tmp'));
102: $this->out(sprintf(__('chmod -R 0777 %s', true), $path . DS .'tmp'));
103: }
104:
105: $this->params['working'] = $path;
106: $this->params['app'] = basename($path);
107: return true;
108: }
109: }
110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120:
121: function bake($path, $skel = null, $skip = array('empty')) {
122: if (!$skel) {
123: $skel = $this->params['skel'];
124: }
125:
126: while (!$skel) {
127: $skel = $this->in(sprintf(__("What is the path to the directory layout you wish to copy?\nExample: %s"), APP, null, ROOT . DS . 'myapp' . DS));
128: if ($skel == '') {
129: $this->out(__('The directory path you supplied was empty. Please try again.', true));
130: } else {
131: while (is_dir($skel) === false) {
132: $skel = $this->in(__('Directory path does not exist please choose another:', true));
133: }
134: }
135: }
136:
137: $app = basename($path);
138:
139: $this->out('Bake Project');
140: $this->out("Skel Directory: $skel");
141: $this->out("Will be copied to: {$path}");
142: $this->hr();
143:
144: $looksGood = $this->in('Look okay?', array('y', 'n', 'q'), 'y');
145:
146: if (strtolower($looksGood) == 'y' || strtolower($looksGood) == 'yes') {
147: $verbose = $this->in(__('Do you want verbose output?', true), array('y', 'n'), 'n');
148:
149: $Folder = new Folder($skel);
150: if ($Folder->copy(array('to' => $path, 'skip' => $skip))) {
151: $this->hr();
152: $this->out(sprintf(__("Created: %s in %s", true), $app, $path));
153: $this->hr();
154: } else {
155: $this->err(" '" . $app . "' could not be created properly");
156: return false;
157: }
158:
159: if (strtolower($verbose) == 'y' || strtolower($verbose) == 'yes') {
160: foreach ($Folder->messages() as $message) {
161: $this->out($message);
162: }
163: }
164:
165: return true;
166: } elseif (strtolower($looksGood) == 'q' || strtolower($looksGood) == 'quit') {
167: $this->out('Bake Aborted.');
168: } else {
169: $this->execute(false);
170: return false;
171: }
172: }
173: 174: 175: 176: 177: 178: 179:
180: function createHome($dir) {
181: $app = basename($dir);
182: $path = $dir . 'views' . DS . 'pages' . DS;
183: include(CAKE_CORE_INCLUDE_PATH.DS.'cake'.DS.'console'.DS.'libs'.DS.'templates'.DS.'views'.DS.'home.ctp');
184: return $this->createFile($path.'home.ctp', $output);
185: }
186: 187: 188: 189: 190: 191: 192:
193: function securitySalt($path) {
194: $File =& new File($path . 'config' . DS . 'core.php');
195: $contents = $File->read();
196: if (preg_match('/([\\t\\x20]*Configure::write\\(\\\'Security.salt\\\',[\\t\\x20\'A-z0-9]*\\);)/', $contents, $match)) {
197: if (!class_exists('Security')) {
198: uses('Security');
199: }
200: $string = Security::generateAuthKey();
201: $result = str_replace($match[0], "\t" . 'Configure::write(\'Security.salt\', \''.$string.'\');', $contents);
202: if ($File->write($result)) {
203: return true;
204: }
205: return false;
206: }
207: return false;
208: }
209: 210: 211: 212: 213: 214: 215:
216: function corePath($path) {
217: if (dirname($path) !== CAKE_CORE_INCLUDE_PATH) {
218: $File =& new File($path . 'webroot' . DS . 'index.php');
219: $contents = $File->read();
220: if (preg_match('/([\\t\\x20]*define\\(\\\'CAKE_CORE_INCLUDE_PATH\\\',[\\t\\x20\'A-z0-9]*\\);)/', $contents, $match)) {
221: $result = str_replace($match[0], "\t\tdefine('CAKE_CORE_INCLUDE_PATH', '" . CAKE_CORE_INCLUDE_PATH . "');", $contents);
222: if (!$File->write($result)) {
223: return false;
224: }
225: } else {
226: return false;
227: }
228:
229: $File =& new File($path . 'webroot' . DS . 'test.php');
230: $contents = $File->read();
231: if (preg_match('/([\\t\\x20]*define\\(\\\'CAKE_CORE_INCLUDE_PATH\\\',[\\t\\x20\'A-z0-9]*\\);)/', $contents, $match)) {
232: $result = str_replace($match[0], "\t\tdefine('CAKE_CORE_INCLUDE_PATH', '" . CAKE_CORE_INCLUDE_PATH . "');", $contents);
233: if (!$File->write($result)) {
234: return false;
235: }
236: } else {
237: return false;
238: }
239: return true;
240: }
241: }
242: 243: 244: 245: 246: 247: 248:
249: function cakeAdmin($name) {
250: $File =& new File(CONFIGS . 'core.php');
251: $contents = $File->read();
252: if (preg_match('%([/\\t\\x20]*Configure::write\(\'Routing.admin\',[\\t\\x20\'a-z]*\\);)%', $contents, $match)) {
253: $result = str_replace($match[0], "\t" . 'Configure::write(\'Routing.admin\', \''.$name.'\');', $contents);
254: if ($File->write($result)) {
255: Configure::write('Routing.admin', $name);
256: return true;
257: } else {
258: return false;
259: }
260: } else {
261: return false;
262: }
263: }
264: 265: 266: 267: 268: 269:
270: function help() {
271: $this->hr();
272: $this->out("Usage: cake bake project <arg1>");
273: $this->hr();
274: $this->out('Commands:');
275: $this->out("\n\tproject <name>\n\t\tbakes app directory structure.\n\t\tif <name> begins with '/' path is absolute.");
276: $this->out("");
277: $this->_stop();
278: }
279:
280: }
281: ?>