26
26
*/
27
27
class UpgradeCommand extends BaseCommand
28
28
{
29
- /**
29
+ /**
30
+ * @var \Cake\Console\Arguments
31
+ */
32
+ protected Arguments $ args ;
33
+
34
+ /**
35
+ * @var \Cake\Console\ConsoleIo
36
+ */
37
+ protected ConsoleIo $ io ;
38
+
39
+ /**
30
40
* Execute.
31
41
*
32
42
* @param \Cake\Console\Arguments $args The command arguments.
@@ -35,6 +45,9 @@ class UpgradeCommand extends BaseCommand
35
45
*/
36
46
public function execute (Arguments $ args , ConsoleIo $ io ): ?int
37
47
{
48
+ $ this ->args = $ args ;
49
+ $ this ->io = $ io ;
50
+
38
51
$ path = rtrim ((string )$ args ->getArgument ('path ' ), DIRECTORY_SEPARATOR );
39
52
$ path = realpath ($ path );
40
53
@@ -44,7 +57,10 @@ public function execute(Arguments $args, ConsoleIo $io): ?int
44
57
$ io ->abort ('Aborted ' );
45
58
}
46
59
47
- $ this ->skeletonUpgrade ($ args );
60
+ $ result = $ this ->skeletonUpgrade ($ path );
61
+ if (!$ result ) {
62
+ $ io ->error ('Could not fully process the upgrade task ' );
63
+ }
48
64
49
65
$ io ->warning ('Now check the changes via diff in your IDE and revert the lines you want to keep. ' );
50
66
@@ -61,37 +77,127 @@ protected function buildOptionParser(ConsoleOptionParser $parser): ConsoleOption
61
77
{
62
78
$ parser
63
79
->setDescription ([
64
- '<question>Upgrade tool for CakePHP 4.0</question> ' ,
65
- '' ,
66
- 'Runs all of the sub commands on an application/plugin. The <info>path</info> ' .
67
- 'argument should be the application or plugin root directory. ' ,
80
+ '<question>Upgrade tool addon for CakePHP 5.x</question> ' ,
68
81
'' ,
69
- 'You can also run each command individually on specific directories if you want more control. ' ,
82
+ '<info>Runs the following tasks:</info> ' ,
70
83
'' ,
71
- '<info>Sub-Commands</info> ' ,
72
- '' ,
73
- '- file_rename Rename template and locale files ' ,
74
- '- rector Apply rector rules for phpunit80 and cakephp40 ' ,
84
+ '- skeleton ' ,
75
85
])
76
86
->addArgument ('path ' , [
77
87
'help ' => 'The path to the application or plugin. ' ,
78
88
'required ' => true ,
79
89
])
90
+ ->addOption ('overwrite ' , [
91
+ 'help ' => 'Overwrite. ' ,
92
+ 'boolean ' => true ,
93
+ 'short ' => 'o ' ,
94
+ ])
80
95
->addOption ('dry-run ' , [
81
96
'help ' => 'Dry run. ' ,
82
97
'boolean ' => true ,
98
+ 'short ' => 'd ' ,
83
99
]);
84
100
85
101
return $ parser ;
86
102
}
87
103
88
- /**
89
- * @param \Cake\Console\Arguments $args
90
- *
91
- * @return void
92
- */
93
- protected function skeletonUpgrade (Arguments $ args ): void
104
+ /**
105
+ * @param string $path
106
+ *
107
+ * @return bool
108
+ */
109
+ protected function skeletonUpgrade (string $ path ): bool
94
110
{
111
+ $ sourcePath = ROOT . DS . 'tmp ' . DS . 'app ' . DS ;
112
+ $ this ->prepareSkeletonAppCode ($ sourcePath );
95
113
114
+ $ files = [
115
+ 'bin ' . DS . 'cake ' ,
116
+ 'bin ' . DS . 'cake.bat ' ,
117
+ 'bin ' . DS . 'cake.php ' ,
118
+ 'phpunit.xml.dist ' ,
119
+ 'index.php ' ,
120
+ 'webroot ' . DS . 'index.php ' ,
121
+ 'webroot ' . DS . 'css ' . DS . 'cake.css ' ,
122
+ 'webroot ' . DS . 'css ' . DS . 'home.css ' ,
123
+ 'webroot ' . DS . 'css ' . DS . 'milligram.min.css ' ,
124
+ 'webroot ' . DS . 'css ' . DS . 'normalize.min.css ' ,
125
+ 'config ' . DS . 'bootstrap.php ' ,
126
+ 'config ' . DS . 'bootstrap_cli.php ' ,
127
+ 'config ' . DS . 'paths.php ' ,
128
+ 'config ' . DS . 'routes.php ' ,
129
+ 'tests ' . DS . 'bootstrap.php ' ,
130
+ 'src ' . DS . 'Application.php ' ,
131
+ 'src ' . DS . 'View ' . DS . 'AppView.php ' ,
132
+ 'src ' . DS . 'View ' . DS . 'AjaxView.php ' ,
133
+ 'src ' . DS . 'Controller ' . DS . 'PagesController.php ' ,
134
+ 'templates ' . DS . 'Error ' . DS . 'error400.php ' ,
135
+ 'templates ' . DS . 'Error ' . DS . 'error500.php ' ,
136
+ 'templates ' . DS . 'layout ' . DS . 'error.php ' ,
137
+ 'templates ' . DS . 'element ' . DS . 'flash ' . DS . 'default.php ' ,
138
+ 'templates ' . DS . 'element ' . DS . 'flash ' . DS . 'error.php ' ,
139
+ 'templates ' . DS . 'element ' . DS . 'flash ' . DS . 'success.php ' ,
140
+ ];
141
+ $ ret = 0 ;
142
+ foreach ($ files as $ file ) {
143
+ $ ret |= $ this ->_addFile ($ file , $ sourcePath , $ path );
144
+ }
145
+ $ ret |= $ this ->_addFile ('config ' . DS . 'app.php ' , $ sourcePath , $ path , 'config ' . DS . 'app.php ' );
146
+
147
+ return (bool )$ ret ;
96
148
}
149
+
150
+ /**
151
+ * _addFile()
152
+ *
153
+ * @param string $file
154
+ * @param string $sourcePath
155
+ * @param string $targetPath
156
+ * @param string|null $targetFile
157
+ * @return bool
158
+ */
159
+ protected function _addFile ($ file , $ sourcePath , $ targetPath , $ targetFile = null ) {
160
+ $ result = false ;
161
+
162
+ if (!file_exists ($ sourcePath . $ file )) {
163
+ $ this ->io ->info ('Source file ' . $ file . 'cannot be found, skipping. ' );
164
+
165
+ return false ;
166
+ }
167
+
168
+ $ fileExists = file_exists ($ targetPath . $ file );
169
+ if (!$ fileExists || $ this ->args ->getOption ('overwrite ' )) {
170
+ $ result = true ;
171
+ if (empty ($ this ->params ['dry-run ' ])) {
172
+ if ($ targetFile === null ) {
173
+ $ targetFile = $ file ;
174
+ }
175
+ $ targetPathName = $ targetPath . dirname ($ targetFile );
176
+ if (!is_dir ($ targetPathName )) {
177
+ mkdir ($ targetPathName , 0755 , true );
178
+ }
179
+ $ result = copy ($ sourcePath . $ file , $ targetPath . $ targetFile );
180
+ }
181
+ $ this ->io ->verbose (($ fileExists ? 'Replacing ' : 'Adding ' ) . ' ' . $ file );
182
+ }
183
+
184
+ return $ result ;
185
+ }
186
+
187
+ /**
188
+ * @param string $sourcePath
189
+ *
190
+ * @return void
191
+ */
192
+ protected function prepareSkeletonAppCode (string $ sourcePath ): void {
193
+ if (!is_dir ($ sourcePath )) {
194
+ $ parentPath = dirname ($ sourcePath );
195
+ if (!is_dir ($ parentPath )) {
196
+ mkdir ($ parentPath , 0770 , true );
197
+ }
198
+ exec ('cd ' . $ parentPath . ' && git clone https://github.com/cakephp/app.git ' );
199
+ }
200
+
201
+ exec ('cd ' . $ sourcePath . ' && git pull ' );
202
+ }
97
203
}
0 commit comments