|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * This file is part of fast-forward/dev-tools. |
| 7 | + * |
| 8 | + * This source file is subject to the license bundled |
| 9 | + * with this source code in the file LICENSE. |
| 10 | + * |
| 11 | + * @copyright Copyright (c) 2026 Felipe Sayão Lobato Abreu <github@mentordosnerds.com> |
| 12 | + * @license https://opensource.org/licenses/MIT MIT License |
| 13 | + * |
| 14 | + * @see https://github.com/php-fast-forward/dev-tools |
| 15 | + * @see https://github.com/php-fast-forward |
| 16 | + * @see https://datatracker.ietf.org/doc/html/rfc2119 |
| 17 | + */ |
| 18 | + |
| 19 | +namespace FastForward\DevTools\Command\Skills; |
| 20 | + |
| 21 | +use Symfony\Component\Filesystem\Filesystem; |
| 22 | +use Symfony\Component\Finder\Finder; |
| 23 | +use Symfony\Component\Filesystem\Path; |
| 24 | + |
| 25 | +/** |
| 26 | + * Synchronizes Fast Forward skills into consumer repositories. |
| 27 | + */ |
| 28 | +final class SkillsSynchronizer |
| 29 | +{ |
| 30 | + private readonly Filesystem $filesystem; |
| 31 | + |
| 32 | + public function __construct(?Filesystem $filesystem = null) |
| 33 | + { |
| 34 | + $this->filesystem = $filesystem ?? new Filesystem(); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Synchronizes skills from the package to the consumer repository. |
| 39 | + * |
| 40 | + * @param string $rootPath The consumer repository root path |
| 41 | + * @param string $skillsDir The target .agents/skills directory |
| 42 | + * @param string $packageSkillsPath The source skills directory in the package |
| 43 | + * @param callable(string): void $logger Callback for logging messages |
| 44 | + * |
| 45 | + * @return SynchronizeResult The result of the synchronization |
| 46 | + */ |
| 47 | + public function synchronize( |
| 48 | + string $rootPath, |
| 49 | + string $skillsDir, |
| 50 | + string $packageSkillsPath, |
| 51 | + callable $logger, |
| 52 | + ): SynchronizeResult { |
| 53 | + $result = new SynchronizeResult(); |
| 54 | + |
| 55 | + if (! $this->filesystem->exists($packageSkillsPath)) { |
| 56 | + $logger('<comment>No packaged skills found at: ' . $packageSkillsPath . '</comment>'); |
| 57 | + $result->markFailed(); |
| 58 | + |
| 59 | + return $result; |
| 60 | + } |
| 61 | + |
| 62 | + if (! $this->filesystem->exists($skillsDir)) { |
| 63 | + $this->filesystem->mkdir($skillsDir); |
| 64 | + $logger('<info>Created .agents/skills directory.</info>'); |
| 65 | + } |
| 66 | + |
| 67 | + $this->syncPackageSkills($rootPath, $skillsDir, $packageSkillsPath, $logger, $result); |
| 68 | + $this->cleanupBrokenLinks($skillsDir, $logger, $result); |
| 69 | + |
| 70 | + return $result; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Syncs skills from the package to the consumer repository. |
| 75 | + * |
| 76 | + * @param string $rootPath |
| 77 | + * @param string $skillsDir |
| 78 | + * @param string $packageSkillsPath |
| 79 | + * @param callable $logger |
| 80 | + * @param SynchronizeResult $result |
| 81 | + */ |
| 82 | + private function syncPackageSkills( |
| 83 | + string $rootPath, |
| 84 | + string $skillsDir, |
| 85 | + string $packageSkillsPath, |
| 86 | + callable $logger, |
| 87 | + SynchronizeResult $result, |
| 88 | + ): void { |
| 89 | + $finder = Finder::create() |
| 90 | + ->directories() |
| 91 | + ->in($packageSkillsPath) |
| 92 | + ->depth('== 0'); |
| 93 | + |
| 94 | + foreach ($finder as $skillDir) { |
| 95 | + $skillName = $skillDir->getFilename(); |
| 96 | + $targetLink = Path::makeAbsolute($skillName, $skillsDir); |
| 97 | + $sourcePath = $skillDir->getRealPath(); |
| 98 | + |
| 99 | + if ($this->filesystem->exists($targetLink)) { |
| 100 | + // Check if existing target is a valid symlink pointing to source |
| 101 | + if ($this->isSymlink($targetLink)) { |
| 102 | + $existingTarget = readlink($targetLink); |
| 103 | + |
| 104 | + if ($existingTarget === $sourcePath) { |
| 105 | + $logger('<comment>Preserved existing link: ' . $skillName . '</comment>'); |
| 106 | + $result->addPreservedLink($skillName); |
| 107 | + |
| 108 | + continue; |
| 109 | + } |
| 110 | + |
| 111 | + // Broken or wrong symlink - remove and recreate |
| 112 | + $this->filesystem->remove($targetLink); |
| 113 | + } else { |
| 114 | + // Non-symlink exists - check if it's the same content |
| 115 | + // For development mode in dev-tools repo, we might have actual directories |
| 116 | + // In that case, offer to convert to symlink |
| 117 | + $logger('<comment>Found existing directory: ' . $skillName . ' (converting to symlink)</comment>'); |
| 118 | + $this->filesystem->remove($targetLink); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + $this->filesystem->symlink($sourcePath, $targetLink); |
| 123 | + $logger('<info>Created link: ' . $skillName . ' -> ' . $sourcePath . '</info>'); |
| 124 | + $result->addCreatedLink($skillName); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Cleans up broken symlinks in the skills directory. |
| 130 | + * |
| 131 | + * @param string $skillsDir |
| 132 | + * @param callable $logger |
| 133 | + * @param SynchronizeResult $result |
| 134 | + */ |
| 135 | + private function cleanupBrokenLinks(string $skillsDir, callable $logger, SynchronizeResult $result): void |
| 136 | + { |
| 137 | + if (! $this->filesystem->exists($skillsDir)) { |
| 138 | + return; |
| 139 | + } |
| 140 | + |
| 141 | + $items = scandir($skillsDir); |
| 142 | + |
| 143 | + foreach ($items as $item) { |
| 144 | + if ('.' === $item || '..' === $item) { |
| 145 | + continue; |
| 146 | + } |
| 147 | + |
| 148 | + $itemPath = Path::makeAbsolute($item, $skillsDir); |
| 149 | + |
| 150 | + if (! is_link($itemPath)) { |
| 151 | + continue; |
| 152 | + } |
| 153 | + |
| 154 | + $target = readlink($itemPath); |
| 155 | + |
| 156 | + if (false === $target) { |
| 157 | + continue; |
| 158 | + } |
| 159 | + |
| 160 | + if (! file_exists($target)) { |
| 161 | + $this->filesystem->remove($itemPath); |
| 162 | + $logger('<info>Removed broken link: ' . $item . '</info>'); |
| 163 | + $result->addRemovedBrokenLink($item); |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * Checks if a path is a symbolic link. |
| 170 | + * |
| 171 | + * @param string $path |
| 172 | + */ |
| 173 | + private function isSymlink(string $path): bool |
| 174 | + { |
| 175 | + return is_link($path); |
| 176 | + } |
| 177 | +} |
0 commit comments