2019-10-25 09:04:15 +00:00
< ? php
namespace Drupal\Composer ;
use Composer\Composer as ComposerApp ;
use Composer\Script\Event ;
use Composer\Semver\Comparator ;
2020-10-16 14:55:30 +00:00
use Composer\Semver\VersionParser ;
2019-10-25 09:04:15 +00:00
use Drupal\Composer\Generator\PackageGenerator ;
2020-10-16 14:55:30 +00:00
use Symfony\Component\Finder\Finder ;
2019-10-25 09:04:15 +00:00
/**
* Provides static functions for composer script events . See also
* core / lib / Drupal / Composer / Composer . php , which contains similar
* scripts needed by projects that include drupal / core . Scripts that
* are only needed by drupal / drupal go here .
*
* @ see https :// getcomposer . org / doc / articles / scripts . md
*/
class Composer {
/**
* Update metapackages whenever composer . lock is updated .
*
* @ param \Composer\Script\Event $event
2020-10-16 14:55:30 +00:00
* The Composer event .
2019-10-25 09:04:15 +00:00
*/
2020-10-16 14:55:30 +00:00
public static function generateMetapackages ( Event $event ) : void {
2019-10-25 09:04:15 +00:00
$generator = new PackageGenerator ();
$generator -> generate ( $event -> getIO (), getcwd ());
}
2020-10-16 14:55:30 +00:00
/**
* Set the version of Drupal ; used in release process and by the test suite .
*
* @ param string $root
* Path to root of drupal / drupal repository .
* @ param string $version
* Semver version to set Drupal ' s version to .
*
* @ return string
* Stability level of the provided version ( stable , RC , alpha , etc . )
*
* @ throws \UnexpectedValueException
*/
public static function setDrupalVersion ( string $root , string $version ) : void {
// We use VersionParser::normalize to validate that $version is valid.
// It will throw an exception if it is not.
$versionParser = new VersionParser ();
$versionParser -> normalize ( $version );
// Rewrite Drupal.php with the provided version string.
$drupal_static_path = " $root /core/lib/Drupal.php " ;
$drupal_static_source = file_get_contents ( $drupal_static_path );
$drupal_static_source = preg_replace ( '#const VERSION = [^;]*#' , " const VERSION = ' $version ' " , $drupal_static_source );
file_put_contents ( $drupal_static_path , $drupal_static_source );
// Update the template project stability to match the version we set.
static :: setTemplateProjectStability ( $root , $version );
}
/**
* Set the stability of the template projects to match the Drupal version .
*
* @ param string $root
* Path to root of drupal / drupal repository .
* @ param string $version
* Semver version that Drupal was set to .
*
* @ return string
* Stability level of the provided version ( stable , RC , alpha , etc . )
*/
protected static function setTemplateProjectStability ( string $root , string $version ) : void {
$stability = VersionParser :: parseStability ( $version );
$templateProjectPaths = static :: composerSubprojectPaths ( $root , 'Template' );
foreach ( $templateProjectPaths as $path ) {
$dir = dirname ( $path );
exec ( " composer --working-dir= $dir config minimum-stability $stability " , $output , $status );
if ( $status ) {
throw new \Exception ( 'Could not set minimum-stability for template project ' . basename ( $dir ));
}
}
}
2019-10-25 09:04:15 +00:00
/**
* Ensure that the minimum required version of Composer is running .
* Throw an exception if Composer is too old .
*/
2020-10-16 14:55:30 +00:00
public static function ensureComposerVersion () : void {
2019-10-25 09:04:15 +00:00
$composerVersion = method_exists ( ComposerApp :: class , 'getVersion' ) ?
ComposerApp :: getVersion () : ComposerApp :: VERSION ;
if ( Comparator :: lessThan ( $composerVersion , '1.9.0' )) {
throw new \RuntimeException ( " Drupal core development requires Composer 1.9.0, but Composer $composerVersion is installed. Please run 'composer self-update'. " );
}
}
/**
* Return the branch name the current Drupal version is associated with .
*
* @ return string
* A branch name , e . g . 8.9 . x or 9.0 . x .
*/
2020-10-16 14:55:30 +00:00
public static function drupalVersionBranch () : string {
2019-10-25 09:04:15 +00:00
return preg_replace ( '#\.[0-9]+-dev#' , '.x-dev' , \Drupal :: VERSION );
}
2020-10-16 14:55:30 +00:00
/**
* Return the list of subprojects of a given type .
*
* @ param string $root
* Path to root of drupal / drupal repository .
* @ param string $subprojectType
* Type of subproject - one of Metapackage , Plugin , or Template
*
* @ return \Symfony\Component\Finder\Finder
*/
public static function composerSubprojectPaths ( string $root , string $subprojectType ) : Finder {
return Finder :: create ()
-> files ()
-> name ( 'composer.json' )
-> in ( " $root /composer/ $subprojectType " );
}
2019-10-25 09:04:15 +00:00
}