diff --git a/public/kicad/.gitignore b/public/kicad/.gitignore new file mode 100644 index 00000000..1f2ab53d --- /dev/null +++ b/public/kicad/.gitignore @@ -0,0 +1,3 @@ +# They are user generated and should not be tracked by git +footprints_custom.txt +symbols_custom.txt diff --git a/public/kicad/footprints_custom.txt b/public/kicad/footprints_custom.txt deleted file mode 100644 index c3a29c90..00000000 --- a/public/kicad/footprints_custom.txt +++ /dev/null @@ -1 +0,0 @@ -# Custom KiCad autocomplete entries. One entry per line. diff --git a/public/kicad/symbols_custom.txt b/public/kicad/symbols_custom.txt deleted file mode 100644 index c3a29c90..00000000 --- a/public/kicad/symbols_custom.txt +++ /dev/null @@ -1 +0,0 @@ -# Custom KiCad autocomplete entries. One entry per line. diff --git a/src/Services/EDA/KicadListFileManager.php b/src/Services/EDA/KicadListFileManager.php index a293aad5..f2b1f0d5 100644 --- a/src/Services/EDA/KicadListFileManager.php +++ b/src/Services/EDA/KicadListFileManager.php @@ -24,14 +24,20 @@ namespace App\Services\EDA; use RuntimeException; use Symfony\Component\DependencyInjection\Attribute\Autowire; +use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface; -final class KicadListFileManager +final class KicadListFileManager implements CacheWarmerInterface { private const FOOTPRINTS_PATH = '/public/kicad/footprints.txt'; private const SYMBOLS_PATH = '/public/kicad/symbols.txt'; private const CUSTOM_FOOTPRINTS_PATH = '/public/kicad/footprints_custom.txt'; private const CUSTOM_SYMBOLS_PATH = '/public/kicad/symbols_custom.txt'; + private const CUSTOM_TEMPLATE = <<<'EOT' + # Custom KiCad autocomplete entries. One entry per line. + + EOT; + public function __construct( #[Autowire('%kernel.project_dir%')] private readonly string $projectDir, @@ -45,6 +51,8 @@ final class KicadListFileManager public function getCustomFootprintsContent(): string { + //Ensure that the custom file exists, so that the UI can always display it without error. + $this->createCustomFileIfNotExists(self::CUSTOM_FOOTPRINTS_PATH); return $this->readFile(self::CUSTOM_FOOTPRINTS_PATH); } @@ -55,6 +63,8 @@ final class KicadListFileManager public function getCustomSymbolsContent(): string { + //Ensure that the custom file exists, so that the UI can always display it without error. + $this->createCustomFileIfNotExists(self::CUSTOM_SYMBOLS_PATH); return $this->readFile(self::CUSTOM_SYMBOLS_PATH); } @@ -105,4 +115,37 @@ final class KicadListFileManager return $normalized; } + + private function createCustomFileIfNotExists(string $path): void + { + $fullPath = $this->projectDir . $path; + + if (!is_file($fullPath)) { + if (file_put_contents($fullPath, self::CUSTOM_TEMPLATE, LOCK_EX) === false) { + throw new RuntimeException(sprintf('Failed to create custom footprints file "%s".', $fullPath)); + } + } + } + + /** + * Ensures that the custom footprints and symbols files exist, so that the UI can always display them without error. + * @return void + */ + public function createCustomFilesIfNotExist(): void + { + $this->createCustomFileIfNotExists(self::CUSTOM_FOOTPRINTS_PATH); + $this->createCustomFileIfNotExists(self::CUSTOM_SYMBOLS_PATH); + } + + + public function isOptional(): bool + { + return false; + } + + public function warmUp(string $cacheDir, ?string $buildDir = null): array + { + $this->createCustomFilesIfNotExist(); + return []; + } }