2222use Composer \Command \BaseCommand ;
2323use FastForward \DevTools \Filesystem \FinderFactoryInterface ;
2424use FastForward \DevTools \Filesystem \FilesystemInterface ;
25- use FastForward \DevTools \Resource \OverwriteDiffRenderer ;
25+ use FastForward \DevTools \Resource \FileDiffer ;
2626use Symfony \Component \Config \FileLocatorInterface ;
2727use Symfony \Component \Console \Attribute \AsCommand ;
2828use Symfony \Component \Console \Input \InputInterface ;
@@ -47,13 +47,13 @@ final class CopyResourceCommand extends BaseCommand
4747 * @param FilesystemInterface $filesystem the filesystem used for copy operations
4848 * @param FileLocatorInterface $fileLocator the locator used to resolve source resources
4949 * @param FinderFactoryInterface $finderFactory the factory used to create finders for directory resources
50- * @param OverwriteDiffRenderer $overwriteDiffRenderer the renderer used to summarize overwrite changes
50+ * @param FileDiffer $fileDiffer the service used to summarize overwrite changes
5151 */
5252 public function __construct (
5353 private readonly FilesystemInterface $ filesystem ,
5454 private readonly FileLocatorInterface $ fileLocator ,
5555 private readonly FinderFactoryInterface $ finderFactory ,
56- private readonly OverwriteDiffRenderer $ overwriteDiffRenderer ,
56+ private readonly FileDiffer $ fileDiffer ,
5757 ) {
5858 parent ::__construct ();
5959 }
@@ -126,7 +126,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int
126126 $ targetPath = (string ) $ this ->filesystem ->getAbsolutePath ($ target );
127127
128128 if (is_dir ($ sourcePath )) {
129- return $ this ->copyDirectory ($ sourcePath , $ targetPath , $ overwrite , $ dryRun , $ check , $ interactive , $ input , $ output );
129+ return $ this ->copyDirectory (
130+ $ sourcePath ,
131+ $ targetPath ,
132+ $ overwrite ,
133+ $ dryRun ,
134+ $ check ,
135+ $ interactive ,
136+ $ input ,
137+ $ output
138+ );
130139 }
131140
132141 return $ this ->copyFile ($ sourcePath , $ targetPath , $ overwrite , $ dryRun , $ check , $ interactive , $ input , $ output );
@@ -139,6 +148,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
139148 * @param string $targetPath the resolved target directory
140149 * @param bool $overwrite whether existing files MAY be overwritten
141150 * @param OutputInterface $output the output used to report copy results
151+ * @param bool $dryRun
152+ * @param bool $check
153+ * @param bool $interactive
154+ * @param InputInterface $input
142155 *
143156 * @return int the command status code
144157 */
@@ -163,7 +176,16 @@ private function copyDirectory(
163176 $ destination = Path::join ($ targetPath , $ file ->getRelativePathname ());
164177 $ status = max (
165178 $ status ,
166- $ this ->copyFile ($ file ->getRealPath (), $ destination , $ overwrite , $ dryRun , $ check , $ interactive , $ input , $ output ),
179+ $ this ->copyFile (
180+ $ file ->getRealPath (),
181+ $ destination ,
182+ $ overwrite ,
183+ $ dryRun ,
184+ $ check ,
185+ $ interactive ,
186+ $ input ,
187+ $ output
188+ ),
167189 );
168190 }
169191
@@ -177,6 +199,10 @@ private function copyDirectory(
177199 * @param string $targetPath the resolved target file
178200 * @param bool $overwrite whether an existing target file MAY be overwritten
179201 * @param OutputInterface $output the output used to report copy results
202+ * @param bool $dryRun
203+ * @param bool $check
204+ * @param bool $interactive
205+ * @param InputInterface $input
180206 *
181207 * @return int the command status code
182208 */
@@ -189,21 +215,24 @@ private function copyFile(
189215 bool $ interactive ,
190216 InputInterface $ input ,
191217 OutputInterface $ output ,
192- ): int
193- {
218+ ): int {
194219 if (! $ overwrite && ! $ dryRun && ! $ check && ! $ interactive && $ this ->filesystem ->exists ($ targetPath )) {
195220 $ output ->writeln (\sprintf ('<comment>Skipped existing resource %s.</comment> ' , $ targetPath ));
196221
197222 return self ::SUCCESS ;
198223 }
199224
200225 if (($ overwrite || $ dryRun || $ check || $ interactive ) && $ this ->filesystem ->exists ($ targetPath )) {
201- $ comparison = $ this ->overwriteDiffRenderer ->render ($ sourcePath , $ targetPath );
226+ $ comparison = $ this ->fileDiffer ->diff ($ sourcePath , $ targetPath );
227+
228+ $ output ->writeln (\sprintf ('<comment>%s</comment> ' , $ comparison ->getSummary ()));
202229
203- $ output ->writeln (\sprintf ('<comment>%s</comment> ' , $ comparison ->summary ()));
230+ if ($ comparison ->isChanged ()) {
231+ $ consoleDiff = $ this ->fileDiffer ->formatForConsole ($ comparison ->getDiff (), $ output ->isDecorated ());
204232
205- if ($ comparison ->isChanged () && null !== $ comparison ->diff ()) {
206- $ output ->writeln ($ comparison ->diff ());
233+ if (null !== $ consoleDiff ) {
234+ $ output ->writeln ($ consoleDiff );
235+ }
207236 }
208237
209238 if ($ comparison ->isUnchanged ()) {
@@ -218,7 +247,11 @@ private function copyFile(
218247 return self ::SUCCESS ;
219248 }
220249
221- if ($ interactive && $ input ->isInteractive () && ! $ this ->shouldReplaceResource ($ input , $ output , $ targetPath )) {
250+ if ($ interactive && $ input ->isInteractive () && ! $ this ->shouldReplaceResource (
251+ $ input ,
252+ $ output ,
253+ $ targetPath
254+ )) {
222255 $ output ->writeln (\sprintf ('<comment>Skipped replacing %s.</comment> ' , $ targetPath ));
223256
224257 return self ::SUCCESS ;
@@ -244,6 +277,7 @@ private function shouldReplaceResource(InputInterface $input, OutputInterface $o
244277 {
245278 $ question = new ConfirmationQuestion (\sprintf ('Replace drifted resource %s? [y/N] ' , $ targetPath ), false );
246279
247- return (bool ) $ this ->getHelper ('question ' )->ask ($ input , $ output , $ question );
280+ return (bool ) $ this ->getHelper ('question ' )
281+ ->ask ($ input , $ output , $ question );
248282 }
249283}
0 commit comments