From d0aac44ed210e13ec4a4370908a5b36553a2f16c Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Wed, 24 Feb 2021 21:17:05 +0100 Subject: [PATCH] Auto-detect dev-mode in autoload-dump and deprecate dump-autoload --no-dev (#9714) --- doc/03-cli.md | 5 +++-- doc/articles/scripts.md | 5 +++-- src/Composer/Autoload/AutoloadGenerator.php | 22 ++++++++++++++++++-- src/Composer/Command/DumpAutoloadCommand.php | 7 +++++-- src/Composer/Installer.php | 1 - 5 files changed, 31 insertions(+), 9 deletions(-) diff --git a/doc/03-cli.md b/doc/03-cli.md index 99d4272b8..7413d919c 100644 --- a/doc/03-cli.md +++ b/doc/03-cli.md @@ -284,7 +284,7 @@ uninstalled. * **--no-scripts:** Skips execution of scripts defined in `composer.json`. * **--update-no-dev:** Run the dependency update with the --no-dev option. * **--update-with-dependencies (-w):** Also update dependencies of the removed packages. - (Deprecrated, is now default behavior) + (Deprecated, is now default behavior) * **--update-with-all-dependencies (-W):** Allows all inherited dependencies to be updated, including those that are root requirements. * **--ignore-platform-reqs:** ignore all platform requirements (`php`, `hhvm`, @@ -790,7 +790,8 @@ performance. * **--apcu:** Use APCu to cache found/not-found classes. * **--apcu-prefix:** Use a custom prefix for the APCu autoloader cache. Implicitly enables `--apcu`. -* **--no-dev:** Disables autoload-dev rules. +* **--no-dev:** Disables autoload-dev rules. (Deprecated: Composer now infers this + automatically according to the last `install` or `update` run.) * **--ignore-platform-reqs:** ignore all `php`, `hhvm`, `lib-*` and `ext-*` requirements and skip the [platform check](07-runtime.md#platform-check) for these. See also the [`platform`](06-config.md#platform) config option. diff --git a/doc/articles/scripts.md b/doc/articles/scripts.md index 9b5ca4a88..9290f7382 100644 --- a/doc/articles/scripts.md +++ b/doc/articles/scripts.md @@ -162,10 +162,11 @@ class MyClass } ``` -**Note:** During a composer install or update process, a variable named +**Note:** During a Composer `install` or `update` command run, a variable named `COMPOSER_DEV_MODE` will be added to the environment. If the command was run with the `--no-dev` flag, this variable will be set to 0, otherwise it will be -set to 1. +set to 1. The variable is also available while `dump-autoload` runs, and it +will be set to same as the last `install` or `update` was run in. ## Event classes diff --git a/src/Composer/Autoload/AutoloadGenerator.php b/src/Composer/Autoload/AutoloadGenerator.php index fd5a594e5..6f4191443 100644 --- a/src/Composer/Autoload/AutoloadGenerator.php +++ b/src/Composer/Autoload/AutoloadGenerator.php @@ -26,6 +26,7 @@ use Composer\Semver\Constraint\MatchAllConstraint; use Composer\Util\Filesystem; use Composer\Script\ScriptEvents; use Composer\Util\PackageSorter; +use Composer\Json\JsonFile; /** * @author Igor Wiedler @@ -44,9 +45,9 @@ class AutoloadGenerator private $io; /** - * @var bool + * @var ?bool */ - private $devMode = false; + private $devMode = null; /** * @var bool @@ -144,6 +145,23 @@ class AutoloadGenerator $scanPsrPackages = true; } if ($this->runScripts) { + $installedJson = new JsonFile($config->get('vendor-dir').'/composer/installed.json'); + $isDevInstall = null; + if ($installedJson->exists()) { + $installedJson = $installedJson->read(); + $isDevInstall = isset($installedJson['dev']) ? $installedJson['dev'] : null; + } + // auto-set devMode based on whether dev dependencies are installed or not + if (null !== $isDevInstall && null === $this->devMode) { + $this->devMode = $isDevInstall; + } + + // set COMPOSER_DEV_MODE in case not set yet so it is available in the dump-autoload autoload + if (!isset($_SERVER['COMPOSER_DEV_MODE'])) { + $_SERVER['COMPOSER_DEV_MODE'] = $this->devMode ? '1' : '0'; + putenv('COMPOSER_DEV_MODE='.$_SERVER['COMPOSER_DEV_MODE']); + } + $this->eventDispatcher->dispatchScript(ScriptEvents::PRE_AUTOLOAD_DUMP, $this->devMode, array(), array( 'optimize' => (bool) $scanPsrPackages, )); diff --git a/src/Composer/Command/DumpAutoloadCommand.php b/src/Composer/Command/DumpAutoloadCommand.php index 0b2413925..dd06139b8 100644 --- a/src/Composer/Command/DumpAutoloadCommand.php +++ b/src/Composer/Command/DumpAutoloadCommand.php @@ -35,7 +35,7 @@ class DumpAutoloadCommand extends BaseCommand new InputOption('classmap-authoritative', 'a', InputOption::VALUE_NONE, 'Autoload classes from the classmap only. Implicitly enables `--optimize`.'), new InputOption('apcu', null, InputOption::VALUE_NONE, 'Use APCu to cache found/not-found classes.'), new InputOption('apcu-prefix', null, InputOption::VALUE_REQUIRED, 'Use a custom prefix for the APCu autoloader cache. Implicitly enables --apcu'), - new InputOption('no-dev', null, InputOption::VALUE_NONE, 'Disables autoload-dev rules.'), + new InputOption('no-dev', null, InputOption::VALUE_NONE, 'DEPRECATED: Disables autoload-dev rules. Composer now infers this automatically according to the last install or update run.'), new InputOption('ignore-platform-req', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Ignore a specific platform requirement (php & ext- packages).'), new InputOption('ignore-platform-reqs', null, InputOption::VALUE_NONE, 'Ignore all platform requirements (php & ext- packages).'), )) @@ -77,7 +77,10 @@ EOT $ignorePlatformReqs = $input->getOption('ignore-platform-reqs') ?: ($input->getOption('ignore-platform-req') ?: false); $generator = $composer->getAutoloadGenerator(); - $generator->setDevMode(!$input->getOption('no-dev')); + if ($input->getOption('no-dev')) { + $this->getIO()->writeError('You are using the deprecated option "--no-dev". Composer now infers the value automatically according to the last install or update run.'); + $generator->setDevMode(false); + } $generator->setClassMapAuthoritative($authoritative); $generator->setApcu($apcu, $apcuPrefix); $generator->setRunScripts(!$input->getOption('no-scripts')); diff --git a/src/Composer/Installer.php b/src/Composer/Installer.php index 1a7c5e694..ea3a6151a 100644 --- a/src/Composer/Installer.php +++ b/src/Composer/Installer.php @@ -302,7 +302,6 @@ class Installer $this->io->writeError('Generating autoload files'); } - $this->autoloadGenerator->setDevMode($this->devMode); $this->autoloadGenerator->setClassMapAuthoritative($this->classMapAuthoritative); $this->autoloadGenerator->setApcu($this->apcuAutoloader, $this->apcuAutoloaderPrefix); $this->autoloadGenerator->setRunScripts($this->runScripts);