-
-
Notifications
You must be signed in to change notification settings - Fork 218
/
NodeMigrationCommandController.php
167 lines (148 loc) · 6.64 KB
/
NodeMigrationCommandController.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
<?php
namespace Neos\ContentRepositoryRegistry\Command;
/*
* This file is part of the Neos.ContentRepository package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
use Neos\ContentRepository\NodeMigration\NodeMigrationServiceFactory;
use Neos\ContentRepository\Core\SharedModel\Workspace\WorkspaceName;
use Neos\ContentRepository\NodeMigration\Command\ExecuteMigration;
use Neos\ContentRepositoryRegistry\ContentRepositoryRegistry;
use Neos\ContentRepositoryRegistry\Migration\Factory\MigrationFactory;
use Neos\ContentRepository\Core\Factory\ContentRepositoryId;
use Neos\ContentRepositoryRegistry\Service\NodeMigrationGeneratorService;
use Neos\Flow\Cli\CommandController;
use Neos\ContentRepository\NodeMigration\MigrationException;
use Neos\ContentRepository\NodeMigration\Command\MigrationConfiguration;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Cli\Exception\StopCommandException;
use Neos\Flow\Package\Exception\UnknownPackageException;
use Neos\Flow\Package\PackageManager;
use Neos\Utility\Exception\FilesException;
/**
* Command controller for tasks related to node migration.
*/
#[Flow\Scope('singleton')]
class NodeMigrationCommandController extends CommandController
{
public function __construct(
private readonly MigrationFactory $migrationFactory,
private readonly ContentRepositoryRegistry $contentRepositoryRegistry,
private readonly PackageManager $packageManager,
private readonly NodeMigrationGeneratorService $nodeMigrationGeneratorService
) {
parent::__construct();
}
/**
* Do the configured migrations in the given migration.
*
* @param string $version The version of the migration configuration you want to use.
* @param string $workspace The workspace where the migration should be applied; by default "live"
* @param ?string $targetWorkspace The workspace where the migration result should end; by default "live". If set to null, each migration creates a new workspace for selectiv publishing.
* @param boolean $force Confirm application of this migration, only needed if the given migration contains any warnings.
* @return void
* @throws StopCommandException
* @see neos.contentrepositoryregistry:nodemigration:execute
*/
public function executeCommand(string $version, string $workspace = 'live', ?string $targetWorkspace = 'live', bool $force = false, string $contentRepositoryIdentifier = 'default'): void
{
$workspace = WorkspaceName::fromString($workspace);
$targetWorkspace = $targetWorkspace === null ?: WorkspaceName::fromString($targetWorkspace);
$contentRepositoryId = ContentRepositoryId::fromString($contentRepositoryIdentifier);
try {
$migrationConfiguration = $this->migrationFactory->getMigrationForVersion($version);
$this->outputCommentsAndWarnings($migrationConfiguration);
if ($migrationConfiguration->hasWarnings() && $force === false) {
$this->outputLine();
$this->outputLine('Migration has warnings.'
. ' You need to confirm execution by adding the "--force true" option to the command.');
$this->quit(1);
}
$nodeMigrationService = $this->contentRepositoryRegistry->buildService($contentRepositoryId, new NodeMigrationServiceFactory());
$nodeMigrationService->executeMigration(
new ExecuteMigration(
$migrationConfiguration,
$workspace,
$targetWorkspace,
)
);
// Rebase depending workspaces?
$this->outputLine();
$this->outputLine('Successfully applied migration.');
} catch (MigrationException $e) {
$this->outputLine();
$this->outputLine('Error: ' . $e->getMessage());
$this->quit(1);
}
}
/**
* Creates a node migration for the given package Key.
*
* @param string $packageKey The packageKey for the given package
* @return void
* @throws UnknownPackageException
* @throws FilesException
* @throws StopCommandException
* @see neos.contentrepositoryregistry:nodemigration:kickstart
*/
public function kickstartCommand(string $packageKey): void
{
if (!$this->packageManager->isPackageAvailable($packageKey)) {
$this->outputLine('Package "%s" is not available.', [$packageKey]);
$this->quit(1);
}
$createdMigration = $this->nodeMigrationGeneratorService->generateBoilerplateMigrationFileInPackage($packageKey);
$this->outputLine($createdMigration);
$this->outputLine('Your node migration has been created successfully.');
}
/**
* List available migrations
*
* @see neos.contentrepositoryregistry:nodemigration:list
*/
public function listCommand(): void
{
$availableMigrations = $this->migrationFactory->getAvailableVersions();
if (count($availableMigrations) === 0) {
$this->outputLine('No migrations available.');
$this->quit();
}
$tableRows = [];
foreach ($availableMigrations as $version => $migration) {
$migrationUpConfigurationComments = $this->migrationFactory->getMigrationForVersion($version)->getComments();
$tableRows[] = [
$version,
$migration['formattedVersionNumber'],
$migration['package']->getPackageKey(),
wordwrap($migrationUpConfigurationComments, 60)
];
}
$this->outputLine('<b>Available migrations</b>');
$this->outputLine();
$this->output->outputTable($tableRows, ['Version', 'Date', 'Package', 'Comments']);
}
/**
* Helper to output comments and warnings for the given configuration.
*
* @param MigrationConfiguration $migrationConfiguration
* @return void
*/
protected function outputCommentsAndWarnings(MigrationConfiguration $migrationConfiguration): void
{
if ($migrationConfiguration->hasComments()) {
$this->outputLine();
$this->outputLine('<b>Comments</b>');
$this->outputFormatted($migrationConfiguration->getComments(), [], 2);
}
if ($migrationConfiguration->hasWarnings()) {
$this->outputLine();
$this->outputLine('<b><u>Warnings</u></b>');
$this->outputFormatted($migrationConfiguration->getWarnings(), [], 2);
}
}
}