@@ -38,8 +38,8 @@ discard block |
||
| 38 | 38 | |
| 39 | 39 | class GenerateCommand extends Command implements CompletionAwareInterface { |
| 40 | 40 | |
| 41 | - protected static $_templateSimple = |
|
| 42 | - '<?php |
|
| 41 | + protected static $_templateSimple = |
|
| 42 | + '<?php |
|
| 43 | 43 | |
| 44 | 44 | declare(strict_types=1); |
| 45 | 45 | |
@@ -83,129 +83,129 @@ discard block |
||
| 83 | 83 | } |
| 84 | 84 | '; |
| 85 | 85 | |
| 86 | - /** @var IDBConnection */ |
|
| 87 | - protected $connection; |
|
| 88 | - |
|
| 89 | - /** @var IAppManager */ |
|
| 90 | - protected $appManager; |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * @param IDBConnection $connection |
|
| 94 | - * @param IAppManager $appManager |
|
| 95 | - */ |
|
| 96 | - public function __construct(IDBConnection $connection, IAppManager $appManager) { |
|
| 97 | - $this->connection = $connection; |
|
| 98 | - $this->appManager = $appManager; |
|
| 99 | - |
|
| 100 | - parent::__construct(); |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - protected function configure() { |
|
| 104 | - $this |
|
| 105 | - ->setName('migrations:generate') |
|
| 106 | - ->addArgument('app', InputArgument::REQUIRED, 'Name of the app this migration command shall work on') |
|
| 107 | - ->addArgument('version', InputArgument::REQUIRED, 'Major version of this app, to allow versions on parallel development branches') |
|
| 108 | - ; |
|
| 109 | - |
|
| 110 | - parent::configure(); |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - public function execute(InputInterface $input, OutputInterface $output) { |
|
| 114 | - $appName = $input->getArgument('app'); |
|
| 115 | - $version = $input->getArgument('version'); |
|
| 116 | - |
|
| 117 | - if (!preg_match('/^\d{1,16}$/',$version)) { |
|
| 118 | - $output->writeln('<error>The given version is invalid. Only 0-9 are allowed (max. 16 digits)</error>'); |
|
| 119 | - return 1; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - $ms = new MigrationService($appName, $this->connection, new ConsoleOutput($output)); |
|
| 123 | - |
|
| 124 | - $date = date('YmdHis'); |
|
| 125 | - $path = $this->generateMigration($ms, 'Version' . $version . 'Date' . $date); |
|
| 126 | - |
|
| 127 | - $output->writeln("New migration class has been generated to <info>$path</info>"); |
|
| 128 | - return 0; |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * @param string $optionName |
|
| 133 | - * @param CompletionContext $context |
|
| 134 | - * @return string[] |
|
| 135 | - */ |
|
| 136 | - public function completeOptionValues($optionName, CompletionContext $context) { |
|
| 137 | - return []; |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * @param string $argumentName |
|
| 142 | - * @param CompletionContext $context |
|
| 143 | - * @return string[] |
|
| 144 | - */ |
|
| 145 | - public function completeArgumentValues($argumentName, CompletionContext $context) { |
|
| 146 | - if ($argumentName === 'app') { |
|
| 147 | - $allApps = \OC_App::getAllApps(); |
|
| 148 | - return array_diff($allApps, \OC_App::getEnabledApps(true, true)); |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - if ($argumentName === 'version') { |
|
| 152 | - $appName = $context->getWordAtIndex($context->getWordIndex() - 1); |
|
| 153 | - |
|
| 154 | - $version = explode('.', $this->appManager->getAppVersion($appName)); |
|
| 155 | - return [$version[0] . sprintf('%1$03d', $version[1])]; |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - return []; |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - /** |
|
| 162 | - * @param MigrationService $ms |
|
| 163 | - * @param string $className |
|
| 164 | - * @param string $schemaBody |
|
| 165 | - * @return string |
|
| 166 | - */ |
|
| 167 | - protected function generateMigration(MigrationService $ms, $className, $schemaBody = '') { |
|
| 168 | - if ($schemaBody === '') { |
|
| 169 | - $schemaBody = "\t\t" . 'return null;'; |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - |
|
| 173 | - $placeHolders = [ |
|
| 174 | - '{{namespace}}', |
|
| 175 | - '{{classname}}', |
|
| 176 | - '{{schemabody}}', |
|
| 177 | - ]; |
|
| 178 | - $replacements = [ |
|
| 179 | - $ms->getMigrationsNamespace(), |
|
| 180 | - $className, |
|
| 181 | - $schemaBody, |
|
| 182 | - ]; |
|
| 183 | - $code = str_replace($placeHolders, $replacements, self::$_templateSimple); |
|
| 184 | - $dir = $ms->getMigrationsDirectory(); |
|
| 185 | - |
|
| 186 | - $this->ensureMigrationDirExists($dir); |
|
| 187 | - $path = $dir . '/' . $className . '.php'; |
|
| 188 | - |
|
| 189 | - if (file_put_contents($path, $code) === false) { |
|
| 190 | - throw new RuntimeException('Failed to generate new migration step.'); |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - return $path; |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - protected function ensureMigrationDirExists($directory) { |
|
| 197 | - if (file_exists($directory) && is_dir($directory)) { |
|
| 198 | - return; |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - if (file_exists($directory)) { |
|
| 202 | - throw new \RuntimeException("Could not create folder \"$directory\""); |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - $this->ensureMigrationDirExists(dirname($directory)); |
|
| 206 | - |
|
| 207 | - if (!@mkdir($directory) && !is_dir($directory)) { |
|
| 208 | - throw new \RuntimeException("Could not create folder \"$directory\""); |
|
| 209 | - } |
|
| 210 | - } |
|
| 86 | + /** @var IDBConnection */ |
|
| 87 | + protected $connection; |
|
| 88 | + |
|
| 89 | + /** @var IAppManager */ |
|
| 90 | + protected $appManager; |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * @param IDBConnection $connection |
|
| 94 | + * @param IAppManager $appManager |
|
| 95 | + */ |
|
| 96 | + public function __construct(IDBConnection $connection, IAppManager $appManager) { |
|
| 97 | + $this->connection = $connection; |
|
| 98 | + $this->appManager = $appManager; |
|
| 99 | + |
|
| 100 | + parent::__construct(); |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + protected function configure() { |
|
| 104 | + $this |
|
| 105 | + ->setName('migrations:generate') |
|
| 106 | + ->addArgument('app', InputArgument::REQUIRED, 'Name of the app this migration command shall work on') |
|
| 107 | + ->addArgument('version', InputArgument::REQUIRED, 'Major version of this app, to allow versions on parallel development branches') |
|
| 108 | + ; |
|
| 109 | + |
|
| 110 | + parent::configure(); |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + public function execute(InputInterface $input, OutputInterface $output) { |
|
| 114 | + $appName = $input->getArgument('app'); |
|
| 115 | + $version = $input->getArgument('version'); |
|
| 116 | + |
|
| 117 | + if (!preg_match('/^\d{1,16}$/',$version)) { |
|
| 118 | + $output->writeln('<error>The given version is invalid. Only 0-9 are allowed (max. 16 digits)</error>'); |
|
| 119 | + return 1; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + $ms = new MigrationService($appName, $this->connection, new ConsoleOutput($output)); |
|
| 123 | + |
|
| 124 | + $date = date('YmdHis'); |
|
| 125 | + $path = $this->generateMigration($ms, 'Version' . $version . 'Date' . $date); |
|
| 126 | + |
|
| 127 | + $output->writeln("New migration class has been generated to <info>$path</info>"); |
|
| 128 | + return 0; |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * @param string $optionName |
|
| 133 | + * @param CompletionContext $context |
|
| 134 | + * @return string[] |
|
| 135 | + */ |
|
| 136 | + public function completeOptionValues($optionName, CompletionContext $context) { |
|
| 137 | + return []; |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * @param string $argumentName |
|
| 142 | + * @param CompletionContext $context |
|
| 143 | + * @return string[] |
|
| 144 | + */ |
|
| 145 | + public function completeArgumentValues($argumentName, CompletionContext $context) { |
|
| 146 | + if ($argumentName === 'app') { |
|
| 147 | + $allApps = \OC_App::getAllApps(); |
|
| 148 | + return array_diff($allApps, \OC_App::getEnabledApps(true, true)); |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + if ($argumentName === 'version') { |
|
| 152 | + $appName = $context->getWordAtIndex($context->getWordIndex() - 1); |
|
| 153 | + |
|
| 154 | + $version = explode('.', $this->appManager->getAppVersion($appName)); |
|
| 155 | + return [$version[0] . sprintf('%1$03d', $version[1])]; |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + return []; |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + /** |
|
| 162 | + * @param MigrationService $ms |
|
| 163 | + * @param string $className |
|
| 164 | + * @param string $schemaBody |
|
| 165 | + * @return string |
|
| 166 | + */ |
|
| 167 | + protected function generateMigration(MigrationService $ms, $className, $schemaBody = '') { |
|
| 168 | + if ($schemaBody === '') { |
|
| 169 | + $schemaBody = "\t\t" . 'return null;'; |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + |
|
| 173 | + $placeHolders = [ |
|
| 174 | + '{{namespace}}', |
|
| 175 | + '{{classname}}', |
|
| 176 | + '{{schemabody}}', |
|
| 177 | + ]; |
|
| 178 | + $replacements = [ |
|
| 179 | + $ms->getMigrationsNamespace(), |
|
| 180 | + $className, |
|
| 181 | + $schemaBody, |
|
| 182 | + ]; |
|
| 183 | + $code = str_replace($placeHolders, $replacements, self::$_templateSimple); |
|
| 184 | + $dir = $ms->getMigrationsDirectory(); |
|
| 185 | + |
|
| 186 | + $this->ensureMigrationDirExists($dir); |
|
| 187 | + $path = $dir . '/' . $className . '.php'; |
|
| 188 | + |
|
| 189 | + if (file_put_contents($path, $code) === false) { |
|
| 190 | + throw new RuntimeException('Failed to generate new migration step.'); |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + return $path; |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + protected function ensureMigrationDirExists($directory) { |
|
| 197 | + if (file_exists($directory) && is_dir($directory)) { |
|
| 198 | + return; |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + if (file_exists($directory)) { |
|
| 202 | + throw new \RuntimeException("Could not create folder \"$directory\""); |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + $this->ensureMigrationDirExists(dirname($directory)); |
|
| 206 | + |
|
| 207 | + if (!@mkdir($directory) && !is_dir($directory)) { |
|
| 208 | + throw new \RuntimeException("Could not create folder \"$directory\""); |
|
| 209 | + } |
|
| 210 | + } |
|
| 211 | 211 | } |