-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSyncCommand.php
More file actions
256 lines (219 loc) · 8.43 KB
/
SyncCommand.php
File metadata and controls
256 lines (219 loc) · 8.43 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<?php
declare(strict_types=1);
/**
* This file is part of fast-forward/dev-tools.
*
* This source file is subject to the license bundled
* with this source code in the file LICENSE.
*
* @copyright Copyright (c) 2026 Felipe Sayão Lobato Abreu <github@mentordosnerds.com>
* @license https://opensource.org/licenses/MIT MIT License
*
* @see https://github.com/php-fast-forward/dev-tools
* @see https://github.com/php-fast-forward
* @see https://datatracker.ietf.org/doc/html/rfc2119
*/
namespace FastForward\DevTools\Console\Command;
use Composer\Factory;
use Composer\Json\JsonManipulator;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Filesystem\Path;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Process\Process;
use function Safe\file_get_contents;
/**
* Represents the command responsible for installing development scripts into `composer.json`.
*/
#[AsCommand(
name: 'dev-tools:sync',
description: 'Installs and synchronizes dev-tools scripts, GitHub Actions workflows, .editorconfig, and .gitattributes in the root project.',
help: 'This command adds or updates dev-tools scripts in composer.json, copies reusable GitHub Actions workflows, ensures .editorconfig is present and up to date, '
. 'and manages .gitattributes export-ignore rules.'
)]
final class SyncCommand extends AbstractCommand
{
/**
* Executes the script installation block.
*
* The method MUST leverage the `ScriptsInstallerTrait` to update the configuration.
* It SHALL return `self::SUCCESS` upon completion.
*
* @param InputInterface $input the input interface
* @param OutputInterface $output the output interface
*
* @return int the status code of the command
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$output->writeln('<info>Starting script installation...</info>');
$this->updateComposerJson();
$this->createGitHubActionWorkflows();
$this->copyEditorConfig();
$this->copyDependabotConfig();
$this->addRepositoryWikiGitSubmodule();
$this->runCommand('gitignore', $output);
$this->runCommand('gitattributes', $output);
$this->runCommand('skills', $output);
$this->runCommand('license', $output);
$this->copyGitHooks($output);
return self::SUCCESS;
}
/**
* Updates the root composer.json file with required scripts and extra configuration.
*
* This method adds or updates the dev-tools scripts and extra configuration for tools like grumphp.
* It does nothing if the composer.json file does not exist.
*
* @return void
*/
private function updateComposerJson(): void
{
$file = Factory::getComposerFile();
if (! $this->filesystem->exists($file)) {
return;
}
$contents = file_get_contents($file);
$manipulator = new JsonManipulator($contents);
$scripts = [
'dev-tools' => 'dev-tools',
'dev-tools:fix' => '@dev-tools --fix',
];
$extra = [
'grumphp' => [
'config-default-path' => Path::makeRelative(
\dirname(__DIR__, 3) . '/grumphp.yml',
$this->getCurrentWorkingDirectory(),
),
],
];
foreach ($scripts as $name => $command) {
$manipulator->addSubNode('scripts', $name, $command);
}
foreach ($extra as $name => $config) {
$manipulator->addSubNode('extra', $name, $config, true);
}
$this->filesystem->dumpFile($file, $manipulator->getContents());
}
/**
* Creates GitHub Actions workflow templates in the consumer repository.
*
* This method copies all .yml workflow templates from resources/github-actions to .github/workflows,
* unless the target file already exists. It is intended to provide reusable workflow_call templates for consumers.
*
* @return void
*/
private function createGitHubActionWorkflows(): void
{
$finder = Finder::create()
->files()
->in(parent::getDevToolsFile('resources/github-actions'))
->name('*.yml');
foreach ($finder as $file) {
$targetPath = Path::join('.github', 'workflows', $file->getFilename());
if ($this->filesystem->exists($targetPath)) {
continue;
}
$content = file_get_contents($file->getRealPath());
$this->filesystem->dumpFile($targetPath, $content);
}
}
/**
* Installs or updates the .editorconfig file in the root project directory.
*
* This method copies the .editorconfig from the package resources to the project root,
* always overwriting to ensure it is up to date.
*
* @return void
*/
private function copyEditorConfig(): void
{
$source = parent::getDevToolsFile('.editorconfig');
$target = parent::getConfigFile('.editorconfig', true);
if ($this->filesystem->exists($target)) {
return;
}
$content = file_get_contents($source);
$this->filesystem->dumpFile($target, $content);
}
/**
* Installs the dependabot.yml configuration file in the .github directory if it does not exist.
*
* This method copies the dependabot.yml from the package resources to .github/dependabot.yml if it is not already present.
*
* @return void
*/
private function copyDependabotConfig(): void
{
$source = parent::getDevToolsFile('resources/dependabot.yml');
$target = parent::getConfigFile('.github/dependabot.yml', true);
if ($this->filesystem->exists($target)) {
return;
}
$content = file_get_contents($source);
$this->filesystem->dumpFile($target, $content);
}
/**
* Ensures the repository wiki is added as a git submodule in .github/wiki.
*
* This method checks if the .github/wiki directory exists. If not, it adds the repository's wiki as a submodule
* using the remote origin URL, replacing .git with .wiki.git. This allows automated documentation and wiki updates.
*
* @return void
*/
private function addRepositoryWikiGitSubmodule(): void
{
$wikiSubmodulePath = parent::getConfigFile('.github/wiki', true);
if ($this->filesystem->exists($wikiSubmodulePath)) {
return;
}
$repositoryUrl = $this->getGitRepositoryUrl();
$wikiRepoUrl = str_replace('.git', '.wiki.git', $repositoryUrl);
$process = new Process([
'git',
'submodule',
'add',
$wikiRepoUrl,
Path::makeRelative($wikiSubmodulePath, $this->getCurrentWorkingDirectory()),
]);
$process->mustRun();
}
/**
* Retrieves the git remote origin URL for the current repository.
*
* This method runs 'git config --get remote.origin.url' and returns the trimmed output.
*
* @return string The remote origin URL of the repository
*/
private function getGitRepositoryUrl(): string
{
$process = new Process(['git', 'config', '--get', 'remote.origin.url']);
$process->mustRun();
return trim($process->getOutput());
}
/**
* Copies git hooks from resources to .git/hooks for vendor synchronization.
*
* This method copies post-checkout and post-merge hooks from the package resources to .git/hooks,
* ensuring vendor dependencies stay synchronized when switching branches.
*
* @param OutputInterface $output the output interface
*
* @return void
*/
private function copyGitHooks(OutputInterface $output): void
{
$hooksDir = parent::getDevToolsFile('resources/git-hooks');
$targetDir = Path::join($this->getCurrentWorkingDirectory(), '.git', 'hooks');
$finder = Finder::create()
->files()
->in($hooksDir);
foreach ($finder as $file) {
$targetPath = Path::join($targetDir, $file->getFilename());
$this->filesystem->copy($file->getRealPath(), $targetPath, true);
$this->filesystem->chmod($targetPath, 755, 0o755);
$output->writeln(\sprintf('<info>Installed %s hook</info>', $file->getFilename()));
}
}
}