Select Git revision
AnnotationCreation.js
Forked from
IIIF / Mirador / Mirador annotations
Source project has a limited visibility.
UpdateAllProjects.php 5.82 KiB
<?php
namespace App\Command;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\ConsoleOutputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Path;
use Symfony\Component\HttpKernel\KernelInterface;
#[AsCommand(
name: 'rekall:projects:update',
description: 'Update commons files on projects.',
aliases: ['rekall-update-projects'],
hidden: false
)]
class UpdateAllProjects extends Command
{
private Filesystem $filesystem;
private KernelInterface $kernel;
/**
* @var string[] $black_list
*/
private array $black_list =
['css', 'create.zip', 'favicon.ico', 'la-page-des-malins.php', 'js', 'php', 'shared', '.', '..'];
public function __construct(Filesystem $filesystem, KernelInterface $kernel)
{
parent::__construct();
// best practices recommend to call the parent constructor first and
// then set your own properties. That wouldn't work in this case
// because configure() needs the properties set in this constructor
$this->filesystem = $filesystem;
$this->kernel = $kernel;
}
protected function configure(): void
{
$this
// If you don't like using the $defaultDescription static property,
// you can also define the short description using this method:
->setDescription('Update every project php files and index.html')
// the command help shown when running the command with the "--help" option
->setHelp('This command allows update projects common files...');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
if (!$output instanceof ConsoleOutputInterface) {
throw new \LogicException('This command accepts only an instance of "ConsoleOutputInterface".');
}
ProgressBar::setFormatDefinition(
'project_update_message',
' %current%/%max% [%bar%] %percent%% : Updating project :%project% %elapsed%'
);
$output->writeln("Updating all projects started");
$legacy_root = $this->kernel->getProjectDir() . '/legacy';
$capsule_prototype_root = './capsule-prototype';
$directory_files = scandir($legacy_root);
if (false === $directory_files) {
return Command::FAILURE;
}
$all_file_in_directory = $this->getProjectsDirectories($directory_files);
$php_files_count_per_projects = $this->getPhpFilesCountPerProjects($capsule_prototype_root);
$all_projects_progress_bar = new ProgressBar($output->section());
$all_projects_progress_bar->setFormat(
'%current%/%max% [%bar%] %percent%% : Overall project update progressing (%elapsed%)'
);
$all_projects_progress_bar->SetMessage('Projects update progress');
$all_projects_progress_bar->start();
foreach ($all_projects_progress_bar->iterate($all_file_in_directory) as $project_directory) {
$current_legacy_path = Path::normalize($legacy_root . '/' . $project_directory);
if (!$this->filesystem->exists($current_legacy_path)) {
continue;
}
if (!is_dir($current_legacy_path)) {
continue;
}
if (in_array($project_directory, $this->black_list)) {
continue;
}
$this->updateProjectDirectory(
$output,
$php_files_count_per_projects,
$current_legacy_path,
$capsule_prototype_root
);
$all_projects_progress_bar->advance($php_files_count_per_projects);
}
$all_projects_progress_bar->finish();
return Command::SUCCESS;
}
/**
* @param string[] $directory_files
* @return string[] projects directories
*/
private function getProjectsDirectories(array $directory_files): array
{
return array_diff($directory_files, $this->black_list);
}
/**
* @param string $capsule_prototype_root
* @return int
*/
protected function getPhpFilesCountPerProjects(string $capsule_prototype_root): int
{
$php_files = glob($capsule_prototype_root . '/php/*.php');
// add +1 for index.html file
return count($php_files ?: []) + 1;
}
/**
* @param ConsoleOutputInterface $output
* @param int $php_files_count_per_projects
* @param string $current_legacy_path
* @param string $capsule_prototype_root
* @return void
*/
protected function updateProjectDirectory(
ConsoleOutputInterface $output,
int $php_files_count_per_projects,
string $current_legacy_path,
string $capsule_prototype_root
): void {
$progress_bar = new ProgressBar($output->section(), $php_files_count_per_projects);
$progress_bar->setFormat('project_update_message');
$progress_bar->setMessage($current_legacy_path, 'project');
$progress_bar->start();
// override index.html
$this->filesystem->copy(
Path::normalize($capsule_prototype_root . '/index.html'),
Path::normalize($current_legacy_path . '/index.html'),
true
);
$progress_bar->advance();
// override php files
$this->filesystem->mirror(
Path::normalize($capsule_prototype_root . '/php'),
Path::normalize($current_legacy_path . '/php'),
null,
['override' => true, 'delete' => true]
);
$progress_bar->advance($php_files_count_per_projects - 1);
$progress_bar->finish();
}
}