From 6b5c1b5aee8364e016d4807c16b2a2a954bae0ac Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Fri, 10 Mar 2023 22:19:10 +0000 Subject: [PATCH] feat: add `project_root` setting Allow for overriding the project root from `/app` to something else, such as `/var/www/html`. The default is set within a new `build.defaults.yaml` file which the project specific file is merged into. Fixes #27 --- resources/build.defaults.yaml | 1 + src/Console/Command/BuildConfigurationCommand.php | 5 ++++- src/Validator/ConfigurationValidator.php | 5 +++++ templates/Dockerfile.twig | 10 +++++----- templates/docker-compose.yaml.twig | 2 +- templates/env.example.twig | 2 +- templates/web/caddy/Caddyfile.twig | 2 +- templates/web/nginx/default.conf.twig | 2 +- 8 files changed, 19 insertions(+), 10 deletions(-) create mode 100644 resources/build.defaults.yaml diff --git a/resources/build.defaults.yaml b/resources/build.defaults.yaml new file mode 100644 index 0000000..2706130 --- /dev/null +++ b/resources/build.defaults.yaml @@ -0,0 +1 @@ +project_root: /app diff --git a/src/Console/Command/BuildConfigurationCommand.php b/src/Console/Command/BuildConfigurationCommand.php index a2d5181..0cf20e4 100644 --- a/src/Console/Command/BuildConfigurationCommand.php +++ b/src/Console/Command/BuildConfigurationCommand.php @@ -55,7 +55,10 @@ final class BuildConfigurationCommand extends Command $io = new SymfonyStyle($input, $output); - $configurationData = Yaml::parseFile($configFile); + $configurationData = array_merge( + Yaml::parseFile(__DIR__.'/../../../resources/build.defaults.yaml'), + Yaml::parseFile($configFile), + ); $violations = $this->configurationValidator->validate($configurationData); diff --git a/src/Validator/ConfigurationValidator.php b/src/Validator/ConfigurationValidator.php index 377b2c9..7007aff 100644 --- a/src/Validator/ConfigurationValidator.php +++ b/src/Validator/ConfigurationValidator.php @@ -37,6 +37,11 @@ final class ConfigurationValidator implements ValidatorInterface new Assert\Choice(['drupal-project', 'php-library']), ], + 'project_root' => [ + new Assert\NotNull(), + new Assert\Type('string'), + ], + 'database' => new Assert\Optional(), 'drupal' => new Assert\Optional(), diff --git a/templates/Dockerfile.twig b/templates/Dockerfile.twig index b3889fc..fc81755 100644 --- a/templates/Dockerfile.twig +++ b/templates/Dockerfile.twig @@ -8,14 +8,14 @@ RUN which composer && composer -V ARG DOCKER_UID=1000 ENV DOCKER_UID="${DOCKER_UID}" -WORKDIR /app +WORKDIR {{ project_root }} RUN adduser --disabled-password --uid "${DOCKER_UID}" app \ - && chown app:app -R /app + && chown app:app -R {{ project_root }} USER app -ENV PATH="${PATH}:/app/bin:/app/vendor/bin" +ENV PATH="${PATH}:{{ project_root }}/bin:{{ project_root }}/vendor/bin" COPY --chown=app:app composer.* ./ @@ -78,7 +78,7 @@ RUN {% for command in dockerfile.stages.test.commands -%} FROM caddy:2 as web -WORKDIR /app +WORKDIR {{ project_root }} COPY tools/docker/images/web/root / {% endif %} @@ -90,7 +90,7 @@ FROM nginx:1 as web EXPOSE 8080 -WORKDIR /app +WORKDIR {{ project_root }} COPY tools/docker/images/web/root / {% endif %} diff --git a/templates/docker-compose.yaml.twig b/templates/docker-compose.yaml.twig index df8eeb3..58f8ba6 100644 --- a/templates/docker-compose.yaml.twig +++ b/templates/docker-compose.yaml.twig @@ -2,7 +2,7 @@ x-app: &default-app volumes: - - "${DOCKER_WEB_VOLUME:-./{{ drupal.docroot }}:/app/{{ drupal.docroot }}}" + - "${DOCKER_WEB_VOLUME:-./{{ drupal.docroot }}:{{ project_root }}/{{ drupal.docroot }}}" env_file: - .env restart: "${DOCKER_RESTART_POLICY:-unless-stopped}" diff --git a/templates/env.example.twig b/templates/env.example.twig index cfdb8c9..9b3e8eb 100644 --- a/templates/env.example.twig +++ b/templates/env.example.twig @@ -6,7 +6,7 @@ export DOCKER_UID=1000 export COMPOSE_PROJECT_NAME={{ name }} export COMPOSE_PROFILES=web,php,database -export DOCKER_WEB_VOLUME=.:/app +export DOCKER_WEB_VOLUME=.:{{ project_root }} {% endif %} {% if "drupal-project" == type %} diff --git a/templates/web/caddy/Caddyfile.twig b/templates/web/caddy/Caddyfile.twig index c6026d8..ac30a0d 100644 --- a/templates/web/caddy/Caddyfile.twig +++ b/templates/web/caddy/Caddyfile.twig @@ -1,6 +1,6 @@ # {{ managedText | raw }} :80 -root * /app/{{ drupal.docroot }} +root * {{ project_root }}/{{ drupal.docroot }} file_server php_fastcgi php:9000 diff --git a/templates/web/nginx/default.conf.twig b/templates/web/nginx/default.conf.twig index 205cba5..0655031 100644 --- a/templates/web/nginx/default.conf.twig +++ b/templates/web/nginx/default.conf.twig @@ -3,7 +3,7 @@ server { server_name _; - root /app/{{ drupal.docroot }}; + root {{ project_root }}/{{ drupal.docroot }}; location / { try_files $uri /index.php?$query_string;