From ed6d032bdeea87c5708482c989870fa330916d89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Sch=C3=B6ldstr=C3=B6m?= Date: Fri, 24 Mar 2017 12:30:36 -0500 Subject: [PATCH 01/13] Add Composer plugin which scaffolds a Vagtranfile --- Vagrantfile | 30 +++++++++++++------ composer.json | 14 +++++++-- composer/src/Plugin.php | 65 +++++++++++++++++++++++++++++++++++++++++ lib/drupalvm/vagrant.rb | 11 +++++++ 4 files changed, 109 insertions(+), 11 deletions(-) create mode 100644 composer/src/Plugin.php diff --git a/Vagrantfile b/Vagrantfile index 9e9a5d24b..bb4fd44f6 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -3,17 +3,29 @@ require_relative 'lib/drupalvm/vagrant' -# Absolute paths on the host machine. -host_drupalvm_dir = File.dirname(File.expand_path(__FILE__)) -host_project_dir = ENV['DRUPALVM_PROJECT_ROOT'] || host_drupalvm_dir -host_config_dir = ENV['DRUPALVM_CONFIG_DIR'] ? "#{host_project_dir}/#{ENV['DRUPALVM_CONFIG_DIR']}" : host_project_dir +drupalvm_env = ENV['DRUPALVM_ENV'] || 'vagrant' -# Absolute paths on the guest machine. -guest_project_dir = '/vagrant' -guest_drupalvm_dir = ENV['DRUPALVM_DIR'] ? "/vagrant/#{ENV['DRUPALVM_DIR']}" : guest_project_dir -guest_config_dir = ENV['DRUPALVM_CONFIG_DIR'] ? "/vagrant/#{ENV['DRUPALVM_CONFIG_DIR']}" : guest_project_dir +# Default paths when the project is based on Drupal VM. +host_project_dir = host_drupalvm_dir = host_config_dir = __dir__ +guest_project_dir = guest_drupalvm_dir = guest_config_dir = '/vagrant' -drupalvm_env = ENV['DRUPALVM_ENV'] || 'vagrant' +if File.exist?("#{host_project_dir}/composer.json") + cconfig = load_composer_config("#{host_project_dir}/composer.json") + + # If Drupal VM is a Composer dependency set the correct path. + vendor_dir = `composer config vendor-dir`.strip + drupalvm_path = "#{vendor_dir}/geerlingguy/drupal-vm" + if Dir.exist?("#{host_project_dir}/#{drupalvm_path}") + host_drupalvm_dir = "#{host_project_dir}/#{drupalvm_path}" + guest_drupalvm_dir = "#{guest_project_dir}/#{drupalvm_path}" + end + + # Read config_dir from composer.json if set. + if cconfig.include?('config_dir') + host_config_dir = "#{host_project_dir}/#{cconfig['config_dir']}" + guest_config_dir = "#{guest_project_dir}/#{cconfig['config_dir']}" + end +end default_config_file = "#{host_drupalvm_dir}/default.config.yml" unless File.exist?(default_config_file) diff --git a/composer.json b/composer.json index 7bdfb1fd0..b4ddbe1cf 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "geerlingguy/drupal-vm", - "type": "vm", + "type": "composer-plugin", "description": "A VM for local Drupal development, built with Vagrant + Ansible", "keywords": ["vagrant", "vm", "virtual machine", "drupal"], "homepage": "https://www.drupalvm.com", @@ -20,10 +20,20 @@ "source": "https://github.com/geerlingguy/drupal-vm", "docs": "http://docs.drupalvm.com" }, - "require": {}, + "require": { + "composer-plugin-api": "^1.0" + }, "config": { "process-timeout": 1800 }, + "autoload": { + "psr-4": { + "JJG\\DrupalVM\\": "composer/src/" + } + }, + "extra": { + "class": "JJG\\DrupalVM\\Plugin" + }, "scripts": { "run-tests": "./tests/run-tests.sh", "docker-bake": "./provisioning/docker/bake.sh", diff --git a/composer/src/Plugin.php b/composer/src/Plugin.php new file mode 100644 index 000000000..925b49e4c --- /dev/null +++ b/composer/src/Plugin.php @@ -0,0 +1,65 @@ +composer = $composer; + $this->io = $io; + } + + /** + * {@inheritdoc} + */ + public static function getSubscribedEvents() { + return array( + ScriptEvents::POST_INSTALL_CMD => 'addVagrantfile', + ScriptEvents::POST_UPDATE_CMD => 'addVagrantfile', + ); + } + + /** + * Add/update project Vagrantfile. + * + * @param \Composer\Script\Event $event + */ + public function addVagrantfile(Event $event) { + + $baseDir = dirname(Factory::getComposerFile()); + $source = __DIR__ . '/../../Vagrantfile'; + $target = $baseDir . '/Vagrantfile'; + + if (file_exists($source)) { + if (!file_exists($target) || md5_file($source) != md5_file($target)) { + copy($source, $target); + } + } + } + +} diff --git a/lib/drupalvm/vagrant.rb b/lib/drupalvm/vagrant.rb index beaa3c432..f19d62b12 100644 --- a/lib/drupalvm/vagrant.rb +++ b/lib/drupalvm/vagrant.rb @@ -1,3 +1,4 @@ +require 'json' require 'yaml' # Cross-platform way of finding an executable in the $PATH. @@ -33,6 +34,16 @@ def resolve_jinja_variables(vconfig) end end +# Return the drupalvm extra-field from the passed composer.json +def load_composer_config(path) + cconfig = {} + composer_conf = JSON.parse(File.read(path)) + if composer_conf['extra'] && composer_conf['extra']['drupalvm'] + cconfig = composer_conf['extra']['drupalvm'] + end + cconfig +end + # Return the combined configuration content all files provided. def load_config(files) vconfig = {} From 20db9b5d9f4cb8dbf877240968d5255ee274115d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Sch=C3=B6ldstr=C3=B6m?= Date: Sun, 23 Apr 2017 17:30:28 -0500 Subject: [PATCH 02/13] Write a Composer warning message when upgrading from < 5.0.0 --- composer/src/Plugin.php | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/composer/src/Plugin.php b/composer/src/Plugin.php index 925b49e4c..713ec99b4 100644 --- a/composer/src/Plugin.php +++ b/composer/src/Plugin.php @@ -57,9 +57,35 @@ public function addVagrantfile(Event $event) { if (file_exists($source)) { if (!file_exists($target) || md5_file($source) != md5_file($target)) { + $isLegacy = $this->isLegacyVagrantfile($target); + copy($source, $target); + + $extra = $this->composer->getPackage()->getExtra(); + if ($isLegacy && !isset($extra['drupalvm']['config_dir'])) { + $this->io->writeError( + '' + . 'Drupal VM has been updated and consequently written over your Vagrantfile which from now on will be managed by Drupal VM. ' + . 'Due to this change, you are required to set the `config_dir` location in your composer.json file:' + . "\n" + . "\n $ composer config extra.drupalvm.config_dir " + . "\n" + . '' + ); + } } } } + /** + * Return if the parent project is using the < 5.0.0 delegating Vagrantfile. + * + * @return bool + */ + private function isLegacyVagrantfile($vagrantfile) { + if (!file_exists($vagrantfile)) { + return false; + } + return strpos(file_get_contents($vagrantfile), '# Load the real Vagrantfile') !== false; + } } From 558b82e672eaa58b5a7348742cb3a00e0c1f0f56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Sch=C3=B6ldstr=C3=B6m?= Date: Mon, 24 Apr 2017 15:34:06 -0500 Subject: [PATCH 03/13] Update docs [ci skip] --- docs/deployment/composer-dependency.md | 157 +++---------------- docs/deployment/composer.md | 14 +- docs/deployment/overview.md | 21 +++ docs/getting-started/installation-linux.md | 2 + docs/getting-started/installation-macos.md | 6 +- docs/getting-started/installation-windows.md | 2 + docs/other/wordpress-other-applications.md | 14 +- mkdocs.yml | 14 +- 8 files changed, 60 insertions(+), 170 deletions(-) create mode 100644 docs/deployment/overview.md diff --git a/docs/deployment/composer-dependency.md b/docs/deployment/composer-dependency.md index 8fd48d263..c96822313 100644 --- a/docs/deployment/composer-dependency.md +++ b/docs/deployment/composer-dependency.md @@ -1,151 +1,34 @@ -To make future Drupal VM updates easier to integrate with an existing project, you might consider the more complex setup of installing Drupal VM as a `composer` dependency. Using a delegating `Vagrantfile` you are able to run `vagrant` commands anywhere in your project as well as separate your custom configuration files from Drupal VM's own files. +If you're setting up a new project you can get up and running quickly using [drupal-composer/drupal-project](https://github.com/drupal-composer/drupal-project) as a project template. -### Add Drupal VM as a Composer dependency - -Add Drupal VM as a development dependency to your `composer.json`. - -``` -composer require --dev geerlingguy/drupal-vm -``` - -### Setup your configuration files - -Add and configure the `config.yml` anywhere you like, in this example we place it in a `config/` directory. - -_Note: This will be the directory where Drupal VM looks for other local configuration files as well. Such as `drupal_build_makefile` and `local.config.yml`._ - -``` -├── composer.json -├── config/ -│ ├── config.yml -│ ├── local.config.yml -│ └── Vagrantfile.local -├── docroot/ -│ ├── ... -│ └── index.php -└── vendor/ - ├── ... - └── geerlingguy/ - └── drupal-vm/ -``` - -Change the [build strategy to use your `composer.json`](composer.md#using-composer-when-drupal-vm-is-a-composer-dependency-itself) file by setting: - -```yaml -drupal_build_composer_project: false -drupal_build_composer: true -drupal_composer_path: false -drupal_composer_install_dir: "/var/www/drupalvm" -drupal_core_path: "{{ drupal_composer_install_dir }}/docroot" -``` - -If you intened to use the devel module, it must be added as a requirement to your `composer.json` file. Alternatively, if you do not intend to use it remove it from `drupal_enabled_modules` in your `config.yml` file: - -```yaml -drupal_enabled_modules: [] -``` - -If you're using `pre_provision_scripts` or `post_provision_scripts` you also need to adjust their paths to take into account the new directory structure. The examples used in `default.config.yml` assume the files are located in the Drupal VM directory. You can use the `config_dir` variable which is the absolute path of the directory where your `config.yml` is located. - -```yaml -post_provision_scripts: - # The default provided in `default.config.yml`: - - "../../examples/scripts/configure-solr.sh" - # With Drupal VM as a Composer dependency: - - "{{ config_dir }}/../examples/scripts/configure-solr.sh" -``` - -### Create a delegating `Vagrantfile` - -Create a delegating `Vagrantfile` that will catch all your `vagrant` commands and send them to Drupal VM's own `Vagrantfile`. Place this file in your project's root directory. - -```ruby -# The absolute path to the root directory of the project. Both Drupal VM and -# the config file need to be contained within this path. -ENV['DRUPALVM_PROJECT_ROOT'] = "#{__dir__}" -# The relative path from the project root to the config directory where you -# placed your config.yml file. -ENV['DRUPALVM_CONFIG_DIR'] = "config" -# The relative path from the project root to the directory where Drupal VM is located. -ENV['DRUPALVM_DIR'] = "vendor/geerlingguy/drupal-vm" - -# Load the real Vagrantfile -load "#{__dir__}/#{ENV['DRUPALVM_DIR']}/Vagrantfile" -``` - -When you issue `vagrant` commands anywhere in your project tree this file will be detected and used as a delegator for Drupal VM's own Vagrantfile. - -Your project structure should now look like this: - -``` -├── Vagrantfile -├── composer.json -├── config/ -│ ├── config.yml -│ ├── local.config.yml -│ └── Vagrantfile.local -├── docroot/ -│ ├── ... -│ └── index.php -└── vendor/ - ├── ... - └── geerlingguy/ - └── drupal-vm/ +```sh +composer create-project drupal-composer/drupal-project:8.x-dev --stability dev --no-interaction ``` -### Provision the VM - -Finally provision the VM using the delegating `Vagrantfile`. +### Add Drupal VM as a Composer dependency -```sh -vagrant up -``` +Require Drupal VM as a development dependency to your project. -_Important: you should never issue `vagrant` commands through Drupal VM's own `Vagrantfile` from now on. If you do, it will create a secondary VM in that directory._ + composer require --dev geerlingguy/drupal-vm -## Drupal VM in a subdirectory without composer +_This command will scaffold a `Vagrantfile` in the root of your project. Feel free to add it to your `.gitignore` as the file is now managed by Drupal VM and will be reset each time you run `composer update`._ -If you're not using `composer` in your project you can still download Drupal VM (or add it as a git submodule) to any subdirectory in your project. As an example let's name that directory `box/`. +### Create a `config.yml` to configure the VM -``` -├── docroot/ -│ ├── ... -│ └── index.php -└── box/ - ├── ... - ├── default.config.yml - └── Vagrantfile -``` +Create and configure a config.yml file, eg. in `vm/config.yml`. You'll need at least the following variables: -Configure your `config.yml` as mentioned in the [`composer` section](#setup-your-configuration-files) above. + # Change the build strategy to use a composer.json file. + drupal_build_composer: true + drupal_build_composer_project: false -```yaml -post_provision_scripts: - # The default provided in `default.config.yml`: - - "../../examples/scripts/configure-solr.sh" - # With Drupal VM in a toplevel subdirectory - - "{{ config_dir }}/../examples/scripts/configure-solr.sh" -``` + # Tell Drupal VM that the composer.json file already exists and doesn't need to be transfered. + drupal_composer_path: false -Your directory structure should now look like this: + # Set the location of the composer.json file and Drupal core. + drupal_composer_install_dir: "/var/www/drupalvm" + drupal_core_path: "{{ drupal_composer_install_dir }}/web" -``` -├── Vagrantfile -├── config/ -│ ├── config.yml -│ ├── local.config.yml -│ └── Vagrantfile.local -├── docroot/ -│ ├── ... -│ └── index.php -└── box/ - ├── ... - ├── default.config.yml - └── Vagrantfile -``` +_Note that `drupal_core_path` needs to match your `composer.json` configuration. `drupal-project` uses `web/` whereas Lightning and BLT uses `docroot/`_ -Provision the VM using the delegating `Vagrantfile`. +If you placed the `config.yml` file in a subdirectory, tell Drupal VM where by adding the location to your `composer.json`. If not, Drupal VM will look for all configuration files in the root of your project. -```sh -vagrant up -``` + composer config extra.drupalvm.config_dir 'vm' diff --git a/docs/deployment/composer.md b/docs/deployment/composer.md index fbfb8e967..b3d546f0e 100644 --- a/docs/deployment/composer.md +++ b/docs/deployment/composer.md @@ -11,18 +11,6 @@ drupal_build_composer: true drupal_core_path: "{{ drupal_composer_install_dir }}/web" ``` -_The file set in `drupal_composer_path` (which defaults to `drupal.composer.json`) will be copied from your host computer into the VM's `drupal_composer_install_dir` and renamed `composer.json`._ - -## Using Composer when [Drupal VM is a composer dependency itself](composer-dependency.md) - -In the scenario where you have an existing `composer.json` in the root of your project, follow the usual steps for installing with a composer.json but instead of creating a `drupal.composer.json` file, disable the transfering of the file by setting `drupal_composer_path: false`, and change `drupal_composer_install_dir` to point to the the directory where it will be located. If `drupal_composer_path` is not truthy, Drupal VM assumes it already exists. - -```yaml -drupal_build_composer_project: false -drupal_build_composer: true -drupal_composer_path: false -drupal_composer_install_dir: "/var/www/drupalvm" -drupal_core_path: "{{ drupal_composer_install_dir }}/docroot" -``` +The file set in `drupal_composer_path` (which defaults to `drupal.composer.json`) will be copied from your host computer into the VM's `drupal_composer_install_dir` and renamed `composer.json`. _Opting for composer based installs will most likely increase your VM's time to provision considerably. Find out how you can [improve composer build performance](../other/performance.md#improving-composer-build-performance)._ diff --git a/docs/deployment/overview.md b/docs/deployment/overview.md new file mode 100644 index 000000000..c03ecfd27 --- /dev/null +++ b/docs/deployment/overview.md @@ -0,0 +1,21 @@ +There are two supported methods of integrating Drupal VM with your site. You can either download Drupal VM and integrate your site inside the project, or you can add Drupal VM as a Composer dependency to your existing project. + +## Download Drupal VM and integrate your project. + +The easiest way to get started with Drupal VM is to download the [latest relase](https://www.drupalvm.com/), open the terminal, `cd` to the directory, and type `vagrant up`. + +Using this method you have various options of how your site will be built, or if it will be built by Drupal VM at all: + +- [Build using a local codebase](../deployment/local-codebase.md) +- [Build using a Composer package](../deployment/composer-package.md) (default) +- [Build using a composer.json](../deployment/composer.md) +- [Build using a Drush Make file](../deployment/drush-make.md) +- [Deploy Drupal via Git](../deployment/git.md) + +## Add Drupal VM as a Composer depedency to an existing project + +_If you're using Composer to manage your project, having Drupal VM as dependency makes it easier to pull in future updates._ + +Using this method you only have one option of how your site will be built, it will be built using your parent project's `composer.json` file. + +- [Add Drupal VM to your project using Composer](../deployment/composer-dependency.md) diff --git a/docs/getting-started/installation-linux.md b/docs/getting-started/installation-linux.md index e728426b3..e9462e332 100644 --- a/docs/getting-started/installation-linux.md +++ b/docs/getting-started/installation-linux.md @@ -1,5 +1,7 @@ Please read through the [Quick Start Guiden the README](https://github.com/geerlingguy/drupal-vm#quick-start-guide) to get started. +_Note that there are two methods of using Drupal VM; either you [download Drupal VM and configure the project for your site](../deployment/overview.md#download-drupal-vm-and-integrate-your-project), or you require it as a [Composer dependency in an existing project](../deployment/overview.md#add-drupal-vm-as-a-composer-depedency-to-an-existing-project)._ + For a quick introduction to setting up Drupal VM, the [macOS video tutorial](installation-macos.md) applies somwhat to Linux as well. There are a few caveats when using Drupal VM on Linux, and this page will try to identify the main gotchas or optimization tips for those wishing to use Drupal VM on a Linux host. diff --git a/docs/getting-started/installation-macos.md b/docs/getting-started/installation-macos.md index 66f103ce9..970a2489c 100644 --- a/docs/getting-started/installation-macos.md +++ b/docs/getting-started/installation-macos.md @@ -1,5 +1,7 @@ Please read through the [Quick Start Guide](https://github.com/geerlingguy/drupal-vm#quick-start-guide) to get started. - +_Note that there are two methods of using Drupal VM; either you [download Drupal VM and configure the project for your site](../deployment/overview.md#download-drupal-vm-and-integrate-your-project), or you require it as a [Composer dependency in an existing project](../deployment/overview.md#add-drupal-vm-as-a-composer-depedency-to-an-existing-project)._ + +For a quick overview of how to get started and configuring Drupal VM to build a site, watch the the screencast below. -_In this quick overview, Jeff Geerling will show you where you can learn more about Drupal VM, then show you a simple Drupal VM setup._ + diff --git a/docs/getting-started/installation-windows.md b/docs/getting-started/installation-windows.md index 550751ff0..64d853ec7 100644 --- a/docs/getting-started/installation-windows.md +++ b/docs/getting-started/installation-windows.md @@ -1,5 +1,7 @@ Please read through the [Quick Start Guide in the README](https://github.com/geerlingguy/drupal-vm#quick-start-guide) to get started. +_Note that there are two methods of using Drupal VM; either you [download Drupal VM and configure the project for your site](../deployment/overview.md#download-drupal-vm-and-integrate-your-project), or you require it as a [Composer dependency in an existing project](../deployment/overview.md#add-drupal-vm-as-a-composer-depedency-to-an-existing-project)._ + _In this video, Jeff Geerling walk you through getting a Drupal 8 website built on your Windows 10 laptop using Drupal VM 3._ diff --git a/docs/other/wordpress-other-applications.md b/docs/other/wordpress-other-applications.md index 5714db435..99f893299 100644 --- a/docs/other/wordpress-other-applications.md +++ b/docs/other/wordpress-other-applications.md @@ -46,20 +46,10 @@ composer_global_packages: - { name: wp-cli/wp-cli, release: '^1.0.0' } ``` -Create the delegating `Vagrantfile` in the root of the project: +Tell Drupal VM where this config file is located: ```rb -# The absolute path to the root directory of the project. Both Drupal VM and -# the config file need to be contained within this path. -ENV['DRUPALVM_PROJECT_ROOT'] = "#{__dir__}" -# The relative path from the project root to the config directory where you -# placed your config.yml file. -ENV['DRUPALVM_CONFIG_DIR'] = "config" -# The relative path from the project root to the directory where Drupal VM is located. -ENV['DRUPALVM_DIR'] = "vendor/geerlingguy/drupal-vm" - -# Load the real Vagrantfile -load "#{__dir__}/#{ENV['DRUPALVM_DIR']}/Vagrantfile" +composer config extra.drupalvm.config_dir 'config' ``` Edit your `.env` file to match the values you set in `config/config.yml`: diff --git a/mkdocs.yml b/mkdocs.yml index 46ab76f57..31d46e10b 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -21,12 +21,14 @@ pages: - 'Configuring Drupal VM': 'getting-started/configure-drupalvm.md' - 'Syncing Folders': 'getting-started/syncing-folders.md' - Building your codebase: - - 'Using a local Drupal codebase': 'deployment/local-codebase.md' - - 'Using a composer package': 'deployment/composer-package.md' - - 'Using composer.json': 'deployment/composer.md' - - 'Using a Drush Make file': 'deployment/drush-make.md' - - 'Drupal VM as a Composer Dependency': 'deployment/composer-dependency.md' - - 'Deploying Drupal via Git': 'deployment/git.md' + - 'Overview': 'deployment/overview.md' + - Add your project to Drupal VM: + - 'Using a local Drupal codebase': 'deployment/local-codebase.md' + - 'Using a composer package': 'deployment/composer-package.md' + - 'Using composer.json': 'deployment/composer.md' + - 'Using a Drush Make file': 'deployment/drush-make.md' + - 'Deploying Drupal via Git': 'deployment/git.md' + - 'Add Drupal VM to your project using Composer': 'deployment/composer-dependency.md' - Basic configurations: - 'Using different base OSes': 'configurations/base-os.md' - 'Using a different PHP version': 'configurations/php.md' From 2aa06203cdb7d6f7c20e4c567fe1d52912d97a52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Sch=C3=B6ldstr=C3=B6m?= Date: Mon, 24 Apr 2017 15:34:53 -0500 Subject: [PATCH 04/13] Update scripts docs to apply to both methods of integrating Drupal VM [ci skip] --- docs/extending/scripts.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/docs/extending/scripts.md b/docs/extending/scripts.md index af42cf40d..bcfb5c4b8 100644 --- a/docs/extending/scripts.md +++ b/docs/extending/scripts.md @@ -2,30 +2,30 @@ Drupal VM allows you to run extra shell scripts and ansible task files in the be ## Shell scripts -To use an extra script, configure the path to the script (relative to `provisioning/playbook.yml`) in `config.yml`: +To use an extra script, configure the path to the script (relative to `provisioning/playbook.yml`) in `config.yml`. You can use the `{{ config_dir }}` variable to point to the directory where your `config.yml` is located. ```yaml pre_provision_scripts: - - "../scripts/pre-provision.sh" + - "{{ config_dir }}/scripts/pre-provision.sh" post_provision_scripts: - - "../scripts/post-provision.sh" + - "{{ config_dir }}/scripts/post-provision.sh" ``` The above example results in a `pre-provision.sh` script running before the provisioning starts and a `post-provision.sh` script running after the main Drupal VM setup is complete. Pre and post provision scripts run after the first `vagrant up`, and then any time you run Vagrant provisioning (e.g. `vagrant provision` or `vagrant up --provision`). _Note: The pre provision scripts run before any other packages are installed. If you want to use commands such as `git`, you need to install the packages yourself._ -You can define as many scripts as you would like, and any arguments after the path will be passed to the shell script itself (e.g. `"- "../scripts/setup-paths.sh --option"`). +You can define as many scripts as you would like, and any arguments after the path will be passed to the shell script itself (e.g. `- "{{ config_dir }}/scripts/setup-paths.sh --option"`). Place your pre and post provision scripts inside a `scripts` directory in the root of your Drupal VM project directory; this directory is gitignored, so you can continue to update Drupal VM without overwriting your scripts. ## Ansible task files -To use an extra ansible task file, configure the path to the file (relative to `provisioning/playbook.yml`) in `config.yml`: +To use an extra ansible task file, configure the path to the file in `config.yml`: ```yaml -pre_provision_tasks_dir: "../scripts/pre/*" -post_provision_tasks_dir: "../scripts/post-provision.yml" +pre_provision_tasks_dir: "{{ config_dir }}/scripts/pre/*" +post_provision_tasks_dir: "{{ config_dir }}/scripts/post-provision.yml" ``` The path will be evaluated as a [glob pattern](https://docs.python.org/2/library/glob.html) so you can point to a single file or a directory matching a set of files. From d2b6c9ad2584f4b78f58ed4e2a888f3819224481 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Sch=C3=B6ldstr=C3=B6m?= Date: Mon, 24 Apr 2017 15:35:49 -0500 Subject: [PATCH 05/13] Add a docs note about drupal_build_composer not always being optimal [ci skip] --- docs/deployment/composer.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/deployment/composer.md b/docs/deployment/composer.md index b3d546f0e..553650321 100644 --- a/docs/deployment/composer.md +++ b/docs/deployment/composer.md @@ -1,5 +1,7 @@ Drupal VM is configured to use `composer create-project` to build a Drupal 8 codebase by default but supports building Drupal from a custom `composer.json` file as well. +_Note that if you already have a project built using a `composer.json` file you should instead add [Drupal VM as a dependency](composer-dependency.md) of your project. The method described below will make it somewhat difficult to manage your `composer.json` together with Drupal VM as the file will be copied from `drupal_composer_path` into `drupal_composer_install_dir` as a secondary `composer.json`._ + 1. Copy `example.drupal.composer.json` to `drupal.composer.json` and modify it to your liking. 2. Use the Composer build system by setting `drupal_build_composer: true` in your `config.yml` (make sure `drupal_build_makefile` and `drupal_build_composer_project` are set to `false`). 3. Ensure `drupal_core_path` points to the webroot directory: `drupal_core_path: {{ drupal_composer_install_dir }}/web` From 1c53c9b400b582b2ac38300c823128a2b04a6288 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Sch=C3=B6ldstr=C3=B6m?= Date: Mon, 24 Apr 2017 19:40:10 -0500 Subject: [PATCH 06/13] Add docs on how to patch Drupal VM [ci skip] --- docs/deployment/composer-dependency.md | 4 +++ docs/extending/patching.md | 36 ++++++++++++++++++++++++++ mkdocs.yml | 1 + 3 files changed, 41 insertions(+) create mode 100644 docs/extending/patching.md diff --git a/docs/deployment/composer-dependency.md b/docs/deployment/composer-dependency.md index c96822313..ef9233dd7 100644 --- a/docs/deployment/composer-dependency.md +++ b/docs/deployment/composer-dependency.md @@ -32,3 +32,7 @@ _Note that `drupal_core_path` needs to match your `composer.json` configuration. If you placed the `config.yml` file in a subdirectory, tell Drupal VM where by adding the location to your `composer.json`. If not, Drupal VM will look for all configuration files in the root of your project. composer config extra.drupalvm.config_dir 'vm' + +## Patching Drupal VM + +If you need to patch something in Drupal VM that you're otherwise unable to configure, you can do so with the help of the `composer-patches` plugin. Read the [documentation on how to create and apply patches](../extending/patching.md). diff --git a/docs/extending/patching.md b/docs/extending/patching.md new file mode 100644 index 000000000..a2c6374f2 --- /dev/null +++ b/docs/extending/patching.md @@ -0,0 +1,36 @@ +If you're using Drupal VM as a Composer dependency and need to patch something that you're otherwise unable to configure, you can do so with the help of the [`composer-patches` plugin](https://github.com/cweagans/composer-patches). + +Grab a clone of the Drupal VM version your project is using: + + git clone https://github.com/geerlingguy/drupal-vm.git drupal-vm + cd drupal-vm + git checkout 5.0.0 + +Make the changes you need and create a patch using `git diff`: + + git diff --cached > custom-roles.patch + +Move this file to your project somewhere, eg. `vm/patches/custom-roles.patch`. + +Require the [`composer-patches` plugin](https://github.com/cweagans/composer-patches) as a dependency to your project unless you already have it: + + composer require cweagans/composer-patches + +Add the patch to the `extra`-field in your `composer.json`: + + { + "extra": { + "patches": { + "geerlingguy/drupal-vm": { + "Include custom Drupal VM roles": "vm/patches/custom-roles.patch" + } + } + } + } + +Re-install the Drupal VM package to apply the patch: + + composer remove --dev geerlingguy/drupal-vm + composer require --dev geerlingguy/drupal-vm:5.0.0 + +Your Drupal VM version is now been patched! diff --git a/mkdocs.yml b/mkdocs.yml index 31d46e10b..62acd83e7 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -64,6 +64,7 @@ pages: - 'Adding Vagrant plugins Vagrantfile.local': 'extending/vagrantfile.md' - 'Passing on CLI arguments to ansible': 'extending/ansible-args.md' - 'Pre- and Post-Provision Scripts': 'extending/scripts.md' + - 'Patching Drupal VM': 'extending/patching.md' - Other Information: - 'Networking Notes': 'other/networking.md' - 'Vagrant and VirtualBox': 'other/vagrant-virtualbox.md' From 0da90e2ebb05dfd56f28279e3a8dfc8ca196c429 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Sch=C3=B6ldstr=C3=B6m?= Date: Tue, 25 Apr 2017 20:31:50 -0500 Subject: [PATCH 07/13] Drop host composer dependency --- Vagrantfile | 1 - lib/drupalvm/vagrant.rb | 10 ++++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/Vagrantfile b/Vagrantfile index bb4fd44f6..711fd867b 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -13,7 +13,6 @@ if File.exist?("#{host_project_dir}/composer.json") cconfig = load_composer_config("#{host_project_dir}/composer.json") # If Drupal VM is a Composer dependency set the correct path. - vendor_dir = `composer config vendor-dir`.strip drupalvm_path = "#{vendor_dir}/geerlingguy/drupal-vm" if Dir.exist?("#{host_project_dir}/#{drupalvm_path}") host_drupalvm_dir = "#{host_project_dir}/#{drupalvm_path}" diff --git a/lib/drupalvm/vagrant.rb b/lib/drupalvm/vagrant.rb index f19d62b12..ffb151a22 100644 --- a/lib/drupalvm/vagrant.rb +++ b/lib/drupalvm/vagrant.rb @@ -66,6 +66,16 @@ def ansible_version /^[^\s]+ (.+)$/.match(`#{ansible_bin} --version`) { |match| return match[1] } end +# Return the path to the composer executable +def composer_bin + @composer_bin ||= which('composer') +end + +# Return Composer's vendor directory. +def vendor_dir + @vendor_dir ||= composer_bin ? `#{composer_bin} config vendor-dir`.strip : 'vendor' +end + # Require that if installed, the ansible version meets the requirements. def require_ansible_version(requirement) return unless ansible_bin From 9a8b183b1cbd5a799f8fd4508e75fc39f146f586 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Sch=C3=B6ldstr=C3=B6m?= Date: Tue, 25 Apr 2017 20:52:52 -0500 Subject: [PATCH 08/13] Revert previous commit and fix the loading of Drupal VM ruby lib --- Vagrantfile | 11 ++++++++--- lib/drupalvm/vagrant.rb | 20 -------------------- 2 files changed, 8 insertions(+), 23 deletions(-) diff --git a/Vagrantfile b/Vagrantfile index 711fd867b..0ae1deb52 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -1,8 +1,6 @@ # -*- mode: ruby -*- # vi: set ft=ruby : -require_relative 'lib/drupalvm/vagrant' - drupalvm_env = ENV['DRUPALVM_ENV'] || 'vagrant' # Default paths when the project is based on Drupal VM. @@ -10,9 +8,14 @@ host_project_dir = host_drupalvm_dir = host_config_dir = __dir__ guest_project_dir = guest_drupalvm_dir = guest_config_dir = '/vagrant' if File.exist?("#{host_project_dir}/composer.json") - cconfig = load_composer_config("#{host_project_dir}/composer.json") + cconfig = {} + composer_conf = JSON.parse(File.read("#{host_project_dir}/composer.json")) + if composer_conf['extra'] && composer_conf['extra']['drupalvm'] + cconfig = composer_conf['extra']['drupalvm'] + end # If Drupal VM is a Composer dependency set the correct path. + vendor_dir = composer_conf.fetch('extra', {}).fetch('vendor-dir', 'vendor') drupalvm_path = "#{vendor_dir}/geerlingguy/drupal-vm" if Dir.exist?("#{host_project_dir}/#{drupalvm_path}") host_drupalvm_dir = "#{host_project_dir}/#{drupalvm_path}" @@ -26,6 +29,8 @@ if File.exist?("#{host_project_dir}/composer.json") end end +require "#{host_drupalvm_dir}/lib/drupalvm/vagrant" + default_config_file = "#{host_drupalvm_dir}/default.config.yml" unless File.exist?(default_config_file) raise_message "Configuration file not found! Expected in #{default_config_file}" diff --git a/lib/drupalvm/vagrant.rb b/lib/drupalvm/vagrant.rb index ffb151a22..9a4c47feb 100644 --- a/lib/drupalvm/vagrant.rb +++ b/lib/drupalvm/vagrant.rb @@ -34,16 +34,6 @@ def resolve_jinja_variables(vconfig) end end -# Return the drupalvm extra-field from the passed composer.json -def load_composer_config(path) - cconfig = {} - composer_conf = JSON.parse(File.read(path)) - if composer_conf['extra'] && composer_conf['extra']['drupalvm'] - cconfig = composer_conf['extra']['drupalvm'] - end - cconfig -end - # Return the combined configuration content all files provided. def load_config(files) vconfig = {} @@ -66,16 +56,6 @@ def ansible_version /^[^\s]+ (.+)$/.match(`#{ansible_bin} --version`) { |match| return match[1] } end -# Return the path to the composer executable -def composer_bin - @composer_bin ||= which('composer') -end - -# Return Composer's vendor directory. -def vendor_dir - @vendor_dir ||= composer_bin ? `#{composer_bin} config vendor-dir`.strip : 'vendor' -end - # Require that if installed, the ansible version meets the requirements. def require_ansible_version(requirement) return unless ansible_bin From 5df9430274e2c54823856f68d387c9a5ecad6912 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Sch=C3=B6ldstr=C3=B6m?= Date: Tue, 25 Apr 2017 22:12:35 -0500 Subject: [PATCH 09/13] Take into account COMPOSER_VENDOR_DIR env variable --- Vagrantfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Vagrantfile b/Vagrantfile index 0ae1deb52..b6d01a33a 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -15,7 +15,7 @@ if File.exist?("#{host_project_dir}/composer.json") end # If Drupal VM is a Composer dependency set the correct path. - vendor_dir = composer_conf.fetch('extra', {}).fetch('vendor-dir', 'vendor') + vendor_dir = ENV['COMPOSER_VENDOR_DIR'] || composer_conf.fetch('extra', {}).fetch('vendor-dir', 'vendor') drupalvm_path = "#{vendor_dir}/geerlingguy/drupal-vm" if Dir.exist?("#{host_project_dir}/#{drupalvm_path}") host_drupalvm_dir = "#{host_project_dir}/#{drupalvm_path}" From 25a7650fa032fc5c88935de1d434589c815dce2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Sch=C3=B6ldstr=C3=B6m?= Date: Fri, 2 Jun 2017 00:13:53 -0500 Subject: [PATCH 10/13] fix vendor-dir location in composer.json --- Vagrantfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Vagrantfile b/Vagrantfile index b6d01a33a..a636e954b 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -15,7 +15,7 @@ if File.exist?("#{host_project_dir}/composer.json") end # If Drupal VM is a Composer dependency set the correct path. - vendor_dir = ENV['COMPOSER_VENDOR_DIR'] || composer_conf.fetch('extra', {}).fetch('vendor-dir', 'vendor') + vendor_dir = ENV['COMPOSER_VENDOR_DIR'] || composer_conf.fetch('config', {}).fetch('vendor-dir', 'vendor') drupalvm_path = "#{vendor_dir}/geerlingguy/drupal-vm" if Dir.exist?("#{host_project_dir}/#{drupalvm_path}") host_drupalvm_dir = "#{host_project_dir}/#{drupalvm_path}" From e27c427a69ec481a30f9a58546acdfe6295a0d7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Sch=C3=B6ldstr=C3=B6m?= Date: Fri, 2 Jun 2017 00:15:10 -0500 Subject: [PATCH 11/13] fix rebase miss --- Vagrantfile | 2 ++ lib/drupalvm/vagrant.rb | 1 - 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Vagrantfile b/Vagrantfile index a636e954b..da2edb86d 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -1,6 +1,8 @@ # -*- mode: ruby -*- # vi: set ft=ruby : +require 'json' + drupalvm_env = ENV['DRUPALVM_ENV'] || 'vagrant' # Default paths when the project is based on Drupal VM. diff --git a/lib/drupalvm/vagrant.rb b/lib/drupalvm/vagrant.rb index 9a4c47feb..beaa3c432 100644 --- a/lib/drupalvm/vagrant.rb +++ b/lib/drupalvm/vagrant.rb @@ -1,4 +1,3 @@ -require 'json' require 'yaml' # Cross-platform way of finding an executable in the $PATH. From 5592ed1d70d1d4933aa3b9b4dd47f7b5fdd6c798 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Sch=C3=B6ldstr=C3=B6m?= Date: Fri, 2 Jun 2017 00:18:11 -0500 Subject: [PATCH 12/13] typo --- docs/extending/patching.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/extending/patching.md b/docs/extending/patching.md index a2c6374f2..af08732b7 100644 --- a/docs/extending/patching.md +++ b/docs/extending/patching.md @@ -33,4 +33,4 @@ Re-install the Drupal VM package to apply the patch: composer remove --dev geerlingguy/drupal-vm composer require --dev geerlingguy/drupal-vm:5.0.0 -Your Drupal VM version is now been patched! +Your Drupal VM version is now patched! From fba5045d7208d232758f2eb960d54c4b1ec7e9b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Sch=C3=B6ldstr=C3=B6m?= Date: Fri, 2 Jun 2017 00:24:56 -0500 Subject: [PATCH 13/13] typo [ci skip] --- docs/deployment/overview.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/deployment/overview.md b/docs/deployment/overview.md index c03ecfd27..cb4394a5b 100644 --- a/docs/deployment/overview.md +++ b/docs/deployment/overview.md @@ -2,7 +2,7 @@ There are two supported methods of integrating Drupal VM with your site. You can ## Download Drupal VM and integrate your project. -The easiest way to get started with Drupal VM is to download the [latest relase](https://www.drupalvm.com/), open the terminal, `cd` to the directory, and type `vagrant up`. +The easiest way to get started with Drupal VM is to download the [latest release](https://www.drupalvm.com/), open the terminal, `cd` to the directory, and type `vagrant up`. Using this method you have various options of how your site will be built, or if it will be built by Drupal VM at all: