Skip to content

Commit b753276

Browse files
committed
DD#main: feat: add schema and data patch generation
1 parent fd24964 commit b753276

File tree

8 files changed

+394
-0
lines changed

8 files changed

+394
-0
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,13 @@ Create the acl.xml file with a store config resource.
107107
`magegen make:acl [<module>]`
108108

109109
* module - The module name, e.g. MyCompany_MyModule
110+
111+
#### Make Schema and Data Patches
112+
113+
Create a schema or data patch class.
114+
115+
`magegen make:schema-patch [<module> [<patch_name>]]`
116+
`magegen make:data-patch [<module> [<patch_name>]]`
117+
118+
* module - The module name, e.g. MyCompany_MyModule
119+
* patch_name - The patch class name without namespace, e.g. MyPatch

magegen.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,7 @@
2727
$application->add(new \MageGen\MakeSchemaCommand($twig));
2828
$application->add(new \MageGen\MakeExtensionAttributeCommand($twig));
2929
$application->add(new \MageGen\MakeAclCommand($twig));
30+
$application->add(new \MageGen\MakeDataPatchCommand($twig));
31+
$application->add(new \MageGen\MakeSchemaPatchCommand($twig));
3032

3133
$application->run();
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
/**
4+
* Copyright Skywire. All rights reserved.
5+
* See LICENSE.txt for license details.
6+
*
7+
* @author Skywire Core Team
8+
* @copyright Copyright (c) 2021 Skywire (http://www.skywire.co.uk)
9+
*/
10+
declare(strict_types=1);
11+
12+
namespace MageGen\Generator;
13+
14+
use MageGen\Writer\ModuleFile;
15+
use Twig\Environment;
16+
17+
18+
class DataPatchGenerator
19+
{
20+
/**
21+
* @var Environment
22+
*/
23+
protected $twig;
24+
25+
/**
26+
* DiGenerator constructor.
27+
*
28+
* @param Environment $twig
29+
*/
30+
public function __construct(Environment $twig)
31+
{
32+
$this->twig = $twig;
33+
}
34+
35+
/**
36+
* Create a file if it doesn't exist and return the path
37+
*
38+
* @param string $vendor
39+
* @param string $module
40+
* @param ModuleFile $writer
41+
*
42+
* @return string
43+
* @throws \Twig\Error\LoaderError
44+
* @throws \Twig\Error\RuntimeError
45+
* @throws \Twig\Error\SyntaxError
46+
*/
47+
public function createDataPatch(string $vendor, string $module, string $patchName, ModuleFile $writer): string
48+
{
49+
return $writer->writeFile(
50+
$vendor,
51+
$module,
52+
'Setup/Patch/Data',
53+
$patchName . '.php',
54+
$this->twig->render('Setup/Patch/Data/Patch.php.twig', ['vendor' => $vendor, 'module' => $module, 'patch_name' => $patchName])
55+
);
56+
}
57+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
/**
4+
* Copyright Skywire. All rights reserved.
5+
* See LICENSE.txt for license details.
6+
*
7+
* @author Skywire Core Team
8+
* @copyright Copyright (c) 2021 Skywire (http://www.skywire.co.uk)
9+
*/
10+
declare(strict_types=1);
11+
12+
namespace MageGen\Generator;
13+
14+
use MageGen\Writer\ModuleFile;
15+
use Twig\Environment;
16+
17+
18+
class SchemaPatchGenerator
19+
{
20+
/**
21+
* @var Environment
22+
*/
23+
protected $twig;
24+
25+
/**
26+
* DiGenerator constructor.
27+
*
28+
* @param Environment $twig
29+
*/
30+
public function __construct(Environment $twig)
31+
{
32+
$this->twig = $twig;
33+
}
34+
35+
/**
36+
* Create a file if it doesn't exist and return the path
37+
*
38+
* @param string $vendor
39+
* @param string $module
40+
* @param ModuleFile $writer
41+
*
42+
* @return string
43+
* @throws \Twig\Error\LoaderError
44+
* @throws \Twig\Error\RuntimeError
45+
* @throws \Twig\Error\SyntaxError
46+
*/
47+
public function createSchemaPatch(string $vendor, string $module, string $patchName, ModuleFile $writer): string
48+
{
49+
return $writer->writeFile(
50+
$vendor,
51+
$module,
52+
'Setup/Patch/Schema',
53+
$patchName . '.php',
54+
$this->twig->render('Setup/Patch/Schema/Patch.php.twig', ['vendor' => $vendor, 'module' => $module, 'patch_name' => $patchName])
55+
);
56+
}
57+
}

src/MakeDataPatchCommand.php

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
/**
4+
* Copyright Skywire. All rights reserved.
5+
* See LICENSE.txt for license details.
6+
*
7+
* @author Skywire Core Team
8+
* @copyright Copyright (c) 2021 Skywire (http://www.skywire.co.uk)
9+
*/
10+
declare(strict_types=1);
11+
12+
namespace MageGen;
13+
14+
use MageGen\Autocomplete\ModuleAutocomplete;
15+
use MageGen\Generator\DataPatchGenerator;
16+
use Symfony\Component\Console\Input\InputArgument;
17+
use Symfony\Component\Console\Input\InputInterface;
18+
use Symfony\Component\Console\Input\InputOption;
19+
use Symfony\Component\Console\Output\OutputInterface;
20+
use Symfony\Component\Console\Question\Question;
21+
use Symfony\Component\Console\Style\SymfonyStyle;
22+
use Twig\Environment;
23+
24+
/**
25+
* @method string getCommandDescription()
26+
*/
27+
class MakeDataPatchCommand extends AbstractCommand
28+
{
29+
/**
30+
* @var Environment
31+
*/
32+
protected $twig;
33+
34+
protected static $defaultName = 'make:data-patch';
35+
36+
/**
37+
* @var DataPatchGenerator
38+
*/
39+
private $dataPatchGenerator;
40+
41+
42+
public function __construct(Environment $twig, string $name = null)
43+
{
44+
parent::__construct($twig, $name);
45+
46+
$this->dataPatchGenerator = new DataPatchGenerator($twig);
47+
}
48+
49+
protected function configure(): void
50+
{
51+
parent::configure();
52+
$this->addOption('magepath', 'm', InputOption::VALUE_REQUIRED, 'Path to Magento installation', getcwd());
53+
$this->addArgument('module', InputArgument::OPTIONAL, 'Module name');
54+
$this->addArgument('patch_name', InputArgument::OPTIONAL, 'Patch Name');
55+
}
56+
57+
protected function execute(InputInterface $input, OutputInterface $output): int
58+
{
59+
require $input->getOption('magepath') . '/vendor/autoload.php';
60+
61+
$io = new SymfonyStyle($input, $output);
62+
63+
$module = $input->getArgument('module');
64+
if (!$module) {
65+
$module = $io->askQuestion(
66+
(new Question('Module'))->setAutocompleterValues(
67+
(new ModuleAutocomplete())->getAutocompleteValues(
68+
$input->getOption('magepath')
69+
)
70+
)
71+
);
72+
}
73+
74+
$patchName = $input->getArgument('patch_name');
75+
if (!$patchName) {
76+
$patchName = $io->askQuestion(
77+
new Question('Patch Name')
78+
);
79+
}
80+
81+
[$vendor, $module] = explode('_', $module);
82+
83+
$patchFilePath = $this->dataPatchGenerator->createDataPatch(
84+
$vendor,
85+
$module,
86+
$patchName,
87+
$this->getWriter($input)
88+
);
89+
90+
return 0;
91+
}
92+
}

src/MakeSchemaPatchCommand.php

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
/**
4+
* Copyright Skywire. All rights reserved.
5+
* See LICENSE.txt for license details.
6+
*
7+
* @author Skywire Core Team
8+
* @copyright Copyright (c) 2021 Skywire (http://www.skywire.co.uk)
9+
*/
10+
declare(strict_types=1);
11+
12+
namespace MageGen;
13+
14+
use MageGen\Autocomplete\ModuleAutocomplete;
15+
use MageGen\Generator\SchemaPatchGenerator;
16+
use Symfony\Component\Console\Input\InputArgument;
17+
use Symfony\Component\Console\Input\InputInterface;
18+
use Symfony\Component\Console\Input\InputOption;
19+
use Symfony\Component\Console\Output\OutputInterface;
20+
use Symfony\Component\Console\Question\Question;
21+
use Symfony\Component\Console\Style\SymfonyStyle;
22+
use Twig\Environment;
23+
24+
/**
25+
* @method string getCommandDescription()
26+
*/
27+
class MakeSchemaPatchCommand extends AbstractCommand
28+
{
29+
/**
30+
* @var Environment
31+
*/
32+
protected $twig;
33+
34+
protected static $defaultName = 'make:schema-patch';
35+
36+
/**
37+
* @var SchemaPatchGenerator
38+
*/
39+
private $schemaPatchGenerator;
40+
41+
42+
public function __construct(Environment $twig, string $name = null)
43+
{
44+
parent::__construct($twig, $name);
45+
46+
$this->schemaPatchGenerator = new SchemaPatchGenerator($twig);
47+
}
48+
49+
protected function configure(): void
50+
{
51+
parent::configure();
52+
$this->addOption('magepath', 'm', InputOption::VALUE_REQUIRED, 'Path to Magento installation', getcwd());
53+
$this->addArgument('module', InputArgument::OPTIONAL, 'Module name');
54+
$this->addArgument('patch_name', InputArgument::OPTIONAL, 'Patch Name');
55+
}
56+
57+
protected function execute(InputInterface $input, OutputInterface $output): int
58+
{
59+
require $input->getOption('magepath') . '/vendor/autoload.php';
60+
61+
$io = new SymfonyStyle($input, $output);
62+
63+
$module = $input->getArgument('module');
64+
if (!$module) {
65+
$module = $io->askQuestion(
66+
(new Question('Module'))->setAutocompleterValues(
67+
(new ModuleAutocomplete())->getAutocompleteValues(
68+
$input->getOption('magepath')
69+
)
70+
)
71+
);
72+
}
73+
74+
$patchName = $input->getArgument('patch_name');
75+
if (!$patchName) {
76+
$patchName = $io->askQuestion(
77+
new Question('Patch Name')
78+
);
79+
}
80+
81+
[$vendor, $module] = explode('_', $module);
82+
83+
$patchFilePath = $this->schemaPatchGenerator->createSchemaPatch(
84+
$vendor,
85+
$module,
86+
$patchName,
87+
$this->getWriter($input)
88+
);
89+
90+
return 0;
91+
}
92+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace {{ vendor }}\{{ module }}\Setup\Patch\Data;
5+
6+
use Magento\Framework\Setup\ModuleDataSetupInterface;
7+
use Magento\Framework\Setup\Patch\DataPatchInterface;
8+
9+
class {{ patch_name }} implements DataPatchInterface
10+
{
11+
/**
12+
* @var ModuleDataSetupInterface
13+
*/
14+
private $moduleDataSetup;
15+
16+
/**
17+
* @param ModuleDataSetupInterface $moduleDataSetup
18+
*/
19+
public function __construct(ModuleDataSetupInterface $moduleDataSetup)
20+
{
21+
$this->moduleDataSetup = $moduleDataSetup;
22+
}
23+
24+
public static function getDependencies()
25+
{
26+
return [];
27+
}
28+
29+
public function getAliases()
30+
{
31+
return [];
32+
}
33+
34+
public function apply()
35+
{
36+
$this->moduleDataSetup->getConnection()->startSetup();
37+
38+
// TODO Add patch code
39+
40+
$this->moduleDataSetup->getConnection()->endSetup();
41+
}
42+
}

0 commit comments

Comments
 (0)