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
2 changes: 1 addition & 1 deletion CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ composer install
**`StackCommand`** — Vue/React selection. Takes `--path`; `basePath`/`jsRoot` resolve from `--path` (or an injected constructor path in tests) at `handle()` time. Supports `--dev` (contributor mode — config only, keeps both dirs, uses git skip-worktree) and `--reset`.

### Drivers
`src/Environments/`: `Environment` (abstract base + `make()` factory), `DockerEnvironment`, `NativeEnvironment`. Add a driver (Valet, Herd, Sail) by extending `Environment` and adding a `match` arm to `Environment::make()`.
`src/Environments/`: `Environment` (abstract base + `make()` factory), `DockerEnvironment`, `NativeEnvironment`, and `LinkedSiteEnvironment` (native install, then `<tool> link` / `<tool> secure` at `<site>.test`) with its two drivers `HerdEnvironment` and `LerdEnvironment`, which only name the tool. Add a driver by extending `Environment` (or `LinkedSiteEnvironment` for another tool with `link`/`secure`) and adding a `match` arm to `Environment::make()`.

## Testing

Expand Down
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ saucebase new my-app
- **Docker driver:** [Docker Desktop](https://www.docker.com/products/docker-desktop/) or Docker Engine with the Compose plugin; [mkcert](https://github.com/FiloSottile/mkcert) only if you enable SSL
- **Native driver:** a local PHP capable of running the app; the CLI points you to [php.new](https://php.new) if anything is missing
- **Herd driver:** [Laravel Herd](https://herd.laravel.com); offered in the prompt only when the `herd` command is found
- **Lerd driver:** [Lerd](https://github.com/lerd-env/lerd); offered in the prompt only when the `lerd` command is found

The CLI **checks and instructs** for prerequisites — it never installs them for you.

Expand Down Expand Up @@ -48,9 +49,10 @@ Runs a self-update check, then collects every choice upfront (driver → SSL if

| Option | Description |
|--------|-------------|
| `--driver=docker\|native\|herd` | Environment driver (skips the prompt) |
| `--driver=docker\|native\|herd\|lerd` | Environment driver (skips the prompt) |
| `--stack=vue\|react` | Frontend stack (skips the prompt) |
| `--ssl=yes\|no` | Enable HTTPS via mkcert, Docker only (skips the prompt) |
| `--workers=yes\|no` | Lerd only: let Lerd run the queue worker and scheduler (skips the prompt; default yes, also when non-interactive) |
| `--modules=auth,billing` | Install specific modules by name, or `none` to skip modules |
| `--all-modules` | Install every module compatible with the stack |
| `--using=vendor/skeleton` | Use a different starter kit instead of `saucebase/saucebase` |
Expand Down Expand Up @@ -90,6 +92,8 @@ Takes an optional `stack` argument (`vue`/`react`) and the same `--driver`, `--s

**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`.

**Lerd driver** — the same as Herd, with `lerd link` and `lerd secure`. It doesn't run `lerd env`, so the app keeps the SQLite database the native install set up. To move it to Lerd's MySQL or Postgres, run `lerd env` in the app. Unless `--workers=no`, it then runs `lerd queue:start` and `lerd schedule:start`, so jobs and scheduled tasks run in the background. The queue worker keeps the code it started with: after you edit a job, run `php artisan queue:restart`. A worker that doesn't start is listed as a next step; the install still completes. The domain follows Lerd's own config (`~/.config/lerd/config.yaml`): its `dns.tld`, or `http://<name>.localhost` without HTTPS when Lerd's DNS is off (`lerd dns:disable`).

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 Expand Up @@ -125,7 +129,7 @@ Copies the framework's config (`package.json`, `vite.config.js`, `tsconfig.json`

### SQLite: "database is locked"

The native and Herd drivers use SQLite. By default it doesn't wait when another request or a queue worker is writing, and the second write fails. In `config/database.php`, under `sqlite`, set:
The native, Herd and Lerd drivers use SQLite. By default it doesn't wait when another request or a queue worker is writing, and the second write fails. In `config/database.php`, under `sqlite`, set:

```php
'busy_timeout' => 5000,
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"laravel/installer": "^5.32",
"laravel/prompts": "^0.3.26",
"symfony/process": "^8.1.9",
"illuminate/process": "^13.38"
"illuminate/process": "^13.38",
"symfony/yaml": "^8.0"
},
"require-dev": {
"phpunit/phpunit": "^13.4.3",
Expand Down
11 changes: 8 additions & 3 deletions src/Console/Commands/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Saucebase\Installer\Console\Commands\Concerns\DisplaysBanner;
use Saucebase\Installer\Environments\Environment;
use Saucebase\Installer\Environments\HerdEnvironment;
use Saucebase\Installer\Environments\LerdEnvironment;
use Saucebase\Installer\ModuleRegistry;

use function Laravel\Prompts\callout;
Expand All @@ -25,9 +26,10 @@ class InstallCommand extends Command
protected $signature = 'install
{stack? : The frontend stack to install (vue or react)}
{--path= : The Saucebase application directory (defaults to the current directory)}
{--driver= : Environment driver (docker, native, herd) — prompted if omitted}
{--driver= : Environment driver (docker, native, herd, lerd) — prompted if omitted}
{--ssl= : Enable HTTPS with mkcert for docker (yes/no) — prompted if omitted}
{--domain= : Hostname the app is served on (e.g. myapp.test) — prompted if omitted}
{--workers= : Let Lerd run the queue worker and scheduler (yes/no), prompted if omitted}
{--app-name= : What the app is called (defaults to its directory name)}
{--demo : Load demo content and demo users}
{--template= : A Saucebase template (vendor/name): installs it, the modules it requires, and its starting data}
Expand Down Expand Up @@ -152,6 +154,9 @@ protected function resolveDriver(): Environment
...((new HerdEnvironment)->hasCommand('herd')
? ['herd' => 'Laravel Herd - served at https://<name>.test']
: []),
...((new LerdEnvironment)->hasCommand('lerd')
? ['lerd' => 'Lerd - served at '.(new LerdEnvironment)->siteUrl('<name>')]
: []),
],
default: 'docker',
);
Expand Down Expand Up @@ -227,7 +232,7 @@ public static function normalizeDomain(string $value): ?string
}

/**
* A custom domain resolves only via /etc/hosts, Herd, Valet or dnsmasq. Say so
* A custom domain resolves only via /etc/hosts, Herd, Lerd, Valet or dnsmasq. Say so
* rather than letting the browser fail after a successful install.
*/
protected function warnWhenDomainDoesNotResolve(string $domain): void
Expand Down Expand Up @@ -870,7 +875,7 @@ public function resumeCommand(array $resumeOptions = []): string
$command .= ' '.$this->selectedStack;
}

// Herd's choice is only on the command line; Docker's comes back resolved.
// Herd's and Lerd's choice is only on the command line; Docker's comes back resolved.
if (! isset($resumeOptions['--ssl']) && ($ssl = $this->optionOrNull('ssl'))) {
$resumeOptions['--ssl'] = $ssl;
}
Expand Down
80 changes: 65 additions & 15 deletions src/Console/Commands/NewCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use Saucebase\Installer\Console\Commands\Concerns\DisplaysBanner;
use Saucebase\Installer\Environments\Environment;
use Saucebase\Installer\Environments\HerdEnvironment;
use Saucebase\Installer\Environments\LerdEnvironment;
use Saucebase\Installer\Environments\LinkedSiteEnvironment;
use Saucebase\Installer\ModuleRegistry;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Process\Process as SymfonyProcess;
Expand All @@ -24,12 +26,13 @@

protected $signature = 'new
{name? : The name of the new Saucebase application}
{--driver= : Environment driver (docker, native, herd) — prompted if omitted}
{--driver= : Environment driver (docker, native, herd, lerd) — prompted if omitted}
{--stack= : Frontend stack (vue or react) — prompted if omitted}
{--modules= : Comma-separated list of modules to enable, or "none"}
{--all-modules : Enable and migrate all available modules without prompting}
{--ssl= : Enable HTTPS with mkcert for docker (yes/no) — prompted if omitted}
{--domain= : Hostname the app is served on (e.g. myapp.test) — prompted if omitted}
{--workers= : Let Lerd run the queue worker and scheduler (yes/no), prompted if omitted}
{--app-name= : What the app is called (defaults to its directory name), prompted if omitted}
{--using= : A starter kit to start from, instead of a plain Laravel application}
{--template= : A Saucebase template (vendor/name): installs it, the modules it requires, and its starting data}
Expand Down Expand Up @@ -67,7 +70,7 @@
$ssl = $this->ssl($driver);

// Docker's certificates come from mkcert: checked here, before the project exists, so a
// missing one leaves nothing half-made. Herd secures with `herd secure` instead.
// missing one leaves nothing half-made. Herd and Lerd secure with their own `secure` instead.
if ($ssl === true && $driver->name() === 'docker' && ! $driver->hasCommand('mkcert')) {
$this->error('mkcert is required for SSL. Install it with: brew install mkcert');
$this->info('Official mkcert installation instructions: https://github.com/FiloSottile/mkcert');
Expand All @@ -76,6 +79,7 @@
}

$domain = $this->resolveDomain($name, $driver);
$workers = $this->workers($driver);

// A template brings its own framework, which the install reads once the files are there.
$stack = $this->option('stack') ?: ($this->option('using') ? null : select(
Expand All @@ -96,7 +100,9 @@
// 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);
$arguments = $this->installArguments(
$name, $driver, $stack, $ssl, $modules, $domain, $appName, $demo, $workers,

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

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Either split this list into multiple lines, aligned at column "12" or put all arguments on line "103".

See more on https://sonarcloud.io/project/issues?id=saucebase-dev_installer&issues=AaIth1WiJ29qgLF5InsH&open=AaIth1WiJ29qgLF5InsH&pullRequest=40
);
$result = $this->call('install', $arguments);

if ($result === self::SUCCESS) {
Expand Down Expand Up @@ -137,36 +143,75 @@

protected function resolveDriver(string $name): Environment
{
// Offered only where it can run.
$options = $this->driverOptions(
$name,
herd: (new HerdEnvironment)->hasCommand('herd'),
lerd: (new LerdEnvironment)->hasCommand('lerd'),
);

$driver = $this->option('driver') ?? select(
label: 'How would you like to run Saucebase?',
// Offered only where it can run.
options: $this->driverOptions($name, herd: (new HerdEnvironment)->hasCommand('herd')),
default: $this->savedDriverPreference() ?? 'docker',
options: $options,
default: $this->defaultDriver($options),
);

return Environment::make($driver);
}

/**
* The last install's driver, while it is still on offer: Herd or Lerd may be gone since.
*
* @param array<string, string> $options
*/
protected function defaultDriver(array $options): string
{
$saved = $this->savedDriverPreference();

return $saved !== null && isset($options[$saved]) ? $saved : 'docker';
}

/** @return array<string, string> */
protected function driverOptions(string $name, bool $herd): array
protected function driverOptions(string $name, bool $herd, bool $lerd): array
{
return [
'docker' => 'Docker - recommended for real projects: MySQL, Redis, Mailpit, HTTPS',
'native' => 'Native PHP - minimal setup, ideal for exploring',
...($herd ? ['herd' => 'Laravel Herd - served at https://'.Str::slug($name).'.test'] : []),
...($herd ? ['herd' => 'Laravel Herd - served at '.(new HerdEnvironment)->siteUrl(Str::slug($name))] : []),
...($lerd ? ['lerd' => 'Lerd - served at '.(new LerdEnvironment)->siteUrl(Str::slug($name))] : []),
];
}

/** Docker asks; Herd secures unless told not to; native serves plain HTTP. */
/** Docker asks; Herd and Lerd secure unless told not to; native serves plain HTTP. */
protected function ssl(Environment $driver): ?bool
{
return match ($driver->name()) {
'docker' => $this->resolveSsl($driver),
'herd' => $this->option('ssl') !== 'no',
return match (true) {
$driver->name() === 'docker' => $this->resolveSsl($driver),
$driver instanceof LinkedSiteEnvironment => $driver->canSecure() && $this->option('ssl') !== 'no',
default => null,
};
}

/** Lerd runs the queue and scheduler unless told not to; other drivers aren't asked. */
protected function workers(Environment $driver): ?bool
{
if (! $driver instanceof LerdEnvironment) {
return null;
}

$option = $this->option('workers');

return match (true) {
is_string($option) && $option !== '' => filter_var($option, FILTER_VALIDATE_BOOLEAN),
(bool) $this->option('force') => true,
default => confirm(
label: 'Let Lerd run the queue and scheduler?',
default: true,
hint: 'Jobs and scheduled tasks run in the background while you work.',
),
};
}

protected function resolveSsl(Environment $driver): bool
{
$option = $this->option('ssl');
Expand All @@ -192,8 +237,8 @@

$answer = match (true) {
is_string($option) && $option !== '' => $option,
// Herd serves every site at its own .test name.
$driver->name() === 'herd' => Str::slug($name).'.test',
// Herd and Lerd serve every site at its own name under their TLD.
$driver instanceof LinkedSiteEnvironment => Str::slug($name).'.'.$driver->tld(),
(bool) $this->option('force') => 'localhost',
default => text(
label: 'Which hostname will you use for this app?',
Expand Down Expand Up @@ -324,6 +369,7 @@
string $domain = 'localhost',
?string $appName = null,
bool $demo = false,
?bool $workers = null,
): array {
$arguments = array_filter(['stack' => $stack]) + [
'--path' => getcwd().'/'.$name,
Expand All @@ -336,6 +382,10 @@
$arguments['--ssl'] = $ssl ? 'yes' : 'no';
}

if ($workers !== null) {
$arguments['--workers'] = $workers ? 'yes' : 'no';
}

if ($appName !== null) {
$arguments['--app-name'] = $appName;
}
Expand Down Expand Up @@ -501,7 +551,7 @@

$driver = $config['driver'] ?? null;

return in_array($driver, ['docker', 'native'], true) ? $driver : null;
return is_string($driver) ? $driver : null;
}

protected function saveDriverPreference(string $driver): void
Expand Down
1 change: 1 addition & 0 deletions src/Environments/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public static function make(string $name): Environment
'docker' => new DockerEnvironment,
'native' => new NativeEnvironment,
'herd' => new HerdEnvironment,
'lerd' => new LerdEnvironment,
default => throw new \InvalidArgumentException("Unknown driver: {$name}"),
};
}
Expand Down
67 changes: 1 addition & 66 deletions src/Environments/HerdEnvironment.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@

namespace Saucebase\Installer\Environments;

use Illuminate\Support\Str;
use Saucebase\Installer\Console\Commands\InstallCommand;

/** The native install, served by Laravel Herd at `<site>.test` instead of `composer dev`. */
class HerdEnvironment extends NativeEnvironment
class HerdEnvironment extends LinkedSiteEnvironment
{
public function name(): string
{
Expand All @@ -17,65 +13,4 @@ public function label(): string
{
return 'Laravel Herd';
}

public function missingPrerequisites(): array
{
$missing = parent::missingPrerequisites();

if (! $this->commandExists('herd')) {
$missing[] = 'herd is not installed or not in PATH.';
}

return $missing;
}

protected function boot(InstallCommand $command): int
{
if (($result = parent::boot($command)) !== InstallCommand::SUCCESS) {
return $result;
}

$site = $this->site($command);

if (! $command->runProcess(['herd', 'link', $site])) {
return $this->fail('Linking the site to Herd');
}

if ($this->secure($command) && ! $command->runProcess(['herd', 'secure', $site])) {
return $this->fail('Securing the site with Herd');
}

$env = (string) file_get_contents($command->path('.env'));
$env = InstallCommand::setEnvLine($env, 'APP_HOST', "{$site}.test");
$scheme = $this->secure($command) ? 'https' : 'http';
$env = InstallCommand::setEnvLine($env, 'APP_URL', "{$scheme}://{$site}.test");
file_put_contents($command->path('.env'), $env);

return InstallCommand::SUCCESS;
}

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`',
'Herd already serves your app: `'.($this->readEnvValue($command, 'APP_URL') ?? '').'`',
];
}

/** HTTPS unless `--ssl=no`: Herd trusts its own certificates, so there is nothing to install. */
private function secure(InstallCommand $command): bool
{
return $command->optionOrNull('ssl') !== 'no';
}

/** `shop.test` from `--domain`, otherwise the directory name. */
private function site(InstallCommand $command): string
{
$domain = $command->getDomain();

return str_ends_with($domain, '.test')
? substr($domain, 0, -5)
: Str::slug(basename($command->targetPath()));
}
}
Loading