Skip to content

Commit

Permalink
Migrate to castor
Browse files Browse the repository at this point in the history
  • Loading branch information
lyrixx committed Jun 9, 2024
1 parent 96e6480 commit 93126ce
Show file tree
Hide file tree
Showing 51 changed files with 3,403 additions and 107 deletions.
583 changes: 583 additions & 0 deletions .castor/docker.php

Large diffs are not rendered by default.

64 changes: 64 additions & 0 deletions .castor/qa.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace qa;

use Castor\Attribute\AsTask;

use function Castor\io;
use function Castor\variable;
use function docker\docker_compose_run;
use function docker\docker_exit_code;

#[AsTask(description: 'Runs all QA tasks')]
function all(): int
{
install();
$cs = cs();
$phpstan = phpstan();
// $phpunit = phpunit();

return max($cs, $phpstan/* , $phpunit */);
}

#[AsTask(description: 'Installs tooling')]
function install(): void
{
io()->title('Installing QA tooling');

docker_compose_run('composer install -o', workDir: '/var/www/tools/php-cs-fixer');
docker_compose_run('composer install -o', workDir: '/var/www/tools/phpstan');
}

#[AsTask(description: 'Runs PHPUnit', aliases: ['phpunit'])]
function phpunit(): int
{
return docker_exit_code('vendor/bin/simple-phpunit');
}

#[AsTask(description: 'Runs PHPStan', aliases: ['phpstan'])]
function phpstan(bool $generateBaseline = false): int
{
if (!is_dir(variable('root_dir') . '/tools/phpstan/vendor')) {
io()->error('PHPStan is not installed. Run `castor qa:install` first.');

return 1;
}

return docker_exit_code('phpstan' . ($generateBaseline ? ' -b' : ''), workDir: '/var/www');
}

#[AsTask(description: 'Fixes Coding Style', aliases: ['cs'])]
function cs(bool $dryRun = false): int
{
if (!is_dir(variable('root_dir') . '/tools/php-cs-fixer/vendor')) {
io()->error('PHP-CS-Fixer is not installed. Run `castor qa:install` first.');

return 1;
}

if ($dryRun) {
return docker_exit_code('php-cs-fixer fix --dry-run --diff', workDir: '/var/www');
}

return docker_exit_code('php-cs-fixer fix', workDir: '/var/www');
}
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ MESSENGER_TRANSPORT_DSN=doctrine://default?auto_setup=0
#
# DATABASE_URL="sqlite:///%kernel.project_dir%/var/data.db"
# DATABASE_URL="mysql://app:[email protected]:3306/app?serverVersion=8&charset=utf8mb4"
DATABASE_URL="postgresql://qotd:qotd@postgres:5432/qotd?serverVersion=15&charset=utf8"
DATABASE_URL="postgresql://app:app@postgres:5432/app?serverVersion=15&charset=utf8"
###< doctrine/doctrine-bundle ###

SLACK_SIGNING_SECRET=FIXME
Expand Down
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Force LF line ending (mandatory for Windows)
* text=auto eol=lf
64 changes: 23 additions & 41 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,57 +10,39 @@ permissions:
contents: read

jobs:
phpunit:
name: PHPUnit Test Suite
tests:
name: Run the tests suite
runs-on: ubuntu-latest
env:
UID: 1001
APP_ENV: 'test'
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Build and Run Docker Stack
run: docker-compose up -d

- name: Install Dependencies
run: docker-compose run --rm --user=app frontend composer install

- name: Install Front Dependencies
run: docker-compose run --rm --user=app frontend bin/console importmap:install

- name: Create Database
run: docker-compose run --rm --user=app frontend bin/db --env=test

- name: Load Fixtures
run: docker-compose run --rm --user=app frontend bin/console doctrine:fixtures:load --env=test --no-interaction
# Install official version of docker that correctly supports from-cache option in docker compose
- name: Set up Docker
uses: crazy-max/ghaction-setup-docker@v3
with:
set-host: true

- name: Execute tests
run: docker-compose run --rm --user=app frontend vendor/bin/simple-phpunit
# Docker socket path is different when using setup docker
- name: Set Docker Socket Host
run: echo "DOCKER_SOCKET_PATH=${DOCKER_HOST:5}" >> $GITHUB_ENV

check-cs:
name: Check Coding Standards
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- name: PHP-CS-Fixer
uses: docker://oskarstark/php-cs-fixer-ga
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
args: --config=.php-cs-fixer.php --diff --dry-run
php-version: 8.3
tools: castor

phpstan:
name: PHPStan
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: "Build and start the infrastructure"
run: castor start

- name: Install missing extensions from the docker image
run: docker run --rm -v `pwd`:/app --workdir /app composer/composer composer require symfony/polyfill-intl-normalizer symfony/polyfill-intl-icu --ignore-platform-reqs
- name: CS
run: castor cs

- name: PHPStan
uses: docker://oskarstark/phpstan-ga
env:
REQUIRE_DEV: true
CHECK_PLATFORM_REQUIREMENTS: false
run: castor phpstan

- name: PHPUnit
run: castor phpunit
16 changes: 10 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
# Infra

/.castor.stub.php
/infrastructure/docker/docker-compose.override.yml
/infrastructure/docker/services/router/certs/*.pem

# Tools
.php-cs-fixer.cache


###> symfony/framework-bundle ###
/.env.local
/.env.local.php
Expand All @@ -18,12 +28,6 @@
/phpunit.xml
###< symfony/phpunit-bridge ###

###> friendsofphp/php-cs-fixer ###
/.php-cs-fixer.cache
###< friendsofphp/php-cs-fixer ###
/docker-compose.override.yml
/public/uploads/

###> symfony/asset-mapper ###
/public/assets/
/assets/vendor/
Expand Down
2 changes: 2 additions & 0 deletions .home/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/*
!.gitignore
7 changes: 3 additions & 4 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
<?php

$finder = PhpCsFixer\Finder::create()
->ignoreVCSIgnored(true)
->ignoreDotFiles(false)
->in(__DIR__)
->exclude('var')
->append([
__FILE__,
'rector.php',
])
;

return (new PhpCsFixer\Config())
->setRiskyAllowed(true)
->setRules([
'@PHP74Migration' => true,
'@PHP81Migration' => true,
'@PhpCsFixer' => true,
'@Symfony' => true,
'@Symfony:risky' => true,
'single_line_empty_body' => false,
'php_unit_internal_class' => false, // From @PhpCsFixer but we don't want it
'php_unit_test_class_requires_covers' => false, // From @PhpCsFixer but we don't want it
'phpdoc_add_missing_param_annotation' => false, // From @PhpCsFixer but we don't want it
Expand Down
16 changes: 16 additions & 0 deletions assets/controllers/hello_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Controller } from '@hotwired/stimulus';

/*
* This is an example Stimulus controller!
*
* Any element with a data-controller="hello" attribute will cause
* this controller to be executed. The name "hello" comes from the filename:
* hello_controller.js -> "hello"
*
* Delete this file or adapt it for your use!
*/
export default class extends Controller {
connect() {
this.element.textContent = 'Hello Stimulus! Edit me in assets/controllers/hello_controller.js';
}
}
23 changes: 23 additions & 0 deletions bin/phpunit
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env php
<?php

if (!ini_get('date.timezone')) {
ini_set('date.timezone', 'UTC');
}

if (is_file(dirname(__DIR__).'/vendor/phpunit/phpunit/phpunit')) {
if (PHP_VERSION_ID >= 80000) {
require dirname(__DIR__).'/vendor/phpunit/phpunit/phpunit';
} else {
define('PHPUNIT_COMPOSER_INSTALL', dirname(__DIR__).'/vendor/autoload.php');
require PHPUNIT_COMPOSER_INSTALL;
PHPUnit\TextUI\Command::main();
}
} else {
if (!is_file(dirname(__DIR__).'/vendor/symfony/phpunit-bridge/bin/simple-phpunit.php')) {
echo "Unable to find the `simple-phpunit.php` script in `vendor/symfony/phpunit-bridge/bin/`.\n";
exit(1);
}

require dirname(__DIR__).'/vendor/symfony/phpunit-bridge/bin/simple-phpunit.php';
}
89 changes: 89 additions & 0 deletions castor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

use Castor\Attribute\AsTask;

use function Castor\guard_min_version;
use function Castor\import;
use function Castor\io;
use function Castor\notify;
use function docker\about;
use function docker\build;
use function docker\docker_compose_run;
use function docker\generate_certificates;
use function docker\up;

guard_min_version('0.15.0');

import(__DIR__ . '/.castor');

/**
* @return array<string, mixed>
*/
function create_default_variables(): array
{
return [
'project_name' => 'qotd',
'root_domain' => 'local.qotd.offithings.jolicode.com',
'registry' => 'ghcr.io/jolicode/qotd',
];
}

#[AsTask(description: 'Builds and starts the infrastructure, then install the application (composer, yarn, ...)')]
function start(): void
{
io()->title('Starting the stack');

generate_certificates(force: false);
build();
up();
cache_clear();
install();
migrate();

notify('The stack is now up and running.');
io()->success('The stack is now up and running.');

about();
}

#[AsTask(description: 'Installs the application (composer, yarn, ...)', namespace: 'app', aliases: ['install'])]
function install(): void
{
io()->title('Installing the application');

io()->section('Installing PHP dependencies');
docker_compose_run('composer install -n --prefer-dist --optimize-autoloader');

io()->section('Installing importmap');
docker_compose_run('bin/console importmap:install');

migrate();
fixtures();

qa\install();
}

#[AsTask(description: 'Clear the application cache', namespace: 'app', aliases: ['cache-clear'])]
function cache_clear(): void
{
io()->title('Clearing the application cache');

docker_compose_run('rm -rf var/cache/ && bin/console cache:warmup');
}

#[AsTask(description: 'Migrates database schema', namespace: 'app:db', aliases: ['migrate'])]
function migrate(): void
{
io()->title('Migrating the database schema');

docker_compose_run('bin/console doctrine:database:create --if-not-exists');
docker_compose_run('bin/console doctrine:migration:migrate -n --allow-no-migration');
}

#[AsTask(description: 'Injects fixtures in the database', namespace: 'app:db', aliases: ['fixtures'])]
function fixtures(): void
{
io()->title('Injects fixtures in the database');

docker_compose_run('bin/console doctrine:fixture:load -n');
}
26 changes: 26 additions & 0 deletions infrastructure/docker/docker-compose.builder.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
services:
builder:
build:
context: services/php
target: builder
cache_from:
- "type=registry,ref=${REGISTRY:-}/builder:cache"
depends_on:
- postgres
user: "${USER_ID}:${USER_ID}"
environment:
- COMPOSER_MEMORY_LIMIT=-1
- UID=${USER_ID}
- APP_ENV=${APP_ENV:-dev}
# The following list contains the common environment variables exposed by CI platforms
- GITHUB_ACTIONS
- CI # Travis CI, CircleCI, Cirrus CI, Gitlab CI, Appveyor, CodeShip, dsari
- CONTINUOUS_INTEGRATION # Travis CI, Cirrus CI
- BUILD_NUMBER # Jenkins, TeamCity
- RUN_ID # TaskCluster, dsari
- HOME=/home/app
volumes:
- "../..:/var/www:cached"
- "../../.home:/home/app:cached"
profiles:
- default
13 changes: 13 additions & 0 deletions infrastructure/docker/docker-compose.docker-for-x.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# This file is used only when using Docker for Mac or Windows

services:
router:
# Docker for Mac or Windows does not support the "network_mode=host"
# So we use the default network mode and we map all needed ports
network_mode: "${PROJECT_NAME}_default"
ports:
- "80:80"
- "443:443"
- "8080:8080"
profiles:
- default
Loading

0 comments on commit 93126ce

Please sign in to comment.