Skip to content

Commit 6747d2c

Browse files
authored
feat(core): optionally run composer up after installers (#839)
1 parent bc47ee3 commit 6747d2c

21 files changed

+194
-149
lines changed

src/Tempest/Console/src/Initializers/InvocationExecutorInitializer.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
namespace Tempest\Console\Initializers;
66

77
use Tempest\Console\ConsoleApplication;
8-
use Tempest\Console\Scheduler\GenericShellExecutor;
9-
use Tempest\Console\Scheduler\NullShellExecutor;
10-
use Tempest\Console\ShellExecutor;
118
use Tempest\Container\Container;
129
use Tempest\Container\Initializer;
1310
use Tempest\Container\Singleton;
1411
use Tempest\Core\Application;
12+
use Tempest\Core\ShellExecutor;
13+
use Tempest\Core\ShellExecutors\GenericShellExecutor;
14+
use Tempest\Core\ShellExecutors\NullShellExecutor;
1515

1616
final readonly class InvocationExecutorInitializer implements Initializer
1717
{

src/Tempest/Console/src/Initializers/SchedulerInitializer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
use Tempest\Console\Scheduler\GenericScheduler;
1111
use Tempest\Console\Scheduler\NullScheduler;
1212
use Tempest\Console\Scheduler\SchedulerConfig;
13-
use Tempest\Console\ShellExecutor;
1413
use Tempest\Container\Container;
1514
use Tempest\Container\Initializer;
1615
use Tempest\Container\Singleton;
1716
use Tempest\Core\Application;
17+
use Tempest\Core\ShellExecutor;
1818

1919
final readonly class SchedulerInitializer implements Initializer
2020
{

src/Tempest/Console/src/Installers/ConsoleInstaller.php

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@
55
namespace Tempest\Console\Installers;
66

77
use Tempest\Core\Installer;
8-
use Tempest\Core\PublishesFiles;
8+
use Tempest\Core\IsComponentInstaller;
99
use function Tempest\root_path;
10-
use function Tempest\Support\str;
1110

1211
final class ConsoleInstaller implements Installer
1312
{
14-
use PublishesFiles;
13+
use IsComponentInstaller;
1514

1615
public function getName(): string
1716
{
@@ -32,45 +31,7 @@ public function install(): void
3231
}
3332
},
3433
);
35-
}
36-
37-
private function installMainNamespace(): void
38-
{
39-
if ($this->composer->mainNamespace !== null) {
40-
return;
41-
}
42-
43-
if (! $this->confirm('Tempest detected no main project namespace. Do you want to create it?', default: true)) {
44-
return;
45-
}
46-
47-
$appPath = root_path($this->ask('Which path do you wish to use as your main project directory?', default: 'app/'));
48-
49-
$defaultAppNamespace = str($appPath)
50-
->replaceStart(root_path(), '')
51-
->trim('/')
52-
->explode('/')
53-
->map(fn (string $part) => ucfirst($part))
54-
->implode('\\')
55-
->append('\\')
56-
->toString();
57-
58-
$appNamespace = str($this->ask('Which namespace do you wish to use?', default: $defaultAppNamespace))
59-
->trim('\\')
60-
->append('\\')
61-
->toString();
62-
63-
if (! is_dir($appPath)) {
64-
mkdir($appPath, recursive: true);
65-
}
66-
67-
$this->composer
68-
->addNamespace(
69-
$appNamespace,
70-
$appPath,
71-
)
72-
->save();
7334

74-
$this->success("Project namespace created: {$appPath}");
35+
$this->updateComposer();
7536
}
7637
}

src/Tempest/Console/src/Scheduler/GenericScheduler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use DateTime;
88
use Tempest\Console\Input\ConsoleArgumentBag;
99
use Tempest\Console\Scheduler;
10-
use Tempest\Console\ShellExecutor;
10+
use Tempest\Core\ShellExecutor;
1111
use function Tempest\event;
1212

1313
final readonly class GenericScheduler implements Scheduler

src/Tempest/Console/src/Scheduler/GenericShellExecutor.php

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/Tempest/Console/src/Scheduler/NullShellExecutor.php

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/Tempest/Console/src/ShellExecutor.php

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/Tempest/Console/src/Testing/ConsoleTester.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,16 @@ public function submit(int|string $input = ''): self
132132
return $this;
133133
}
134134

135+
public function confirm(): self
136+
{
137+
return $this->submit('yes');
138+
}
139+
140+
public function deny(): self
141+
{
142+
return $this->submit('no');
143+
}
144+
135145
public function print(): self
136146
{
137147
echo 'OUTPUT:' . PHP_EOL;

src/Tempest/Core/src/Composer.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,13 @@ final class Composer
1919
private array $composer;
2020

2121
public function __construct(
22-
private string $root,
22+
private readonly string $root,
23+
private ShellExecutor $executor,
2324
) {
25+
}
26+
27+
public function load(): self
28+
{
2429
$this->composerPath = path($this->root, 'composer.json')->toString();
2530
$this->composer = $this->loadComposerFile($this->composerPath);
2631
$this->namespaces = arr($this->composer)
@@ -40,6 +45,8 @@ public function __construct(
4045
if (! isset($this->mainNamespace) && count($this->namespaces)) {
4146
$this->mainNamespace = $this->namespaces[0];
4247
}
48+
49+
return $this;
4350
}
4451

4552
public function setMainNamespace(ComposerNamespace $namespace): self
@@ -49,6 +56,13 @@ public function setMainNamespace(ComposerNamespace $namespace): self
4956
return $this;
5057
}
5158

59+
public function setShellExecutor(ShellExecutor $executor): self
60+
{
61+
$this->executor = $executor;
62+
63+
return $this;
64+
}
65+
5266
public function addNamespace(string $namespace, string $path): self
5367
{
5468
$path = str_replace($this->root, '.', $path);
@@ -65,6 +79,13 @@ public function save(): self
6579
return $this;
6680
}
6781

82+
public function executeUpdate(): self
83+
{
84+
$this->executor->execute('composer up');
85+
86+
return $this;
87+
}
88+
6889
private function loadComposerFile(string $path): array
6990
{
7091
if (! file_exists($path)) {
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tempest\Core;
6+
7+
use function Tempest\root_path;
8+
use function Tempest\Support\str;
9+
10+
/**
11+
* @phpstan-require-implements \Tempest\Core\Installer
12+
*/
13+
trait IsComponentInstaller
14+
{
15+
use PublishesFiles;
16+
17+
private function installMainNamespace(): void
18+
{
19+
if ($this->composer->mainNamespace !== null) {
20+
return;
21+
}
22+
23+
if (! $this->confirm('Tempest detected no main project namespace. Do you want to create it?', default: true)) {
24+
return;
25+
}
26+
27+
$appPath = root_path($this->ask('Which path do you wish to use as your main project directory?', default: 'app/'));
28+
29+
$defaultAppNamespace = str($appPath)
30+
->replaceStart(root_path(), '')
31+
->trim('/')
32+
->explode('/')
33+
->map(fn (string $part) => ucfirst($part))
34+
->implode('\\')
35+
->append('\\')
36+
->toString();
37+
38+
$appNamespace = str($this->ask('Which namespace do you wish to use?', default: $defaultAppNamespace))
39+
->trim('\\')
40+
->append('\\')
41+
->toString();
42+
43+
if (! is_dir($appPath)) {
44+
mkdir($appPath, recursive: true);
45+
}
46+
47+
$this->composer
48+
->addNamespace(
49+
$appNamespace,
50+
$appPath,
51+
)
52+
->save();
53+
54+
$this->success("Project namespace created: {$appPath}");
55+
}
56+
57+
private function updateComposer(): void
58+
{
59+
if ($this->confirm(question: 'Run composer update?', default: true)) {
60+
$this->composer->executeUpdate();
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)