1212namespace App \Command ;
1313
1414use App \Analysis \SnippetAnalysis ;
15- use App \Model \DependencyFileFormat ;
16- use App \Model \FileGroup ;
15+ use App \Service \FileGroupFinder ;
1716use App \Utility \Utility ;
1817use Debricked \Shared \API \API ;
1918use Symfony \Component \Console \Command \Command ;
2322use Symfony \Component \Console \Input \InputOption ;
2423use Symfony \Component \Console \Output \OutputInterface ;
2524use Symfony \Component \Console \Style \SymfonyStyle ;
26- use Symfony \Component \Finder \Finder ;
25+ use Symfony \Component \Finder \Exception \ DirectoryNotFoundException ;
2726use Symfony \Component \Finder \SplFileInfo ;
2827use Symfony \Component \HttpFoundation \Request ;
2928use Symfony \Component \Mime \Part \DataPart ;
3029use Symfony \Component \Mime \Part \Multipart \FormDataPart ;
3130use Symfony \Contracts \HttpClient \Exception \ClientExceptionInterface ;
31+ use Symfony \Contracts \HttpClient \Exception \HttpExceptionInterface ;
3232use Symfony \Contracts \HttpClient \Exception \RedirectionExceptionInterface ;
3333use Symfony \Contracts \HttpClient \Exception \ServerExceptionInterface ;
3434use Symfony \Contracts \HttpClient \Exception \TransportExceptionInterface ;
@@ -40,7 +40,7 @@ class FindAndUploadFilesCommand extends Command
4040
4141 protected static $ defaultName = 'debricked:find-and-upload-files ' ;
4242
43- private const ARGUMENT_BASE_DIRECTORY = 'base-directory ' ;
43+ public const ARGUMENT_BASE_DIRECTORY = 'base-directory ' ;
4444 public const ARGUMENT_USERNAME = 'username ' ;
4545 public const ARGUMENT_PASSWORD = 'password ' ;
4646 private const ARGUMENT_REPOSITORY_NAME = 'repository-name ' ;
@@ -49,10 +49,10 @@ class FindAndUploadFilesCommand extends Command
4949 private const ARGUMENT_INTEGRATION_NAME = 'integration-name ' ;
5050 private const OPTION_BRANCH_NAME = 'branch-name ' ;
5151 private const OPTION_DEFAULT_BRANCH = 'default-branch ' ;
52- private const OPTION_DIRECTORIES_TO_EXCLUDE = 'excluded-directories ' ;
52+ public const OPTION_DIRECTORIES_TO_EXCLUDE = 'excluded-directories ' ;
5353 private const OPTION_SNIPPET_ANALYSIS = 'snippet-analysis ' ;
5454 private const OPTION_KEEP_ZIP = 'keep-zip ' ;
55- private const OPTION_RECURSIVE_FILE_SEARCH = 'recursive-file-search ' ;
55+ public const OPTION_RECURSIVE_FILE_SEARCH = 'recursive-file-search ' ;
5656 private const OPTION_UPLOAD_ALL_FILES = 'upload-all-files ' ;
5757 private const OPTION_AUTHOR = 'author ' ;
5858
@@ -187,40 +187,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int
187187 'Failed to get current working directory, command might be searching through unexpected directories and files '
188188 );
189189 }
190-
191190 /** @var string $baseDirectory */
192191 $ baseDirectory = $ input ->getArgument (self ::ARGUMENT_BASE_DIRECTORY );
193-
194- $ io ->writeln ('Getting supported dependency file names from Debricked ' , OutputInterface::VERBOSITY_VERBOSE );
195-
196- try {
197- $ dependencyFileNamesResponse = $ api ->makeApiCall (
198- Request::METHOD_GET ,
199- '/api/1.0/open/files/supported-formats '
200- );
201- $ dependencyFileFormats = \json_decode ($ dependencyFileNamesResponse ->getContent (), true );
202- } catch (TransportExceptionInterface $ e ) {
203- $ io ->error ("Failed to get supported dependency file names: {$ e ->getMessage ()}" );
204-
205- return 1 ;
206- } catch (ClientExceptionInterface |RedirectionExceptionInterface |ServerExceptionInterface $ e ) {
207- /* @noinspection PhpUnhandledExceptionInspection */
208- $ io ->error ("Failed to get supported dependency file names: {$ e ->getResponse ()->getContent (false )}" );
209-
210- return 1 ;
211- }
212-
213- $ directoriesToExcludeString = \strval ($ input ->getOption (self ::OPTION_DIRECTORIES_TO_EXCLUDE ));
214192 $ searchDirectory = "{$ workingDirectory }/ {$ baseDirectory }" ;
215193 $ searchDirectory = preg_replace ('#/+# ' , '/ ' , $ searchDirectory ); // remove duplicate slashes.
216194
217- $ finder = new Finder ();
218- $ finder ->ignoreDotFiles (false );
219- $ finder ->files ()->in ($ searchDirectory );
220- if (empty ($ directoriesToExcludeString ) === false && \is_array (
221- $ directoriesToExcludeArray = \explode (', ' , $ directoriesToExcludeString )
222- )) {
223- $ finder ->exclude ($ directoriesToExcludeArray );
195+ $ directoriesToExcludeString = \strval ($ input ->getOption (self ::OPTION_DIRECTORIES_TO_EXCLUDE ));
196+ $ directoriesToExcludeArray = [];
197+ if (empty ($ directoriesToExcludeString ) === false ) {
198+ $ directoriesToExcludeArray = \explode (', ' , $ directoriesToExcludeString ) ?? [];
224199 } else {
225200 $ io ->note ('No directories will be ignored ' );
226201 }
@@ -231,7 +206,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
231206
232207 return 1 ;
233208 } elseif ($ recursiveFileSearch === false ) {
234- $ finder ->depth (0 );
235209 $ io ->note ('Recursive search is disabled, only base directory will be searched ' );
236210 }
237211
@@ -246,46 +220,27 @@ protected function execute(InputInterface $input, OutputInterface $output): int
246220
247221 $ uploadId = null ;
248222
249- $ dependencyFileFormats = DependencyFileFormat::make ($ dependencyFileFormats );
250- $ numberOfMatchedFiles = 0 ;
251- // Find lock files
252- $ lockFileRegexes = \array_merge (...\array_map (fn ($ format ) => $ format ->getLockFileRegexes (), $ dependencyFileFormats ));
253- $ lockFiles = [];
254- foreach ($ finder as $ file ) {
255- if (Utility::pregMatchInArray ($ file ->getFilename (), $ lockFileRegexes )) {
256- $ lockFiles [$ file ->getPathname ()] = $ file ;
257- ++$ numberOfMatchedFiles ;
258- }
259- }
223+ try {
224+ $ io ->writeln ('Getting supported dependency file names from Debricked ' , OutputInterface::VERBOSITY_VERBOSE );
225+ $ fileGroups = FileGroupFinder::find ($ api , $ searchDirectory , $ recursiveFileSearch , $ directoriesToExcludeArray );
226+ } catch (TransportExceptionInterface $ e ) {
227+ $ io ->error ("Failed to get supported dependency file names: {$ e ->getMessage ()}" );
260228
261- /** Find dependency files and create FileGroups(@see FileGroup) */
262- $ fileGroups = [];
263- foreach ($ finder as $ file ) {
264- if (($ dependencyFileFormat = DependencyFileFormat::findFormatByFileName ($ dependencyFileFormats , $ file ->getFilename ())) !== null ) {
265- $ fileGroup = new FileGroup ($ file , $ dependencyFileFormat );
266- $ lockFileRegexes = $ dependencyFileFormat ->getLockFileRegexes ();
267- // Find matching lock file
268- foreach ($ lockFileRegexes as $ lockFileRegex ) {
269- foreach ($ lockFiles as $ key => $ lockFile ) {
270- $ quotedLockfilePath = \preg_quote ($ file ->getPath (), '/ ' );
271- if (\preg_match ("/ $ quotedLockfilePath\/ $ lockFileRegex/ " , $ key ) === 1 ) {
272- $ fileGroup ->addLockFile ($ lockFile );
273- unset($ lockFiles [$ key ]);
274- break ;
275- }
276- }
277- }
278- $ fileGroups [] = $ fileGroup ;
279- ++$ numberOfMatchedFiles ;
280- }
229+ return 1 ;
230+ } catch (HttpExceptionInterface $ e ) {
231+ /* @noinspection PhpUnhandledExceptionInspection */
232+ $ io ->error ("Failed to get supported dependency file names: {$ e ->getResponse ()->getContent (false )}" );
233+
234+ return 1 ;
235+ } catch (DirectoryNotFoundException $ e ) {
236+ $ io ->error ("Failed to find directory: {$ e ->getMessage ()}" );
237+
238+ return 1 ;
281239 }
282240
283- // Create FileGroups from leftover lock files.
284- foreach ($ lockFiles as $ key => $ lockFile ) {
285- $ lockFileGroup = new FileGroup (null , null );
286- $ lockFileGroup ->addLockFile ($ lockFile );
287- $ fileGroups [] = $ lockFileGroup ;
288- unset($ lockFiles [$ key ]);
241+ $ numberOfMatchedFiles = 0 ;
242+ foreach ($ fileGroups as $ fileGroup ) {
243+ $ numberOfMatchedFiles += count ($ fileGroup ->getFiles ());
289244 }
290245
291246 if ($ numberOfMatchedFiles > 0 ) {
@@ -341,6 +296,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
341296 }
342297 $ uploadAllFiles = $ uploadAllFiles && $ numberOfMatchedFiles > 0 ;
343298 if ($ enableSnippetAnalysis === true || $ uploadAllFiles === true ) {
299+ $ finder = FileGroupFinder::makeFinder ($ searchDirectory , $ recursiveFileSearch , $ directoriesToExcludeArray );
344300 $ zip = null ;
345301 $ progressBar = null ;
346302 if ($ uploadAllFiles === true ) {
0 commit comments