Skip to content

WIP: Add setup wizard? #384

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@
},
"autoload": {
"classmap": [
"scripts/composer/ScriptHandler.php"
"scripts/composer/ScriptHandler.php",
"scripts/composer/Setup.php"
],
"files": ["load.environment.php"]
},
"scripts": {
"drupal-scaffold": "DrupalComposer\\DrupalScaffold\\Plugin::scaffold",
"setup": "DrupalProject\\composer\\Setup::setup",
"pre-install-cmd": [
"DrupalProject\\composer\\ScriptHandler::checkComposerVersion"
],
Expand All @@ -56,6 +58,9 @@
],
"post-update-cmd": [
"DrupalProject\\composer\\ScriptHandler::createRequiredFiles"
],
"post-root-package-install": [
"@setup"
]
},
"extra": {
Expand Down
69 changes: 69 additions & 0 deletions scripts/composer/Setup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace DrupalProject\composer;

use Composer\Json\JsonFile;
use Composer\Script\Event;
use Symfony\Component\Filesystem\Filesystem;

class Setup {

public static function setup(Event $event) {
$jsonFile = new JsonFile($event->getComposer()
->getConfig()
->getConfigSource()
->getName());
$config = $jsonFile->read();
$fs = new Filesystem();
$drupalRoot = $event->getIO()->ask('<info>Customize Drupal root path?</info> [<comment>web</comment>]? ', 'web');
if ($drupalRoot !== 'web') {
$drupalRoot = rtrim($drupalRoot, '/');
$gitIgnore = file_get_contents('.gitignore');
$gitIgnore = preg_replace('/' . preg_quote('web/', '/') . '/', $drupalRoot . '/', $gitIgnore);
if (isset($config['extra']['installer-paths'])) {
$installer_paths = [];
foreach ($config['extra']['installer-paths'] as $path => $spec) {
$newPath = preg_replace('/' . preg_quote('web/', '/') . '/', $drupalRoot . '/', $path);
$installer_paths[$newPath] = $spec;
}
$config['extra']['installer-paths'] = $installer_paths;
}
$fs->dumpFile('.gitignore', $gitIgnore);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this before line 23. The logical flow would be to save the new gitignore just after the preg_replace on the gitIgnore lines, and not after the installer_paths block.

}
if ($event->getIO()->askConfirmation('<info>Remove dotenv?</info> [<comment>y,N</comment>]? ', TRUE)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since default is true, it should be 'Y,n'.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, remove the .env.example and load.environment.php files (same as other scaffold files below).

$fs->remove(['.env.example', 'load.environment.php']);
if (!empty($config['autoload']['files'])) {
$config['autoload']['files'] = array_diff($config['autoload']['files'], ['load.environment.php']);
if (empty($config['autoload']['files'])) {
unset($config['autoload']['files']);
}
}
unset($config['require']['vlucas/phpdotenv']);
unset($config['require-dev']['vlucas/phpdotenv']);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no phpdotenv in the require-dev section of composer.json.

}
if ($event->getIO()->askConfirmation('<info>Remove drush?</info> [<comment>y,N</comment>]? ', FALSE)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, also remove the drush/Commands/PolicyCommands.php, drush/README.md, drush/drush.yml and drush/sites/self.site.yml files (same as other scaffold files below).

unset($config['require']['drush/drush']);
unset($config['require-dev']['drush/drush']);
}
if ($event->getIO()->askConfirmation('<info>Remove drupal-console?</info> [<comment>y,N</comment>]? ', FALSE)) {
unset($config['require']['drupal/console']);
unset($config['require-dev']['drupal/console']);
}
if ($event->getIO()->askConfirmation('<info>Remove the installer and other scaffold files?</info> [<comment>y,N</comment>]? ', FALSE)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Project would be more user friendly if this option was TRUE by default. In which case, make the comment text 'Y,n'.

unset($config['scripts']['setup']);
$config['autoload']['classmap'] = array_diff($config['autoload']['classmap'], ['scripts/composer/Setup.php']);
$config['scripts']['post-root-package-install'] = array_diff($config['scripts']['post-root-package-install'], ['@setup']);
if (empty($config['scripts']['post-root-package-install'])) {
unset($config['scripts']['post-root-package-install']);
}
$fs->remove([
'.travis.yml',
'phpunit.xml.dist',
'scripts/composer/Setup.php',
]);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove also the drupal-composer README.md file.

}
$jsonFile->write($config);
return TRUE;
}

}