Skip to content

Commit

Permalink
Don't run if there exists a file
Browse files Browse the repository at this point in the history
Makes it unpredictable when rerunning
  • Loading branch information
caendesilva committed Jul 12, 2024
1 parent 62b99d4 commit a2c492c
Showing 1 changed file with 10 additions and 30 deletions.
40 changes: 10 additions & 30 deletions monorepo/DevTools/src/RefactorConfigCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,16 @@
namespace Hyde\MonorepoDevTools;

use Hyde\Hyde;
use Throwable;
use RuntimeException;
use Hyde\Enums\Feature;
use Illuminate\Support\Str;
use Symfony\Component\Yaml\Yaml;
use Hyde\Console\Concerns\Command;
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;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit a2c492c

Please sign in to comment.