Skip to content

Commit

Permalink
Change logic, readme, to support channels. Tests in the future commits
Browse files Browse the repository at this point in the history
  • Loading branch information
Barys Biankouski committed May 25, 2023
1 parent 6c53ebc commit 31ae1b7
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 43 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ Done, your colleagues should worry never again about installing composer and kee

There are 3 parameters that can be set via [environment variables](https://www.digitalocean.com/community/tutorials/how-to-read-and-set-environmental-and-shell-variables-on-a-linux-vps) or in composer.json file (`extra.wrapper` section). Environment variables take precedence over composer.json, so it's safe to keep defaults for local dev environment in composer.json and override them via environment variables on a build server if needed.

| Environment variable | composer.json `extra.wrapper` option | Meaning |
|----------------------|--------------------------------------|---------|
| `COMPOSER_UPDATE_FREQ` | `update-freq` | Time between checks for updates (defaults to 7 days). This is a [relative time specifier](http://php.net/manual/en/datetime.formats.relative.php) that is fed to [`DateTime::modify`](http://php.net/manual/en/datetime.modify.php). It's chosen because it can be perfectly readable by someone who knows no PHP and doesn't want to (e.g. ops people), and it's recommended to keep it that way, e.g. "5 days", "2 weeks", "1 month". |
| `COMPOSER_DIR` | `composer-dir` | Directory where composer.phar will be searched for and copied to. Defaults to the directory where the script is located (`__DIR__`); note: *not* the current working directory! Sometimes it's useful to keep real composer.phar a couple of levels higher up the directory where wrapper is placed, for example, on a CI server: it would help avoid downloading composer afresh for every build. |
| `COMPOSER_FORCE_MAJOR_VERSION` | `major-version` | When set to either 1 or 2, forces composer to self-update to the most recent version in the specified major branch (1.x or 2.x respectively) or install the specified version. |
| Environment variable | composer.json `extra.wrapper` option | Meaning |
|--------------------------------|--------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `COMPOSER_UPDATE_FREQ` | `update-freq` | Time between checks for updates (defaults to 7 days). This is a [relative time specifier](http://php.net/manual/en/datetime.formats.relative.php) that is fed to [`DateTime::modify`](http://php.net/manual/en/datetime.modify.php). It's chosen because it can be perfectly readable by someone who knows no PHP and doesn't want to (e.g. ops people), and it's recommended to keep it that way, e.g. "5 days", "2 weeks", "1 month". |
| `COMPOSER_DIR` | `composer-dir` | Directory where composer.phar will be searched for and copied to. Defaults to the directory where the script is located (`__DIR__`); note: *not* the current working directory! Sometimes it's useful to keep real composer.phar a couple of levels higher up the directory where wrapper is placed, for example, on a CI server: it would help avoid downloading composer afresh for every build. |
| `COMPOSER_FORCE_MAJOR_VERSION` | `major-version` | **REMOVED IN v1.3.0 in favor of ** When set to either 1 or 2, forces composer to self-update to the most recent version in the specified major branch (1.x or 2.x respectively) or install the specified version. |
| `COMPOSER_CHANNEL` | `channel` | List of values may be found in https://getcomposer.org/versions When set, forces composer to self-update to the most recent version in the specified channel or install the last version in requested channel. |

Here is an example of composer.json file that contains wrapper parameter that forces using of Composer 1:
```json
Expand All @@ -43,7 +44,7 @@ Here is an example of composer.json file that contains wrapper parameter that fo
},
"extra": {
"wrapper": {
"major-version": 1
"channel": 2.2
}
}
}
Expand Down
65 changes: 32 additions & 33 deletions composer
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
* If it breaks, check out newer version or report an issue at
* https://github.com/kamazee/composer-wrapper
*
* @version 1.2.1
* @version 1.3.0
*/
if (!class_exists('ComposerWrapper')) {
class ComposerWrapperParams {
/**
* @var string|null
*/
private $forceMajorVersion;
private $channel;
/**
* @var string
*/
Expand Down Expand Up @@ -51,8 +51,8 @@ if (!class_exists('ComposerWrapper')) {
if ( false !== $value = \getenv('COMPOSER_UPDATE_FREQ')) {
$this->setUpdateFreq($value);
}
if ( false !== $value = \getenv('COMPOSER_FORCE_MAJOR_VERSION')) {
$this->setForceMajorVersion($value);
if ( false !== $value = \getenv('COMPOSER_CHANNEL')) {
$this->setChannel($value);
}
if ( false !== $value = \getenv('COMPOSER_DIR')) {
$this->setComposerDir($value);
Expand All @@ -77,8 +77,8 @@ if (!class_exists('ComposerWrapper')) {
if (array_key_exists('update-freq', $wrapper)) {
$this->setUpdateFreq($wrapper['update-freq']);
}
if (array_key_exists('major-version', $wrapper)) {
$this->setForceMajorVersion($wrapper['major-version']);
if (array_key_exists('channel', $wrapper)) {
$this->setChannel($wrapper['channel']);
}
if (array_key_exists('composer-dir', $wrapper)) {
$this->setComposerDir($wrapper['composer-dir']);
Expand All @@ -88,17 +88,17 @@ if (!class_exists('ComposerWrapper')) {
/**
* @return string|null
*/
public function getForceMajorVersion()
public function getChannel()
{
return $this->forceMajorVersion;
return $this->channel;
}

/**
* @param false|string $version
* @param false|string $channel
*/
public function setForceMajorVersion($version)
public function setChannel($channel)
{
$this->forceMajorVersion = (string) $version;
$this->channel = (string) $channel;
}

/**
Expand Down Expand Up @@ -210,11 +210,10 @@ if (!class_exists('ComposerWrapper')) {

/**
* @param array $arrayCommand
* @param string $version
* @param string $channel
* @return bool
* @throws Exception
*/
protected function supportsForceVersionFlag($arrayCommand, $version)
protected function supportsChannelFlag($arrayCommand, $channel)
{
$command = implode(' ', $arrayCommand);
try {
Expand All @@ -223,15 +222,15 @@ if (!class_exists('ComposerWrapper')) {
} catch (Exception $e) {
throw new \Exception(
'Error when trying to check support for forcing ' .
'specific major version on either self-update or installer',
'specific channel on either self-update or installer',
0,
$e
);
}

foreach ($output as $line) {
$line = trim($line);
if (0 === strpos($line, "--$version ")) {
if (0 === strpos($line, "--$channel ")) {
return true;
}
}
Expand Down Expand Up @@ -274,23 +273,23 @@ if (!class_exists('ComposerWrapper')) {

$this->verifyChecksum($installerPathName);

$majorVersion = $this->params->getForceMajorVersion();
$majorVersionFlag = null;
if (null !== $majorVersion) {
$requestedChannel = $this->params->getChannel();
$channelFlag = null;
if (null !== $requestedChannel) {
$installerHelpCommand = array(
\escapeshellarg($installerPathName),
'--no-ansi',
'--help',
);

if ($this->supportsForceVersionFlag($installerHelpCommand, $majorVersion)) {
$majorVersionFlag = '--' . $majorVersion;
if ($this->supportsChannelFlag($installerHelpCommand, $requestedChannel)) {
$channelFlag = '--' . $requestedChannel;
} else {
$this->showError(
"Installer doesn't allow to specify major version.\n" .
"Requested major version $majorVersion flag is ignored.\n" .
"Installer doesn't allow to specify channel or channel is wrong.\n" .
"Requested channel $requestedChannel flag is ignored.\n" .
"The installer's default is going to be installed.\n" .
"Please remove major version requirement from wrapper's environment variables or extra.wrapper section of composer.json file (as installer doesn't seem to support major version flags anymore), or report an issue at https://github.com/kamazee/composer-wrapper/issues is you think it does and the support is not detected properly"
"Please remove channel requirement from wrapper's environment variables or extra.wrapper section of composer.json file (as installer doesn't seem to support channel flags anymore), or report an issue at https://github.com/kamazee/composer-wrapper/issues is you think it does and the support is not detected properly"
);
}
}
Expand All @@ -302,8 +301,8 @@ if (!class_exists('ComposerWrapper')) {
'--install-dir=' . \escapeshellarg($dir),
);

if (null !== $majorVersionFlag) {
$commandParts[] = $majorVersionFlag;
if (null !== $channelFlag) {
$commandParts[] = $channelFlag;
}

$this->passthru(
Expand Down Expand Up @@ -426,9 +425,9 @@ if (!class_exists('ComposerWrapper')) {

private function getSelfUpdateFlags($filename)
{
$forceVersionRequested = $this->params->getForceMajorVersion();
$channelRequested = $this->params->getChannel();
$flags = array();
if (null === $forceVersionRequested) {
if (null === $channelRequested) {
return $flags;
}

Expand All @@ -439,14 +438,14 @@ if (!class_exists('ComposerWrapper')) {
'self-update',
);

if ($this->supportsForceVersionFlag($selfUpdateHelpCommand, $forceVersionRequested)) {
$flags[] = "--$forceVersionRequested";
} elseif (1 == $forceVersionRequested) {
// 1.10.5 supports flags, so should be a good intermediate version
if ($this->supportsChannelFlag($selfUpdateHelpCommand, $channelRequested)) {
$flags[] = "--$channelRequested";
} elseif (1 == $channelRequested) {
// 1.10.5 supports channel flags, so should be a good intermediate version
$flags[] = '1.10.5';
} else {
$this->showError(
"Forcing version $forceVersionRequested is requested but current composer version doesn't support --$forceVersionRequested flag, so nothing will be forced."
"Forcing channel $channelRequested is requested but current composer version doesn't support --$channelRequested flag, so nothing will be forced."
);
}

Expand Down
6 changes: 3 additions & 3 deletions tests/ComposerWrapperParamsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function () use ($params) {
);

self::assertSame("101 days", $params->getUpdateFreq());
self::assertSame("2", $params->getForceMajorVersion());
self::assertSame("2", $params->getChannel());
self::assertSame("dir_from_env", $params->getComposerDir());
}

Expand All @@ -48,7 +48,7 @@ function () use (&$params) {
);

self::assertSame('101 days', $params->getUpdateFreq());
self::assertSame('2', $params->getForceMajorVersion());
self::assertSame('2', $params->getChannel());
self::assertSame('dir_from_composer-from-env.json', $params->getComposerDir());
}

Expand Down Expand Up @@ -108,7 +108,7 @@ public function setUpdateFreqThrowsOnBadValues($input)
public function forceMajorVersionLoadDefault()
{
$params = $this->loadParamsDefault();
self::assertNull($params->getForceMajorVersion());
self::assertNull($params->getChannel());
}


Expand Down
2 changes: 1 addition & 1 deletion tests/ComposerWrapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ function($command, &$exitCode) use ($self, $flag) {
);

$params = new ComposerWrapperParams();
$params->setForceMajorVersion($version);
$params->setChannel($version);
self::setNonPublic($wrapper, 'params', $params);

self::callNonPublic($wrapper, 'selfUpdate', array(__FILE__));
Expand Down

0 comments on commit 31ae1b7

Please sign in to comment.