Skip to content

Commit

Permalink
cleanup and rework
Browse files Browse the repository at this point in the history
  • Loading branch information
gaydabura authored and andypost committed Feb 5, 2019
1 parent f656cd8 commit 1fa60d4
Show file tree
Hide file tree
Showing 14 changed files with 490 additions and 292 deletions.
13 changes: 8 additions & 5 deletions .env.default
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
COMPOSE_FILE=./docker/docker-compose.yml:./docker/docker-compose.override.yml
COMPOSE_PROJECT_NAME=projectname
PROFILE_NAME=sdd
MODULES= default_content
THEME_NAME=projectname_theme
SITE_NAME=Site name
MODULES=default_content
THEME_NAME=
SITE_NAME=Example
[email protected]
PROJECT_INSTALL=
PHP_VERSION=7
IMAGE_PHP=skilldlabs/php:72-fpm
IMAGE_NGINX=skilldlabs/nginx:1.14.1
IMAGE_FRONT=skilldlabs/frontend:zen
MAIN_DOMAIN_NAME=docker.localhost
DB_URL=sqlite:///dev/shm/d8.sqlite
MYSQL_BASE_PATH=/dev/shm
#DB_URL=mysql://d8:d8@mysql/d8
DB_DATA_DIR=/dev/shm
#DB_DATA_DIR=../mysql #Please include path to this folder to your .gitignore
14 changes: 2 additions & 12 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,14 @@ nbproject
.settings
.project
config/sync/.htaccess
config/sync/README.txt
docker/docker-compose.override.yml
node_modules
vendor
web/.csslintrc
web/.editorconfig
web/.eslintignore
web/.eslintrc.json
web/.gitattributes
web/.htaccess
web/autoload.php
web/*.*
web/core/
web/index.php
web/libraries/
web/modules/contrib/
web/profiles/contrib/config_installer/
web/robots.txt
web/sites/
web/themes/contrib/
web/update.php
web/web.config
mysql/
236 changes: 84 additions & 152 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,217 +1,149 @@
# Add utility functions and scripts to the container
include scripts/makefile/*.mk

.PHONY: all up down cex cim prepare install si exec info phpcs phpcbf drush
.PHONY: all provision si exec exec0 down clean dev info phpcs phpcbf drush check
.DEFAULT_GOAL := help

# Read project name from .env file
# https://stackoverflow.com/a/6273809/1826109
%:
@:

# Prepare enviroment variables from defaults.
$(shell false | cp -i \.env.default \.env 2>/dev/null)
$(shell false | cp -i \.\/docker\/docker-compose\.override\.yml\.default \.\/docker\/docker-compose\.override\.yml 2>/dev/null)
include .env

# Get local values only once.
# Get user/group id to manage permissions between host and containers.
LOCAL_UID := $(shell id -u)
LOCAL_GID := $(shell id -g)

# Evaluate recursively.
CUID ?= $(LOCAL_UID)
CGID ?= $(LOCAL_GID)

# Define network name.
COMPOSE_NET_NAME := $(COMPOSE_PROJECT_NAME)_front
MYSQL_DATADIR := $(MYSQL_BASE_PATH)/$(COMPOSE_PROJECT_NAME)_mysql

php = docker-compose exec -T --user $(CUID):$(CGID) php time ${1}
php-0 = docker-compose exec -T php time ${1}
front = docker run --rm -u $(CUID):$(CGID) -v $(shell pwd)/web/themes/$(THEME_NAME):/work $(IMAGE_FRONT) ${1}
# Define mysql storage folder.
MYSQL_DATADIR := $(DB_DATA_DIR)/$(COMPOSE_PROJECT_NAME)_mysql

# Execute php container as regular user.
php = docker-compose exec -T --user $(CUID):$(CGID) php ${1}
# Execute php container as root user.
php-0 = docker-compose exec -T php ${1}
# Function for code sniffer images.
phpcsexec = docker run --rm \
-v $(shell pwd)/web/profiles/$(PROFILE_NAME):/work/profile \
-v $(shell pwd)/web/modules/custom:/work/modules \
-v $(shell pwd)/web/themes/custom:/work/themes \
skilldlabs/docker-phpcs-drupal ${1} -s --colors \
--standard=Drupal,DrupalPractice \
--extensions=php,module,inc,install,profile,theme,yml,txt,md,js \
--ignore=*.css,libraries/*,dist/*,styleguide/* \
.

## Full site install from the scratch
all: | include prepare install si info
all: | provision composer si info

include:
## Provision enviroment
provision:
# Check if enviroment variables has been defined.
ifeq ($(strip $(COMPOSE_PROJECT_NAME)),projectname)
#todo: ask user to make a project name and mv folders.
$(error Project name can not be default, please edit ".env" and set COMPOSE_PROJECT_NAME variable.)
$(info Project name can not be default, please enter project name.)
$(eval COMPOSE_PROJECT_NAME = $(strip $(shell read -p "Project name: " REPLY;echo -n $$REPLY)))
$(shell sed -i -e '/COMPOSE_PROJECT_NAME=/ s/=.*/=$(COMPOSE_PROJECT_NAME)/' .env)
$(info Please review your project settings and run `make all` again.)
exit 1
endif

prepare:
ifeq ($(shell docker-compose config --services | grep mysql),mysql)
mkdir -p $(MYSQL_DATADIR)
endif
make -s down
make -s up
@echo "Updating containers..."
docker-compose pull
@echo "Build and run containers..."
docker-compose up -d --remove-orphans
$(call php-0, apk add --no-cache --repository http://dl-cdn.alpinelinux.org/alpine/edge/community graphicsmagick)
$(call php-0, kill -USR2 1)
$(call php, composer global require -o --update-no-dev --no-suggest "hirak/prestissimo:^0.3")

install:
composer:
$(call php, composer install --prefer-dist -o --no-dev)
$(call php, composer drupal-scaffold)
$(call php, composer create-required-files)
# Uncomment this string to build front separately. See scripts/makefile/front.mk
# make -s front

## Reinstall site
## Install drupal.
si:
$(call php-0, chmod +w web/sites/default)
ifneq ("$(wildcard web/sites/default/settings.php)","")
$(call php-0, rm -f web/sites/default/settings.php)
endif
@echo "Installing from: $(PROJECT_INSTALL)"
ifeq ($(PROJECT_INSTALL), config)
$(call php, drush si config_installer --db-url=$(DB_URL) --account-pass=admin -y config_installer_sync_configure_form.sync_directory=../config/sync)
else
$(call php, drush si $(PROFILE_NAME) --db-url=$(DB_URL) --account-pass=admin -y --site-name="$(SITE_NAME)" --site-mail="$(SITE_MAIL)" install_configure_form.site_default_country=FR install_configure_form.date_default_timezone=Europe/Paris)
endif
#$(call php, drush en $(MODULES) -y)
#$(call php, drush pmu $(MODULES) -y)
#make -s locale-update
#make -s cim
#make -s update-alias
#make -s _local-settings
make -s info

## Update locale
locale-update:
$(call php, drush locale-check)
$(call php, drush locale-update)

_local-settings:
@echo "Turn on settings.local"
$(call php, chmod +w web/sites/default)
$(call php, cp settings/settings.local.php web/sites/default/settings.local.php)
$(call php-0, sed -i "/settings.local.php';/s/# //g" web/sites/default/settings.php)

## Execute config export to config/sync directory
cex:
$(call php, drush cex -y)
ifneq ($(PROJECT_INSTALL), config)
rm -rf config/sync/*
cp -R web/$(shell docker-compose exec -T --user $(CUID):$(CGID) php drush ev 'echo substr(\Drupal::service("config.storage.sync")->getFilePath("drush"), 0, -10);')/* config/sync
ifneq ($(strip $(MODULES)),)
$(call php, drush en $(MODULES) -y)
$(call php, drush pmu $(MODULES) -y)
endif

## Execute config import from config/sync directory
cim:
$(call php, drush cr)
$(call php, drush cim -y)

update-alias:
$(call php, drush pag canonical_entities:node update)
$(call php, drush cr)

## Project's containers information
info:
ifeq ($(shell docker inspect --format="{{ .State.Running }}" $(COMPOSE_PROJECT_NAME)_web 2> /dev/null),true)
@echo Project http://$(shell docker inspect --format='{{(index .NetworkSettings.Networks "$(COMPOSE_NET_NAME)").IPAddress}}' $(COMPOSE_PROJECT_NAME)_web)
endif
ifeq ($(shell docker inspect --format="{{ .State.Running }}" $(COMPOSE_PROJECT_NAME)_mail 2> /dev/null),true)
@echo Mailhog http://$(shell docker inspect --format='{{(index .NetworkSettings.Networks "$(COMPOSE_NET_NAME)").IPAddress}}' $(COMPOSE_PROJECT_NAME)_mail):8025
endif
ifeq ($(shell docker inspect --format="{{ .State.Running }}" $(COMPOSE_PROJECT_NAME)_adminer 2> /dev/null),true)
@echo Adminer http://$(shell docker inspect --format='{{(index .NetworkSettings.Networks "$(COMPOSE_NET_NAME)").IPAddress}}' $(COMPOSE_PROJECT_NAME)_adminer)
endif
$(info Containers for "$(COMPOSE_PROJECT_NAME)" info:)
$(eval CONTAINERS = $(shell docker ps -f name=$(COMPOSE_PROJECT_NAME) --format "{{ .ID }}"))
$(foreach CONTAINER, $(CONTAINERS),$(info $(shell printf '%-19s \n' $(shell docker inspect --format='{{.NetworkSettings.Networks.$(COMPOSE_NET_NAME).IPAddress}} {{range $$p, $$conf := .NetworkSettings.Ports}}{{$$p}}{{end}} {{.Name}}' $(CONTAINER) | rev | sed "s/pct\//,pct:/g" | sed "s/,//" | rev | awk '{ print $0}')) ))
@echo "$(RESULT)"

chown:
# Use this goal to set permissions in docker container
$(call php-0, /bin/sh -c "chown $(CUID):$(CGID) /var/www/html/web -R")
# Need this to fix files folder
$(call php-0, /bin/sh -c "chown www-data: /var/www/html/web/sites/default/files -R")

## Run shell in PHP container as CUID:CGID user
## Run shell in PHP container as regular user
exec:
docker-compose exec --user $(CUID):$(CGID) php ash

## Run shel in PHP container as root
## Run shell in PHP container as root
exec0:
docker-compose exec php ash

up: net
@echo "Updating containers..."
docker-compose pull
@echo "Build and run containers..."
docker-compose up -d --remove-orphans

down:
@echo "Removing network & containers for $(COMPOSE_PROJECT_NAME)"
@docker-compose down -v --remove-orphans --rmi local

DIRS = web/core web/libraries web/modules/contrib web/profiles/contrib web/sites web/themes/contrib vendor

## Totally remove project build folder, docker containers and network
clean: DIRS := core libraries modules/contrib profiles/contrib sites themes/contrib
clean: info down
@for i in $(DIRS); do if [ -d "web/$$i" ]; then echo "Removing web/$$i..."; docker run --rm -v $(shell pwd):/mnt $(IMAGE_PHP) sh -c "rm -rf /mnt/web/$$i"; fi; done
@if [ -d $(MYSQL_DATADIR) ]; then echo "Removing mysql data $(MYSQL_DATADIR)..."; docker run --rm -v $(MYSQL_BASE_PATH):/mnt/2rm $(IMAGE_PHP) sh -c "rm -rf /mnt/2rm/$(COMPOSE_PROJECT_NAME)_mysql"; fi

net:
ifeq ($(shell docker network ls -q -f Name=$(COMPOSE_NET_NAME)),)
docker network create $(COMPOSE_NET_NAME)
endif
ifeq ($(shell grep -c -F 'IPRANGE=' .env), 0)
# @echo Define IP range $(net-range)
@printf "\nIPRANGE=%s\n" $(shell docker network inspect $(COMPOSE_NET_NAME) --format '{{(index .IPAM.Config 0).Subnet}}') >> .env
else
# @if [ '$(IPRANGE)' != '$(shell docker network inspect $(COMPOSE_NET_NAME) --format '{{(index .IPAM.Config 0).Subnet}}')' ]; then echo "Replace IP range $(IPRANGE)"; sed -i "s#IPRANGE=.*#IPRANGE=$(shell docker network inspect $(COMPOSE_NET_NAME) --format '{{(index .IPAM.Config 0).Subnet}}')#" .env; fi;
clean: info
@for i in $(DIRS); do if [ -d "$$i" ]; then echo "Removing $$i..."; docker run --rm -v $(shell pwd):/mnt $(IMAGE_PHP) sh -c "rm -rf /mnt/$$i"; fi; done
$(eval SCAFFOLD = $(shell docker-compose exec -T --user $(CUID):$(CGID) php composer run-script list-scaffold-files | grep -P '^(?!>)'))
@for i in $(SCAFFOLD); do if [ -e "web/$$i" ]; then echo "Removing web/$$i..."; rm -rf web/$$i; fi; done
make -s down
ifeq ($(shell docker-compose config --services | grep mysql),mysql)
@if [ -d $(MYSQL_DATADIR) ]; then echo "Removing mysql data $(MYSQL_DATADIR) ..."; docker run --rm -v $(shell pwd):/mnt/2rm $(IMAGE_PHP) sh -c "rm -rf /mnt/2rm/$(DB_DATA_DIR)"; fi
endif
# grep -q -F 'IPRANGE=' .env || printf "\nIPRANGE=$(shell docker network inspect $(COMPOSE_NET_NAME) --format '{{(index .IPAM.Config 0).Subnet}}')" >> .env

front:
@echo "Building front tasks..."
docker pull $(IMAGE_FRONT)
$(call front, bower install)
$(call front)
$(call php-0, rm -rf web/themes/$(THEME_NAME)/node_modules)

lint:
@echo "Running linters..."
$(call front, gulp lint)
$(call php-0, rm -rf web/themes/$(THEME_NAME)/node_modules)

## Enable development mode and disable caching.
dev:
@echo "Dev tasks..."
$(call php, composer install --prefer-dist -o)
$(call php-0, chmod +w web/sites/default)
$(call php, cp web/sites/default/default.services.yml web/sites/default/services.yml)
$(call php, sed -i -e 's/debug: false/debug: true/g' web/sites/default/services.yml)
$(call php, cp web/sites/example.settings.local.php web/sites/default/settings.local.php)
$(call php, drush -y config-set system.performance css.preprocess 0)
$(call php, drush -y config-set system.performance js.preprocess 0)
$(call php, drush en devel devel_generate webform_devel kint -y)
$(call php, drush pm-uninstall dynamic_page_cache page_cache -y)
$(call php, drush cr)

@$(call php-0, chmod +w web/sites/default)
@$(call php, cp web/sites/default/default.services.yml web/sites/default/services.yml)
@$(call php, sed -i -e 's/debug: false/debug: true/g' web/sites/default/services.yml)
@$(call php, cp web/sites/example.settings.local.php web/sites/default/settings.local.php)
@echo "Including settings.local.php."
@$(call php-0, sed -i "/settings.local.php';/s/# //g" web/sites/default/settings.php)
@$(call php, drush -y -q config-set system.performance css.preprocess 0)
@$(call php, drush -y -q config-set system.performance js.preprocess 0)
@echo "Enabling devel module."
@$(call php, drush -y -q en devel devel_generate kint)
@echo "Disabling caches."
@$(call php, drush -y -q pm-uninstall dynamic_page_cache page_cache)
@$(call php, drush cr)

## Run drush command in PHP container. To pass arguments use double dash: "make drush dl devel -- -y"
drush:
$(call php, drush $(filter-out $@,$(MAKECMDGOALS)))

# https://stackoverflow.com/a/6273809/1826109
%:
@:
$(call php, $(filter-out "$@",$(MAKECMDGOALS)))
$(info "To pass arguments use double dash: "make drush dl devel -- -y"")

## Check codebase with phpcs sniffers to make sure it conforms https://www.drupal.org/docs/develop/standards
phpcs:
docker run --rm \
-v $(shell pwd)/web/profiles/$(PROFILE_NAME):/work/profile \
-v $(shell pwd)/web/modules/custom:/work/modules \
-v $(shell pwd)/web/themes/$(THEME_NAME):/work/themes \
skilldlabs/docker-phpcs-drupal phpcs -s --colors \
--standard=Drupal,DrupalPractice \
--extensions=php,module,inc,install,profile,theme,yml \
--ignore=*.css,*.md,*.js .
docker run --rm \
-v $(shell pwd)/web/profiles/$(PROFILE_NAME):/work/profile \
-v $(shell pwd)/web/modules/custom:/work/modules \
-v $(shell pwd)/web/themes/$(THEME_NAME):/work/themes \
skilldlabs/docker-phpcs-drupal phpcs -s --colors \
--standard=Drupal,DrupalPractice \
--extensions=js \
--ignore=*.css,*.md,libraries/*,styleguide/* .
@$(call phpcsexec, phpcs)

## Fix codebase according to Drupal standards https://www.drupal.org/docs/develop/standards
phpcbf:
docker run --rm \
-v $(shell pwd)/web/profiles/$(PROFILE_NAME):/work/profile \
-v $(shell pwd)/web/modules/custom:/work/modules \
-v $(shell pwd)/web/themes/$(THEME_NAME):/work/themes \
skilldlabs/docker-phpcs-drupal phpcbf -s --colors \
--standard=Drupal,DrupalPractice \
--extensions=php,module,inc,install,profile,theme,yml,txt,md \
--ignore=*.css,*.md,*.js .
docker run --rm \
-v $(shell pwd)/web/profiles/$(PROFILE_NAME):/work/profile \
-v $(shell pwd)/web/modules/custom:/work/modules \
-v $(shell pwd)/web/themes/$(THEME_NAME):/work/themes \
skilldlabs/docker-phpcs-drupal phpcbf -s --colors \
--standard=Drupal,DrupalPractice \
--extensions=js \
--ignore=*.css,*.md,libraries/*,styleguide/* .
@$(call phpcsexec, phpcbf)
Loading

0 comments on commit 1fa60d4

Please sign in to comment.