Skip to content

Commit

Permalink
Implement interactive choice and Meilisearch
Browse files Browse the repository at this point in the history
  • Loading branch information
driesvints committed Feb 23, 2021
1 parent 3b27da8 commit ef2b3f9
Show file tree
Hide file tree
Showing 12 changed files with 250 additions and 121 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"require": {
"php": "^7.3|^8.0",
"illuminate/contracts": "^8.0|^9.0",
"illuminate/console": "^8.0|^9.0",
"illuminate/support": "^8.0|^9.0"
},
"bin": [
Expand Down
107 changes: 107 additions & 0 deletions src/Console/InstallCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

namespace Laravel\Sail\Console;

use Illuminate\Console\Command;

class InstallCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'sail:install';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Install Laravel Sail\'s default Docker Compose file';

/**
* Execute the console command.
*
* @return void
*/
public function handle()
{
$services = $this->choice('Which services would you like to install?', [
'mysql',
'pgsql',
'redis',
'selenium',
'mailhog',
'meilisearch',
], 0, null, true);

$this->buildDockerCompose($services);
$this->replaceEnvVariables($services);

$this->info('Sail scaffolding installed successfully.');
}

/**
* Build the Docker Compose file.
*
* @param array $services
* @return void
*/
protected function buildDockerCompose(array $services)
{
$depends = collect($services)
->filter(function ($service) {
return in_array($service, ['mysql', 'pgsql', 'redis', 'selenium']);
})->map(function ($service) {
return " - {$service}";
})->whenNotEmpty(function ($collection) {
return $collection->prepend('depends_on:');
})->implode("\n");

$stubs = rtrim(collect($services)->map(function ($service) {
return file_get_contents(__DIR__ . "/../../stubs/{$service}.stub");
})->implode(''));

$volumes = collect($services)
->filter(function ($service) {
return in_array($service, ['mysql', 'pgsql', 'redis', 'meilisearch']);
})->map(function ($service) {
return " sail{$service}:\n driver: local";
})->whenNotEmpty(function ($collection) {
return $collection->prepend('volumes:');
})->implode("\n");

$dockerCompose = file_get_contents(__DIR__ . '/../../stubs/docker-compose.stub');

$dockerCompose = str_replace('{{depends}}', $depends, $dockerCompose);
$dockerCompose = str_replace('{{services}}', $stubs, $dockerCompose);
$dockerCompose = str_replace('{{volumes}}', $volumes, $dockerCompose);

file_put_contents($this->laravel->basePath('docker-compose.yml'), $dockerCompose);
}

/**
* Replace the Host environment variables in the app's .env file.
*
* @param array $services
* @return void
*/
protected function replaceEnvVariables(array $services)
{
$environment = file_get_contents($this->laravel->basePath('.env'));

$host = in_array('pgsql', $services) ? 'pgsql' : 'mysql';

$environment = str_replace('DB_HOST=127.0.0.1', "DB_HOST=$host", $environment);
$environment = str_replace('MEMCACHED_HOST=127.0.0.1', 'MEMCACHED_HOST=memcached', $environment);
$environment = str_replace('REDIS_HOST=127.0.0.1', 'REDIS_HOST=redis', $environment);

if (in_array('meilisearch', $services)) {
$environment .= "\nSCOUT_DRIVER=meilisearch";
$environment .= "\nMEILISEARCH_HOST=http://meilisearch:7700\n";
}

file_put_contents($this->laravel->basePath('.env'), $environment);
}
}
38 changes: 38 additions & 0 deletions src/Console/PublishCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Laravel\Sail\Console;

use Illuminate\Console\Command;

class PublishCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'sail:publish';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Publish the Laravel Sail Docker files';

/**
* Execute the console command.
*
* @return void
*/
public function handle()
{
$this->call('vendor:publish', ['--tag' => 'sail']);

file_put_contents($this->laravel->basePath('docker-compose.yml'), str_replace(
'./vendor/laravel/sail/runtimes/8.0',
'./docker/8.0',
file_get_contents($this->laravel->basePath('docker-compose.yml'))
));
}
}
50 changes: 17 additions & 33 deletions src/SailServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
namespace Laravel\Sail;

use Illuminate\Contracts\Support\DeferrableProvider;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\ServiceProvider;
use Laravel\Sail\Console\InstallCommand;
use Laravel\Sail\Console\PublishCommand;

class SailServiceProvider extends ServiceProvider implements DeferrableProvider
{
Expand All @@ -15,10 +16,8 @@ class SailServiceProvider extends ServiceProvider implements DeferrableProvider
*/
public function boot()
{
if ($this->app->runningInConsole()) {
$this->registerCommands();
$this->configurePublishing();
}
$this->registerCommands();
$this->configurePublishing();
}

/**
Expand All @@ -28,29 +27,12 @@ public function boot()
*/
protected function registerCommands()
{
Artisan::command('sail:install', function () {
copy(__DIR__.'/../stubs/docker-compose.yml', base_path('docker-compose.yml'));

$environment = file_get_contents(base_path('.env'));

$environment = str_replace('DB_HOST=127.0.0.1', 'DB_HOST=mysql', $environment);
$environment = str_replace('MEMCACHED_HOST=127.0.0.1', 'MEMCACHED_HOST=memcached', $environment);
$environment = str_replace('REDIS_HOST=127.0.0.1', 'REDIS_HOST=redis', $environment);

file_put_contents(base_path('.env'), $environment);

$this->info('Sail scaffolding installed successfully.');
})->purpose('Install Laravel Sail\'s default Docker Compose file');

Artisan::command('sail:publish', function () {
$this->call('vendor:publish', ['--tag' => 'sail']);

file_put_contents(base_path('docker-compose.yml'), str_replace(
'./vendor/laravel/sail/runtimes/8.0',
'./docker/8.0',
file_get_contents(base_path('docker-compose.yml'))
));
})->purpose('Publish the Laravel Sail Docker files');
if ($this->app->runningInConsole()) {
$this->commands([
InstallCommand::class,
PublishCommand::class,
]);
}
}

/**
Expand All @@ -60,9 +42,11 @@ protected function registerCommands()
*/
protected function configurePublishing()
{
$this->publishes([
__DIR__.'/../runtimes' => base_path('docker'),
], 'sail');
if ($this->app->runningInConsole()) {
$this->publishes([
__DIR__ . '/../runtimes' => $this->app->basePath('docker'),
], 'sail');
}
}

/**
Expand All @@ -73,8 +57,8 @@ protected function configurePublishing()
public function provides()
{
return [
'sail.install-command',
'sail.publish-command',
InstallCommand::class,
PublishCommand::class,
];
}
}
25 changes: 25 additions & 0 deletions stubs/docker-compose.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# For more information: https://laravel.com/docs/sail
version: '3'
services:
laravel.test:
build:
context: ./vendor/laravel/sail/runtimes/8.0
dockerfile: Dockerfile
args:
WWWGROUP: '${WWWGROUP}'
image: sail-8.0/app
ports:
- '${APP_PORT:-80}:80'
environment:
WWWUSER: '${WWWUSER}'
LARAVEL_SAIL: 1
volumes:
- '.:/var/www/html'
networks:
- sail
{{depends}}
{{services}}
networks:
sail:
driver: bridge
{{volumes}}
88 changes: 0 additions & 88 deletions stubs/docker-compose.yml

This file was deleted.

7 changes: 7 additions & 0 deletions stubs/mailhog.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
mailhog:
image: 'mailhog/mailhog:latest'
ports:
- '${FORWARD_MAILHOG_PORT:-1025}:1025'
- '${FORWARD_MAILHOG_DASHBOARD_PORT:-8025}:8025'
networks:
- sail
8 changes: 8 additions & 0 deletions stubs/meilisearch.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
meilisearch:
image: 'getmeili/meilisearch:latest'
ports:
- '${FORWARD_MEILISEARCH_PORT:-7700}:7700'
volumes:
- 'sailmeilisearch:/data.ms'
networks:
- sail
16 changes: 16 additions & 0 deletions stubs/mysql.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
mysql:
image: 'mysql:8.0'
ports:
- '${FORWARD_DB_PORT:-3306}:3306'
environment:
MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
MYSQL_DATABASE: '${DB_DATABASE}'
MYSQL_USER: '${DB_USERNAME}'
MYSQL_PASSWORD: '${DB_PASSWORD}'
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
volumes:
- 'sailmysql:/var/lib/mysql'
networks:
- sail
healthcheck:
test: ["CMD", "mysqladmin", "ping"]

0 comments on commit ef2b3f9

Please sign in to comment.