@@ -39,8 +39,8 @@ discard block |
||
| 39 | 39 | use Symfony\Component\Console\Question\ConfirmationQuestion; |
| 40 | 40 | |
| 41 | 41 | class GenerateCommand extends Command implements CompletionAwareInterface { |
| 42 | - protected static $_templateSimple = |
|
| 43 | - '<?php |
|
| 42 | + protected static $_templateSimple = |
|
| 43 | + '<?php |
|
| 44 | 44 | |
| 45 | 45 | declare(strict_types=1); |
| 46 | 46 | |
@@ -106,161 +106,161 @@ discard block |
||
| 106 | 106 | } |
| 107 | 107 | '; |
| 108 | 108 | |
| 109 | - /** @var Connection */ |
|
| 110 | - protected $connection; |
|
| 111 | - |
|
| 112 | - /** @var IAppManager */ |
|
| 113 | - protected $appManager; |
|
| 114 | - |
|
| 115 | - /** |
|
| 116 | - * @param Connection $connection |
|
| 117 | - * @param IAppManager $appManager |
|
| 118 | - */ |
|
| 119 | - public function __construct(Connection $connection, IAppManager $appManager) { |
|
| 120 | - $this->connection = $connection; |
|
| 121 | - $this->appManager = $appManager; |
|
| 122 | - |
|
| 123 | - parent::__construct(); |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - protected function configure() { |
|
| 127 | - $this |
|
| 128 | - ->setName('migrations:generate') |
|
| 129 | - ->addArgument('app', InputArgument::REQUIRED, 'Name of the app this migration command shall work on') |
|
| 130 | - ->addArgument('version', InputArgument::REQUIRED, 'Major version of this app, to allow versions on parallel development branches') |
|
| 131 | - ; |
|
| 132 | - |
|
| 133 | - parent::configure(); |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - public function execute(InputInterface $input, OutputInterface $output): int { |
|
| 137 | - $appName = $input->getArgument('app'); |
|
| 138 | - $version = $input->getArgument('version'); |
|
| 139 | - |
|
| 140 | - if (!preg_match('/^\d{1,16}$/', $version)) { |
|
| 141 | - $output->writeln('<error>The given version is invalid. Only 0-9 are allowed (max. 16 digits)</error>'); |
|
| 142 | - return 1; |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - if ($appName === 'core') { |
|
| 146 | - $fullVersion = implode('.', Util::getVersion()); |
|
| 147 | - } else { |
|
| 148 | - try { |
|
| 149 | - $fullVersion = $this->appManager->getAppVersion($appName, false); |
|
| 150 | - } catch (\Throwable $e) { |
|
| 151 | - $fullVersion = ''; |
|
| 152 | - } |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - if ($fullVersion) { |
|
| 156 | - [$major, $minor] = explode('.', $fullVersion); |
|
| 157 | - $shouldVersion = (string) ((int)$major * 1000 + (int)$minor); |
|
| 158 | - if ($version !== $shouldVersion) { |
|
| 159 | - $output->writeln('<comment>Unexpected migration version for current version: ' . $fullVersion . '</comment>'); |
|
| 160 | - $output->writeln('<comment> - Pattern: XYYY </comment>'); |
|
| 161 | - $output->writeln('<comment> - Expected: ' . $shouldVersion . '</comment>'); |
|
| 162 | - $output->writeln('<comment> - Actual: ' . $version . '</comment>'); |
|
| 163 | - |
|
| 164 | - if ($input->isInteractive()) { |
|
| 165 | - $helper = $this->getHelper('question'); |
|
| 166 | - $question = new ConfirmationQuestion('Continue with your given version? (y/n) [n] ', false); |
|
| 167 | - |
|
| 168 | - if (!$helper->ask($input, $output, $question)) { |
|
| 169 | - return 1; |
|
| 170 | - } |
|
| 171 | - } |
|
| 172 | - } |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - $ms = new MigrationService($appName, $this->connection, new ConsoleOutput($output)); |
|
| 176 | - |
|
| 177 | - $date = date('YmdHis'); |
|
| 178 | - $path = $this->generateMigration($ms, 'Version' . $version . 'Date' . $date); |
|
| 179 | - |
|
| 180 | - $output->writeln("New migration class has been generated to <info>$path</info>"); |
|
| 181 | - return 0; |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - /** |
|
| 185 | - * @param string $optionName |
|
| 186 | - * @param CompletionContext $context |
|
| 187 | - * @return string[] |
|
| 188 | - */ |
|
| 189 | - public function completeOptionValues($optionName, CompletionContext $context) { |
|
| 190 | - return []; |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - /** |
|
| 194 | - * @param string $argumentName |
|
| 195 | - * @param CompletionContext $context |
|
| 196 | - * @return string[] |
|
| 197 | - */ |
|
| 198 | - public function completeArgumentValues($argumentName, CompletionContext $context) { |
|
| 199 | - if ($argumentName === 'app') { |
|
| 200 | - $allApps = \OC_App::getAllApps(); |
|
| 201 | - return array_diff($allApps, \OC_App::getEnabledApps(true, true)); |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - if ($argumentName === 'version') { |
|
| 205 | - $appName = $context->getWordAtIndex($context->getWordIndex() - 1); |
|
| 206 | - |
|
| 207 | - $version = explode('.', $this->appManager->getAppVersion($appName)); |
|
| 208 | - return [$version[0] . sprintf('%1$03d', $version[1])]; |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - return []; |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - /** |
|
| 215 | - * @param MigrationService $ms |
|
| 216 | - * @param string $className |
|
| 217 | - * @param string $schemaBody |
|
| 218 | - * @return string |
|
| 219 | - */ |
|
| 220 | - protected function generateMigration(MigrationService $ms, $className, $schemaBody = '') { |
|
| 221 | - if ($schemaBody === '') { |
|
| 222 | - $schemaBody = "\t\t" . 'return null;'; |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - |
|
| 226 | - $placeHolders = [ |
|
| 227 | - '{{namespace}}', |
|
| 228 | - '{{classname}}', |
|
| 229 | - '{{schemabody}}', |
|
| 230 | - '{{year}}', |
|
| 231 | - ]; |
|
| 232 | - $replacements = [ |
|
| 233 | - $ms->getMigrationsNamespace(), |
|
| 234 | - $className, |
|
| 235 | - $schemaBody, |
|
| 236 | - date('Y') |
|
| 237 | - ]; |
|
| 238 | - $code = str_replace($placeHolders, $replacements, self::$_templateSimple); |
|
| 239 | - $dir = $ms->getMigrationsDirectory(); |
|
| 240 | - |
|
| 241 | - $this->ensureMigrationDirExists($dir); |
|
| 242 | - $path = $dir . '/' . $className . '.php'; |
|
| 243 | - |
|
| 244 | - if (file_put_contents($path, $code) === false) { |
|
| 245 | - throw new RuntimeException('Failed to generate new migration step.'); |
|
| 246 | - } |
|
| 247 | - |
|
| 248 | - return $path; |
|
| 249 | - } |
|
| 250 | - |
|
| 251 | - protected function ensureMigrationDirExists($directory) { |
|
| 252 | - if (file_exists($directory) && is_dir($directory)) { |
|
| 253 | - return; |
|
| 254 | - } |
|
| 255 | - |
|
| 256 | - if (file_exists($directory)) { |
|
| 257 | - throw new \RuntimeException("Could not create folder \"$directory\""); |
|
| 258 | - } |
|
| 259 | - |
|
| 260 | - $this->ensureMigrationDirExists(dirname($directory)); |
|
| 261 | - |
|
| 262 | - if (!@mkdir($directory) && !is_dir($directory)) { |
|
| 263 | - throw new \RuntimeException("Could not create folder \"$directory\""); |
|
| 264 | - } |
|
| 265 | - } |
|
| 109 | + /** @var Connection */ |
|
| 110 | + protected $connection; |
|
| 111 | + |
|
| 112 | + /** @var IAppManager */ |
|
| 113 | + protected $appManager; |
|
| 114 | + |
|
| 115 | + /** |
|
| 116 | + * @param Connection $connection |
|
| 117 | + * @param IAppManager $appManager |
|
| 118 | + */ |
|
| 119 | + public function __construct(Connection $connection, IAppManager $appManager) { |
|
| 120 | + $this->connection = $connection; |
|
| 121 | + $this->appManager = $appManager; |
|
| 122 | + |
|
| 123 | + parent::__construct(); |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + protected function configure() { |
|
| 127 | + $this |
|
| 128 | + ->setName('migrations:generate') |
|
| 129 | + ->addArgument('app', InputArgument::REQUIRED, 'Name of the app this migration command shall work on') |
|
| 130 | + ->addArgument('version', InputArgument::REQUIRED, 'Major version of this app, to allow versions on parallel development branches') |
|
| 131 | + ; |
|
| 132 | + |
|
| 133 | + parent::configure(); |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + public function execute(InputInterface $input, OutputInterface $output): int { |
|
| 137 | + $appName = $input->getArgument('app'); |
|
| 138 | + $version = $input->getArgument('version'); |
|
| 139 | + |
|
| 140 | + if (!preg_match('/^\d{1,16}$/', $version)) { |
|
| 141 | + $output->writeln('<error>The given version is invalid. Only 0-9 are allowed (max. 16 digits)</error>'); |
|
| 142 | + return 1; |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + if ($appName === 'core') { |
|
| 146 | + $fullVersion = implode('.', Util::getVersion()); |
|
| 147 | + } else { |
|
| 148 | + try { |
|
| 149 | + $fullVersion = $this->appManager->getAppVersion($appName, false); |
|
| 150 | + } catch (\Throwable $e) { |
|
| 151 | + $fullVersion = ''; |
|
| 152 | + } |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + if ($fullVersion) { |
|
| 156 | + [$major, $minor] = explode('.', $fullVersion); |
|
| 157 | + $shouldVersion = (string) ((int)$major * 1000 + (int)$minor); |
|
| 158 | + if ($version !== $shouldVersion) { |
|
| 159 | + $output->writeln('<comment>Unexpected migration version for current version: ' . $fullVersion . '</comment>'); |
|
| 160 | + $output->writeln('<comment> - Pattern: XYYY </comment>'); |
|
| 161 | + $output->writeln('<comment> - Expected: ' . $shouldVersion . '</comment>'); |
|
| 162 | + $output->writeln('<comment> - Actual: ' . $version . '</comment>'); |
|
| 163 | + |
|
| 164 | + if ($input->isInteractive()) { |
|
| 165 | + $helper = $this->getHelper('question'); |
|
| 166 | + $question = new ConfirmationQuestion('Continue with your given version? (y/n) [n] ', false); |
|
| 167 | + |
|
| 168 | + if (!$helper->ask($input, $output, $question)) { |
|
| 169 | + return 1; |
|
| 170 | + } |
|
| 171 | + } |
|
| 172 | + } |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + $ms = new MigrationService($appName, $this->connection, new ConsoleOutput($output)); |
|
| 176 | + |
|
| 177 | + $date = date('YmdHis'); |
|
| 178 | + $path = $this->generateMigration($ms, 'Version' . $version . 'Date' . $date); |
|
| 179 | + |
|
| 180 | + $output->writeln("New migration class has been generated to <info>$path</info>"); |
|
| 181 | + return 0; |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + /** |
|
| 185 | + * @param string $optionName |
|
| 186 | + * @param CompletionContext $context |
|
| 187 | + * @return string[] |
|
| 188 | + */ |
|
| 189 | + public function completeOptionValues($optionName, CompletionContext $context) { |
|
| 190 | + return []; |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + /** |
|
| 194 | + * @param string $argumentName |
|
| 195 | + * @param CompletionContext $context |
|
| 196 | + * @return string[] |
|
| 197 | + */ |
|
| 198 | + public function completeArgumentValues($argumentName, CompletionContext $context) { |
|
| 199 | + if ($argumentName === 'app') { |
|
| 200 | + $allApps = \OC_App::getAllApps(); |
|
| 201 | + return array_diff($allApps, \OC_App::getEnabledApps(true, true)); |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + if ($argumentName === 'version') { |
|
| 205 | + $appName = $context->getWordAtIndex($context->getWordIndex() - 1); |
|
| 206 | + |
|
| 207 | + $version = explode('.', $this->appManager->getAppVersion($appName)); |
|
| 208 | + return [$version[0] . sprintf('%1$03d', $version[1])]; |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + return []; |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + /** |
|
| 215 | + * @param MigrationService $ms |
|
| 216 | + * @param string $className |
|
| 217 | + * @param string $schemaBody |
|
| 218 | + * @return string |
|
| 219 | + */ |
|
| 220 | + protected function generateMigration(MigrationService $ms, $className, $schemaBody = '') { |
|
| 221 | + if ($schemaBody === '') { |
|
| 222 | + $schemaBody = "\t\t" . 'return null;'; |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + |
|
| 226 | + $placeHolders = [ |
|
| 227 | + '{{namespace}}', |
|
| 228 | + '{{classname}}', |
|
| 229 | + '{{schemabody}}', |
|
| 230 | + '{{year}}', |
|
| 231 | + ]; |
|
| 232 | + $replacements = [ |
|
| 233 | + $ms->getMigrationsNamespace(), |
|
| 234 | + $className, |
|
| 235 | + $schemaBody, |
|
| 236 | + date('Y') |
|
| 237 | + ]; |
|
| 238 | + $code = str_replace($placeHolders, $replacements, self::$_templateSimple); |
|
| 239 | + $dir = $ms->getMigrationsDirectory(); |
|
| 240 | + |
|
| 241 | + $this->ensureMigrationDirExists($dir); |
|
| 242 | + $path = $dir . '/' . $className . '.php'; |
|
| 243 | + |
|
| 244 | + if (file_put_contents($path, $code) === false) { |
|
| 245 | + throw new RuntimeException('Failed to generate new migration step.'); |
|
| 246 | + } |
|
| 247 | + |
|
| 248 | + return $path; |
|
| 249 | + } |
|
| 250 | + |
|
| 251 | + protected function ensureMigrationDirExists($directory) { |
|
| 252 | + if (file_exists($directory) && is_dir($directory)) { |
|
| 253 | + return; |
|
| 254 | + } |
|
| 255 | + |
|
| 256 | + if (file_exists($directory)) { |
|
| 257 | + throw new \RuntimeException("Could not create folder \"$directory\""); |
|
| 258 | + } |
|
| 259 | + |
|
| 260 | + $this->ensureMigrationDirExists(dirname($directory)); |
|
| 261 | + |
|
| 262 | + if (!@mkdir($directory) && !is_dir($directory)) { |
|
| 263 | + throw new \RuntimeException("Could not create folder \"$directory\""); |
|
| 264 | + } |
|
| 265 | + } |
|
| 266 | 266 | } |
@@ -154,12 +154,12 @@ discard block |
||
| 154 | 154 | |
| 155 | 155 | if ($fullVersion) { |
| 156 | 156 | [$major, $minor] = explode('.', $fullVersion); |
| 157 | - $shouldVersion = (string) ((int)$major * 1000 + (int)$minor); |
|
| 157 | + $shouldVersion = (string) ((int) $major * 1000 + (int) $minor); |
|
| 158 | 158 | if ($version !== $shouldVersion) { |
| 159 | - $output->writeln('<comment>Unexpected migration version for current version: ' . $fullVersion . '</comment>'); |
|
| 159 | + $output->writeln('<comment>Unexpected migration version for current version: '.$fullVersion.'</comment>'); |
|
| 160 | 160 | $output->writeln('<comment> - Pattern: XYYY </comment>'); |
| 161 | - $output->writeln('<comment> - Expected: ' . $shouldVersion . '</comment>'); |
|
| 162 | - $output->writeln('<comment> - Actual: ' . $version . '</comment>'); |
|
| 161 | + $output->writeln('<comment> - Expected: '.$shouldVersion.'</comment>'); |
|
| 162 | + $output->writeln('<comment> - Actual: '.$version.'</comment>'); |
|
| 163 | 163 | |
| 164 | 164 | if ($input->isInteractive()) { |
| 165 | 165 | $helper = $this->getHelper('question'); |
@@ -175,7 +175,7 @@ discard block |
||
| 175 | 175 | $ms = new MigrationService($appName, $this->connection, new ConsoleOutput($output)); |
| 176 | 176 | |
| 177 | 177 | $date = date('YmdHis'); |
| 178 | - $path = $this->generateMigration($ms, 'Version' . $version . 'Date' . $date); |
|
| 178 | + $path = $this->generateMigration($ms, 'Version'.$version.'Date'.$date); |
|
| 179 | 179 | |
| 180 | 180 | $output->writeln("New migration class has been generated to <info>$path</info>"); |
| 181 | 181 | return 0; |
@@ -205,7 +205,7 @@ discard block |
||
| 205 | 205 | $appName = $context->getWordAtIndex($context->getWordIndex() - 1); |
| 206 | 206 | |
| 207 | 207 | $version = explode('.', $this->appManager->getAppVersion($appName)); |
| 208 | - return [$version[0] . sprintf('%1$03d', $version[1])]; |
|
| 208 | + return [$version[0].sprintf('%1$03d', $version[1])]; |
|
| 209 | 209 | } |
| 210 | 210 | |
| 211 | 211 | return []; |
@@ -219,7 +219,7 @@ discard block |
||
| 219 | 219 | */ |
| 220 | 220 | protected function generateMigration(MigrationService $ms, $className, $schemaBody = '') { |
| 221 | 221 | if ($schemaBody === '') { |
| 222 | - $schemaBody = "\t\t" . 'return null;'; |
|
| 222 | + $schemaBody = "\t\t".'return null;'; |
|
| 223 | 223 | } |
| 224 | 224 | |
| 225 | 225 | |
@@ -239,7 +239,7 @@ discard block |
||
| 239 | 239 | $dir = $ms->getMigrationsDirectory(); |
| 240 | 240 | |
| 241 | 241 | $this->ensureMigrationDirExists($dir); |
| 242 | - $path = $dir . '/' . $className . '.php'; |
|
| 242 | + $path = $dir.'/'.$className.'.php'; |
|
| 243 | 243 | |
| 244 | 244 | if (file_put_contents($path, $code) === false) { |
| 245 | 245 | throw new RuntimeException('Failed to generate new migration step.'); |