Skip to content
Snippets Groups Projects
Commit 3fcfe73f authored by Sebastien's avatar Sebastien
Browse files

Refactoring of code for MR

parent 333e861d
No related branches found
No related tags found
1 merge request!74tuleap-190 : add command to update projects with latest version of capsule-prototype
Pipeline #971 passed
......@@ -2,6 +2,9 @@
namespace App\Command;
use LogicException;
use phpDocumentor\Reflection\Types\Array_;
use phpDocumentor\Reflection\Types\Boolean;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\ProgressBar;
......@@ -20,15 +23,19 @@ use Symfony\Component\HttpKernel\KernelInterface;
)]
class UpdateAllProjects extends Command
{
private Filesystem $filesystem;
private Filesystem $file_system;
private KernelInterface $kernel;
private string $legacy_root;
private string $capsule_prototype_root;
/**
* @var string[] $black_list
* @var string[] $BLACK_LIST files and directories that should not be taken into account for projects update
*/
private array $black_list =
private static array $BLACK_LIST =
['css', 'create.zip', 'favicon.ico', 'la-page-des-malins.php', 'js', 'php', 'shared', '.', '..'];
private static string $CAPSULE_PROTOTYPE_DIRECTORY_NAME = 'capsule-prototype';
private static string $LEGACY_DIRECTORY_NAME = 'legacy';
public function __construct(Filesystem $filesystem, KernelInterface $kernel)
......@@ -38,8 +45,13 @@ class UpdateAllProjects extends Command
// 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->file_system = $filesystem;
$this->kernel = $kernel;
$this->legacy_root = Path::join($this->kernel->getProjectDir(), self::$LEGACY_DIRECTORY_NAME);
$this->capsule_prototype_root = Path::join(
$this->kernel->getProjectDir(),
self::$CAPSULE_PROTOTYPE_DIRECTORY_NAME
);
}
protected function configure(): void
......@@ -56,26 +68,21 @@ class UpdateAllProjects extends Command
protected function execute(InputInterface $input, OutputInterface $output): int
{
if (!$output instanceof ConsoleOutputInterface) {
throw new \LogicException('This command accepts only an instance of "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';
$output->writeln('Updating all projects started');
$directory_files = scandir($legacy_root);
if (false === $directory_files) {
$all_file_in_directory = $this->getProjectsDirectories();
if (false === $all_file_in_directory) {
return Command::FAILURE;
}
$all_file_in_directory = $this->getProjectsDirectories($directory_files);
$php_files_count_per_projects = $this->getPhpFilesCountPerProjects($capsule_prototype_root);
$php_files_count_per_projects = $this->getPhpFilesCountPerProjects();
$all_projects_progress_bar = new ProgressBar($output->section());
$all_projects_progress_bar->setFormat(
......@@ -85,9 +92,9 @@ class UpdateAllProjects extends Command
$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);
$current_legacy_path = Path::join($this->legacy_root, $project_directory);
if (!$this->filesystem->exists($current_legacy_path)) {
if (!$this->file_system->exists($current_legacy_path)) {
continue;
}
......@@ -95,7 +102,7 @@ class UpdateAllProjects extends Command
continue;
}
if (in_array($project_directory, $this->black_list)) {
if (in_array($project_directory, self::$BLACK_LIST)) {
continue;
}
......@@ -103,7 +110,7 @@ class UpdateAllProjects extends Command
$output,
$php_files_count_per_projects,
$current_legacy_path,
$capsule_prototype_root
$this->capsule_prototype_root
);
$all_projects_progress_bar->advance($php_files_count_per_projects);
}
......@@ -114,21 +121,24 @@ class UpdateAllProjects extends Command
}
/**
* @param string[] $directory_files
* @return string[] projects directories
* @return string[]|false
*/
private function getProjectsDirectories(array $directory_files): array
private function getProjectsDirectories(): array|false
{
return array_diff($directory_files, $this->black_list);
$all_directories_and_files = scandir($this->legacy_root);
if (false === $all_directories_and_files) {
return false;
}
return array_diff($all_directories_and_files, self::$BLACK_LIST);
}
/**
* @param string $capsule_prototype_root
* @return int
*/
protected function getPhpFilesCountPerProjects(string $capsule_prototype_root): int
protected function getPhpFilesCountPerProjects(): int
{
$php_files = glob($capsule_prototype_root . '/php/*.php');
$php_files = glob($this->capsule_prototype_root . '/php/*.php');
// add +1 for index.html file
return count($php_files ?: []) + 1;
}
......@@ -153,7 +163,7 @@ class UpdateAllProjects extends Command
$progress_bar->start();
// override index.html
$this->filesystem->copy(
$this->file_system->copy(
Path::normalize($capsule_prototype_root . '/index.html'),
Path::normalize($current_legacy_path . '/index.html'),
true
......@@ -161,9 +171,9 @@ class UpdateAllProjects extends Command
$progress_bar->advance();
// override php files
$this->filesystem->mirror(
Path::normalize($capsule_prototype_root . '/php'),
Path::normalize($current_legacy_path . '/php'),
$this->file_system->mirror(
Path::join($capsule_prototype_root, 'php'),
Path::join($current_legacy_path, 'php'),
null,
['override' => true, 'delete' => true]
);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment