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

[FEATURE] Always generate PackageStates.php file #389

Merged
merged 1 commit into from
Jan 14, 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
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ script:
- typo3cms configuration:set EXTCONF/lang/availableLanguages/1 fr_FR && grep fr_FR $TYPO3_PATH_WEB/typo3conf/LocalConfiguration.php
- typo3cms configuration:set EXTCONF/foo/bar baz && grep bar $TYPO3_PATH_WEB/typo3conf/LocalConfiguration.php | grep baz
- rm -f $TYPO3_PATH_WEB/typo3conf/PackageStates.php && typo3cms install:generatepackagestates --activate-default && [ -f "$TYPO3_PATH_WEB/typo3conf/PackageStates.php" ]
- rm -f $TYPO3_PATH_WEB/typo3conf/PackageStates.php && TYPO3_CONSOLE_TEST_SETUP=yes TYPO3_ACTIVATE_DEFAULT_FRAMEWORK_EXTENSIONS=yes composer dump-autoload -vv 2>&1 | grep 'generated PackageStates.php' && [ -f "$TYPO3_PATH_WEB/typo3conf/PackageStates.php" ]
- grep sys_note "$TYPO3_PATH_WEB/typo3conf/PackageStates.php"
- rm $TYPO3_PATH_WEB/typo3temp/index.html && typo3cms install:fixfolderstructure && [ -f "$TYPO3_PATH_WEB/typo3temp/index.html" ]
- typo3cms extension:list
- typo3cms extension:list --raw | grep extbase
Expand All @@ -123,6 +125,7 @@ script:
- typo3cms extension:activate core && typo3cms database:updateschema | grep 'No schema updates were performed'
- typo3cms extension:setup core && typo3cms database:updateschema | grep 'No schema updates were performed'
- typo3cms extension:setupactive && typo3cms database:updateschema | grep 'No schema updates were performed'
- typo3cms extension:removeinactive --force | grep 'The following directories have been removed'

after_script:
- >
Expand Down
51 changes: 51 additions & 0 deletions Classes/Command/ExtensionCommandController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
use TYPO3\CMS\Core\Core\Bootstrap;
use TYPO3\CMS\Core\Core\ClassLoadingInformation;
use TYPO3\CMS\Core\Package\PackageInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\PathUtility;

/**
* CommandController for working with extension management through CLI
Expand Down Expand Up @@ -65,6 +67,13 @@ class ExtensionCommandController extends CommandController
*/
public function activateCommand(array $extensionKeys)
{
if (Bootstrap::usesComposerClassLoading()) {
$this->output->outputLine('<warning>This command has been deprecated to be used in composer mode, as it might lead to unexpected results</warning>');
$this->output->outputLine('<warning>The PackageStates.php file that tracks which extension should be active,</warning>');
$this->output->outputLine('<warning>is now generated automatically when installing the console with composer.</warning>');
$this->output->outputLine('<warning>To set up all active extensions correctly, please use the extension:setupactive command</warning>');
}

$this->emitPackagesMayHaveChangedSignal();
$activatedExtensions = [];
$extensionsToSetUp = [];
Expand Down Expand Up @@ -104,6 +113,13 @@ public function activateCommand(array $extensionKeys)
*/
public function deactivateCommand(array $extensionKeys)
{
if (Bootstrap::usesComposerClassLoading()) {
$this->output->outputLine('<warning>This command has been deprecated to be used in composer mode, as it might lead to unexpected results</warning>');
$this->output->outputLine('<warning>The PackageStates.php file that tracks which extension should be active,</warning>');
$this->output->outputLine('<warning>is now generated automatically when installing the console with composer.</warning>');
$this->output->outputLine('<warning>To set up all active extensions correctly, please use the extension:setupactive command</warning>');
}

foreach ($extensionKeys as $extensionKey) {
$this->extensionInstaller->uninstall($extensionKey);
}
Expand Down Expand Up @@ -181,6 +197,41 @@ public function setupActiveCommand()
$this->setupExtensions($this->packageManager->getActivePackages());
}

/**
* Removes all extensions that are not marked as active
*
* Directories of inactive extension are <comment>removed</comment> from <code>typo3/sysext</code> and <code>typo3conf/ext</code>.
* This is a one way command with no way back. Don't blame anybody if this command destroys your data.
* <comment>Handle with care!</comment>
*
* @param bool $force The option has to be specified, otherwise nothing happens
*/
public function removeInactiveCommand($force = false)
{
if ($force) {
$activePackages = $this->packageManager->getActivePackages();
$this->packageManager->scanAvailablePackages();
foreach ($this->packageManager->getAvailablePackages() as $package) {
if (empty($activePackages[$package->getPackageKey()])) {
$this->packageManager->unregisterPackage($package);
if (is_dir($package->getPackagePath())) {
GeneralUtility::flushDirectory($package->getPackagePath());
$removedPaths[] = PathUtility::stripPathSitePrefix($package->getPackagePath());
}
}
}
$this->packageManager->forceSortAndSavePackageStates();
if (!empty($removedPaths)) {
$this->outputLine('<info>The following directories have been removed:</info>' . chr(10) . implode(chr(10), $removedPaths));
} else {
$this->outputLine('<info>Nothing was removed</info>');
}
} else {
$this->outputLine('<warning>Operation not confirmed and has been skipped</warning>');
$this->quit(1);
}
}

/**
* Dump class auto-load
*
Expand Down
34 changes: 11 additions & 23 deletions Classes/Command/InstallCommandController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
*/

use Helhum\Typo3Console\Install\FolderStructure\ExtensionFactory;
use Helhum\Typo3Console\Install\PackageStatesGenerator;
use Helhum\Typo3Console\Mvc\Controller\CommandController;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Core\Bootstrap;

/**
* Alpha version of a setup command controller
Expand All @@ -35,12 +36,6 @@ class InstallCommandController extends CommandController
*/
protected $cliSetupRequestHandler;

/**
* @var \Helhum\Typo3Console\Install\PackageStatesGenerator
* @inject
*/
protected $packageStatesGenerator;

/**
* TYPO3 Setup
*
Expand Down Expand Up @@ -101,26 +96,19 @@ public function setupCommand(
*
* <b>Example:</b> <code>TYPO3_ACTIVE_FRAMEWORK_EXTENSIONS="info,info_pagetsconfig" typo3cms install:generatepackagestates</code>
*
* @param bool $removeInactive Inactive extensions are <comment>removed</comment> from <code>typo3/sysext</code>. <comment>Handle with care!</comment>
* @param array $frameworkExtensions If given, this argument takes precedence over the environment variable
* @param bool $activateDefault If true, <code>typo3/cms</code> extensions that are marked as TYPO3 factory default, will be activated, even if not in the list of configured active framework extensions.
* @throws \TYPO3\CMS\Core\Package\Exception\InvalidPackageStateException
* @throws \TYPO3\CMS\Core\Package\Exception\ProtectedPackageKeyException
* @param array $excludedExtensions Extensions in typo3conf/ext/ directory, which should stay inactive
*/
public function generatePackageStatesCommand($removeInactive = false, $activateDefault = false)
public function generatePackageStatesCommand(array $frameworkExtensions = [], $activateDefault = false, array $excludedExtensions = [])
{
$this->packageStatesGenerator->generate($this->packageManager, $activateDefault);

if ($removeInactive) {
$activePackages = $this->packageManager->getActivePackages();
foreach ($this->packageManager->getAvailablePackages() as $package) {
if (empty($activePackages[$package->getPackageKey()])) {
$this->packageManager->unregisterPackage($package);
GeneralUtility::flushDirectory($package->getPackagePath());
$this->outputLine('Removed Package: ' . $package->getPackageKey());
}
}
$this->packageManager->forceSortAndSavePackageStates();
if (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);
}

/**
Expand Down
78 changes: 77 additions & 1 deletion Classes/Composer/InstallerScripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@
*
*/

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

/**
* Class for Composer and Extension Manager install scripts
Expand All @@ -33,12 +37,84 @@ class InstallerScripts
*/
public static function setupConsole(ScriptEvent $event)
{
if ($event->getComposer()->getPackage()->getName() === 'helhum/typo3-console') {
if (!getenv('TYPO3_CONSOLE_TEST_SETUP') && $event->getComposer()->getPackage()->getName() === 'helhum/typo3-console') {
return;
}
self::generatePackageStates($event);
self::installExtension($event);
}

/**
* @return bool
*/
private static function hasTypo3Booted()
{
// 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();
}

/**
* @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();
}
return $bootstrap;
}

/**
* @param ScriptEvent $event
*/
private static function generatePackageStates(ScriptEvent $event)
{
$io = $event->getIO();
$pluginConfig = PluginConfig::load($io, $event->getComposer()->getConfig());

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;
}
$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);

$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,
IOInterface::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,
IOInterface::VERBOSE
);
}
}

/**
* @param ScriptEvent $event
* @deprecated will be removed with 5.0
Expand Down