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

Commit 570ca42

Browse files
committed
Add Composer plugin which scaffolds a Vagtranfile
1 parent f1c07ee commit 570ca42

File tree

3 files changed

+101
-3
lines changed

3 files changed

+101
-3
lines changed

Vagrantfile

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# -*- mode: ruby -*-
22
# vi: set ft=ruby :
3+
4+
require 'json'
5+
require 'yaml'
6+
37
VAGRANTFILE_API_VERSION = '2' unless defined? VAGRANTFILE_API_VERSION
48

59
# Absolute paths on the host machine.
@@ -40,7 +44,26 @@ def walk(obj, &fn)
4044
end
4145
end
4246

43-
require 'yaml'
47+
if File.exist?("#{host_project_dir}/composer.json")
48+
composer_conf = JSON.parse(File.read("#{host_project_dir}/composer.json"))
49+
cconfig = composer_conf['extra']['drupalvm'] rescue Hash.new
50+
cconfig = Hash.new if cconfig.nil?
51+
52+
# If Drupal VM is a Composer dependency set the correct paths.
53+
if Dir.exists?("#{host_drupalvm_dir}/vendor/geerlingguy/drupal-vm")
54+
host_project_dir = File.dirname(File.expand_path(__FILE__))
55+
host_drupalvm_dir = "#{host_project_dir}/vendor/geerlingguy/drupal-vm"
56+
host_config_dir = ENV['DRUPALVM_CONFIG_DIR'] ? "#{host_project_dir}/#{ENV['DRUPALVM_CONFIG_DIR']}" : host_project_dir
57+
guest_drupalvm_dir = '/vagrant/vendor/geerlingguy/drupal-vm'
58+
end
59+
60+
# Read config_dir from composer.json if set.
61+
if !ENV['DRUPALVM_CONFIG_DIR'] && cconfig.include?('config_dir')
62+
host_config_dir = "#{host_project_dir}/#{cconfig['config_dir']}"
63+
guest_config_dir = "/vagrant/#{cconfig['config_dir']}"
64+
end
65+
end
66+
4467
# Load default VM configurations.
4568
vconfig = YAML.load_file("#{host_drupalvm_dir}/default.config.yml")
4669
# Use optional config.yml and local.config.yml for configuration overrides.

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+
}

0 commit comments

Comments
 (0)