Skip to content
Snippets Groups Projects
Commit 333e861d authored by Sebastien's avatar Sebastien
Browse files

Refactoring of UpadateAllProjects command

parent 561a167f
No related branches found
No related tags found
1 merge request!74tuleap-190 : add command to update projects with latest version of capsule-prototype
......@@ -20,15 +20,16 @@ use Symfony\Component\HttpKernel\KernelInterface;
)]
class UpdateAllProjects extends Command
{
/**
* @var Filesystem $filesystem
*/
private $filesystem;
private Filesystem $filesystem;
private KernelInterface $kernel;
/**
* @var KernelInterface $kernel
* @var string[] $black_list
*/
private $kernel;
private array $black_list =
['css', 'create.zip', 'favicon.ico', 'la-page-des-malins.php', 'js', 'php', 'shared', '.', '..'];
public function __construct(Filesystem $filesystem, KernelInterface $kernel)
{
......@@ -58,11 +59,6 @@ class UpdateAllProjects extends Command
throw new \LogicException('This command accepts only an instance of "ConsoleOutputInterface".');
}
if (!$this->filesystem instanceof Filesystem) {
$output->writeln("filesystem is not defined");
return Command::INVALID;
}
ProgressBar::setFormatDefinition(
'project_update_message',
' %current%/%max% [%bar%] %percent%% : Updating project :%project% %elapsed%'
......@@ -71,18 +67,15 @@ class UpdateAllProjects extends Command
$legacy_root = $this->kernel->getProjectDir() . '/legacy';
$capsule_prototype_root = './capsule-prototype';
$black_list = ['css', 'create.zip', 'favicon.ico', 'la-page-des-malins.php', 'js', 'php', 'shared', '.', '..'];
$directory_files = scandir($legacy_root);
if (false === $directory_files) {
return Command::FAILURE;
}
$all_file_in_directory = array_diff($directory_files, $black_list);
$all_file_in_directory = $this->getProjectsDirectories($directory_files);
$php_files = glob($capsule_prototype_root . '/php/*.php');
// add +1 for index.html file
$php_files_count_per_projects = count($php_files ?: []) + 1;
$php_files_count_per_projects = $this->getPhpFilesCountPerProjects($capsule_prototype_root);
$all_projects_progress_bar = new ProgressBar($output->section());
$all_projects_progress_bar->setFormat(
......@@ -91,8 +84,8 @@ class UpdateAllProjects extends Command
$all_projects_progress_bar->SetMessage('Projects update progress');
$all_projects_progress_bar->start();
foreach ($all_projects_progress_bar->iterate($all_file_in_directory) as $value) {
$current_legacy_path = Path::normalize($legacy_root . '/' . $value);
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;
......@@ -102,10 +95,57 @@ class UpdateAllProjects extends Command
continue;
}
if (in_array($value, $black_list)) {
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');
......@@ -129,11 +169,5 @@ class UpdateAllProjects extends Command
);
$progress_bar->advance($php_files_count_per_projects - 1);
$progress_bar->finish();
$all_projects_progress_bar->advance($php_files_count_per_projects);
}
$all_projects_progress_bar->finish();
return Command::SUCCESS;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment