Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TASK] Simplify package states generation #412

Merged
merged 1 commit into from
Jan 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 21 additions & 2 deletions Classes/Command/InstallCommandController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Helhum\Typo3Console\Install\PackageStatesGenerator;
use Helhum\Typo3Console\Mvc\Controller\CommandController;
use TYPO3\CMS\Core\Core\Bootstrap;
use TYPO3\CMS\Core\Package\PackageInterface;

/**
* Alpha version of a setup command controller
Expand Down Expand Up @@ -102,13 +103,31 @@ public function setupCommand(
*/
public function generatePackageStatesCommand(array $frameworkExtensions = [], $activateDefault = false, array $excludedExtensions = [])
{
if (Bootstrap::usesComposerClassLoading()) {
$ranFromComposerPlugin = getenv('TYPO3_CONSOLE_PLUGIN_RUN');
if (!$ranFromComposerPlugin && Bootstrap::usesComposerClassLoading()) {
$this->output->outputLine('<warning>This command is now always automatically executed after Composer has written the autoload information.</warning>');
$this->output->outputLine('<warning>It is therefore deprecated to be used in Composer mode.</warning>');
}
$frameworkExtensions = $frameworkExtensions ?: explode(',', (string)getenv('TYPO3_ACTIVE_FRAMEWORK_EXTENSIONS'));
$packageStatesGenerator = new PackageStatesGenerator($this->packageManager);
$packageStatesGenerator->generate($frameworkExtensions, $activateDefault, $excludedExtensions);
$activatedExtensions = $packageStatesGenerator->generate($frameworkExtensions, $activateDefault, $excludedExtensions);

$this->outputLine(
'<info>The following extensions have been added to the generated PackageStates.php file:</info> %s',
[
implode(', ', array_map(function (PackageInterface $package) {
return $package->getPackageKey();
}, $activatedExtensions))
]
);
if (!empty($excludedExtensions)) {
$this->outputLine(
'<info>The following third party extensions were excluded during this process:</info> %s',
[
implode(', ', $excludedExtensions)
]
);
}
}

/**
Expand Down
95 changes: 34 additions & 61 deletions Classes/Composer/InstallerScript/GeneratePackageStates.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
*/

use Composer\Script\Event as ScriptEvent;
use Composer\Util\Filesystem;
use Helhum\Typo3Console\Composer\InstallerScriptInterface;
use Helhum\Typo3Console\Core\ConsoleBootstrap;
use Helhum\Typo3Console\Install\PackageStatesGenerator;
use Helhum\Typo3Console\Mvc\Cli\CommandDispatcher;
use Helhum\Typo3ConsolePlugin\Config as PluginConfig;
use TYPO3\CMS\Core\Package\PackageInterface;
use TYPO3\CMS\Composer\Plugin\Config as Typo3Config;

class GeneratePackageStates implements InstallerScriptInterface
{
Expand All @@ -27,82 +27,55 @@ class GeneratePackageStates implements InstallerScriptInterface
* @return bool
*/
public function shouldRun(ScriptEvent $event)
{
return getenv('TYPO3_CONSOLE_TEST_SETUP') || $event->getComposer()->getPackage()->getName() !== 'helhum/typo3-console';
}

/**
* @param ScriptEvent $event
* @return bool
* @throws \RuntimeException
* @internal
*/
public function run(ScriptEvent $event)
{
$io = $event->getIO();
$pluginConfig = PluginConfig::load($io, $event->getComposer()->getConfig());
$composer = $event->getComposer();
$pluginConfig = PluginConfig::load($io, $composer->getConfig());
$typo3PluginConfig = Typo3Config::load($composer);

if ($pluginConfig->get('skip-packagestates-write')) {
$io->writeError('<warning>It is highly recommended to let the PackageStates.php file be generated automatically</warning>');
$io->writeError('<warning>Disabling this functionality will be removed with TYPO3 Console 5.0</warning>');
return;
return false;
}
$bootstrap = self::ensureTypo3Booted();
$packageManager = $bootstrap->getEarlyInstance(\TYPO3\CMS\Core\Package\PackageManager::class);
$packageStateGenerator = new PackageStatesGenerator($packageManager);

$frameworkExtensions = explode(',', (string)getenv('TYPO3_ACTIVE_FRAMEWORK_EXTENSIONS'));
$activateDefault = (bool)getenv('TYPO3_ACTIVATE_DEFAULT_FRAMEWORK_EXTENSIONS');
$excludedExtensions = $event->isDevMode() ? explode(',', (string)getenv('TYPO3_EXCLUDED_EXTENSIONS')) : [];

$activatedExtensions = $packageStateGenerator->generate($frameworkExtensions, $activateDefault, $excludedExtensions);
if ($typo3PluginConfig->get('prepare-web-dir') === false) {
return false;
}

$io->writeError(
sprintf(
'<info>The following extensions have been added to the generated PackageStates.php file:</info> %s',
implode(', ', array_map(function (PackageInterface $package) {
return $package->getPackageKey();
}, $activatedExtensions))
),
true,
$io::VERBOSE
);
if ($event->isDevMode() && !empty(getenv('TYPO3_EXCLUDED_EXTENSIONS'))) {
$io->writeError(
sprintf(
'<info>The following third party extensions were excluded during this process:</info> %s',
getenv('TYPO3_EXCLUDED_EXTENSIONS')
),
true,
$io::VERBOSE
);
if (!getenv('TYPO3_CONSOLE_TEST_SETUP') && $composer->getPackage()->getName() === 'helhum/typo3-console') {
return false;
}

// Ensure we have at least the typo3conf folder present
(new Filesystem())->ensureDirectoryExists($typo3PluginConfig->get('web-dir') . '/typo3conf');
return true;
}

/**
* @param ScriptEvent $event
* @return bool
* @throws \RuntimeException
* @internal
*/
private static function hasTypo3Booted()
public function run(ScriptEvent $event)
{
// Since this code is executed in composer runtime,
// we can safely assume that TYPO3 has not been bootstrapped
// until this API has been initialized to return true
return ConsoleBootstrap::usesComposerClassLoading();
}
$io = $event->getIO();
$composerConfig = $event->getComposer()->getConfig();

/**
* @return ConsoleBootstrap
*/
private static function ensureTypo3Booted()
{
if (!self::hasTypo3Booted()) {
define('PATH_site', getenv('TYPO3_PATH_WEB') . '/');
$bootstrap = ConsoleBootstrap::create('Production');
$bootstrap->initialize(new \Composer\Autoload\ClassLoader());
} else {
$bootstrap = ConsoleBootstrap::getInstance();
$commandDispatcher = CommandDispatcher::createFromComposerRun([$composerConfig->get('bin-dir'), realpath('./Scripts')]);
$commandOptions = [
'frameworkExtensions' => (string)getenv('TYPO3_ACTIVE_FRAMEWORK_EXTENSIONS'),
];
if (getenv('TYPO3_ACTIVATE_DEFAULT_FRAMEWORK_EXTENSIONS')) {
$commandOptions['activateDefault'] = null;
}
return $bootstrap;
if ($event->isDevMode()) {
$commandOptions['excludedExtensions'] = (string)getenv('TYPO3_EXCLUDED_EXTENSIONS');
}
$output = $commandDispatcher->executeCommand('install:generatepackagestates', $commandOptions);
$io->writeError($output, true, $io::VERBOSE);

return true;
}
}
2 changes: 1 addition & 1 deletion Classes/Composer/InstallerScript/InstallDummyExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class InstallDummyExtension implements InstallerScriptInterface
*/
public function shouldRun(ScriptEvent $event)
{
return getenv('TYPO3_CONSOLE_TEST_SETUP') || $event->getComposer()->getPackage()->getName() !== 'helhum/typo3-console';
return $event->getComposer()->getPackage()->getName() !== 'helhum/typo3-console';
}

/**
Expand Down
6 changes: 5 additions & 1 deletion Classes/Mvc/Cli/CommandDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ public static function create($typo3cmsCommandPath, ProcessBuilder $processBuild
} else {
$processBuilder->setPrefix($typo3cmsCommandPath);
}
$processBuilder->addEnvironmentVariables(['TYPO3_CONSOLE_SUB_PROCESS' => true]);
return new self($processBuilder);
}

Expand All @@ -127,15 +128,18 @@ public static function create($typo3cmsCommandPath, ProcessBuilder $processBuild
*
* @param string $command Command identifier
* @param array $arguments Argument names will automatically be converted to dashed version, fi not provided like so
* @param array $environment Environment vars to be added to the command
* @return string Output of the executed command
* @throws FailedSubProcessCommandException
*/
public function executeCommand($command, array $arguments = [])
public function executeCommand($command, array $arguments = [], array $environment = [])
{
// We need to clone the builder to be able to re-use the object for multiple commands
$processBuilder = clone $this->processBuilder;

$processBuilder->add($command);
$processBuilder->addEnvironmentVariables($environment);

foreach ($arguments as $argumentName => $argumentValue) {
if (strpos($argumentName, '--') === 0) {
$dashedName = $argumentName;
Expand Down
7 changes: 6 additions & 1 deletion Classes/Mvc/Cli/ConsoleOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Output\ConsoleOutput as SymfonyConsoleOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ChoiceQuestion;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Console\Question\Question;
Expand Down Expand Up @@ -105,7 +106,11 @@ public function output($text, array $arguments = [])
if ($arguments !== []) {
$text = vsprintf($text, $arguments);
}
$this->output->write($text);
if (getenv('TYPO3_CONSOLE_SUB_PROCESS')) {
$this->output->write($text, false, OutputInterface::OUTPUT_RAW);
} else {
$this->output->write($text);
}
}

/**
Expand Down
4 changes: 2 additions & 2 deletions Scripts/typo3cms.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@
if (file_exists($autoLoadFile = realpath($typo3Root . '/typo3') . '/../vendor/autoload.php')) {
// The extension is in typo3conf/ext, so we load the main autoload.php from TYPO3 sources
// Applicable in both Composer and non-Composer mode.
$classLoader = require_once $autoLoadFile;
$classLoader = require $autoLoadFile;
} elseif (!empty($vendorDir) && file_exists($autoLoadFile = $vendorDir . '/autoload.php')) {
// The package is in vendor dir, so we load the main autoload.php from vendor folder
// Applicable in Composer mode only.
$classLoader = require_once $autoLoadFile;
$classLoader = require $autoLoadFile;
} else {
echo 'Could not find autoload.php file. Is TYPO3_PATH_WEB specified correctly?' . PHP_EOL;
exit(1);
Expand Down
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@
"typo3/cms-scheduler": "^7.6 || >=8.3.0 <8.6",

"symfony/console": "^2.7 || ^3.0",
"symfony/process": "^2.7 || ^3.0",
"typo3/cms": "dev-master as 8.5.1"
"symfony/process": "^2.7 || ^3.0"
},
"require-dev": {
"namelesscoder/typo3-repository-client": "^1.2.0",
Expand Down