Skip to content

Commit

Permalink
Add COMPOSER_NO_DEV environment variable
Browse files Browse the repository at this point in the history
This works fine in practice, but the tests fail because it doesn't call the initialization function, so the setup isn't done.
  • Loading branch information
yakatz committed Nov 16, 2021
1 parent 28c3412 commit 2cff65b
Show file tree
Hide file tree
Showing 13 changed files with 55 additions and 20 deletions.
5 changes: 5 additions & 0 deletions doc/03-cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -1147,3 +1147,8 @@ If set to `1`, outputs information about events being dispatched, which can be
useful for plugin authors to identify what is firing when exactly.
← [Libraries](02-libraries.md) | [Schema](04-schema.md) →
### COMPOSER_NO_DEV
If set to `1`, it is the equivalent of passing the `--no-dev` arguement to `install` or
`update`. You can override this for a single command by setting `COMPOSER_NO_DEV=0`.
30 changes: 30 additions & 0 deletions src/Composer/Command/BaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ abstract class BaseCommand extends Command
*/
private $io;

/**
* @var bool
*/
private $noDevOptOrEnv = null;

/**
* @param bool $required
* @param bool|null $disablePlugins
Expand Down Expand Up @@ -131,6 +136,23 @@ public function setIO(IOInterface $io)
$this->io = $io;
}

/**
* @return bool|null
*/
protected function getOptionNoDev()
{
return $this->noDevOptOrEnv;
}

/**
* @param bool|null $noDevOptOrEnv
* @return void
*/
public function setOptionNoDev($noDevOptOrEnv)
{
$this->noDevOptOrEnv = $noDevOptOrEnv;
}

/**
* @inheritDoc
*
Expand All @@ -153,6 +175,14 @@ protected function initialize(InputInterface $input, OutputInterface $output)
$input->setOption('no-progress', true);
}

if (true == $input->hasOption('no-dev')) {
if (true == $input->getOption('no-dev') || true == getenv('COMPOSER_NO_DEV')) {
$this->setOptionNoDev(true);
} else {
$this->setOptionNoDev(false);
}
}

parent::initialize($input, $output);
}

Expand Down
14 changes: 7 additions & 7 deletions src/Composer/Command/CheckPlatformReqsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,23 @@ protected function execute(InputInterface $input, OutputInterface $output)
$requires = array();
$removePackages = array();
if ($input->getOption('lock')) {
$this->getIO()->writeError('<info>Checking '.($input->getOption('no-dev') ? 'non-dev ' : '').'platform requirements using the lock file</info>');
$installedRepo = $composer->getLocker()->getLockedRepository(!$input->getOption('no-dev'));
$this->getIO()->writeError('<info>Checking '.($this->getOptionNoDev() ? 'non-dev ' : '').'platform requirements using the lock file</info>');
$installedRepo = $composer->getLocker()->getLockedRepository(!$this->getOptionNoDev());
} else {
$installedRepo = $composer->getRepositoryManager()->getLocalRepository();
// fallback to lockfile if installed repo is empty
if (!$installedRepo->getPackages()) {
$this->getIO()->writeError('<warning>No vendor dir present, checking '.($input->getOption('no-dev') ? 'non-dev ' : '').'platform requirements from the lock file</warning>');
$installedRepo = $composer->getLocker()->getLockedRepository(!$input->getOption('no-dev'));
$this->getIO()->writeError('<warning>No vendor dir present, checking '.($this->getOptionNoDev() ? 'non-dev ' : '').'platform requirements from the lock file</warning>');
$installedRepo = $composer->getLocker()->getLockedRepository(!$this->getOptionNoDev());
} else {
if ($input->getOption('no-dev')) {
if ($this->getOptionNoDev()) {
$removePackages = $installedRepo->getDevPackageNames();
}

$this->getIO()->writeError('<info>Checking '.($input->getOption('no-dev') ? 'non-dev ' : '').'platform requirements for packages in the vendor dir</info>');
$this->getIO()->writeError('<info>Checking '.($this->getOptionNoDev() ? 'non-dev ' : '').'platform requirements for packages in the vendor dir</info>');
}
}
if (!$input->getOption('no-dev')) {
if (!$this->getOptionNoDev()) {
$requires += $composer->getPackage()->getDevRequires();
}

Expand Down
2 changes: 1 addition & 1 deletion src/Composer/Command/CreateProjectCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$input->getOption('stability'),
$preferSource,
$preferDist,
!$input->getOption('no-dev'),
!$this->getOptionNoDev(),
$input->getOption('repository') ?: $input->getOption('repository-url'),
$input->getOption('no-plugins'),
$input->getOption('no-scripts'),
Expand Down
4 changes: 2 additions & 2 deletions src/Composer/Command/DumpAutoloadCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
$ignorePlatformReqs = $input->getOption('ignore-platform-reqs') ?: ($input->getOption('ignore-platform-req') ?: false);

$generator = $composer->getAutoloadGenerator();
if ($input->getOption('no-dev')) {
if ($this->getOptionNoDev()) {
$generator->setDevMode(false);
}
if ($input->getOption('dev')) {
if ($input->getOption('no-dev')) {
if ($this->getOptionNoDev()) {
throw new \InvalidArgumentException('You can not use both --no-dev and --dev as they conflict with each other.');
}
$generator->setDevMode(true);
Expand Down
2 changes: 1 addition & 1 deletion src/Composer/Command/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
->setVerbose($input->getOption('verbose'))
->setPreferSource($preferSource)
->setPreferDist($preferDist)
->setDevMode(!$input->getOption('no-dev'))
->setDevMode(!$this->getOptionNoDev())
->setDumpAutoloader(!$input->getOption('no-autoloader'))
->setOptimizeAutoloader($optimize)
->setClassMapAuthoritative($authoritative)
Expand Down
2 changes: 1 addition & 1 deletion src/Composer/Command/LicensesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$root = $composer->getPackage();
$repo = $composer->getRepositoryManager()->getLocalRepository();

if ($input->getOption('no-dev')) {
if ($this->getOptionNoDev()) {
$packages = $this->filterRequiredPackages($repo, $root);
} else {
$packages = $this->appendPackages($repo->getPackages(), array());
Expand Down
2 changes: 1 addition & 1 deletion src/Composer/Command/OutdatedCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
if ($input->getOption('locked')) {
$args['--locked'] = true;
}
if ($input->getOption('no-dev')) {
if ($this->getOptionNoDev()) {
$args['--no-dev'] = true;
}
$args['--format'] = $input->getOption('format');
Expand Down
2 changes: 1 addition & 1 deletion src/Composer/Command/RunScriptCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

$composer = $this->getComposer();
$devMode = $input->getOption('dev') || !$input->getOption('no-dev');
$devMode = $input->getOption('dev') || !$this->getOptionNoDev();
$event = new ScriptEvent($script, $composer, $this->getIO(), $devMode);
$hasListeners = $composer->getEventDispatcher()->hasEventListeners($event);
if (!$hasListeners) {
Expand Down
2 changes: 1 addition & 1 deletion src/Composer/Command/ScriptAliasCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,6 @@ protected function execute(InputInterface $input, OutputInterface $output)

$args = $input->getArguments();

return $composer->getEventDispatcher()->dispatchScript($this->script, $input->getOption('dev') || !$input->getOption('no-dev'), $args['args']);
return $composer->getEventDispatcher()->dispatchScript($this->script, $input->getOption('dev') || !$this->getOptionNoDev(), $args['args']);
}
}
4 changes: 2 additions & 2 deletions src/Composer/Command/ShowCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
throw new \UnexpectedValueException('A valid composer.json and composer.lock files is required to run this command with --locked');
}
$locker = $composer->getLocker();
$lockedRepo = $locker->getLockedRepository(!$input->getOption('no-dev'));
$lockedRepo = $locker->getLockedRepository(!$this->getOptionNoDev());
$repos = $installedRepo = new InstalledRepository(array($lockedRepo));
} else {
// --installed / default case
Expand All @@ -209,7 +209,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$rootPkg = $composer->getPackage();
$repos = $installedRepo = new InstalledRepository(array($composer->getRepositoryManager()->getLocalRepository()));

if ($input->getOption('no-dev')) {
if ($this->getOptionNoDev()) {
$packages = $this->filterRequiredPackages($installedRepo, $rootPkg);
$repos = $installedRepo = new InstalledRepository(array(new InstalledArrayRepository(array_map(function ($pkg) {
return clone $pkg;
Expand Down
2 changes: 1 addition & 1 deletion src/Composer/Command/SuggestsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$locker = $composer->getLocker();
if ($locker->isLocked()) {
$installedRepos[] = new PlatformRepository(array(), $locker->getPlatformOverrides());
$installedRepos[] = $locker->getLockedRepository(!$input->getOption('no-dev'));
$installedRepos[] = $locker->getLockedRepository(!$this->getOptionNoDev());
} else {
$installedRepos[] = new PlatformRepository(array(), $composer->getConfig()->get('platform') ?: array());
$installedRepos[] = $composer->getRepositoryManager()->getLocalRepository();
Expand Down
4 changes: 2 additions & 2 deletions src/Composer/Command/UpdateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ protected function execute(InputInterface $input, OutputInterface $output)

if ($input->getOption('root-reqs')) {
$requires = array_keys($rootRequires);
if (!$input->getOption('no-dev')) {
if (!$this->getOptionNoDev()) {
$requires = array_merge($requires, array_keys($rootDevRequires));
}

Expand Down Expand Up @@ -226,7 +226,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
->setVerbose($input->getOption('verbose'))
->setPreferSource($preferSource)
->setPreferDist($preferDist)
->setDevMode(!$input->getOption('no-dev'))
->setDevMode(!$this->getOptionNoDev())
->setDumpAutoloader(!$input->getOption('no-autoloader'))
->setOptimizeAutoloader($optimize)
->setClassMapAuthoritative($authoritative)
Expand Down

0 comments on commit 2cff65b

Please sign in to comment.