Skip to content

Commit 90b6321

Browse files
authored
feat(console): add installer (#837)
1 parent 985be66 commit 90b6321

File tree

3 files changed

+128
-0
lines changed

3 files changed

+128
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tempest\Console\Installers;
6+
7+
use Tempest\Core\Installer;
8+
use Tempest\Core\PublishesFiles;
9+
use function Tempest\root_path;
10+
use function Tempest\Support\str;
11+
12+
final class ConsoleInstaller implements Installer
13+
{
14+
use PublishesFiles;
15+
16+
public function getName(): string
17+
{
18+
return 'console';
19+
}
20+
21+
public function install(): void
22+
{
23+
$this->installMainNamespace();
24+
25+
$this->publish(
26+
source: __DIR__ . '/tempest',
27+
destination: root_path('tempest'),
28+
callback: function (string $source, string $destination): void {
29+
if (PHP_OS_FAMILY !== 'Windows') {
30+
/** @phpstan-ignore-next-line */
31+
exec("chmod +x {$destination}");
32+
}
33+
},
34+
);
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();
73+
74+
$this->success("Project namespace created: {$appPath}");
75+
}
76+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
use Tempest\Console\ConsoleApplication;
5+
6+
require_once getcwd() . '/vendor/autoload.php';
7+
8+
ConsoleApplication::boot()->run();
9+
10+
exit;
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Integration\Console\Installer;
6+
7+
use Tempest\Core\ComposerNamespace;
8+
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;
9+
10+
/**
11+
* @internal
12+
*/
13+
final class ConsoleInstallerTest extends FrameworkIntegrationTestCase
14+
{
15+
protected function setUp(): void
16+
{
17+
parent::setUp();
18+
19+
$this->installer
20+
->configure(
21+
__DIR__ . '/install',
22+
new ComposerNamespace('App\\', __DIR__ . '/install/App'),
23+
)
24+
->setRoot(__DIR__ . '/install');
25+
}
26+
27+
protected function tearDown(): void
28+
{
29+
$this->installer->clean();
30+
31+
parent::tearDown();
32+
}
33+
34+
public function test_console_installer(): void
35+
{
36+
$this->console
37+
->call('install console -f');
38+
39+
$this->installer
40+
->assertFileExists('tempest');
41+
}
42+
}

0 commit comments

Comments
 (0)