|
| 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 | +using System.Threading; |
| 9 | +using Microsoft.Dnx.Runtime; |
| 10 | + |
| 11 | +namespace Microsoft.Dnx.Testing |
| 12 | +{ |
| 13 | + internal class ProjectResolver |
| 14 | + { |
| 15 | + private readonly HashSet<string> _searchPaths = new HashSet<string>(); |
| 16 | + private ILookup<string, ProjectInformation> _projects; |
| 17 | + |
| 18 | + public ProjectResolver(string projectPath) |
| 19 | + { |
| 20 | + var rootPath = ResolveRootDirectory(projectPath); |
| 21 | + Initialize(projectPath, rootPath); |
| 22 | + } |
| 23 | + |
| 24 | + public ProjectResolver(string projectPath, string rootPath) |
| 25 | + { |
| 26 | + Initialize(projectPath, rootPath); |
| 27 | + } |
| 28 | + |
| 29 | + public IEnumerable<string> SearchPaths |
| 30 | + { |
| 31 | + get |
| 32 | + { |
| 33 | + return _searchPaths; |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + public bool TryResolveProject(string name, out Project project) |
| 38 | + { |
| 39 | + project = null; |
| 40 | + |
| 41 | + if (!_projects.Contains(name)) |
| 42 | + { |
| 43 | + return false; |
| 44 | + } |
| 45 | + |
| 46 | + // ProjectInformation.Project is lazily evaluated only once and |
| 47 | + // it returns null when ProjectInformation.FullPath doesn't contain project.json |
| 48 | + var candidates = _projects[name].Where(p => p.Project != null); |
| 49 | + if (candidates.Count() > 1) |
| 50 | + { |
| 51 | + var allCandidatePaths = string.Join(Environment.NewLine, candidates.Select(x => x.FullPath).OrderBy(x => x)); |
| 52 | + throw new InvalidOperationException( |
| 53 | + $"The project name '{name}' is ambiguous between the following projects:{Environment.NewLine}{allCandidatePaths}"); |
| 54 | + } |
| 55 | + |
| 56 | + project = candidates.SingleOrDefault()?.Project; |
| 57 | + return project != null; |
| 58 | + } |
| 59 | + |
| 60 | + private void Initialize(string projectPath, string rootPath) |
| 61 | + { |
| 62 | + _searchPaths.Add(new DirectoryInfo(projectPath).Parent.FullName); |
| 63 | + |
| 64 | + GlobalSettings global; |
| 65 | + |
| 66 | + if (GlobalSettings.TryGetGlobalSettings(rootPath, out global)) |
| 67 | + { |
| 68 | + foreach (var sourcePath in global.ProjectSearchPaths) |
| 69 | + { |
| 70 | + _searchPaths.Add(Path.Combine(rootPath, sourcePath)); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + Func<DirectoryInfo, ProjectInformation> dirInfoToProjectInfo = d => new ProjectInformation |
| 75 | + { |
| 76 | + // The name of the folder is the project |
| 77 | + Name = d.Name, |
| 78 | + FullPath = d.FullName |
| 79 | + }; |
| 80 | + |
| 81 | + // Resolve all of the potential projects |
| 82 | + _projects = _searchPaths.Select(path => new DirectoryInfo(path)) |
| 83 | + .Where(d => d.Exists) |
| 84 | + .SelectMany(d => new[] { d }.Concat(d.EnumerateDirectories())) |
| 85 | + .Distinct(new DirectoryInfoFullPathComparator()) |
| 86 | + .Select(dirInfoToProjectInfo) |
| 87 | + .ToLookup(d => d.Name); |
| 88 | + } |
| 89 | + |
| 90 | + public void Clear() |
| 91 | + { |
| 92 | + _searchPaths.Clear(); |
| 93 | + _projects = null; |
| 94 | + } |
| 95 | + |
| 96 | + public static string ResolveRootDirectory(string projectPath) |
| 97 | + { |
| 98 | + return ProjectRootResolver.ResolveRootDirectory(projectPath); |
| 99 | + } |
| 100 | + |
| 101 | + private class ProjectInformation |
| 102 | + { |
| 103 | + private Project _project; |
| 104 | + private bool _initialized; |
| 105 | + private object _lockObj = new object(); |
| 106 | + |
| 107 | + public string Name { get; set; } |
| 108 | + |
| 109 | + public string FullPath { get; set; } |
| 110 | + |
| 111 | + public Project Project |
| 112 | + { |
| 113 | + get |
| 114 | + { |
| 115 | + return LazyInitializer.EnsureInitialized(ref _project, ref _initialized, ref _lockObj, () => |
| 116 | + { |
| 117 | + Project project; |
| 118 | + Project.TryGetProject(FullPath, out project); |
| 119 | + return project; |
| 120 | + }); |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + private class DirectoryInfoFullPathComparator : IEqualityComparer<DirectoryInfo> |
| 126 | + { |
| 127 | + public bool Equals(DirectoryInfo x, DirectoryInfo y) |
| 128 | + { |
| 129 | + return string.Equals(x.FullName, y.FullName); |
| 130 | + } |
| 131 | + |
| 132 | + public int GetHashCode(DirectoryInfo obj) |
| 133 | + { |
| 134 | + return obj.FullName.GetHashCode(); |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments