/
ProjectTask.php
205 lines (185 loc) · 5.67 KB
/
ProjectTask.php
1
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 1.2.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Console\Command\Task;
use Cake\Console\Command\Task\BakeTask;
use Cake\Core\App;
use Cake\Core\Configure;
use Cake\Utility\File;
use Cake\Utility\Folder;
use Cake\Utility\Security;
use Cake\Utility\String;
/**
* Task class for creating new project apps and plugins
*
*/
class ProjectTask extends BakeTask {
/**
* App path (used in testing).
*
* @var string
*/
public $appPath = null;
/**
* Checks that given project path does not already exist, and
* finds the app directory in it. Then it calls bake() with that information.
*
* @return mixed
*/
public function main() {
$project = null;
if (isset($this->args[0])) {
$project = $this->args[0];
} else {
$appContents = array_diff(scandir(APP), ['.', '..']);
if (empty($appContents)) {
$suggestedPath = rtrim(APP, DS);
} else {
$suggestedPath = APP . 'MyApp';
}
}
while (!$project) {
$prompt = __d('cake_console', 'What is the path to the project you want to bake?');
$project = $this->in($prompt, null, $suggestedPath);
}
$namespace = basename($project);
if (!preg_match('/^\w[\w\d_]+$/', $namespace)) {
$this->err(__d('cake_console', 'Project Name/Namespace needs to start with a letter and can only contain letters, digits and underscore'));
$this->args = [];
return $this->main();
}
if ($project && !Folder::isAbsolute($project) && isset($_SERVER['PWD'])) {
$project = $_SERVER['PWD'] . DS . $project;
}
$response = false;
while (!$response && is_dir($project) === true && file_exists($project . 'Config' . 'boostrap.php')) {
$prompt = __d('cake_console', '<warning>A project already exists in this location:</warning> %s Overwrite?', $project);
$response = $this->in($prompt, ['y', 'n'], 'n');
if (strtolower($response) === 'n') {
$response = $project = false;
}
}
if ($project === false) {
$this->out(__d('cake_console', 'Aborting project creation.'));
return;
}
if ($this->bake($project)) {
$this->out(__d('cake_console', '<success>Project baked successfully!</success>'));
return $project;
}
}
/**
* Uses either the CLI option or looks in $PATH and cwd for composer.
*
* @return string|false Either the path to composer or false if it cannot be found.
*/
public function findComposer() {
if (!empty($this->params['composer'])) {
$path = $this->params['composer'];
if (file_exists($path)) {
return $path;
}
}
$composer = false;
$path = env('PATH');
if (!empty($path)) {
$paths = explode(PATH_SEPARATOR, $path);
$composer = $this->_searchPath($paths);
}
return $composer;
}
/**
* Search the $PATH for composer.
*
* @param array $path The paths to search.
* @return string|bool
*/
protected function _searchPath($path) {
$composer = ['composer.phar', 'composer'];
foreach ($path as $dir) {
foreach ($composer as $cmd) {
if (is_file($dir . DS . $cmd)) {
$this->_io->verbose('Found composer executable in ' . $dir);
return $dir . DS . $cmd;
}
}
}
return false;
}
/**
* Uses composer to generate a new package using the cakephp/app project.
*
* @param string $path Project path
* @return mixed
*/
public function bake($path) {
$composer = $this->findComposer();
if (!$composer) {
$this->error(__d('cake_console', 'Cannot bake project. Could not find composer. Add composer to your PATH, or use the -composer option.'));
return false;
}
$this->out(__d('cake_console', '<info>Downloading a new cakephp app from packagist.org</info>'));
$command = 'php ' . escapeshellarg($composer) . ' create-project -s dev cakephp/app ' . escapeshellarg($path);
$descriptorSpec = [
0 => ['pipe', 'r'],
1 => ['pipe', 'w'],
2 => ['pipe', 'w']
];
$this->_io->verbose('Running ' . $command);
$process = proc_open(
$command,
$descriptorSpec,
$pipes
);
if (!is_resource($process)) {
$this->error(__d('cake_console', 'Could not start subprocess.'));
return false;
}
$output = $error = '';
fclose($pipes[0]);
$output = stream_get_contents($pipes[1]);
fclose($pipes[1]);
$error = stream_get_contents($pipes[2]);
fclose($pipes[2]);
proc_close($process);
if ($error) {
$this->error(__d('cake_console', 'Installation from packagist.org failed with: %s', $error));
return false;
}
$this->out($output);
return true;
}
/**
* get the option parser.
*
* @return \Cake\Console\ConsoleOptionParser
*/
public function getOptionParser() {
$parser = parent::getOptionParser();
return $parser->description(
__d('cake_console', 'Generate a new CakePHP project skeleton.')
)->addArgument('name', [
'help' => __d('cake_console', 'Application directory to make, if it starts with "/" the path is absolute.')
])->addOption('empty', [
'boolean' => true,
'help' => __d('cake_console', 'Create empty files in each of the directories. Good if you are using git')
])->addOption('theme', [
'short' => 't',
'help' => __d('cake_console', 'Theme to use when baking code.')
])->addOption('composer', [
'default' => ROOT . '/composer.phar',
'help' => __d('cake_console', 'The path to the composer executable.')
])->removeOption('plugin');
}
}