Skip to content

Commit

Permalink
Consolidate scaffolding
Browse files Browse the repository at this point in the history
* Consolidate scaffolding into this project.

* Add consolidated scaffold files.

* Fix typo.

* Add missing path to file existance check.

* Fixed incorrectly named function.

* Add missing filesystem dep, refactor to use.

* Changing docker-compose.yml file to copy only if absent.

* Add static function back in to enable calling script manually.

* Fixed RoboFile.php to include default install profile.

* Add bin dir config to composer.json.
  • Loading branch information
caseyfw authored Aug 30, 2017
1 parent 6e8914b commit 88b6ecd
Show file tree
Hide file tree
Showing 5 changed files with 289 additions and 24 deletions.
37 changes: 37 additions & 0 deletions RoboFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/**
* @file
* Contains \Robo\RoboFile.
*
* Implementation of class for Robo - http://robo.li/
*
* You may override methods provided by RoboFileBase.php in this file.
* Configuration overrides should be made in the constructor.
*/

include_once 'RoboFileBase.php';

/**
* Class RoboFile.
*/
class RoboFile extends RoboFileBase
{

/**
* {@inheritdoc}
*/
public function __construct()
{
parent::__construct();
// Put project specific overrides here, below the parent constructor.
}

/**
* {@inheritdoc}
*/
protected function getDrupalProfile()
{
// Replace this with the profile of your choice.
return "standard";
}
}
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
"require": {
"php": ">=5.4.5",
"composer-plugin-api": "^1.0.0",
"drupal-composer/drupal-scaffold": "^2.2"
"drupal-composer/drupal-scaffold": "^2.2",
"symfony/filesystem": "~2.8|~3.0"
},
"config": {
"bin-dir": "bin/"
},
"autoload": {
"psr-4": {
Expand Down
40 changes: 40 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
version: '2'
services:
web:
image: uofa/apache2-php7-dev:shepherd
hostname: ${PROJECT}
environment:
SITE_TITLE: Drupal site
SITE_MAIL: [email protected]
SITE_ADMIN_EMAIL: [email protected]
SITE_ADMIN_USERNAME: admin
SITE_ADMIN_PASSWORD: password
VIRTUAL_HOST: ${PROJECT}.${DOMAIN}
SSH_AUTH_SOCK: ${CONTAINER_SSH_AUTH_SOCK}
DATABASE_HOST: db
DATABASE_PORT: 3306
DATABASE_NAME: drupal
DATABASE_USER: user
DATABASE_PASSWORD: password
PRIVATE_DIR: /shared/private
PUBLIC_DIR: web/sites/default/files
HASH_SALT: random-hash
CONFIG_SYNC_DIRECTORY: /shared/private/random-hash/sync
volumes:
- .:/code
- shared:/shared
- ${HOST_SSH_AUTH_SOCK_DIR}:/ssh
db:
image: mariadb
environment:
MYSQL_DATABASE: drupal
MYSQL_USER: user
MYSQL_PASSWORD: password
MYSQL_ROOT_PASSWORD: super-secret-password
mail:
image: helder/mailcatcher
environment:
- VIRTUAL_HOST=mail.${PROJECT}.${DOMAIN}
volumes:
shared:
ssh:
225 changes: 204 additions & 21 deletions src/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@

use Composer\Composer;
use Composer\IO\IOInterface;
use Composer\Util\Filesystem;
use Composer\Util\RemoteFilesystem;
use DrupalComposer\DrupalScaffold\FileFetcher;
use Symfony\Component\Filesystem\Filesystem as SymfonyFilesystem;
use Composer\Util\Filesystem as ComposerFilesystem;
use Symfony\Component\Filesystem\Filesystem;

class Handler
{
Expand All @@ -22,6 +20,11 @@ class Handler
*/
protected $io;

/**
* @var \Symfony\Component\Filesystem\Filesystem
*/
protected $filesystem;

/**
* Handler constructor.
*
Expand All @@ -32,6 +35,7 @@ public function __construct(Composer $composer, IOInterface $io)
{
$this->composer = $composer;
$this->io = $io;
$this->filesystem = new Filesystem();
}

/**
Expand All @@ -41,28 +45,207 @@ public function __construct(Composer $composer, IOInterface $io)
*/
public function onPostCmdEvent(\Composer\Script\Event $event)
{
$this->downloadScaffold();
$event->getIO()->write("Updating Shepherd scaffold files.");
$this->updateShepherdScaffoldFiles();
$event->getIO()->write("Creating necessary directories.");
$this->createDirectories();
$event->getIO()->write("Creating settings.php file if not present.");
$this->createSettingsFile();
$event->getIO()->write("Creating services.yml file if not present.");
$this->createServicesFile();
}

/**
* Downloads Shepherd Drupal scaffold files.
* Update the Shepherd scaffold files.
*/
public function downloadScaffold()
public function updateShepherdScaffoldFiles()
{
$source = 'https://raw.githubusercontent.com/universityofadelaide/shepherd-drupal-scaffold/{version}/{path}';
$filenames = [
'dsh',
'RoboFileBase.php',
];
$version = 'master';
$destination = dirname($this->composer->getConfig()
->get('vendor-dir'));

$fetcher = new FileFetcher(
new RemoteFilesystem($this->io),
$source,
$filenames
$packagePath = $this->getPackagePath();
$projectPath = $this->getProjectPath();

// Always copy and replace these files.
$this->copyFiles(
$packagePath,
$projectPath,
[
'dsh',
'RoboFileBase.php',
],
true
);
$fetcher->fetch($version, $destination);

// Only copy these files if they do not exist at the destination.
$this->copyFiles(
$packagePath,
$projectPath,
[
'docker-compose.yml',
'RoboFile.php',
]
);
}

/**
* Ensure necessary directories exist.
*/
public function createDirectories()
{
$root = $this->getDrupalRootPath();
$dirs = [
'modules',
'profiles',
'themes',
];

// Required for unit testing.
foreach ($dirs as $dir) {
if (!$this->filesystem->exists($root . '/'. $dir)) {
$this->filesystem->mkdir($root . '/'. $dir);
$this->filesystem->touch($root . '/'. $dir . '/.gitkeep');
}
}
}

/**
* Create settings.php file and inject Shepherd-specific settings.
*
* Note: does nothing if the file already exists.
*/
public function createSettingsFile()
{
$root = $this->getDrupalRootPath();

// If the settings.php is not present, and the default version is...
if (!$this->filesystem->exists($root . '/sites/default/settings.php') && $this->filesystem->exists($root . '/sites/default/default.settings.php')) {
$this->filesystem->copy($root . '/sites/default/default.settings.php', $root . '/sites/default/settings.php');
$this->filesystem->chmod($root . '/sites/default/settings.php', 0666);

$shepherdSettings = "\n/**\n * START SHEPHERD CONFIG\n */\n" .
"\$databases['default']['default'] = array (\n" .
" 'database' => getenv('DATABASE_NAME'),\n" .
" 'username' => getenv('DATABASE_USER'),\n" .
" 'password' => getenv('DATABASE_PASSWORD_FILE') ? file_get_contents(getenv('DATABASE_PASSWORD_FILE')) : getenv('DATABASE_PASSWORD'),\n" .
" 'host' => getenv('DATABASE_HOST'),\n" .
" 'port' => getenv('DATABASE_PORT') ?: '3306',\n" .
" 'driver' => getenv('DATABASE_DRIVER') ?: 'mysql',\n" .
" 'prefix' => getenv('DATABASE_PREFIX') ?: '',\n" .
" 'collation' => getenv('DATABASE_COLLATION') ?: 'utf8mb4_general_ci',\n" .
" 'namespace' => getenv('DATABASE_NAMESPACE') ?: 'Drupal\\\\Core\\\\Database\\\\Driver\\\\mysql',\n" .
");\n" .
"\$settings['file_private_path'] = getenv('PRIVATE_DIR');\n" .
"\$settings['hash_salt'] = getenv('HASH_SALT') ?: '" . str_replace(['+', '/', '='], ['-', '_', ''], base64_encode(random_bytes(55))) . "';\n" .
"\$config_directories['sync'] = getenv('CONFIG_SYNC_DIRECTORY') ?: 'sites/default/files/config_" . str_replace(['+', '/', '='], ['-', '_', ''], base64_encode(random_bytes(55))) . "/sync';\n" .
"if (! is_dir(\$app_root . '/' . \$config_directories['sync'])) mkdir(\$app_root . '/' . \$config_directories['sync'], 0777, true);\n" .
"\$settings['shepherd_site_id'] = getenv('SHEPHERD_SITE_ID');\n" .
"\$settings['shepherd_url'] = getenv('SHEPHERD_URL');\n" .
"\$settings['shepherd_token'] = getenv('SHEPHERD_TOKEN_FILE') ? file_get_contents(getenv('SHEPHERD_TOKEN_FILE')) : getenv('SHEPHERD_TOKEN');\n" .
"/**\n * END SHEPHERD CONFIG\n */\n" .
"\n" .
"/**\n * START LOCAL CONFIG\n */\n" .
"if (file_exists(__DIR__ . '/settings.local.php')) {\n" .
" include __DIR__ . '/settings.local.php';\n" .
"}\n" .
"/**\n * END LOCAL CONFIG\n */\n"
;

// Append Shepherd-specific environment variable settings to settings.php.
file_put_contents(
$root . '/sites/default/settings.php',
$shepherdSettings,
FILE_APPEND
);
}
}

/**
* Create services.yml file if not present.
*/
public function createServicesFile()
{
$root = $this->getDrupalRootPath();

if (!$this->filesystem->exists($root . '/sites/default/services.yml') && $this->filesystem->exists($root . '/sites/default/default.services.yml')) {
$this->filesystem->copy($root . '/sites/default/default.services.yml', $root . '/sites/default/services.yml');
$this->filesystem->chmod($root . '/sites/default/services.yml', 0666);
}
}

/**
* Copy files from origin to destination, optionally overwritting existing.
*
* @param bool $overwriteExisting
* If true, replace existing files. Defaults to false.
*/
public function copyFiles($origin, $destination, $filenames, $overwriteExisting = false)
{
foreach ($filenames as $filename) {
// Skip copying files that already exist at the destination.
if (! $overwriteExisting && $this->filesystem->exists($destination . '/' . $filename)) {
continue;
}
$this->filesystem->copy(
$origin . '/' . $filename,
$destination . '/' . $filename,
true
);
}
}

/**
* Get the path to the vendor directory.
*
* E.g. /home/user/code/project/vendor
*
* @return string
*/
public function getVendorPath()
{
// Load ComposerFilesystem to get access to path normalisation.
$composerFilesystem = new ComposerFilesystem();

$config = $this->composer->getConfig();
$composerFilesystem->ensureDirectoryExists($config->get('vendor-dir'));
$vendorPath = $composerFilesystem->normalizePath(realpath($config->get('vendor-dir')));

return $vendorPath;
}

/**
* Get the path to the project directory.
*
* E.g. /home/user/code/project
*
* @return string
*/
public function getProjectPath()
{
$projectPath = dirname($this->getVendorPath());
return $projectPath;
}

/**
* Get the path to the package directory.
*
* E.g. /home/user/code/project/vendor/universityofadelaide/shepherd-drupal-scaffold
*
* @return string
*/
public function getPackagePath()
{
$packagePath = $this->getVendorPath() . '/universityofadelaide/shepherd-drupal-scaffold';
return $packagePath;
}

/**
* Get the path to the Drupal root directory.
*
* E.g. /home/user/code/project/web
*
* @return string
*/
public function getDrupalRootPath()
{
$drupalRootPath = $this->getProjectPath() . '/web';
return $drupalRootPath;
}
}
5 changes: 3 additions & 2 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function activate(Composer $composer, IOInterface $io)
public static function getSubscribedEvents()
{
return array(
ScriptEvents::POST_INSTALL_CMD => 'postCmd',
ScriptEvents::POST_UPDATE_CMD => 'postCmd',
);
}
Expand All @@ -58,10 +59,10 @@ public function postCmd(\Composer\Script\Event $event)
* scaffold files.
*
* @param \Composer\Script\Event $event
*/
*/
public static function scaffold(\Composer\Script\Event $event)
{
$handler = new Handler($event->getComposer(), $event->getIO());
$handler->downloadScaffold();
$handler->onPostCmdEvent($event);
}
}

0 comments on commit 88b6ecd

Please sign in to comment.