|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.IO; |
| 7 | +using System.Linq; |
| 8 | + |
| 9 | +namespace Microsoft.Dnx.Tooling |
| 10 | +{ |
| 11 | + internal class RestoreProjectsCollector |
| 12 | + { |
| 13 | + public static bool Find(string restoreDirectory, out IEnumerable<string> projectJsonFiles) |
| 14 | + { |
| 15 | + if (string.Equals(Runtime.Project.ProjectFileName, |
| 16 | + Path.GetFileName(restoreDirectory), |
| 17 | + StringComparison.OrdinalIgnoreCase) && File.Exists(restoreDirectory)) |
| 18 | + { |
| 19 | + // If the path is a project.json file we don't do recursive search in subfolders |
| 20 | + projectJsonFiles = new List<string> { restoreDirectory }; |
| 21 | + |
| 22 | + return true; |
| 23 | + } |
| 24 | + |
| 25 | + if (!Directory.Exists(restoreDirectory)) |
| 26 | + { |
| 27 | + projectJsonFiles = Enumerable.Empty<string>(); |
| 28 | + return false; |
| 29 | + } |
| 30 | + |
| 31 | + var result = new List<string>(); |
| 32 | + CollectProjectFiles(restoreDirectory, result); |
| 33 | + |
| 34 | + projectJsonFiles = result; |
| 35 | + return true; |
| 36 | + } |
| 37 | + |
| 38 | + private static void CollectProjectFiles(string directory, List<string> results) |
| 39 | + { |
| 40 | + var candidate = Path.Combine(directory, Runtime.Project.ProjectFileName); |
| 41 | + if (File.Exists(candidate)) |
| 42 | + { |
| 43 | + results.Add(candidate); |
| 44 | + } |
| 45 | + else |
| 46 | + { |
| 47 | + foreach (var subdirectory in Directory.EnumerateDirectories(directory)) |
| 48 | + { |
| 49 | + CollectProjectFiles(subdirectory, results); |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | +} |
0 commit comments