Skip to content

Commit

Permalink
Help migrate starter kit and package config to new package folder c…
Browse files Browse the repository at this point in the history
…onvention.
  • Loading branch information
jesseleite committed Nov 25, 2024
1 parent 9cec292 commit 47a84ee
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/Console/Commands/StarterKitExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ class StarterKitExport extends Command
*/
public function handle()
{
if ($this->isUsingLegacyExporterConventions()) {
$this->askToMigrateToPackageFolder();
}

if (! File::exists($path = $this->getAbsolutePath())) {
$this->askToCreateExportPath($path);
}
Expand Down Expand Up @@ -81,4 +85,38 @@ protected function askToCreateExportPath(string $path): void

$this->components->info("A new directory has been created at [{$path}].");
}

/**
* Determine if dev sandbox has starter-kit.yaml at root and/or customized composer.json at target path.
*/
protected function isUsingLegacyExporterConventions(): bool
{
return File::exists(base_path('starter-kit.yaml'));
}

/**
* Determine if dev sandbox has starter-kit.yaml at root and/or customized composer.json at target path.
*/
protected function askToMigrateToPackageFolder(): void
{
if ($this->input->isInteractive()) {
if (! confirm('Config should now live in the [package] folder. Would you like Statamic to move it for you?', true)) {
return;
}
}

if (! File::exists($dir = base_path('package'))) {
File::makeDirectory($dir, 0755, true);
}

if (File::exists($starterKitConfig = base_path('starter-kit.yaml'))) {
File::move($starterKitConfig, base_path('package/starter-kit.yaml'));
$this->components->info('Starter kit config moved to [package/starter-kit.yaml].');
}

if (File::exists($packageComposerJson = $this->getAbsolutePath().'/composer.json')) {
File::move($packageComposerJson, base_path('package/composer.json'));
$this->components->info('Composer package config moved to [package/composer.json].');
}
}
}
39 changes: 39 additions & 0 deletions tests/StarterKits/ExportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -573,10 +573,12 @@ public function it_exports_custom_package_composer_json_file()
$this->files->put(base_path('package/composer.json'), $composerJson = 'custom composer.json!');

$this->assertFileExists(base_path('composer.json'));
$this->assertFileDoesNotExist($filesystemsConfig = $this->exportPath('config/filesystems.php'));

$this->exportCoolRunnings();

$this->assertEquals($composerJson, $this->files->get($this->targetPath('composer.json')));
$this->assertFileExists($filesystemsConfig);
}

#[Test]
Expand Down Expand Up @@ -1558,6 +1560,43 @@ public function it_normalizes_module_key_order()
$this->cleanPaths($paths);
}

#[Test]
public function it_can_help_migrate_to_new_package_folder_convention()
{
$this->setExportPaths([
'config',
]);

$this->files->move(base_path('package/starter-kit.yaml'), base_path('starter-kit.yaml'));
$this->files->put($this->targetPath('starter-kit.yaml'), 'this should get stomped!');
$this->files->put($this->targetPath('composer.json'), $packageComposerJson = 'custom composer.json!');
$this->files->deleteDirectory(base_path('package'));

$this->assertFileDoesNotExist(base_path('package'));
$this->assertFileDoesNotExist($filesystemsConfig = $this->exportPath('config/filesystems.php'));

$this->exportCoolRunnings()
// ->expectsOutput('Starter kit config moved to [package/starter-kit.yaml].') // TODO: Why does this work in InstallTest?
// ->expectsOutput('Composer package config moved to [package/composer.json].') // TODO: Why does this work in InstallTest?
->assertSuccessful();

$this->assertFileDoesNotExist(base_path('starter-kit.yaml'));
$this->assertFileExists(base_path('package/starter-kit.yaml'));

$expectedConfig = [
'export_paths' => [
'config',
],
];

$this->assertEquals($expectedConfig, YAML::parse($this->files->get(base_path('package/starter-kit.yaml'))));
$this->assertEquals($expectedConfig, YAML::parse($this->files->get($this->targetPath('starter-kit.yaml'))));

$this->assertEquals($packageComposerJson, $this->files->get($this->targetPath('composer.json')));

$this->assertFileExists($filesystemsConfig);
}

private function targetPath($path = null)
{
return collect([$this->targetPath, $path])->filter()->implode('/');
Expand Down

0 comments on commit 47a84ee

Please sign in to comment.