From c453e2e8b86ba15e6a8bd5ea99cc866d56adc3bd Mon Sep 17 00:00:00 2001 From: Konstantinos Samaras-Tsakiris <ksamtsak@gmail.com> Date: Fri, 14 May 2021 14:42:03 +0200 Subject: [PATCH] Update project with latest upstream recommendations --- README.md | 59 +++++++--------- composer.json | 108 +++++++++++++---------------- load.environment.php | 17 +++++ phpunit.xml.dist | 15 ++++ scripts/composer/ScriptHandler.php | 100 ++++++++++++++++++++++++++ 5 files changed, 208 insertions(+), 91 deletions(-) create mode 100644 load.environment.php create mode 100644 phpunit.xml.dist create mode 100644 scripts/composer/ScriptHandler.php diff --git a/README.md b/README.md index 918321d6e..805dde908 100644 --- a/README.md +++ b/README.md @@ -1,26 +1,22 @@ # Composer template for Drupal projects -[](https://travis-ci.org/drupal-composer/drupal-project) +[](https://github.com/drupal-composer/drupal-project/actions/workflows/ci.yml) This project template provides a starter kit for managing your site dependencies with [Composer](https://getcomposer.org/). -If you want to know how to use it as replacement for -[Drush Make](https://github.com/drush-ops/drush/blob/8.x/docs/make.md) visit -the [Documentation on drupal.org](https://www.drupal.org/node/2471553). - ## Usage -First you need to [install composer](https://getcomposer.org/doc/00-intro.md#installation-linux-unix-osx). +First you need to [install Composer](https://getcomposer.org/doc/00-intro.md#installation-linux-unix-osx). -> Note: The instructions below refer to the [global composer installation](https://getcomposer.org/doc/00-intro.md#globally). +> Note: The instructions below refer to the [global Composer installation](https://getcomposer.org/doc/00-intro.md#globally). You might need to replace `composer` with `php composer.phar` (or similar) for your setup. After that you can create the project: ``` -composer create-project drupal-composer/drupal-project:8.x-dev some-dir --stability dev --no-interaction +composer create-project drupal-composer/drupal-project:9.x-dev some-dir --no-interaction ``` With `composer require ...` you can download new dependencies to your @@ -28,12 +24,12 @@ installation. ``` cd some-dir -composer require drupal/devel:~1.0 +composer require drupal/devel ``` The `composer create-project` command passes ownership of all files to the -project that is created. You should create a new git repository, and commit -all files not excluded by the .gitignore file. +project that is created. You should create a new Git repository, and commit +all files not excluded by the `.gitignore` file. ## What does the template do? @@ -54,16 +50,16 @@ When installing the given `composer.json` some tasks are taken care of: ## Updating Drupal Core This project will attempt to keep all of your Drupal Core files up-to-date; the -project [drupal-composer/drupal-scaffold](https://github.com/drupal-composer/drupal-scaffold) +project [drupal/core-composer-scaffold](https://github.com/drupal/core-composer-scaffold) is used to ensure that your scaffold files are updated every time drupal/core is -updated. If you customize any of the "scaffolding" files (commonly .htaccess), +updated. If you customize any of the "scaffolding" files (commonly `.htaccess`), you may need to merge conflicts if any of your modified files are updated in a new release of Drupal core. Follow the steps below to update your core files. -1. Run `composer update drupal/core webflo/drupal-core-require-dev symfony/* --with-dependencies` to update Drupal Core and its dependencies. -1. Run `git diff` to determine if any of the scaffolding files have changed. +1. Run `composer update "drupal/core-*" --with-dependencies` to update Drupal Core and its dependencies. +2. Run `git diff` to determine if any of the scaffolding files have changed. Review the files for any changes and restore any customizations to `.htaccess` or `robots.txt`. 1. Commit everything all together in a single commit, so `web` will remain in @@ -75,13 +71,6 @@ Follow the steps below to update your core files. keeping all of your modifications at the beginning or end of the file is a good strategy to keep merges easy. -## Generate composer.json from existing project - -With using [the "Composer Generate" drush extension](https://www.drupal.org/project/composer_generate) -you can now generate a basic `composer.json` file from an existing project. Note -that the generated `composer.json` might differ from this project's file. - - ## FAQ ### Should I commit the contrib modules I download? @@ -91,11 +80,14 @@ workrounds if a project decides to do it anyway](https://getcomposer.org/doc/faq ### Should I commit the scaffolding files? -The [drupal-scaffold](https://github.com/drupal-composer/drupal-scaffold) plugin can download the scaffold files (like -index.php, update.php, …) to the web/ directory of your project. If you have not customized those files you could choose -to not check them into your version control system (e.g. git). If that is the case for your project it might be -convenient to automatically run the drupal-scaffold plugin after every install or update of your project. You can -achieve that by registering `@composer drupal:scaffold` as post-install and post-update command in your composer.json: +The [Drupal Composer Scaffold](https://github.com/drupal/core-composer-scaffold) +plugin can download the scaffold files (like index.php, update.php, …) to the +web/ directory of your project. If you have not customized those files you could +choose to not check them into your version control system (e.g. git). If that is +the case for your project it might be convenient to automatically run the +drupal-scaffold plugin after every install or update of your project. You can +achieve that by registering `@composer drupal:scaffold` as post-install and +post-update command in your composer.json: ```json "scripts": { @@ -109,6 +101,7 @@ achieve that by registering `@composer drupal:scaffold` as post-install and post ] }, ``` + ### How can I apply patches to downloaded modules? If you need to apply patches (depending on the project being modified, a pull @@ -117,6 +110,7 @@ request is often a better solution), you can do so with the To add a patch to drupal module foobar insert the patches section in the extra section of composer.json: + ```json "extra": { "patches": { @@ -126,22 +120,21 @@ section of composer.json: } } ``` -### How do I switch from packagist.drupal-composer.org to packages.drupal.org? - -Follow the instructions in the [documentation on drupal.org](https://www.drupal.org/docs/develop/using-composer/using-packagesdrupalorg). ### How do I specify a PHP version ? -Currently Drupal 8 supports PHP 5.5.9 as minimum version (see [Drupal 8 PHP requirements](https://www.drupal.org/docs/8/system-requirements/drupal-8-php-requirements)), however it's possible that a `composer update` will upgrade some package that will then require PHP 7+. +This project supports PHP 7.3 as minimum version (see [Environment requirements of Drupal 9](https://www.drupal.org/docs/understanding-drupal/how-drupal-9-was-made-and-what-is-included/environment-requirements-of)), however it's possible that a `composer update` will upgrade some package that will then require PHP 7.3+. To prevent this you can add this code to specify the PHP version you want to use in the `config` section of `composer.json`: + ```json "config": { "sort-packages": true, - "platform": {"php": "5.5.9"} + "platform": { + "php": "7.3.19" + } }, ``` - ### How do I edit a profile? The profiles are imported during image creation, to edit one please see the diff --git a/composer.json b/composer.json index fec7096a5..d2ba21bd4 100644 --- a/composer.json +++ b/composer.json @@ -1,9 +1,15 @@ { - "name": "drupal/recommended-project", - "description": "Project template for Drupal 8 projects with a relocated document root", + "name": "drupal/cern-drupal-distribution", + "description": "Defines the Drupal environment supported at CERN", "type": "project", - "license": "GPL-2.0-or-later", - "homepage": "https://www.drupal.org/project/drupal", + "license": "Apache-2.0", + "authors": [ + { + "name": "drupal-admins", + "email": "drupal-admins@cern.ch" + } + ], + "homepage": "https://home.cern", "support": { "docs": "https://www.drupal.org/docs/user_guide/en/index.html", "chat": "https://www.drupal.org/node/314178" @@ -99,8 +105,12 @@ } ], "require": { + "php": ">=7.3", "composer/installers": "~1.10.0", "cweagans/composer-patches": "~1.7.0", + "drush/drush": "~10.3.0", + "vlucas/phpdotenv": "~5.1.0", + "webflo/drupal-finder": "~1.2.0", "drupal/admin_toolbar": "~2.4.0", "drupal/adminimal_theme": "~1.6.0", "drupal/allowed_formats": "~1.3.0", @@ -214,14 +224,39 @@ "drupal/workbench_moderation": "~1.6.0", "wikimedia/composer-merge-plugin": "~2.0.1" }, + "require-dev": { + "drupal/core-dev": "~8.9.14", + "zaporylie/composer-drupal-optimizations": "~1.2.0" + }, "conflict": { "drupal/drupal": "*" }, "minimum-stability": "dev", "prefer-stable": true, "config": { + "discard-changes": true, "sort-packages": true }, + "autoload": { + "classmap": [ + "scripts/composer/ScriptHandler.php" + ], + "files": ["load.environment.php"] + }, + "scripts": { + "pre-install-cmd": [ + "DrupalProject\\composer\\ScriptHandler::checkComposerVersion" + ], + "pre-update-cmd": [ + "DrupalProject\\composer\\ScriptHandler::checkComposerVersion" + ], + "post-install-cmd": [ + "DrupalProject\\composer\\ScriptHandler::createRequiredFiles" + ], + "post-update-cmd": [ + "DrupalProject\\composer\\ScriptHandler::createRequiredFiles" + ] + }, "extra": { "drupal-scaffold": { "locations": { @@ -229,30 +264,16 @@ } }, "installer-paths": { - "web/core": [ - "type:drupal-core" - ], - "web/libraries/{$name}": [ - "type:drupal-library" - ], - "web/modules/contrib/{$name}": [ - "type:drupal-module" - ], - "web/profiles/contrib/{$name}": [ - "type:drupal-profile" - ], - "web/themes/contrib/{$name}": [ - "type:drupal-theme" - ], - "drush/Commands/contrib/{$name}": [ - "type:drupal-drush" - ], - "web/modules/custom/{$name}": [ - "type:drupal-custom-module" - ], - "web/themes/custom/{$name}": [ - "type:drupal-custom-theme" - ] + "web/core": ["type:drupal-core"], + "web/libraries/{$name}": ["type:drupal-library"], + "web/modules/contrib/{$name}": ["type:drupal-module"], + "web/profiles/contrib/{$name}": ["type:drupal-profile"], + "web/themes/contrib/{$name}": ["type:drupal-theme"], + "drush/Commands/contrib/{$name}": ["type:drupal-drush"], + "web/modules/custom/{$name}": ["type:drupal-custom-module"], + "web/themes/custom/{$name}": ["type:drupal-custom-theme"] + }, + "patchLevel": { }, "patches": { "drupal/core": { @@ -275,27 +296,6 @@ "OIDC autologin (skip Drupal login page) https://www.drupal.org/project/openid_connect/issues/3011413": "patches/openid_connect-3011413-autologin.patch" } }, - "drupal-core-project-message": { - "include-keys": [ - "homepage", - "support" - ], - "post-create-project-cmd-message": [ - "<bg=blue;fg=white> </>", - "<bg=blue;fg=white> Congratulations, you’ve installed the Drupal codebase </>", - "<bg=blue;fg=white> from the drupal/recommended-project template! </>", - "<bg=blue;fg=white> </>", - "", - "<bg=yellow;fg=black>Next steps</>:", - " * Install the site: https://www.drupal.org/docs/8/install", - " * Read the user guide: https://www.drupal.org/docs/user_guide/en/index.html", - " * Get support: https://www.drupal.org/support", - " * Get involved with the Drupal community:", - " https://www.drupal.org/getting-involved", - " * Remove the plugin that prints this message:", - " composer remove drupal/core-project-message" - ] - }, "merge-plugin": { "include": [ "composer.user.json" @@ -307,14 +307,6 @@ "merge-extra": true, "merge-extra-deep": true, "merge-scripts": true - }, - "scripts": { - "post-install-cmd": [ - "@composer drupal:scaffold" - ], - "post-update-cmd": [ - "@composer drupal:scaffold" - ] - } + } } } diff --git a/load.environment.php b/load.environment.php new file mode 100644 index 000000000..eb5e3a0f8 --- /dev/null +++ b/load.environment.php @@ -0,0 +1,17 @@ +<?php + +/** + * This file is included very early. See autoload.files in composer.json and + * https://getcomposer.org/doc/04-schema.md#files + */ + +use Dotenv\Dotenv; + +/** + * Load any .env file. See /.env.example. + * + * Drupal has no official method for loading environment variables and uses + * getenv() in some places. + */ +$dotenv = Dotenv::createUnsafeImmutable(__DIR__); +$dotenv->safeLoad(); diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 000000000..ce29746d7 --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,15 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.1/phpunit.xsd" + backupGlobals="false" + colors="true" + bootstrap="vendor/autoload.php" + verbose="true" + > + <testsuites> + <testsuite name="drupal-composer-project tests"> + <directory>./test/</directory> + </testsuite> + </testsuites> +</phpunit> diff --git a/scripts/composer/ScriptHandler.php b/scripts/composer/ScriptHandler.php new file mode 100644 index 000000000..51026d38a --- /dev/null +++ b/scripts/composer/ScriptHandler.php @@ -0,0 +1,100 @@ +<?php + +/** + * @file + * Contains \DrupalProject\composer\ScriptHandler. + */ + +namespace DrupalProject\composer; + +use Composer\Script\Event; +use Composer\Semver\Comparator; +use Drupal\Core\Site\Settings; +use DrupalFinder\DrupalFinder; +use Symfony\Component\Filesystem\Filesystem; +use Webmozart\PathUtil\Path; + +class ScriptHandler { + + public static function createRequiredFiles(Event $event) { + $fs = new Filesystem(); + $drupalFinder = new DrupalFinder(); + $drupalFinder->locateRoot(getcwd()); + $drupalRoot = $drupalFinder->getDrupalRoot(); + + $dirs = [ + 'modules', + 'profiles', + 'themes', + ]; + + // Required for unit testing + foreach ($dirs as $dir) { + if (!$fs->exists($drupalRoot . '/'. $dir)) { + $fs->mkdir($drupalRoot . '/'. $dir); + $fs->touch($drupalRoot . '/'. $dir . '/.gitkeep'); + } + } + + // Prepare the settings file for installation + if (!$fs->exists($drupalRoot . '/sites/default/settings.php') && $fs->exists($drupalRoot . '/sites/default/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'; + new Settings([]); + $settings['settings']['config_sync_directory'] = (object) [ + 'value' => Path::makeRelative($drupalFinder->getComposerRoot() . '/config/sync', $drupalRoot), + 'required' => TRUE, + ]; + drupal_rewrite_settings($settings, $drupalRoot . '/sites/default/settings.php'); + $fs->chmod($drupalRoot . '/sites/default/settings.php', 0666); + $event->getIO()->write("Created a sites/default/settings.php file with chmod 0666"); + } + + // 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("Created a sites/default/files directory with chmod 0777"); + } + } + + /** + * Checks if the installed version of Composer is compatible. + * + * Composer 1.0.0 and higher consider a `composer install` without having a + * lock file present as equal to `composer update`. We do not ship with a lock + * file to avoid merge conflicts downstream, meaning that if a project is + * installed with an older version of Composer the scaffolding of Drupal will + * not be triggered. We check this here instead of in drupal-scaffold to be + * able to give immediate feedback to the end user, rather than failing the + * installation after going through the lengthy process of compiling and + * downloading the Composer dependencies. + * + * @see https://github.com/composer/composer/pull/5035 + */ + public static function checkComposerVersion(Event $event) { + $composer = $event->getComposer(); + $io = $event->getIO(); + + $version = $composer::VERSION; + + // The dev-channel of composer uses the git revision as version number, + // try to the branch alias instead. + if (preg_match('/^[0-9a-f]{40}$/i', $version)) { + $version = $composer::BRANCH_ALIAS_VERSION; + } + + // If Composer is installed through git we have no easy way to determine if + // it is new enough, just display a warning. + if ($version === '@package_version@' || $version === '@package_branch_alias_version@') { + $io->writeError('<warning>You are running a development version of Composer. If you experience problems, please update Composer to the latest stable version.</warning>'); + } + elseif (Comparator::lessThan($version, '1.0.0')) { + $io->writeError('<error>Drupal-project requires Composer version 1.0.0 or higher. Please update your Composer before continuing</error>.'); + exit(1); + } + } + +} -- GitLab