Skip to content

Commit

Permalink
Merge branch 'release/2.5.0' into craft-vite
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Welch committed Mar 19, 2022
2 parents 595dde9 + 3cc086c commit a3031c4
Show file tree
Hide file tree
Showing 12 changed files with 471 additions and 87 deletions.
372 changes: 363 additions & 9 deletions CHANGELOG.md

Large diffs are not rendered by default.

32 changes: 14 additions & 18 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
CONTAINER?=$(shell basename $(CURDIR))_php_1
BUILDCHAIN?=$(shell basename $(CURDIR))_vite_1
CONTAINER?=$(shell basename $(CURDIR))-php-1
BUILDCHAIN?=$(shell basename $(CURDIR))-vite-1

.PHONY: build clean composer dev npm pulldb restoredb up
.PHONY: build clean composer craft dev npm pulldb restoredb nuke ssh up

build: up
docker exec -it ${BUILDCHAIN} npm run build
clean:
docker-compose down -v
docker-compose up --build
rm -f cms/composer.lock
rm -rf cms/vendor/
rm -f buildchain/package-lock.json
rm -rf buildchain/node_modules/
composer: up
docker exec -it ${CONTAINER} su-exec www-data composer \
$(filter-out $@,$(MAKECMDGOALS))
Expand All @@ -23,21 +25,15 @@ pulldb: up
restoredb: up
cd scripts/ && ./docker_restore_db.sh \
$(filter-out $@,$(MAKECMDGOALS))
update:
docker-compose down
rm -f cms/composer.lock
rm -f buildchain/package-lock.json
docker-compose up
update-clean:
docker-compose down
rm -f cms/composer.lock
rm -rf cms/vendor/
rm -f buildchain/package-lock.json
rm -rf buildchain/node_modules/
docker-compose up
nuke: clean
docker-compose down -v
docker-compose up --build --force-recreate
ssh:
docker exec -it ${CONTAINER} su-exec www-data /bin/sh
up:
if [ ! "$$(docker ps -q -f name=${CONTAINER})" ]; then \
docker-compose up; \
cp -n cms/example.env cms/.env; \
docker-compose up; \
fi
%:
@:
Expand Down
28 changes: 12 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,21 @@ You can read more about it in the [Setting up a New Craft 3 CMS Project](https:/

This project package works exactly the way Pixel & Tonic's [craftcms/craft](https://github.com/craftcms/craft) package works; you create a new project by first creating & installing the project:

composer create-project nystudio107/craft:dev-craft-vite PATH --no-install
composer create-project nystudio107/craft PATH --no-install

Make sure that `PATH` is the path to your project, including the name you want for the project, e.g.:

composer create-project nystudio107/craft:dev-craft-vite vitecraft --no-install --remove-vcs
composer create-project nystudio107/craft craft3 --no-install

We use `--no-install` so that the composer packages for the root project are not installed.

## Setting Up Local Dev

You'll need Docker desktop for your platform installed to run the project in local development
You'll need [Docker desktop](https://www.docker.com/products/docker-desktop) for your platform installed to run devMode in local development

Ensure you're using the [Docker Compose API v2](https://stackoverflow.com/questions/69464001/docker-compose-container-name-use-dash-instead-of-underscore/70295720#70295720) for the `make` commands to all work properly.

Ensure no other local development environments are running that might have port conflicts, then:

* Start up the site by typing `make dev` in terminal in the project's root directory (the first build will be somewhat lengthy)
* Navigate to `http://localhost:8000` to use the site; the `vite-dev-server` runs off of `http://localhost:3000`
Expand Down Expand Up @@ -103,26 +107,18 @@ To make using it easier, we're using a Makefile and the built-in `make` utility

- `make dev` - starts up the local dev server listening on `http://localhost:8000/`
- `make build` - builds the static assets via the Vite buildchain
- `make clean` - shuts down the Docker containers, removes any mounted volumes (including the database), and then rebuilds the containers from scratch
- `make update` - causes the project to update to the latest Composer and NPM dependencies
- `make update-clean` - completely removes `node_modules/` & `vendor/`, then causes the project to update to the latest Composer and NPM dependencies
- `make clean` - removes the `cms/composer.lock` & the entire `cms/vendor/` directory as well as the `buildchain/package-lock.json` & the entire `buildchain/node_modules/` directory
- `make composer xxx` - runs the `composer` command passed in, e.g. `make composer install`
- `make craft xxx` - runs the `craft` [console command](https://craftcms.com/docs/3.x/console-commands.html) passed in, e.g. `make craft project-config/apply` in the php container
- `make npm xxx` - runs the `npm` command passed in, e.g. `make npm install`
- `make nuke` - restarts the project from scratch by running `make clean` (above), then shuts down the Docker containers, removes any mounted volumes (including the database), and then rebuilds the containers from scratch
- `make pulldb` - runs the `scripts/docker_pull_db.sh` script to pull a remote database into the database container; the `scripts/.env.sh` must be set up first
- `make restoredb xxx` - runs the `scripts/docker_restore_db.sh` script to restore a local database dump into the database container; the `scripts/.env.sh` must be set up first
- `make ssh` - opens up a Unix shell inside the PHP container for the project

### Other notes
**Tip:** If you try a command like `make craft project-config/apply --force` you’ll see an error, because the shell thinks the `--force` flag should be applied to the `make` command. To side-step this, use the `--` (double-dash) to disable further option processing, like this: `make -- craft project-config/apply --force`

To update to the latest Composer packages (as constrained by the `cms/composer.json` semvers) and latest npm packages (as constrained by the `buildchain/package.json` semvers), do:
```
make update
```

To start from scratch by removing `buildchain/node_modules/` & `cms/vendor/`, then update to the latest Composer packages (as constrained by the `cms/composer.json` semvers) and latest npm packages (as constrained by the `buildchain/package.json` semvers), do:
```
make update-clean
```
### Other notes

To use Xdebug with VSCode install the [PHP Debug extension](https://marketplace.visualstudio.com/items?itemName=felixfbecker.php-debug ) and use the following configuration in your `.vscode/launch.json`:
```json
Expand Down
2 changes: 1 addition & 1 deletion buddy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
type: "BUILD"
working_directory: "/buddy/${PROJECT_SHORTNAME}"
docker_image_name: "nystudio107/node-dev-base"
docker_image_tag: "14-alpine"
docker_image_tag: "16-alpine"
execute_commands:
- "cd buildchain"
- "npm install"
Expand Down
2 changes: 1 addition & 1 deletion cms/example.env
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ REDIS_CRAFT_DB=1

# webpack settings
PUBLIC_PATH=/dist/
DEVSERVER_PUBLIC=http://localhost:8080
DEVSERVER_HOST=0.0.0.0
DEVSERVER_POLL=0
DEVSERVER_PORT=8080
DEVSERVER_HTTPS=0
DEVSERVER_PUBLIC=http://localhost:8080

# Vite settings
VITE_DEV_SERVER_PUBLIC=http://localhost:3000/
Expand Down
42 changes: 20 additions & 22 deletions cms/templates/_layouts/global-variables.twig
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
{# -- Root global variables that all templates inherit from -- #}
{# -- This allows for defining site-wide Twig variables as needed -- #}
{#
┌─────────────────────────────────────────────────────────────────────────────┐
│ │
│ │
│ │
│ htmlPage │
└─────────────────────────────────────────────────────────────────────────────┘
#}
{% spaceless %}
{% spaceless %}{# -- Root global variables that all templates inherit from -- #}
{# -- This allows for defining site-wide Twig variables as needed -- #}
{#
┌─────────────────────────────────────────────────────────────────────────────┐
│ │
│ │
│ │
│ htmlPage │
└─────────────────────────────────────────────────────────────────────────────┘
#}

{# -- Prefetch & preconnect headers and links -- #}
{% set prefetchUrls = [
alias("@assetsUrl"),
] %}
{# -- General global variables -- #}
{% set baseUrl = alias('@assetsUrl') ~ '/' %}
{% set gaTrackingId = getenv('GA_TRACKING_ID') %}

{# -- Twig output from the render; this must be in a block -- #}
{% block htmlPage %}
{% endblock %}
{# -- Prefetch & preconnect headers and links -- #}
{% set prefetchUrls = [
alias("@assetsUrl"),
] %}
{# -- General global variables -- #}
{% set baseUrl = alias('@assetsUrl') ~ '/' %}
{% set gaTrackingId = getenv('GA_TRACKING_ID') %}

{% endspaceless %}
{# -- Twig output from the render; this must be in a block -- #}
{% endspaceless %}{% block htmlPage %}
{% endblock %}
4 changes: 0 additions & 4 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ services:
expose:
- "9000"
init: true
tty: true
volumes: &php-volumes
- cpresources:/var/www/project/cms/web/cpresources:delegated
- storage:/var/www/project/cms/storage:delegated
Expand All @@ -49,7 +48,6 @@ services:
expose:
- "9000"
init: true
tty: true
volumes:
*php-volumes
# queue - runs queue jobs via php craft queue/listen
Expand All @@ -62,7 +60,6 @@ services:
env_file:
*env
init: true
tty: true
volumes:
*php-volumes
# mariadb - database
Expand Down Expand Up @@ -101,7 +98,6 @@ services:
init: true
ports:
- "3000:3000"
tty: true
volumes:
- ./buildchain:/var/www/project/buildchain:cached
- ./buildchain/node_modules:/var/www/project/buildchain/node_modules:delegated
Expand Down
11 changes: 8 additions & 3 deletions docker-config/node-dev-vite/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
FROM nystudio107/node-dev-base:16-alpine

USER node
WORKDIR /var/www/project/

WORKDIR /var/www/project/buildchain/
COPY ./npm_install.sh .
RUN chmod a+x npm_install.sh

# Run our webpack build in debug mode

Expand All @@ -13,6 +14,10 @@ WORKDIR /var/www/project/buildchain/
# This automatic running adds to the startup overhead of `docker-compose up`
# but saves far more time in not having to deal with out of sync versions
# when working with teams or multiple environments
CMD npm install \
CMD export CPPFLAGS="-DPNG_ARM_NEON_OPT=0" \
&& \
./npm_install.sh \
&& \
cd /var/www/project/buildchain/ \
&& \
npm run dev
16 changes: 16 additions & 0 deletions docker-config/node-dev-vite/npm_install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash

# NPM Install shell script
#
# This shell script runs `npm install` if either the `package-lock.json` file or
# the `node_modules/` directory is not present`
#
# @author nystudio107
# @copyright Copyright (c) 2022 nystudio107
# @link https://nystudio107.com/
# @license MIT

cd /var/www/project/buildchain
if [ ! -f "package-lock.json" ] || [ ! -d "node_modules" ]; then
npm install
fi
8 changes: 3 additions & 5 deletions docker-config/php-prod-craft/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ RUN chmod a+x run_queue.sh \
mkdir -p /var/www/project/cms/web/cpresources \
&& \
chown -R www-data:www-data /var/www/project

WORKDIR /var/www/project/cms
COPY ./composer_install.sh .
RUN chmod a+x composer_install.sh

# Force permissions, update Craft, and start php-fpm

Expand All @@ -68,8 +68,6 @@ WORKDIR /var/www/project/cms
# when working with teams or multiple environments
CMD chown -R www-data:www-data /var/www/project \
&& \
su-exec www-data composer install --verbose --no-progress --no-scripts --optimize-autoloader --no-interaction \
&& \
su-exec www-data composer craft-update \
./composer_install.sh \
&& \
php-fpm
16 changes: 16 additions & 0 deletions docker-config/php-prod-craft/composer_install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash

# Composer Install shell script
#
# This shell script runs `composer install` if either the `composer.lock` file or
# the `vendor/` directory is not present`
#
# @author nystudio107
# @copyright Copyright (c) 2022 nystudio107
# @link https://nystudio107.com/
# @license MIT

cd /var/www/project/cms
if [ ! -f "composer.lock" ] || [ ! -d "vendor" ]; then
su-exec www-data composer install --verbose --no-progress --no-scripts --optimize-autoloader --no-interaction
fi
25 changes: 17 additions & 8 deletions docker-config/php-prod-craft/run_queue.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,27 @@
# Run Queue shell script
#
# This shell script runs the Craft CMS queue via `php craft queue/listen`
# It's wrapped in a "keep alive" infinite loop that restarts the command
# (after a 60 second sleep) should it exit unexpectedly for any reason
# It waits until the database container responds, then runs any pending
# migrations / project config changes via the `craft-update` Composer script,
# then runs the queue listener that listens for and runs pending queue jobs
#
# @author nystudio107
# @copyright Copyright (c) 2020 nystudio107
# @copyright Copyright (c) 2022 nystudio107
# @link https://nystudio107.com/
# @license MIT

while true
cd /var/www/project/cms
# Wait until the Postgres db container responds
until eval "PGPASSWORD=$DB_PASSWORD psql -h postgres -U $DB_USER $DB_DATABASE -c 'select 1' > /dev/null 2>&1"
do
cd /var/www/project/cms
su-exec www-data php craft queue/listen 10
echo "-> craft queue/listen will retry in 60 seconds"
sleep 60
sleep 1
done
# Wait until the `composer install` is done by looking for the `vendor/autoload.php` file
while [ ! -f vendor/autoload.php ]
do
sleep 1
done
# Run any pending migrations/project config changes
su-exec www-data composer craft-update
# Run a queue listener
su-exec www-data php craft queue/listen 10

0 comments on commit a3031c4

Please sign in to comment.