Skip to content

Commit 16bb315

Browse files
authored
[5.x] Add --clear option for starter-kit:export (#11079)
1 parent f012812 commit 16bb315

File tree

4 files changed

+73
-16
lines changed

4 files changed

+73
-16
lines changed

src/Console/Commands/StarterKitExport.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ class StarterKitExport extends Command
2020
*
2121
* @var string
2222
*/
23-
protected $signature = 'statamic:starter-kit:export { path : Specify the path you are exporting to }';
23+
protected $signature = 'statamic:starter-kit:export
24+
{ path : Specify the path you are exporting to }
25+
{ --clear : Clear out everything at target export path before exporting }';
2426

2527
/**
2628
* The console command description.
@@ -42,7 +44,8 @@ public function handle()
4244
$this->askToCreateExportPath($path);
4345
}
4446

45-
$exporter = new StarterKitExporter($path);
47+
$exporter = (new StarterKitExporter($path))
48+
->clear($this->option('clear'));
4649

4750
try {
4851
$exporter->export();

src/StarterKits/Exporter.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99
use Statamic\StarterKits\Exceptions\StarterKitException;
1010
use Statamic\Support\Arr;
1111
use Statamic\Support\Str;
12+
use Statamic\Support\Traits\FluentlyGetsAndSets;
1213

1314
class Exporter
1415
{
15-
use InteractsWithFilesystem;
16+
use FluentlyGetsAndSets, InteractsWithFilesystem;
1617

1718
protected $exportPath;
19+
protected $clear;
1820
protected $files;
1921
protected $vendorName;
2022
protected $modules;
@@ -29,6 +31,14 @@ public function __construct(string $exportPath)
2931
$this->files = app(Filesystem::class);
3032
}
3133

34+
/**
35+
* Get or set whether to clear out everything at target export path before exporting.
36+
*/
37+
public function clear(bool $clear = false): self|bool|null
38+
{
39+
return $this->fluentlyGetOrSet('clear')->args(func_get_args());
40+
}
41+
3242
/**
3343
* Export starter kit.
3444
*
@@ -40,6 +50,7 @@ public function export(): void
4050
->validateExportPath()
4151
->validateConfig()
4252
->instantiateModules()
53+
->clearExportPath()
4354
->exportModules()
4455
->exportConfig()
4556
->exportHooks()
@@ -132,6 +143,20 @@ protected function normalizeModuleKey(string $key, string $childKey): string
132143
return $key !== 'top_level' ? "{$key}.modules.{$childKey}" : $childKey;
133144
}
134145

146+
/**
147+
* Optionally clear out everything at target export path before exporting.
148+
*/
149+
protected function clearExportPath()
150+
{
151+
if (! $this->clear) {
152+
return $this;
153+
}
154+
155+
$this->files->cleanDirectory($this->exportPath);
156+
157+
return $this;
158+
}
159+
135160
/**
136161
* Export all the modules.
137162
*/

src/StarterKits/Installer.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ public function __construct(string $package, ?Command $console = null, ?LicenseM
6363
public function branch(?string $branch = null): self|bool|null
6464
{
6565
return $this->fluentlyGetOrSet('branch')->args(func_get_args());
66-
67-
return $this;
6866
}
6967

7068
/**
@@ -73,8 +71,6 @@ public function branch(?string $branch = null): self|bool|null
7371
public function fromLocalRepo(bool $fromLocalRepo = false): self|bool|null
7472
{
7573
return $this->fluentlyGetOrSet('fromLocalRepo')->args(func_get_args());
76-
77-
return $this;
7874
}
7975

8076
/**
@@ -83,8 +79,6 @@ public function fromLocalRepo(bool $fromLocalRepo = false): self|bool|null
8379
public function withConfig(bool $withConfig = false): self|bool|null
8480
{
8581
return $this->fluentlyGetOrSet('withConfig')->args(func_get_args());
86-
87-
return $this;
8882
}
8983

9084
/**
@@ -101,8 +95,6 @@ public function withoutDependencies(?bool $withoutDependencies = false): self|bo
10195
public function withUserPrompt(bool $withUserPrompt = false): self|bool|null
10296
{
10397
return $this->fluentlyGetOrSet('withUserPrompt')->args(func_get_args());
104-
105-
return $this;
10698
}
10799

108100
/**
@@ -119,8 +111,6 @@ public function isInteractive($isInteractive = false): self|bool|null
119111
public function usingSubProcess(bool $usingSubProcess = false): self|bool|null
120112
{
121113
return $this->fluentlyGetOrSet('usingSubProcess')->args(func_get_args());
122-
123-
return $this;
124114
}
125115

126116
/**

tests/StarterKits/ExportTest.php

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,45 @@ public function it_can_export_as_to_different_destination_path()
149149
$this->cleanPaths($paths);
150150
}
151151

152+
#[Test]
153+
public function it_can_clear_target_export_path_with_clear_option()
154+
{
155+
$paths = $this->cleanPaths([
156+
base_path('one'),
157+
base_path('two'),
158+
]);
159+
160+
// Imagine this exists from previous export
161+
$this->files->makeDirectory($this->exportPath('one'), 0777, true, true);
162+
$this->files->put($this->exportPath('one/file.md'), 'One.');
163+
164+
$this->files->makeDirectory(base_path('two'), 0777, true, true);
165+
$this->files->put(base_path('two/file.md'), 'Two.');
166+
167+
$this->setExportPaths([
168+
'two/file.md',
169+
]);
170+
171+
$this->assertFileExists($this->exportPath('one'));
172+
$this->assertFileDoesNotExist($this->exportPath('two'));
173+
174+
$this->exportCoolRunnings();
175+
176+
// Our 'one' folder should exist after exporting normally
177+
$this->assertFileExists($this->exportPath('one'));
178+
$this->assertFileExists($this->exportPath('two'));
179+
180+
$this->exportCoolRunnings(['--clear' => true]);
181+
182+
// But 'one' folder should exist after exporting with `--clear` option
183+
$this->assertFileDoesNotExist($this->exportPath('one'));
184+
$this->assertFileExists($this->exportPath('two'));
185+
186+
$this->exportCoolRunnings();
187+
188+
$this->cleanPaths($paths);
189+
}
190+
152191
#[Test]
153192
public function it_copies_export_config()
154193
{
@@ -1533,12 +1572,12 @@ private function assertConfigSameOrder($expectedConfig)
15331572
);
15341573
}
15351574

1536-
private function exportCoolRunnings()
1575+
private function exportCoolRunnings($options = [])
15371576
{
1538-
return $this->artisan('statamic:starter-kit:export', [
1577+
return $this->artisan('statamic:starter-kit:export', array_merge([
15391578
'path' => '../cool-runnings',
15401579
'--no-interaction' => true,
1541-
]);
1580+
], $options));
15421581
}
15431582

15441583
private function assertFileHasContent($expected, $path)

0 commit comments

Comments
 (0)