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 Request]: Automatic deletion of no longer used models #27

Closed
GAL-GC opened this issue May 4, 2023 · 2 comments
Closed

[✨Feature Request]: Automatic deletion of no longer used models #27

GAL-GC opened this issue May 4, 2023 · 2 comments
Labels
enhancement New feature or request

Comments

@GAL-GC
Copy link

GAL-GC commented May 4, 2023

I have integrated your package in a new projects and it looks fine so far.

However, I've noticed, that if I remove the annotation within my model, to longer auto-generate the ts interface, it is not automatically removed from the generated output directory.

Would be a great feature, if once generated and the annotation removed at a later point, the corresponding autogenerated file will be replaced too!

@Brainshaker95 Brainshaker95 added the enhancement New feature or request label May 4, 2023
@Brainshaker95
Copy link
Owner

Brainshaker95 commented May 4, 2023

I agree that this would be a nice feature to have but there are a few things to consider:

  • What if the input_dir parameter is changed but there are still TypeScriptable classes in another directory? Should the corresponding TS files still be deleted?
  • What if the output_dir parameter is changed? Were to look for files to delete in that case?
  • What if the file_type or file_name_strategy paramteter is changed? Simple matching by path wouldn't work anymore. Finding the correct files to delete would require parsing of the TS files which I don't really want to implement.

These are just the first few issues I see with that. I am open for any suggestions on how to deal with those.

An alternative solution for what you are asking for could be implemented on a per project basis using the PHP API if you are not intending to change those parameters:

<?php

declare(strict_types=1);

namespace App;

use Brainshaker95\PhpToTsBundle\Interface\Config;
use Brainshaker95\PhpToTsBundle\Model\TsInterface;
use Brainshaker95\PhpToTsBundle\Service\Configuration;
use Brainshaker95\PhpToTsBundle\Service\Dumper;
use Brainshaker95\PhpToTsBundle\Service\Filesystem;

final class Foo
{
    public function __construct(
        private readonly Configuration $config,
        private readonly Dumper $dumper,
        private readonly Filesystem $filesystem,
    ) {
    }

    public function bar(): void
    {
        $config    = $this->config->get();
        $fileNames = [];

        $this->dumper->dumpDir(
            $config, 
            static function (string $path, TsInterface $tsInterface): void {
                $fileNames[] = $tsInterface->getFileName();
            },
        );

        foreach ($this->filesystem->getSplFileInfoArray([$config->getOutputDir()]) as $file) {
            if (!in_array($file->getFilename(), $fileNames, true)) {
                $this->filesystem->remove($file->getRealPath());
            }
        }
    }
}

Note that Filesystem and Configuration are marked as internal though. This functionality can easily be replicated, I just used these classes in this example for brevity. Beware that no other files except the generated TS files should be placed into the output directory or they will be deleted by this implementation.

@Brainshaker95
Copy link
Owner

I am closing this now since no one added any input on this. If there is a need for further discussion in the future I am happy to re-open this issue again. For now I have added the lack of this feature to the known limitations in the readme.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants