From a2c492c506761bf9443dd51e8057b2e4d54c0fd8 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 12 Jul 2024 12:09:04 +0200 Subject: [PATCH] Don't run if there exists a file Makes it unpredictable when rerunning --- .../DevTools/src/RefactorConfigCommand.php | 40 +++++-------------- 1 file changed, 10 insertions(+), 30 deletions(-) diff --git a/monorepo/DevTools/src/RefactorConfigCommand.php b/monorepo/DevTools/src/RefactorConfigCommand.php index 8b23bfad16f..79a754f927b 100644 --- a/monorepo/DevTools/src/RefactorConfigCommand.php +++ b/monorepo/DevTools/src/RefactorConfigCommand.php @@ -5,7 +5,7 @@ namespace Hyde\MonorepoDevTools; use Hyde\Hyde; -use Throwable; +use RuntimeException; use Hyde\Enums\Feature; use Illuminate\Support\Str; use Symfony\Component\Yaml\Yaml; @@ -13,10 +13,8 @@ use Hyde\Framework\Features\Blogging\Models\PostAuthor; use Hyde\Framework\Features\Metadata\MetadataElementContract; -use function copy; use function config; use function substr; -use function unlink; use function collect; use function implode; use function in_array; @@ -61,40 +59,22 @@ public function handle(): int protected function migrateToYaml(): void { - $usesGit = file_exists(Hyde::path('.git')); - - if (file_exists(Hyde::path('hyde.yml')) && ! file_exists(Hyde::path('hyde.yml.bak'))) { - copy(Hyde::path('hyde.yml'), Hyde::path('hyde.yml.bak')); - if (! $usesGit) { - $this->warn("You're not using Git for version control, so a backup of your configuration has been created at hyde.yml.bak."); - } + if (file_exists(Hyde::path('hyde.yml')) || file_exists(Hyde::path('hyde.yaml'))) { + throw new RuntimeException('Configuration already exists in YAML format.'); } - try { - $config = config('hyde'); - - $default = require Hyde::vendorPath('config/hyde.php'); + $config = config('hyde'); - // Todo: Add argument to not diff out defaults - $config = $this->diffConfig($config, $default); + $default = require Hyde::vendorPath('config/hyde.php'); - $config = $this->serializePhpData($config); + // Todo: Add argument to not diff out defaults + $config = $this->diffConfig($config, $default); - $yaml = Yaml::dump($config, 16, 4, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK | Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE); + $config = $this->serializePhpData($config); - file_put_contents(Hyde::path('hyde.yml'), $yaml); - } catch (Throwable $exception) { - $this->error('Failed to migrate configuration: '.$exception->getMessage()); - $this->warn('Rolling back changes...'); - copy(Hyde::path('hyde.yml.bak'), Hyde::path('hyde.yml')); - unlink(Hyde::path('hyde.yml.bak')); + $yaml = Yaml::dump($config, 16, 4, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK | Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE); - throw $exception; - } finally { - if ($usesGit && file_exists(Hyde::path('hyde.yml.bak'))) { - unlink(Hyde::path('hyde.yml.bak')); - } - } + file_put_contents(Hyde::path('hyde.yml'), $yaml); } protected function serializePhpData(array $config): array