Skip to content

Commit 75b5a0c

Browse files
committed
Finish up skeleton task.
1 parent 0ba9dd8 commit 75b5a0c

File tree

3 files changed

+125
-19
lines changed

3 files changed

+125
-19
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ This tool is an add-on to original CakePHP upgrade tool and provides additional
6161
- Skeleton
6262

6363
The FileUpgrade tool also:
64-
- Custom (tons of custom fixes)
64+
- see `src/Task/?` (tons of custom fixes)
6565

6666
Feel free to manually port those things back into the core one.
6767

src/Command/FileUpgradeCommand.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public function execute(Arguments $args, ConsoleIo $io) {
101101
*/
102102
protected function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser {
103103
$parser = parent::buildOptionParser($parser)
104-
->setDescription('A tool to help automate upgrading CakePHP apps and plugins. ' .
104+
->setDescription('An upgrade tool to help automate upgrading CakePHP apps and plugins. ' .
105105
'Be sure to have a backup of your application before running these commands.')->addArgument('path', [
106106
'name' => 'Path to app/plugin ROOT (where composer.json is)',
107107
'required' => true,

src/Command/UpgradeCommand.php

+123-17
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,17 @@
2626
*/
2727
class UpgradeCommand extends BaseCommand
2828
{
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+
/**
3040
* Execute.
3141
*
3242
* @param \Cake\Console\Arguments $args The command arguments.
@@ -35,6 +45,9 @@ class UpgradeCommand extends BaseCommand
3545
*/
3646
public function execute(Arguments $args, ConsoleIo $io): ?int
3747
{
48+
$this->args = $args;
49+
$this->io = $io;
50+
3851
$path = rtrim((string)$args->getArgument('path'), DIRECTORY_SEPARATOR);
3952
$path = realpath($path);
4053

@@ -44,7 +57,10 @@ public function execute(Arguments $args, ConsoleIo $io): ?int
4457
$io->abort('Aborted');
4558
}
4659

47-
$this->skeletonUpgrade($args);
60+
$result = $this->skeletonUpgrade($path);
61+
if (!$result) {
62+
$io->error('Could not fully process the upgrade task');
63+
}
4864

4965
$io->warning('Now check the changes via diff in your IDE and revert the lines you want to keep.');
5066

@@ -61,37 +77,127 @@ protected function buildOptionParser(ConsoleOptionParser $parser): ConsoleOption
6177
{
6278
$parser
6379
->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>',
6881
'',
69-
'You can also run each command individually on specific directories if you want more control.',
82+
'<info>Runs the following tasks:</info>',
7083
'',
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',
7585
])
7686
->addArgument('path', [
7787
'help' => 'The path to the application or plugin.',
7888
'required' => true,
7989
])
90+
->addOption('overwrite', [
91+
'help' => 'Overwrite.',
92+
'boolean' => true,
93+
'short' => 'o',
94+
])
8095
->addOption('dry-run', [
8196
'help' => 'Dry run.',
8297
'boolean' => true,
98+
'short' => 'd',
8399
]);
84100

85101
return $parser;
86102
}
87103

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
94110
{
111+
$sourcePath = ROOT . DS . 'tmp' . DS . 'app' . DS;
112+
$this->prepareSkeletonAppCode($sourcePath);
95113

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;
96148
}
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+
}
97203
}

0 commit comments

Comments
 (0)