NEW Clearer file migration output with colours

This commit is contained in:
Ingo Schommer 2019-05-28 17:20:36 +12:00
parent 14673ffd0a
commit 7301b375b8
2 changed files with 90 additions and 31 deletions

View File

@ -22,6 +22,7 @@
"sake" "sake"
], ],
"require": { "require": {
"bramus/monolog-colored-line-formatter": "~2.0",
"composer/installers": "~1.0", "composer/installers": "~1.0",
"embed/embed": "^3.0", "embed/embed": "^3.0",
"league/csv": "^8", "league/csv": "^8",

View File

@ -15,6 +15,7 @@ use SilverStripe\Core\Injector\Injector;
use SilverStripe\Logging\PreformattedEchoHandler; use SilverStripe\Logging\PreformattedEchoHandler;
use SilverStripe\Dev\BuildTask; use SilverStripe\Dev\BuildTask;
use SilverStripe\Assets\Dev\Tasks\SecureAssetsMigrationHelper; use SilverStripe\Assets\Dev\Tasks\SecureAssetsMigrationHelper;
use \Bramus\Monolog\Formatter\ColoredLineFormatter;
/** /**
* Migrates all 3.x file dataobjects to use the new DBFile field. * Migrates all 3.x file dataobjects to use the new DBFile field.
@ -51,6 +52,11 @@ class MigrateFileTask extends BuildTask
$this->extend('preFileMigration'); $this->extend('preFileMigration');
$this->logger->warn(
'Please read https://docs.silverstripe.org/en/4/developer_guides/files/file_migration/ ' .
'before running this task.'
);
$subtasks = !empty($args['only']) ? explode(',', $args['only']) : $this->defaultSubtasks; $subtasks = !empty($args['only']) ? explode(',', $args['only']) : $this->defaultSubtasks;
$subtask = 'move-files'; $subtask = 'move-files';
@ -59,15 +65,23 @@ class MigrateFileTask extends BuildTask
$this->logger->error("No file migration helper detected"); $this->logger->error("No file migration helper detected");
} else { } else {
$this->extend('preFileMigrationSubtask', $subtask); $this->extend('preFileMigrationSubtask', $subtask);
$this->logger->info("### Migrating filesystem and database records ({$subtask})");
$this->logger->info('If the task fails or times out, run it again and it will start where it left off.');
$migrated = FileMigrationHelper::singleton()->run(); $this->logger->notice("######################################################");
if ($migrated) { $this->logger->notice("Migrating filesystem and database records ({$subtask})");
$this->logger->info("{$migrated} File DataObjects upgraded"); $this->logger->notice("######################################################");
} else {
$this->logger->info("No File DataObjects need upgrading"); FileMigrationHelper::singleton()
} ->setLogger($this->logger)
->run();
// TODO Split file migration helper into two tasks,
// and report back on their process counts consistently here
// if ($count) {
// $this->logger->info("{$count} File objects upgraded");
// } else {
// $this->logger->info("No File objects needed upgrading");
// }
$this->extend('postFileMigrationSubtask', $subtask); $this->extend('postFileMigrationSubtask', $subtask);
} }
} }
@ -78,16 +92,19 @@ class MigrateFileTask extends BuildTask
$this->logger->error("LegacyThumbnailMigrationHelper not found"); $this->logger->error("LegacyThumbnailMigrationHelper not found");
} else { } else {
$this->extend('preFileMigrationSubtask', $subtask); $this->extend('preFileMigrationSubtask', $subtask);
$this->logger->info("### Migrating existing thumbnails ({$subtask})");
$moved = LegacyThumbnailMigrationHelper::singleton() $this->logger->notice("#############################################################");
$this->logger->notice("Migrating existing thumbnails to new file format ({$subtask})");
$this->logger->notice("#############################################################");
$paths = LegacyThumbnailMigrationHelper::singleton()
->setLogger($this->logger) ->setLogger($this->logger)
->run($this->getStore()); ->run($this->getStore());
if ($moved) { if ($paths) {
$this->logger->info(sprintf("%d thumbnails moved", count($moved))); $this->logger->info(sprintf("%d thumbnails moved", count($paths)));
} else { } else {
$this->logger->info("No thumbnails moved"); $this->logger->info("No thumbnails needed to be moved");
} }
$this->extend('postFileMigrationSubtask', $subtask); $this->extend('postFileMigrationSubtask', $subtask);
@ -100,8 +117,21 @@ class MigrateFileTask extends BuildTask
$this->logger->error("ImageThumbnailHelper not found"); $this->logger->error("ImageThumbnailHelper not found");
} else { } else {
$this->extend('preFileMigrationSubtask', $subtask); $this->extend('preFileMigrationSubtask', $subtask);
$this->logger->info("### Generating new CMS UI thumbnails ({$subtask})");
ImageThumbnailHelper::singleton()->run(); $this->logger->notice("#############################################");
$this->logger->notice("Generating new CMS UI thumbnails ({$subtask})");
$this->logger->notice("#############################################");
$count = ImageThumbnailHelper::singleton()
->setLogger($this->logger)
->run();
if ($count > 0) {
$this->logger->info("Created {$count} CMS UI thumbnails");
} else {
$this->logger->info("No CMS UI thumbnails needed to be created");
}
$this->extend('postFileMigrationSubtask', $subtask); $this->extend('postFileMigrationSubtask', $subtask);
} }
} }
@ -113,11 +143,17 @@ class MigrateFileTask extends BuildTask
} else { } else {
$this->extend('preFileMigrationSubtask', $subtask); $this->extend('preFileMigrationSubtask', $subtask);
$this->logger->info("### Fixing folder permissions ({$subtask})"); $this->logger->notice("####################################################");
$updated = FixFolderPermissionsHelper::singleton()->run(); $this->logger->notice("Fixing secure-assets folder permissions ({$subtask})");
$this->logger->notice("####################################################");
$this->logger->debug('Only required if the 3.x project included silverstripe/secure-assets');
if ($updated > 0) { $count = FixFolderPermissionsHelper::singleton()
$this->logger->info("Repaired {$updated} folders with broken CanViewType settings"); ->setLogger($this->logger)
->run();
if ($count > 0) {
$this->logger->info("Repaired {$count} folders with broken CanViewType settings");
} else { } else {
$this->logger->info("No folders required fixes"); $this->logger->info("No folders required fixes");
} }
@ -133,11 +169,21 @@ class MigrateFileTask extends BuildTask
} else { } else {
$this->extend('preFileMigrationSubtask', $subtask); $this->extend('preFileMigrationSubtask', $subtask);
$this->logger->info("### Fixing secure-assets ({$subtask})"); $this->logger->notice("#####################################################");
$moved = SecureAssetsMigrationHelper::singleton() $this->logger->notice("Fixing secure-assets folder restrictions ({$subtask})");
$this->logger->notice("#####################################################");
$this->logger->debug('Only required if the 3.x project included silverstripe/secure-assets');
$paths = SecureAssetsMigrationHelper::singleton()
->setLogger($this->logger) ->setLogger($this->logger)
->run($this->getStore()); ->run($this->getStore());
if (count($paths) > 0) {
$this->logger->info(sprintf("Repaired %d folders broken folder restrictions", count($paths)));
} else {
$this->logger->info("No folders required fixes");
}
$this->extend('postFileMigrationSubtask', $subtask); $this->extend('postFileMigrationSubtask', $subtask);
} }
} }
@ -151,9 +197,8 @@ class MigrateFileTask extends BuildTask
{ {
return <<<TXT return <<<TXT
Imports all files referenced by File dataobjects into the new Asset Persistence Layer introduced in 4.0. Imports all files referenced by File dataobjects into the new Asset Persistence Layer introduced in 4.0.
Moves existing thumbnails, and generates new thumbnail sizes for the CMS UI. Moves existing thumbnails, and generates new thumbnail sizes for the CMS UI. Fixes file permissions.
Fixes file permissions. If the task fails or times out, run it again and if possible the tasks will start where they left off.
If the task fails or times out, run it again and it will start where it left off.
You need to flush your cache after running this task via CLI. You need to flush your cache after running this task via CLI.
See https://docs.silverstripe.org/en/4/developer_guides/files/file_migration/. See https://docs.silverstripe.org/en/4/developer_guides/files/file_migration/.
TXT; TXT;
@ -198,13 +243,26 @@ TXT;
*/ */
protected function addLogHandlers() protected function addLogHandlers()
{ {
if ($logger = Injector::inst()->get(LoggerInterface::class)) { // Create a default logger with a custom name (less misleading than "error-log")
if (Director::is_cli()) { $logger = Injector::inst()->create(LoggerInterface::class, 'log');
$logger->pushHandler(new StreamHandler('php://stdout'));
$logger->pushHandler(new StreamHandler('php://stderr', Logger::WARNING)); if (Director::is_cli()) {
} else { $formatter = new ColoredLineFormatter();
$logger->pushHandler(new PreformattedEchoHandler()); $formatter->ignoreEmptyContextAndExtra();
}
// Don't double process WARNING or higher levels in other handlers (bubble=false)
$errorHandler = new StreamHandler('php://stderr', Logger::ERROR, false);
$errorHandler->setFormatter($formatter);
$standardHandler = new StreamHandler('php://stdout');
$standardHandler->setFormatter($formatter);
$logger->pushHandler($standardHandler);
$logger->pushHandler($errorHandler);
} else {
$logger->pushHandler(new PreformattedEchoHandler());
} }
$this->logger = $logger;
} }
} }