Skip to content

Commit

Permalink
[CMS-474] Do not require composer/composer as a direct dependency of …
Browse files Browse the repository at this point in the history
…Terminus (#2308)

* Delete unused BuildDebianPackage.php

* Delete JsonFile.php and ComposerFile.php

* Remove unused "use" statements

* Remove composer/composer dependency

* Replace usage of composer/composer package with native php in RoboFile::updateDependenciesversion()

* Add todos to replace usages of composer/composer package with native php implementations

* Add RoboFile::getProjectPath()

* Replace usage of composer/composer package with native php in RoboFile::bundleLinux()

* Replace usage of composer/composer package with native php in CommandCoverageReport::getRootDir()

* Replace usage of composer/composer package with native php in CreateTestSiteArchive.php

* Fix code style issue in RoboFile.php

* Update composer dependencies

* Generate Terminus plugins dependencies version bashed on composer.lock file hash

* Use DIRECTORY_SEPARATOR in RoboFile::bundleLinux()

* Update $terminusPluginsDependenciesVersion value

* Improve error verbosity in PluginBaseCommand.php

* Throw TerminusException if Terminus composer.lock not found

* Include composer.json and composer.lock into phar

* Improve error handling in updateTerminusDependencies()
  • Loading branch information
Sergei Churilo authored Jan 14, 2022
1 parent 7692511 commit 5b88d12
Show file tree
Hide file tree
Showing 14 changed files with 127 additions and 1,500 deletions.
99 changes: 55 additions & 44 deletions RoboFile.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
<?php


use Consolidation\AnnotatedCommand\CommandFileDiscovery;

use Pantheon\Terminus\Config\ConfigAwareTrait;
use Pantheon\Terminus\Helpers\CommandCoverageReport;
use Pantheon\Terminus\Helpers\Composer\ComposerFile;
use Pantheon\Terminus\Terminus;
use Robo\Tasks;
use Twig\Environment;
use Twig\Extension\EscaperExtension;
use Twig\Loader\FilesystemLoader;
use wdm\debian\control\StandardFile;
use wdm\debian\Packager;

/**
* Housekeeping tasks for Terminus.
*
* Class RoboFile
*/
class RoboFile extends \Robo\Tasks
class RoboFile extends Tasks
{
use ConfigAwareTrait;

Expand All @@ -25,7 +24,6 @@ class RoboFile extends \Robo\Tasks
*/
protected Terminus $terminus;


/**
* RoboFile constructor.
*/
Expand All @@ -36,29 +34,21 @@ public function __construct()
}

/**
* @param string $file
* @return \Robo\Result
* @throws \Twig\Error\LoaderError
* @throws \Twig\Error\RuntimeError
* @throws \Twig\Error\SyntaxError
* @param string|null $file
*/
public function doc($file = null)
{
//TODO: change this to real documentation building from phpdoc
$readme = (string) CommandCoverageReport::factory();
if ($file) {
file_put_contents($file, $readme);
$readme = "./README.md regenerated.";
$readme = './README.md regenerated.';
}
$this->output()->writeln($readme);
}

/**
* @param string $file
* @return \Robo\Result
* @throws \Twig\Error\LoaderError
* @throws \Twig\Error\RuntimeError
* @throws \Twig\Error\SyntaxError
* @param string|null $file
*/
public function coverage($file = null)
{
Expand All @@ -71,47 +61,54 @@ public function coverage($file = null)
}

/**
* Updates $terminusPluginsDependenciesVersion variable.
* Updates $terminusPluginsDependenciesVersion variable in bin/terminus.
*/
public function updateDependenciesversion()
{
$this->say('Updating terminus dependencies version.');
$composerFilePath = realpath(dirname(\Composer\Factory::getComposerFile()));
$composerLockContents = file_get_contents($composerFilePath . DIRECTORY_SEPARATOR . 'composer.lock');
$composerLockJson = json_decode($composerLockContents, true, 10);
$hash = substr($composerLockJson['content-hash'], 0, 7);
$binFileContents = file_get_contents('bin/terminus');
$newBinFileContents = preg_replace("/(terminusPluginsDependenciesVersion\s?=)(.*)/m", "$1 '${hash}';", $binFileContents);
$this->say('Checking Terminus plugins dependencies version...');
$hash = substr(sha1_file($this->getProjectPath() . DIRECTORY_SEPARATOR . 'composer.lock'), 0, 10);
$binFileContents = file_get_contents(
$this->getProjectPath() . DIRECTORY_SEPARATOR . 'bin' . DIRECTORY_SEPARATOR . 'terminus'
);
$newBinFileContents = preg_replace(
'/(terminusPluginsDependenciesVersion\s=\s\')(.+)(\';)/',
"\${1}$hash\${3}",
$binFileContents
);
if ($newBinFileContents && $newBinFileContents !== $binFileContents) {
file_put_contents('bin/terminus', $newBinFileContents);
file_put_contents('bin' . DIRECTORY_SEPARATOR . 'terminus', $newBinFileContents);
$this->say('Terminus plugins dependencies version has been updated.');
return;
}

$this->say('Terminus plugins dependencies version remains unchanged.');
}

/**
* @return mixed|null
* @throws Exception
*
* @throws \Exception
*/
public function bundleLinux()
{
$this->say('Building DEBIAN/UBUNTU package.');
$composerFilePath = realpath(dirname(\Composer\Factory::getComposerFile()));

$composerContents = new ComposerFile(
$composerFilePath . DIRECTORY_SEPARATOR . 'composer.json'
);
$outputPath = $composerFilePath . DIRECTORY_SEPARATOR . 'package';
$terminus_binary = "{$composerFilePath}/terminus";
$terminus_binary = $this->getProjectPath() . DIRECTORY_SEPARATOR . 'terminus';
$dpkg_installed_size = ceil(filesize($terminus_binary) / 1024);

$outputPath = $this->getProjectPath() . DIRECTORY_SEPARATOR . 'package';
// We need the output path empty.
if (is_dir($outputPath)) {
exec(sprintf('rm -Rf %s', $outputPath));
mkdir($outputPath);
}

$name = $composerContents->getName();
$composerJson = json_decode(
file_get_contents($this->getProjectPath() . DIRECTORY_SEPARATOR . 'composer.json'),
true
);

[$vendor, $package] = explode('/', $name);
[$vendor, $package] = explode('/', $composerJson['name']);
// Create a config object.
$config = $this->getConfig();

Expand All @@ -124,19 +121,22 @@ public function bundleLinux()
->setArchitecture('all')
->setMaintainer('Terminus', '[email protected]')
->setProvides($package)
->setDescription($composerContents->getDescription());
->setDescription($composerJson['description']);

$packager = new \wdm\debian\Packager();
$packager = new Packager();

$packager->setOutputPath($outputPath);
$packager->setControl($control);
$packager->addMount($terminus_binary, '/usr/bin/terminus');
$packager->addMount(
$terminus_binary,
DIRECTORY_SEPARATOR . 'usr' . DIRECTORY_SEPARATOR . 'bin' . DIRECTORY_SEPARATOR . 'terminus'
);

//Creates folders using mount points
// Creates folders using mount points.
$packager->run();

// Get the Debian package command
// Expectation is that this is a command line invocation for dpkg
// Get the Debian package command.
// Expectation is that this is a command line invocation for dpkg.
$packageCommand = $packager->build();
$this->say($packageCommand);

Expand All @@ -146,14 +146,15 @@ public function bundleLinux()
exec($packageCommand, $result, $status);

if ($status !== 0) {
throw new \Exception(join(PHP_EOL, $result));
throw new Exception(join(PHP_EOL, $result));
}
if (!is_array($result)) {
$result = [$result];
}
// Package should be last line of output from command
$packageFile = array_shift($result);
$this->say('Package created: ' . $packageFile);

return $packageFile;
}

Expand All @@ -175,7 +176,7 @@ public function bundleMac()
$twig = new Environment($loader, [
'cache' => false
]);
$twig->getExtension(\Twig\Extension\EscaperExtension::class)
$twig->getExtension(EscaperExtension::class)
->setDefaultStrategy('url');
$formulaFolder = $config->get('root') . DIRECTORY_SEPARATOR . 'Formula';
if (is_dir($formulaFolder)) {
Expand All @@ -186,7 +187,7 @@ public function bundleMac()
$formulaFolder . DIRECTORY_SEPARATOR . 'terminus.rb',
$twig->render('homebrew-receipt.twig', $context)
);
$this->say("Mac Formula Created");
$this->say('Mac Formula Created');
}

/**
Expand All @@ -204,4 +205,14 @@ public function setTerminus(Terminus $terminus): void
{
$this->terminus = $terminus;
}

/**
* Returns the absolute path to the project.
*
* @return string
*/
private function getProjectPath(): string
{
return dirname(__FILE__);
}
}
2 changes: 1 addition & 1 deletion bin/terminus
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ if (!getenv('TERMINUS_ALLOW_UNSUPPORTED_NEWER_PHP') && version_compare(PHP_VERSI

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

// Cannot use $_SERVER superglobal since that's empty during phpunit testing
// getenv('HOME') isn't set on Windows and generates a Notice.
Expand Down
1 change: 1 addition & 0 deletions box.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"alias": "terminus.phar",
"check-requirements": false,
"exclude-composer-files": false,
"directories": [
"assets",
"bin",
Expand Down
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"require": {
"php": ">=7.4",
"ext-json": "*",
"composer/composer": "^2",
"composer/semver": "^3",
"consolidation/comments": "^1.0.2",
"consolidation/filter-via-dot-access-data": "^2.0",
Expand Down
Loading

0 comments on commit 5b88d12

Please sign in to comment.