-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAgentsCommand.php
More file actions
130 lines (114 loc) · 4.28 KB
/
AgentsCommand.php
File metadata and controls
130 lines (114 loc) · 4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
declare(strict_types=1);
/**
* Fast Forward Development Tools for PHP projects.
*
* This file is part of fast-forward/dev-tools project.
*
* @author Felipe Sayão Lobato Abreu <github@mentordosnerds.com>
* @license https://opensource.org/licenses/MIT MIT License
*
* @see https://github.com/php-fast-forward/
* @see https://github.com/php-fast-forward/dev-tools
* @see https://github.com/php-fast-forward/dev-tools/issues
* @see https://php-fast-forward.github.io/dev-tools/
* @see https://datatracker.ietf.org/doc/html/rfc2119
*/
namespace FastForward\DevTools\Console\Command;
use FastForward\DevTools\Console\Command\Traits\LogsCommandResults;
use Composer\Command\BaseCommand;
use FastForward\DevTools\Console\Input\HasJsonOption;
use FastForward\DevTools\Filesystem\FilesystemInterface;
use FastForward\DevTools\Path\DevToolsPathResolver;
use FastForward\DevTools\Sync\PackagedDirectorySynchronizer;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Synchronizes packaged Fast Forward project agents into the consumer repository.
*/
#[AsCommand(
name: 'agents',
description: 'Synchronizes Fast Forward project agents into .agents/agents directory.'
)]
final class AgentsCommand extends BaseCommand implements LoggerAwareCommandInterface
{
use HasJsonOption;
use LogsCommandResults;
private const string AGENTS_DIRECTORY = '.agents/agents';
/**
* @param PackagedDirectorySynchronizer $synchronizer
* @param FilesystemInterface $filesystem
* @param LoggerInterface $logger
*/
public function __construct(
private readonly PackagedDirectorySynchronizer $synchronizer,
private readonly FilesystemInterface $filesystem,
private readonly LoggerInterface $logger,
) {
parent::__construct();
}
/**
* Configures JSON output options for the synchronization command.
*/
protected function configure(): void
{
$this->setHelp(
'This command ensures the consumer repository contains linked Fast Forward project agents by creating'
. ' symlinks to the packaged prompts and removing broken links.'
);
$this->addJsonOption();
}
/**
* @param InputInterface $input
* @param OutputInterface $output
*
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$packageAgentsPath = DevToolsPathResolver::getPackagePath(self::AGENTS_DIRECTORY);
$agentsDir = $this->filesystem->getAbsolutePath(self::AGENTS_DIRECTORY);
$this->logger->info('Starting agents synchronization...');
if (! $this->filesystem->exists($packageAgentsPath)) {
return $this->failure(
'No packaged .agents/agents found at: {packaged_agents_path}',
$input,
[
'packaged_agents_path' => $packageAgentsPath,
'agents_dir' => $agentsDir,
'directory_created' => false,
],
);
}
$directoryCreated = false;
if (! $this->filesystem->exists($agentsDir)) {
$this->filesystem->mkdir($agentsDir);
$directoryCreated = true;
$this->logger->info('Created .agents/agents directory.');
}
$this->synchronizer->setLogger($this->getIO());
$result = $this->synchronizer->synchronize($agentsDir, $packageAgentsPath, self::AGENTS_DIRECTORY);
if ($result->failed()) {
return $this->failure(
'Agents synchronization failed.',
$input,
[
'packaged_agents_path' => $packageAgentsPath,
'agents_dir' => $agentsDir,
'directory_created' => $directoryCreated,
],
);
}
return $this->success(
'Agents synchronization completed successfully.',
$input,
[
'packaged_agents_path' => $packageAgentsPath,
'agents_dir' => $agentsDir,
'directory_created' => $directoryCreated,
],
);
}
}