Skip to content

Commit

Permalink
Fixed composer scripts. (#5)
Browse files Browse the repository at this point in the history
* Fixed composer script handler not correctly working within container install.
  • Loading branch information
AlexSkrypnyk authored Feb 5, 2019
1 parent 66fc6b6 commit aa759f7
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 15 deletions.
5 changes: 3 additions & 2 deletions composer.dev.json
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,13 @@
],
"post-install-cmd": [
"DrupalComposer\\DrupalScaffold\\Plugin::scaffold",
"rm -f docroot/sites/default/settings.php",
"DrupalProject\\composer\\ScriptHandler::createRequiredFiles",
"Utilities\\composer\\DrupalSettings::create"
],
"post-update-cmd": [
"DrupalProject\\composer\\ScriptHandler::createRequiredFiles"
"DrupalComposer\\DrupalScaffold\\Plugin::scaffold",
"DrupalProject\\composer\\ScriptHandler::createRequiredFiles",
"Utilities\\composer\\DrupalSettings::create"
]
},
"extra": {
Expand Down
66 changes: 53 additions & 13 deletions scripts/composer/ScriptHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,36 @@ public static function createRequiredFiles(Event $event) {
}

// Prepare the settings file for installation.
if (!$fs->exists($drupalRoot . '/sites/default/settings.php') and $fs->exists($drupalRoot . '/sites/default/default.settings.php')) {
if ($fs->exists($drupalRoot . '/sites/default/default.settings.php') && !$fs->exists($drupalRoot . '/sites/default/settings.php')) {
$fs->copy($drupalRoot . '/sites/default/default.settings.php', $drupalRoot . '/sites/default/settings.php');
require_once $drupalRoot . '/core/includes/bootstrap.inc';
require_once $drupalRoot . '/core/includes/install.inc';
$settings['config_directories'] = [
CONFIG_SYNC_DIRECTORY => (object) [
'value' => Path::makeRelative($drupalFinder->getComposerRoot() . '/config/default', $drupalRoot),
'required' => TRUE,
],
];
drupal_rewrite_settings($settings, $drupalRoot . '/sites/default/settings.php');
$fs->chmod($drupalRoot . '/sites/default/settings.php', 0644);
$event->getIO()->write("Create a sites/default/settings.php file with chmod 0644");
$event->getIO()->write('Created a sites/default/settings.php file');
}

if (!$fs->exists($drupalRoot . '/sites/default/settings.php')) {
$event->getIO()->writeError('<error>Settings file not found</error>');
exit(1);
}

$fs->chmod($drupalRoot . '/sites/default', 0777);
$fs->chmod($drupalRoot . '/sites/default/settings.php', 0666);

$configPath = Path::makeRelative($drupalFinder->getComposerRoot() . '/config/default', $drupalRoot);
$settings_string = <<<SETTINGS
\$settings['config_directories'] = [
CONFIG_SYNC_DIRECTORY => (object) [
'value' => '$configPath',
'required' => TRUE,
],
];
SETTINGS;
self::appendToFile($drupalRoot . '/sites/default/settings.php', $settings_string);

// Create the files directory with chmod 0777.
if (!$fs->exists($drupalRoot . '/sites/default/files')) {
$oldmask = umask(0);
$fs->mkdir($drupalRoot . '/sites/default/files', 0777);
umask($oldmask);
$event->getIO()->write("Create a sites/default/files directory with chmod 0777");
$event->getIO()->write('Created a sites/default/files directory with chmod 0777');
}
}

Expand Down Expand Up @@ -100,4 +109,35 @@ public static function checkComposerVersion(Event $event) {
}
}

/**
* Appends content to an existing file.
*
* Polyfill for older versions of Filesystem shipped with Composer phar.
*
* @param string $filename
* The file to which to append content.
* @param string $content
* The content to append.
*
* @throws \Symfony\Component\Filesystem\Exception\IOException
* If the file is not writable.
*/
protected static function appendToFile($filename, $content) {
$fs = new Filesystem();

$dir = \dirname($filename);

if (!is_dir($dir)) {
$fs->mkdir($dir);
}

if (!is_writable($dir)) {
throw new \Exception(sprintf('Unable to write to the "%s" directory.', $dir), 0, NULL, $dir);
}

if (FALSE === @file_put_contents($filename, $content, FILE_APPEND)) {
throw new \Exception(sprintf('Failed to write file "%s".', $filename), 0, NULL, $filename);
}
}

}

0 comments on commit aa759f7

Please sign in to comment.