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
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ composer global require saucebase/installer

Make sure Composer's global bin directory is on your `PATH` (`composer global config bin-dir --absolute`).

To upgrade, run `composer global update saucebase/installer`. Running `require` again keeps the version you have. `saucebase new` also updates the installer itself when a newer release exists, then carries on with it.

## Commands

### `saucebase new <name>`
Expand Down Expand Up @@ -86,7 +88,9 @@ Takes an optional `stack` argument (`vue`/`react`) and the same `--driver`, `--s

**Native driver** — copies `.env.example` → `.env`, generates `APP_KEY`, migrates and seeds, wires the frontend, installs modules, and links storage.

**Herd driver** — the native install, then `herd link` and (unless `--ssl=no`) `herd secure`, so the app is served at `https://<name>.test`. The name comes from a `.test` `--domain`, otherwise the directory.
**Herd driver** — the native install, then `herd link` and (unless `--ssl=no`) `herd secure`, so the app is served at `https://<name>.test`. The name comes from a `.test` `--domain`, otherwise the directory. With `npm run dev` running, Vite renders pages on the server. With built assets only, Herd doesn't start the renderer, so pages render in the browser until you run `php artisan inertia:start-ssr`.

Every driver ends by building the frontend (`npm install && npm run build`) when npm is available, then checks the app boots before reporting the install complete.

---

Expand Down
8 changes: 6 additions & 2 deletions src/Console/Commands/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Saucebase\Installer\Environments\Environment;
use Saucebase\Installer\Environments\HerdEnvironment;
use Saucebase\Installer\ModuleRegistry;
use Symfony\Component\Console\Output\OutputInterface;

use function Laravel\Prompts\callout;
use function Laravel\Prompts\select;
Expand Down Expand Up @@ -814,7 +815,8 @@ public function applyModulePatches(array $modules): bool
}

if ($this->gitApply($patch)) {
$this->info('Applied patch: '.basename($patch));
// Routine wiring, so only with -v. Named by module: several ship a user.patch.
$this->info("Applied patch: {$name}/".basename($patch), OutputInterface::VERBOSITY_VERBOSE);

continue;
}
Expand Down Expand Up @@ -909,7 +911,9 @@ protected function successCalloutContent(array $steps): array
return array_filter([
$steps ? 'You can start your local development using:' : null,
$steps ? Element::numberedList(array_values($steps)) : null,
'Learn more: '.Element::link('https://github.com/saucebase-dev/saucebase'),
// Plain: a hyperlink's escape codes count towards the box width and push its
// border out, and terminals link a bare URL by themselves.
'Learn more: https://github.com/saucebase-dev/saucebase',
]);
}

Expand Down
57 changes: 52 additions & 5 deletions src/Console/Commands/NewCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Process;
use Illuminate\Support\Str;
use Saucebase\Installer\Console\Application;
use Saucebase\Installer\Console\Commands\Concerns\DisplaysBanner;
use Saucebase\Installer\Environments\Environment;
use Saucebase\Installer\Environments\HerdEnvironment;
use Saucebase\Installer\ModuleRegistry;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Process\Process as SymfonyProcess;

use function Laravel\Prompts\confirm;
use function Laravel\Prompts\select;
Expand Down Expand Up @@ -355,13 +357,18 @@
}

/**
* Warn when a newer installer exists; block when a major version behind.
* Create apps with the newest installer: an older one updates itself, then runs this
* command again on the new code. Declined or failed, it warns, or stops when a major
* version behind.
*
* @return int|null An exit code to stop with, null to carry on
*/
protected function checkForUpdates(): ?int
{
$current = ltrim(Application::version(), 'v');
$current = $this->currentVersion();

if ($current === '' || str_contains($current, 'dev')) {
// Set for the run after an update: never update twice, whatever Packagist says.
if ($current === '' || str_contains($current, 'dev') || getenv('SAUCEBASE_UPDATED') === '1') {
return null;
}

Expand All @@ -371,18 +378,58 @@
return null;
}

$wantsUpdate = ! $this->input->isInteractive() || confirm(
label: "A newer Saucebase installer is available ({$current} → {$latest}). Update it first?",
default: true,
);

if ($wantsUpdate && $this->updateInstaller($latest)) {
$this->info("Updated the installer to {$latest}. Continuing with it...");

return $this->runAgain();
}

if ((int) explode('.', $latest)[0] > (int) explode('.', $current)[0]) {
$this->error("Your installer ({$current}) is a major version behind the latest release ({$latest}) and may not support current Saucebase applications.");
$this->line('Update it first: <fg=yellow>composer global update saucebase/installer</>');
$this->line("Update it first: <fg=yellow>composer global require saucebase/installer:^{$latest}</>");

return self::FAILURE;
}

$this->components->warn("A new version of the Saucebase installer is available ({$current} → {$latest}). Update with: composer global update saucebase/installer");
$this->warn("Carrying on with installer {$current}. Update later with: composer global require saucebase/installer:^{$latest}");

Check warning on line 399 in src/Console/Commands/NewCommand.php

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

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

See more on https://sonarcloud.io/project/issues?id=saucebase-dev_installer&issues=AaFuMXNXdA8JAur1fNd_&open=AaFuMXNXdA8JAur1fNd_&pullRequest=33

return null;
}

protected function currentVersion(): string
{
return ltrim(Application::version(), 'v');
}

/** Requires the new version itself: `update` keeps the global constraint, so it never crosses a major. */
protected function updateInstaller(string $latest): bool
{
return Process::timeout(300)
->run(['composer', 'global', 'require', "saucebase/installer:^{$latest}", '--with-all-dependencies', '--no-interaction'])

Check warning on line 413 in src/Console/Commands/NewCommand.php

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

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

See more on https://sonarcloud.io/project/issues?id=saucebase-dev_installer&issues=AaFuMXNXdA8JAur1fNeA&open=AaFuMXNXdA8JAur1fNeA&pullRequest=33
->successful();
}

/** The same command on the updated code, attached to this terminal so its prompts still work. */
protected function runAgain(): int
{
return Process::forever()
->env(['SAUCEBASE_UPDATED' => '1'])
->tty(SymfonyProcess::isTtySupported())
->run([PHP_BINARY, ...$this->rerunArguments()], fn ($_type, $buffer) => $this->output->write($buffer))
->exitCode() ?? self::FAILURE;
}

/** @return string[] The script and arguments this process was started with. */
protected function rerunArguments(): array
{
return $_SERVER['argv'] ?? [];
}

/**
* The newest published release of the installer, or null when offline.
*
Expand Down
7 changes: 6 additions & 1 deletion src/Console/Commands/StackCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -312,10 +312,15 @@ private function skipGeneratedFiles(string $framework): void
}

foreach ($this->generatedFiles($framework) as $file) {
// A generated file git doesn't track (an entry point, gitignored) has nothing to hide.
if (! Process::run(['git', '-C', $this->basePath, 'ls-files', '--error-unmatch', $file])->successful()) {
continue;
}

$result = Process::run(['git', '-C', $this->basePath, 'update-index', '--skip-worktree', $file]);

if ($result->exitCode() !== 0) {
$this->warn("Could not skip-worktree {$file} (not in git index).");
$this->warn("Could not skip-worktree {$file}: ".trim($result->errorOutput()));
}
}
}
Expand Down
8 changes: 7 additions & 1 deletion src/Environments/DockerEnvironment.php
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@ protected function nextSteps(InstallCommand $command): array
$mailpit = $this->readEnvValue($command, 'FORWARD_MAILPIT_DASHBOARD_PORT') ?? '8025';

return [
'Compile frontend assets: `npm install && npm run dev`',
...($this->frontendBuilt ? [] : ['Compile frontend assets: `npm install && npm run dev`']),
'Open your app: `'.$appUrl.'`',
'Email testing (Mailpit): `http://localhost:'.$mailpit.'`',
];
Expand All @@ -785,6 +785,12 @@ protected function createStorageLink(InstallCommand $command): void
$this->execInContainer($command, ['php', 'artisan', 'storage:link']);
}

/** Inside the app container: on the host, `.env` points at hostnames only Docker resolves. */
protected function artisan(InstallCommand $command, array $args): bool
{
return $this->execInContainer($command, ['php', 'artisan', ...$args], quiet: true);
}

protected function clearCaches(InstallCommand $command): void
{
$command->info('Clearing caches...');
Expand Down
40 changes: 40 additions & 0 deletions src/Environments/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ abstract class Environment
/** Label of the step that failed, for the failure callout. Set via fail(). */
protected ?string $failedStep = null;

/** Whether buildFrontend() left built assets, so the next steps skip the build. */
protected bool $frontendBuilt = false;

public static function make(string $name): Environment
{
return match ($name) {
Expand Down Expand Up @@ -47,6 +50,16 @@ public function run(InstallCommand $command): int

$result = $this->boot($command);

if ($result === InstallCommand::SUCCESS) {
$this->frontendBuilt = $this->buildFrontend($command);

// A module that doesn't fit the installed core installs fine and then breaks
// every request, so "complete" waits until the app has actually booted.
if (! $this->artisan($command, ['about', '--only=environment'])) {
$result = $this->fail('Checking the app boots');
}
}

if ($result === InstallCommand::SUCCESS) {
$command->displaySuccess(array_merge($this->cdStep($command), $this->nextSteps($command), $this->demoStep($command)));
} else {
Expand All @@ -58,6 +71,33 @@ public function run(InstallCommand $command): int
return $result;
}

/**
* Builds the assets so the app's URL works before anyone runs Vite. Node isn't a
* prerequisite, so without npm, or when the build fails, it stays a next step.
*/
protected function buildFrontend(InstallCommand $command): bool
{
if (! $this->commandExists('npm')) {
return false;
}

$command->info('Building the frontend...');

if ($command->runProcess(['npm', 'install'], 600) && $command->runProcess(['npm', 'run', 'build'], 600)) {
return true;
}

$command->warn('Building the frontend failed. Run `npm install && npm run build` in the app to see why.');

return false;
}

/** @param string[] $args */
protected function artisan(InstallCommand $command, array $args): bool
{
return $command->runArtisan($args);
}

/** Record which step failed, then return FAILURE for boot() to propagate. */
protected function fail(string $step): int
{
Expand Down
4 changes: 2 additions & 2 deletions src/Environments/HerdEnvironment.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ protected function boot(InstallCommand $command): int
protected function nextSteps(InstallCommand $command): array
{
return [
'Install frontend dependencies: `npm install`',
'Start Vite: `npm run dev`',
...($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') ?? '').'`',
];
}
Expand Down
2 changes: 1 addition & 1 deletion src/Environments/NativeEnvironment.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ protected function boot(InstallCommand $command): int
protected function nextSteps(InstallCommand $command): array
{
return [
'Install frontend dependencies: `npm install`',
...($this->frontendBuilt ? [] : ['Install frontend dependencies: `npm install`']),
'Start the dev server: `composer dev`',
'Open your app: `'.($this->readEnvValue($command, 'APP_URL') ?? 'http://localhost').'`',
];
Expand Down
22 changes: 18 additions & 4 deletions stubs/docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ services:
- QUEUE_CONNECTION=${QUEUE_CONNECTION:-redis}
- MAIL_HOST=${MAIL_HOST:-mailpit}
- MAIL_PORT=${MAIL_PORT:-1025}
- INERTIA_SSR_URL=http://ssr:13714
- XDEBUG_MODE=${XDEBUG_MODE:-debug}
- XDEBUG_CONFIG=${XDEBUG_CONFIG:-client_host=host.docker.internal client_port=9003}
depends_on:
Expand Down Expand Up @@ -53,7 +54,7 @@ services:
image: 'mysql:8.0'
restart: unless-stopped
ports:
- '${FORWARD_DB_PORT:-3306}:3306'
- '127.0.0.1:${FORWARD_DB_PORT:-3306}:3306'
environment:
MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
MYSQL_DATABASE: '${DB_DATABASE}'
Expand Down Expand Up @@ -85,7 +86,7 @@ services:
image: 'redis:alpine'
restart: unless-stopped
ports:
- '${FORWARD_REDIS_PORT:-6379}:6379'
- '127.0.0.1:${FORWARD_REDIS_PORT:-6379}:6379'
volumes:
- 'saucebase-redis:/data'
networks:
Expand All @@ -100,8 +101,8 @@ services:
image: 'axllent/mailpit:latest'
restart: unless-stopped
ports:
- '${FORWARD_MAILPIT_PORT:-1025}:1025'
- '${FORWARD_MAILPIT_DASHBOARD_PORT:-8025}:8025'
- '127.0.0.1:${FORWARD_MAILPIT_PORT:-1025}:1025'
- '127.0.0.1:${FORWARD_MAILPIT_DASHBOARD_PORT:-8025}:8025'
networks:
- saucebase
queue:
Expand Down Expand Up @@ -137,6 +138,19 @@ services:
condition: service_healthy
restart: unless-stopped

# The server-side renderer, so pages arrive rendered. Its bundle comes from
# `npm run build`; until the first build it restarts until the file exists.
ssr:
build:
context: .
dockerfile: ./docker/Dockerfile
command: node bootstrap/ssr/app.js
volumes:
- './:/var/www'
networks:
- saucebase
restart: unless-stopped

networks:
saucebase:
driver: bridge
Expand Down
Loading
Loading