Skip to content

Commit

Permalink
Update exporter.
Browse files Browse the repository at this point in the history
  • Loading branch information
jesseleite committed Nov 25, 2024
1 parent 14e5a94 commit 9cec292
Show file tree
Hide file tree
Showing 4 changed files with 188 additions and 285 deletions.
24 changes: 0 additions & 24 deletions src/Console/Commands/StarterKitExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ class StarterKitExport extends Command
*/
public function handle()
{
if (! File::exists(base_path('starter-kit.yaml'))) {
return $this->askToStubStarterKitConfig();
}

if (! File::exists($path = $this->getAbsolutePath())) {
$this->askToCreateExportPath($path);
}
Expand All @@ -58,26 +54,6 @@ public function handle()
$this->components->info("Starter kit was successfully exported to [$path].");
}

/**
* Ask to stub out starter kit config.
*/
protected function askToStubStarterKitConfig(): void
{
$stubPath = __DIR__.'/stubs/starter-kits/starter-kit.yaml.stub';
$newPath = base_path($config = 'starter-kit.yaml');

if ($this->input->isInteractive()) {
if (! confirm("Config [{$config}] does not exist. Would you like to create it now?", true)) {
return;
}
}

File::copy($stubPath, $newPath);

$this->comment("A new config has been created at [{$config}].");
$this->comment('Please configure your `export_paths` and re-run to begin your export!');
}

/**
* Get absolute path.
*/
Expand Down
4 changes: 4 additions & 0 deletions src/StarterKits/ExportableModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ protected function ensureNotExportingComposerJson(): self
->merge($this->exportAsPaths())
->merge($this->exportAsPaths()->keys());

if ($flattenedExportPaths->contains('starter-kit.yaml')) {
throw new StarterKitException('Cannot export [starter-kit.yaml] config.');
}

if ($flattenedExportPaths->contains('composer.json')) {
throw new StarterKitException('Cannot export [composer.json]. Please use `dependencies` array!');
}
Expand Down
98 changes: 9 additions & 89 deletions src/StarterKits/Exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@

use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Collection;
use Statamic\Facades\Path;
use Statamic\Facades\YAML;
use Statamic\StarterKits\Concerns\InteractsWithFilesystem;
use Statamic\StarterKits\Exceptions\StarterKitException;
use Statamic\Support\Arr;
use Statamic\Support\Str;
use Statamic\Support\Traits\FluentlyGetsAndSets;

class Exporter
Expand Down Expand Up @@ -50,11 +48,9 @@ public function export(): void
$this
->validateExportPath()
->validateConfig()
->validatePackage()
->instantiateModules()
->clearExportPath()
->exportModules()
->exportConfig()
->exportHooks()
->exportPackage();
}
Expand All @@ -76,20 +72,8 @@ protected function validateExportPath(): self
*/
protected function validateConfig(): self
{
if (! $this->files->exists(base_path('starter-kit.yaml'))) {
throw new StarterKitException('Export config [starter-kit.yaml] does not exist.');
}

return $this;
}

/**
* Validate package folder, if it exists.
*/
protected function validatePackage(): self
{
if (! $this->files->exists(base_path('package'))) {
return $this;
if (! $this->files->exists(base_path('package/starter-kit.yaml'))) {
throw new StarterKitException('Starter kit config [package/starter-kit.yaml] does not exist.');
}

if (! $this->files->exists(base_path('package/composer.json'))) {
Expand Down Expand Up @@ -180,9 +164,7 @@ protected function clearExportPath()
*/
protected function exportModules(): self
{
$exportPath = $this->files->exists(base_path('package'))
? $this->exportPath.'/export'
: $this->exportPath;
$exportPath = $this->exportPath.'/export';

$this->modules->each(fn ($module) => $module->export($exportPath));

Expand All @@ -194,7 +176,7 @@ protected function exportModules(): self
*/
protected function config(?string $key = null): mixed
{
$config = collect(YAML::parse($this->files->get(base_path('starter-kit.yaml'))));
$config = collect(YAML::parse($this->files->get(base_path('package/starter-kit.yaml'))));

if ($key) {
return $config->get($key);
Expand All @@ -203,20 +185,6 @@ protected function config(?string $key = null): mixed
return $config;
}

/**
* Export starter kit config.
*/
protected function exportConfig(): self
{
$config = $this
->versionModuleDependencies()
->syncConfigWithModules();

$this->files->put("{$this->exportPath}/starter-kit.yaml", YAML::dump($config->all()));

return $this;
}

/**
* Version module dependencies from composer.json.
*/
Expand Down Expand Up @@ -297,62 +265,14 @@ protected function exportHooks(): self
*/
protected function exportPackage(): self
{
if (! $this->files->exists($packageFolder = base_path('package'))) {
return $this->exportComposerJson();
}

$this->copyDirectoryContentsInto($packageFolder, $this->exportPath);

return $this;
}
$this->copyDirectoryContentsInto(base_path('package'), $this->exportPath);

/**
* Export composer.json.
*/
protected function exportComposerJson(): self
{
$composerJson = $this->prepareComposerJsonFromStub()->all();
$config = $this
->versionModuleDependencies()
->syncConfigWithModules();

$this->files->put(
"{$this->exportPath}/composer.json",
json_encode($composerJson, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)."\n"
);
$this->files->put("{$this->exportPath}/starter-kit.yaml", YAML::dump($config->all()));

return $this;
}

/**
* Prepare composer.json from stub.
*/
protected function prepareComposerJsonFromStub(): Collection
{
$stub = $this->getComposerJsonStub();

$directory = preg_replace('/.*\/([^\/]*)/', '$1', $this->exportPath);
$vendorName = $this->vendorName ?? 'my-vendor-name';
$repoName = Str::slug($directory);
$package = "{$vendorName}/{$repoName}";
$title = Str::slugToTitle($repoName);

$stub = str_replace('dummy/package', $package, $stub);
$stub = str_replace('DummyTitle', $title, $stub);

return collect(json_decode($stub, true));
}

/**
* Get composer.json stub.
*/
protected function getComposerJsonStub(): string
{
$stubPath = __DIR__.'/../Console/Commands/stubs/starter-kits/composer.json.stub';

$existingComposerJsonPath = "{$this->exportPath}/composer.json";

if ($this->files->exists($existingComposerJsonPath)) {
return $this->files->get($existingComposerJsonPath);
}

return $this->files->get($stubPath);
}
}
Loading

0 comments on commit 9cec292

Please sign in to comment.