Skip to content

Commit

Permalink
[5.x] Add new starter-kit:init command (#11215)
Browse files Browse the repository at this point in the history
  • Loading branch information
jesseleite authored Dec 5, 2024
1 parent 9962a63 commit 2feec79
Show file tree
Hide file tree
Showing 8 changed files with 1,062 additions and 56 deletions.
67 changes: 67 additions & 0 deletions src/Console/Commands/Concerns/MigratesLegacyStarterKitConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace Statamic\Console\Commands\Concerns;

use Statamic\Facades\File;

use function Laravel\Prompts\confirm;

trait MigratesLegacyStarterKitConfig
{
protected $migrated = false;

/**
* 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 migrateLegacyConfig(?string $exportPath = null): self
{
if (! $this->isUsingLegacyExporterConventions()) {
return $this;
}

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 $this;
}
}

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($postInstallHook = base_path('StarterKitPostInstall.php'))) {
File::move($postInstallHook, base_path('package/StarterKitPostInstall.php'));
$this->components->info('Starter kit post-install hook moved to [package/StarterKitPostInstall.php].');
}

if ($exportPath && File::exists($packageComposerJson = $exportPath.'/composer.json')) {
File::move($packageComposerJson, base_path('package/composer.json'));
$this->components->info('Composer package config moved to [package/composer.json].');
}

$this->migrated = true;

return $this;
}

/**
* Check if migration logic was ran.
*/
protected function migratedLegacyConfig(): bool
{
return $this->migrated;
}
}
56 changes: 11 additions & 45 deletions src/Console/Commands/StarterKitExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Statamic\Console\Commands;

use Illuminate\Console\Command;
use Statamic\Console\Commands\Concerns\MigratesLegacyStarterKitConfig;
use Statamic\Console\RunsInPlease;
use Statamic\Facades\File;
use Statamic\Facades\Path;
Expand All @@ -13,7 +14,7 @@

class StarterKitExport extends Command
{
use RunsInPlease;
use MigratesLegacyStarterKitConfig, RunsInPlease;

/**
* The name and signature of the console command.
Expand All @@ -36,11 +37,11 @@ class StarterKitExport extends Command
*/
public function handle()
{
if ($this->isUsingLegacyExporterConventions()) {
$this->askToMigrateToPackageFolder();
}
$path = $this->getAbsolutePath();

$this->migrateLegacyConfig($path);

if (! File::exists($path = $this->getAbsolutePath())) {
if (! File::exists($path)) {
$this->askToCreateExportPath($path);
}

Expand All @@ -55,7 +56,11 @@ public function handle()
return 1;
}

$this->components->info("Starter kit was successfully exported to [$path].");
if (version_compare(app()->version(), '11', '<')) {
return $this->components->info("Starter kit was successfully exported to [$path].");
}

$this->components->success("Starter kit was successfully exported to [$path].");
}

/**
Expand Down Expand Up @@ -85,43 +90,4 @@ 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($postInstallHook = base_path('StarterKitPostInstall.php'))) {
File::move($postInstallHook, base_path('package/StarterKitPostInstall.php'));
$this->components->info('Starter kit post-install hook moved to [package/StarterKitPostInstall.php].');
}

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].');
}
}
}
Loading

0 comments on commit 2feec79

Please sign in to comment.