Skip to content
This repository was archived by the owner on Feb 13, 2023. It is now read-only.

Commit 12e7115

Browse files
committed
Add Composer plugin which scaffolds a Vagtranfile
1 parent f27806c commit 12e7115

File tree

4 files changed

+109
-11
lines changed

4 files changed

+109
-11
lines changed

Vagrantfile

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,29 @@
33

44
require './lib/drupalvm/vagrant'
55

6-
# Absolute paths on the host machine.
7-
host_drupalvm_dir = File.dirname(File.expand_path(__FILE__))
8-
host_project_dir = ENV['DRUPALVM_PROJECT_ROOT'] || host_drupalvm_dir
9-
host_config_dir = ENV['DRUPALVM_CONFIG_DIR'] ? "#{host_project_dir}/#{ENV['DRUPALVM_CONFIG_DIR']}" : host_project_dir
6+
drupalvm_env = ENV['DRUPALVM_ENV'] || 'vagrant'
107

11-
# Absolute paths on the guest machine.
12-
guest_project_dir = '/vagrant'
13-
guest_drupalvm_dir = ENV['DRUPALVM_DIR'] ? "/vagrant/#{ENV['DRUPALVM_DIR']}" : guest_project_dir
14-
guest_config_dir = ENV['DRUPALVM_CONFIG_DIR'] ? "/vagrant/#{ENV['DRUPALVM_CONFIG_DIR']}" : guest_project_dir
8+
# Default paths when the project is based on Drupal VM.
9+
host_project_dir = host_drupalvm_dir = host_config_dir = __dir__
10+
guest_project_dir = guest_drupalvm_dir = guest_config_dir = '/vagrant'
1511

16-
drupalvm_env = ENV['DRUPALVM_ENV'] || 'vagrant'
12+
if File.exist?("#{host_project_dir}/composer.json")
13+
cconfig = load_composer_config("#{host_project_dir}/composer.json")
14+
15+
# If Drupal VM is a Composer dependency set the correct path.
16+
vendor_dir = `composer config vendor-dir`.strip
17+
drupalvm_path = "#{vendor_dir}/geerlingguy/drupal-vm"
18+
if Dir.exist?("#{host_project_dir}/#{drupalvm_path}")
19+
host_drupalvm_dir = "#{host_project_dir}/#{drupalvm_path}"
20+
guest_drupalvm_dir = "#{guest_project_dir}/#{drupalvm_path}"
21+
end
22+
23+
# Read config_dir from composer.json if set.
24+
if cconfig.include?('config_dir')
25+
host_config_dir = "#{host_project_dir}/#{cconfig['config_dir']}"
26+
guest_config_dir = "#{guest_project_dir}/#{cconfig['config_dir']}"
27+
end
28+
end
1729

1830
default_config_file = "#{host_drupalvm_dir}/default.config.yml"
1931
unless File.exist?(default_config_file)

composer.json

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "geerlingguy/drupal-vm",
3-
"type": "vm",
3+
"type": "composer-plugin",
44
"description": "A VM for local Drupal development, built with Vagrant + Ansible",
55
"keywords": ["vagrant", "vm", "virtual machine", "drupal"],
66
"homepage": "https://www.drupalvm.com",
@@ -20,5 +20,15 @@
2020
"source": "https://github.com/geerlingguy/drupal-vm",
2121
"docs": "http://docs.drupalvm.com"
2222
},
23-
"require": {}
23+
"require": {
24+
"composer-plugin-api": "^1.0"
25+
},
26+
"autoload": {
27+
"psr-4": {
28+
"JJG\\DrupalVM\\": "composer/src/"
29+
}
30+
},
31+
"extra": {
32+
"class": "JJG\\DrupalVM\\Plugin"
33+
}
2434
}

composer/src/Plugin.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
/**
3+
* @file
4+
* Contains JJG\DrupalVM|Plugin.
5+
*/
6+
7+
namespace JJG\DrupalVM;
8+
9+
use Composer\Composer;
10+
use Composer\EventDispatcher\EventSubscriberInterface;
11+
use Composer\Factory;
12+
use Composer\IO\IOInterface;
13+
use Composer\Plugin\PluginInterface;
14+
use Composer\Script\Event;
15+
use Composer\Script\ScriptEvents;
16+
17+
class Plugin implements PluginInterface, EventSubscriberInterface {
18+
19+
/**
20+
* @var \Composer\Composer
21+
*/
22+
protected $composer;
23+
24+
/**
25+
* @var \Composer\IO\IOInterface
26+
*/
27+
protected $io;
28+
29+
/**
30+
* {@inheritdoc}
31+
*/
32+
public function activate(Composer $composer, IOInterface $io) {
33+
$this->composer = $composer;
34+
$this->io = $io;
35+
}
36+
37+
/**
38+
* {@inheritdoc}
39+
*/
40+
public static function getSubscribedEvents() {
41+
return array(
42+
ScriptEvents::POST_INSTALL_CMD => 'addVagrantfile',
43+
ScriptEvents::POST_UPDATE_CMD => 'addVagrantfile',
44+
);
45+
}
46+
47+
/**
48+
* Add/update project Vagrantfile.
49+
*
50+
* @param \Composer\Script\Event $event
51+
*/
52+
public function addVagrantfile(Event $event) {
53+
54+
$baseDir = dirname(Factory::getComposerFile());
55+
$source = __DIR__ . '/../../Vagrantfile';
56+
$target = $baseDir . '/Vagrantfile';
57+
58+
if (file_exists($source)) {
59+
if (!file_exists($target) || md5_file($source) != md5_file($target)) {
60+
copy($source, $target);
61+
}
62+
}
63+
}
64+
65+
}

lib/drupalvm/vagrant.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
require 'json'
12
require 'yaml'
23

34
# Cross-platform way of finding an executable in the $PATH.
@@ -33,6 +34,16 @@ def resolve_jinja_variables(vconfig)
3334
end
3435
end
3536

37+
# Return the drupalvm extra-field from the passed composer.json
38+
def load_composer_config(path)
39+
cconfig = {}
40+
composer_conf = JSON.parse(File.read(path))
41+
if composer_conf['extra'] && composer_conf['extra']['drupalvm']
42+
cconfig = composer_conf['extra']['drupalvm']
43+
end
44+
cconfig
45+
end
46+
3647
# Return the combined configuration content all files provided.
3748
def load_config(files)
3849
vconfig = {}

0 commit comments

Comments
 (0)