Skip to content

Commit

Permalink
Create a bash wrapper that allows users to select a specific PHP vers…
Browse files Browse the repository at this point in the history
…ion via an environment variable.
  • Loading branch information
greg-1-anderson committed Dec 17, 2024
1 parent 353cb73 commit e231b62
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 81 deletions.
131 changes: 50 additions & 81 deletions bin/terminus
Original file line number Diff line number Diff line change
@@ -1,81 +1,50 @@
#!/usr/bin/env php
<?php

/**
* This script runs Terminus. It does the following:
* - Includes the Composer autoload file
* - Starts a container with the input, output, application, and configuration objects
* - Starts a runner instance and runs the command
* - Exits with a status code
*/

// Unset memory limit
ini_set('memory_limit', -1);

if (version_compare(PHP_VERSION, '8.2.0', '<') === true) {
fwrite(STDERR, "\n");
fwrite(STDERR, 'Sorry, your PHP version (' . PHP_VERSION . ') is no longer supported.' . "\n");
fwrite(STDERR, 'Upgrade to PHP 8.2 or newer to use Terminus 4. For PHP versions prior to 8.2, downgrade to Terminus 3.x.' . "\n\n");
fwrite(STDERR, 'For more information, see https://pantheon.io/docs/terminus/updates#php-version-compatibility-matrix' . "\n\n");
exit(1);
}

if (!getenv('TERMINUS_ALLOW_UNSUPPORTED_NEWER_PHP') && version_compare(PHP_VERSION, '8.5.0', '>=') === true) {
fwrite(STDERR, "\n");
fwrite(STDERR, 'PHP 8.5+ is not supported by this version of Terminus.' . "\n");
fwrite(STDERR, 'Check for new versions at https://github.com/pantheon-systems/terminus/releases' . "\n");
fwrite(STDERR, "\n");
fwrite(STDERR, 'Set environment variable TERMINUS_ALLOW_UNSUPPORTED_NEWER_PHP to try continuing anyway.' . "\n");
fwrite(STDERR, "Stopping.\n\n");
exit(1);
}

// This variable is automatically managed via updateDependenciesversion() in /RoboFile.php,
// which is run after every call to composer update.
$terminusPluginsDependenciesVersion = '51e2c517dd';

// Cannot use $_SERVER superglobal since that's empty during phpunit testing
// getenv('HOME') isn't set on Windows and generates a Notice.
$home = @getenv('HOME');
if (!empty($home)) {
// home should never end with a trailing slash.
$home = rtrim($home, '/');
}

if (empty($home) && !empty($_SERVER['HOMEDRIVE']) && !empty($_SERVER['HOMEPATH'])) {
// home on windows
$home = $_SERVER['HOMEDRIVE'] . $_SERVER['HOMEPATH'];
// If HOMEPATH is a root directory the path can end with a slash. Make sure
// that doesn't happen.
$home = rtrim($home, '\\/');
@putenv("HOME={$home}");
}

$pharPath = \Phar::running(true);
if ($pharPath) {
include_once("$pharPath/vendor/autoload.php");
} elseif (file_exists($path = __DIR__ . '/../vendor/autoload.php')
|| file_exists($path = __DIR__ . '/../../autoload.php')
|| file_exists($path = __DIR__ . '/../../../autoload.php')
) {
include_once($path);
} else {
throw new \Exception('Could not locate autoload.php');
}

use Pantheon\Terminus\Terminus;

$home_tokens_folder = '.terminus' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR . 'tokens';

$tokens_dir = $home . DIRECTORY_SEPARATOR . $home_tokens_folder;
if (!is_dir($tokens_dir)) {
mkdir(
$tokens_dir,
0700,
true
);
}

$terminus = Terminus::factory($terminusPluginsDependenciesVersion);
$status_code = $terminus->run();
exit($status_code);
#!/usr/bin/env sh

SCRIPTPATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"

# If we can find a .env file next to the `terminus` binary, source it.
# TODO: Is this a reasonable location? Where else should we look?
# Should we make the filename more specific, e.g. `.terminus-env`?
if [ -f "$SCRIPTPATH/.env" ]; then
source "$SCRIPTPATH/.env"
fi

# One good fallback location is in the Terminus config directory.
# Note that we already have $HOME/.terminus/config.yml, but I
# don't think we should parse that here.
if [ -f "$HOME/.terminus/.env" ]; then
source "$HOME/.terminus/.env"
fi

# The logic below is ideal for scripts that are distributed
# in `vendor/bin`. We might benefit if we decide to support
# `terminus create-project pantheon-systems/terminus` as an
# installation method. If we decide aginst this, we could
# remove "$COMPOSER_RUNTIME_BIN_DIR" checks.
if [ -z "$COMPOSER_RUNTIME_BIN_DIR" ]; then
# This branch is for the development of Drush itself.
# @see https://stackoverflow.com/questions/4774054
TERMINUS_EXECUTABLE="$SCRIPTPATH/terminus.php"
else
TERMINUS_EXECUTABLE="$COMPOSER_RUNTIME_BIN_DIR/terminus.php"
fi

# If we can't find `terminus.php` next to `terminus`,
# look for `terminus.php` in the same directory.
if [ ! -f "$TERMINUS_EXECUTABLE" ]; then
TERMINUS_EXECUTABLE="$SCRIPTPATH/terminus.phar"
fi

# If there is no `terminus.phar` nearby, check for one
# in the $PATH.
if [ ! -f "$TERMINUS_EXECUTABLE" ]; then
TERMINUS_EXECUTABLE="terminus.phar"
fi

# Use the PHP binary indicated by $TERMINUS_PHP, or use
# the one in the $PATH if $TERMINUS_PHP is not set.
PHP="${TERMINUS_PHP=php}"

# Run Terminus with the selected version of PHP, passing
# through all parameters.
exec "$TERMINUS_PHP" "$TERMINUS_EXECUTABLE" "$@"
81 changes: 81 additions & 0 deletions bin/terminus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/usr/bin/env php
<?php

/**
* This script runs Terminus. It does the following:
* - Includes the Composer autoload file
* - Starts a container with the input, output, application, and configuration objects
* - Starts a runner instance and runs the command
* - Exits with a status code
*/

// Unset memory limit
ini_set('memory_limit', -1);

if (version_compare(PHP_VERSION, '8.2.0', '<') === true) {
fwrite(STDERR, "\n");
fwrite(STDERR, 'Sorry, your PHP version (' . PHP_VERSION . ') is no longer supported.' . "\n");
fwrite(STDERR, 'Upgrade to PHP 8.2 or newer to use Terminus 4. For PHP versions prior to 8.2, downgrade to Terminus 3.x.' . "\n\n");
fwrite(STDERR, 'For more information, see https://pantheon.io/docs/terminus/updates#php-version-compatibility-matrix' . "\n\n");
exit(1);
}

if (!getenv('TERMINUS_ALLOW_UNSUPPORTED_NEWER_PHP') && version_compare(PHP_VERSION, '8.5.0', '>=') === true) {
fwrite(STDERR, "\n");
fwrite(STDERR, 'PHP 8.5+ is not supported by this version of Terminus.' . "\n");
fwrite(STDERR, 'Check for new versions at https://github.com/pantheon-systems/terminus/releases' . "\n");
fwrite(STDERR, "\n");
fwrite(STDERR, 'Set environment variable TERMINUS_ALLOW_UNSUPPORTED_NEWER_PHP to try continuing anyway.' . "\n");
fwrite(STDERR, "Stopping.\n\n");
exit(1);
}

// This variable is automatically managed via updateDependenciesversion() in /RoboFile.php,
// which is run after every call to composer update.
$terminusPluginsDependenciesVersion = '51e2c517dd';

// Cannot use $_SERVER superglobal since that's empty during phpunit testing
// getenv('HOME') isn't set on Windows and generates a Notice.
$home = @getenv('HOME');
if (!empty($home)) {
// home should never end with a trailing slash.
$home = rtrim($home, '/');
}

if (empty($home) && !empty($_SERVER['HOMEDRIVE']) && !empty($_SERVER['HOMEPATH'])) {
// home on windows
$home = $_SERVER['HOMEDRIVE'] . $_SERVER['HOMEPATH'];
// If HOMEPATH is a root directory the path can end with a slash. Make sure
// that doesn't happen.
$home = rtrim($home, '\\/');
@putenv("HOME={$home}");
}

$pharPath = \Phar::running(true);
if ($pharPath) {
include_once("$pharPath/vendor/autoload.php");
} elseif (file_exists($path = __DIR__ . '/../vendor/autoload.php')
|| file_exists($path = __DIR__ . '/../../autoload.php')
|| file_exists($path = __DIR__ . '/../../../autoload.php')
) {
include_once($path);
} else {
throw new \Exception('Could not locate autoload.php');
}

use Pantheon\Terminus\Terminus;

$home_tokens_folder = '.terminus' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR . 'tokens';

$tokens_dir = $home . DIRECTORY_SEPARATOR . $home_tokens_folder;
if (!is_dir($tokens_dir)) {
mkdir(
$tokens_dir,
0700,
true
);
}

$terminus = Terminus::factory($terminusPluginsDependenciesVersion);
$status_code = $terminus->run();
exit($status_code);
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
]
},
"bin": [
"bin/terminus.php",
"bin/terminus"
],
"scripts": {
Expand Down

0 comments on commit e231b62

Please sign in to comment.