Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 28 additions & 9 deletions src/Console/Commands/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Saucebase\Installer\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Console\View\TaskResult;
use Illuminate\Support\Facades\Process;
use Illuminate\Support\Str;
use Laravel\Prompts\Elements\Element;
Expand Down Expand Up @@ -318,7 +319,7 @@ public function install(): int
$this->generateApplicationKey();

$installed = false;
$this->components->task('Installing Saucebase', function () use (&$installed) {
$this->task('Installing Saucebase', function () use (&$installed) {
return $installed = $this->installCore();
});

Expand Down Expand Up @@ -351,8 +352,8 @@ protected function handleCIInstallation(): int
$envOk = $this->ensureEnvFile();
$keyOk = $this->envHasAppKey();

$this->components->task('Verifying .env', fn () => $envOk);
$this->components->task('Verifying app key', fn () => $keyOk);
$this->task('Verifying .env', fn () => $envOk);
$this->task('Verifying app key', fn () => $keyOk);

if (! $envOk || ! $keyOk) {
return self::FAILURE;
Expand Down Expand Up @@ -540,7 +541,7 @@ public function envHasAppKey(): bool

protected function generateApplicationKey(): void
{
$this->components->task('Generating application key', function () {
$this->task('Generating application key', function () {
if ($this->envHasAppKey()) {
return true;
}
Expand All @@ -556,7 +557,7 @@ protected function setupDatabase(): bool
$command = $fresh ? 'migrate:fresh' : 'migrate';
$ok = false;

$this->components->task($label, function () use ($command, &$ok) {
$this->task($label, function () use ($command, &$ok) {
return $ok = $this->runArtisan([$command, '--seed', '--force'], 300);
});

Expand Down Expand Up @@ -620,9 +621,11 @@ public function doInstallModules(array $selected, ?\Closure $run = null): bool
$this->newLine();

$ok = false;
$this->components->task('Installing modules', function () use ($selected, $run, &$ok) {
$this->task('Installing modules', function () use ($selected, $run, &$ok) {
return $ok = $run([
'php', 'artisan', 'saucebase:module-install',
'php',
'artisan',
'saucebase:module-install',
...array_map(fn (string $package) => Str::after($package, '/'), $selected),
'--no-interaction',
...($this->option('demo') ? ['--demo'] : []),
Expand Down Expand Up @@ -677,12 +680,28 @@ protected function resolveModuleSelection(array $available): array

protected function createStorageLink(): void
{
$this->components->task('Creating storage link', fn () => $this->runArtisan(['storage:link']));
$this->task('Creating storage link', fn () => $this->runArtisan(['storage:link']));
}

protected function clearCaches(): void
{
$this->components->task('Clearing caches', fn () => $this->runArtisan(['optimize:clear']));
$this->task('Clearing caches', fn () => $this->runArtisan(['optimize:clear']));
}

/**
* A progress line for one step. Laravel shows FAIL only for its own failure value
* and DONE for anything else, `false` included, so a failed step looked done.
*/
protected function task(string $label, callable $step): bool
{
$ok = false;
$this->components->task($label, function () use ($step, &$ok) {
return ($ok = (bool) $step())
? TaskResult::Success->value
: TaskResult::Failure->value;
});

return $ok;
}

protected function isCI(): bool
Expand Down
3 changes: 2 additions & 1 deletion src/Console/Commands/NewCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ public function handle(): int
return self::FAILURE;
}

$this->line(' Setup continues below - see the final summary for the real next steps.');
// Laravel's installer has just printed its own next steps; ours come at the end.
$this->line(' Laravel is ready. Now setting up Saucebase…');

$arguments = $this->installArguments($name, $driver, $stack, $ssl, $modules, $domain, $appName, $demo);
$result = $this->call('install', $arguments);
Expand Down
11 changes: 8 additions & 3 deletions src/Environments/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,14 @@
{
$password = $command->optionOrNull('demo') ? $this->readEnvValue($command, 'DEMO_PASSWORD') : null;

return $password === null ? [] : [
"Sign in to any demo account with the password `{$password}`. It is DEMO_PASSWORD in .env, used when demo data is seeded",
];
if ($password === null) {
return [];
}

// Auth's demo data has the one account that sees everything.
return [is_dir($command->path('modules/auth'))
? "Sign in as `chef@saucebase.dev` (admin) with the password `{$password}`. Every demo account uses it; it is saved in .env as DEMO_PASSWORD"

Check warning on line 144 in src/Environments/Environment.php

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Split this 153 characters long line (which is greater than 120 authorized).

See more on https://sonarcloud.io/project/issues?id=saucebase-dev_installer&issues=AaFzq9doMR3EMC2W5xEn&open=AaFzq9doMR3EMC2W5xEn&pullRequest=35
: "Demo accounts use the password `{$password}`, saved in .env as DEMO_PASSWORD"];
}

/** Hook: perform driver-specific steps before the module prompt. Return an exit code to abort, null to continue. */
Expand Down
2 changes: 1 addition & 1 deletion src/Environments/HerdEnvironment.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ protected function nextSteps(InstallCommand $command): array
return [
...($this->frontendBuilt ? [] : ['Install frontend dependencies: `npm install`']),
$this->frontendBuilt ? 'For live reload while you edit: `npm run dev`' : 'Start Vite: `npm run dev`',
'Open your app: `'.($this->readEnvValue($command, 'APP_URL') ?? '').'`',
'Herd already serves your app: `'.($this->readEnvValue($command, 'APP_URL') ?? '').'`',
];
}

Expand Down
10 changes: 10 additions & 0 deletions tests/Feature/Environments/EnvironmentHelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,16 @@ public function test_a_demo_install_tells_the_user_the_demo_password(): void
$this->assertStringContainsString('`s3cret`', $steps[0]);
}

public function test_with_auth_it_names_the_admin_account_to_sign_in_with(): void
{
file_put_contents($this->tmp.'/.env', "DEMO_PASSWORD=s3cret\n");
mkdir($this->tmp.'/modules/auth', recursive: true);

$steps = $this->environment()->exposedDemoStep($this->command(demo: true));

$this->assertSame(['Sign in as `chef@saucebase.dev` (admin) with the password `s3cret`. Every demo account uses it; it is saved in .env as DEMO_PASSWORD'], $steps);
}

public function test_without_demo_content_no_password_is_shown(): void
{
file_put_contents($this->tmp.'/.env', "DEMO_PASSWORD=s3cret\n");
Expand Down
16 changes: 16 additions & 0 deletions tests/Feature/Environments/HerdEnvironmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,22 @@ public function test_it_links_and_secures_the_app_after_installing_it(): void
$this->assertStringContainsString('APP_HOST=shop.test', (string) file_get_contents($this->app.'/.env'));
}

public function test_the_summary_says_herd_already_serves_the_app(): void
{
$command = $this->command(installs: true, domain: 'shop.test');
$env = new class extends HerdEnvironment
{
/** @return string[] */
public function steps(InstallCommand $command): array
{
return $this->nextSteps($command);
}
};
$env->run($command);

$this->assertContains('Herd already serves your app: `https://shop.test`', $env->steps($command));
}

public function test_without_ssl_it_only_links_and_serves_over_http(): void
{
$command = $this->command(installs: true, ssl: 'no');
Expand Down
54 changes: 54 additions & 0 deletions tests/Unit/TaskResultTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Saucebase\Installer\Tests\Unit;

use Illuminate\Console\OutputStyle;
use Illuminate\Console\View\Components\Factory;
use Saucebase\Installer\Console\Commands\InstallCommand;
use Saucebase\Installer\Tests\TestCase;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;

/** Laravel's task line shows DONE for anything but its own failure value, `false` included. */
class TaskResultTest extends TestCase
{
public function test_a_step_that_fails_is_shown_as_failed(): void
{
[$command, $output] = $this->command();

$this->assertFalse($command->step(fn () => false));
$this->assertStringContainsString('FAIL', $output->fetch());
}

public function test_a_step_that_succeeds_is_shown_as_done(): void
{
[$command, $output] = $this->command();

$this->assertTrue($command->step(fn () => true));
$this->assertStringContainsString('DONE', $output->fetch());
}

/** @return array{0: object, 1: BufferedOutput} */
private function command(): array
{
$output = new BufferedOutput;
$style = new OutputStyle(new ArrayInput([]), $output);

$command = new class($style) extends InstallCommand
{
public function __construct(OutputStyle $style)
{
parent::__construct();
$this->output = $style;
$this->components = new Factory($style);
}

public function step(callable $task): bool
{
return $this->task('Step', $task);
}
};

return [$command, $output];
}
}
Loading