|
| 1 | +#region License |
| 2 | +//MIT License |
| 3 | +// |
| 4 | +//Copyright (c) 2017 Dave Glick |
| 5 | +// |
| 6 | +//Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +//of this software and associated documentation files (the "Software"), to deal |
| 8 | +//in the Software without restriction, including without limitation the rights |
| 9 | +//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +//copies of the Software, and to permit persons to whom the Software is |
| 11 | +//furnished to do so, subject to the following conditions: |
| 12 | +// |
| 13 | +//The above copyright notice and this permission notice shall be included in all |
| 14 | +//copies or substantial portions of the Software. |
| 15 | +// |
| 16 | +//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | +//SOFTWARE. |
| 23 | +#endregion |
| 24 | + |
| 25 | +using System; |
| 26 | +using System.Collections.Generic; |
| 27 | +using System.IO; |
| 28 | +using System.Linq; |
| 29 | +using System.Xml.Linq; |
| 30 | +using DocGenerator.Buildalyzer.Logging; |
| 31 | +using Microsoft.Build.Construction; |
| 32 | +using Microsoft.Build.Framework; |
| 33 | +using Microsoft.Extensions.Logging; |
| 34 | + |
| 35 | +namespace DocGenerator.Buildalyzer |
| 36 | +{ |
| 37 | + public class AnalyzerManager |
| 38 | + { |
| 39 | + private readonly Dictionary<string, ProjectAnalyzer> _projects = new Dictionary<string, ProjectAnalyzer>(); |
| 40 | + |
| 41 | + public IReadOnlyDictionary<string, ProjectAnalyzer> Projects => _projects; |
| 42 | + |
| 43 | + internal ILogger<ProjectAnalyzer> ProjectLogger { get; } |
| 44 | + |
| 45 | + internal LoggerVerbosity LoggerVerbosity { get; } |
| 46 | + |
| 47 | + public string SolutionDirectory { get; } |
| 48 | + |
| 49 | + public AnalyzerManager(ILoggerFactory loggerFactory = null, LoggerVerbosity loggerVerbosity = LoggerVerbosity.Normal) |
| 50 | + : this(null, loggerFactory, loggerVerbosity) |
| 51 | + { |
| 52 | + } |
| 53 | + |
| 54 | + public AnalyzerManager(TextWriter logWriter, LoggerVerbosity loggerVerbosity = LoggerVerbosity.Normal) |
| 55 | + : this(null, logWriter, loggerVerbosity) |
| 56 | + { |
| 57 | + } |
| 58 | + |
| 59 | + public AnalyzerManager(string solutionFilePath, ILoggerFactory loggerFactory = null, LoggerVerbosity loggerVerbosity = LoggerVerbosity.Normal) |
| 60 | + { |
| 61 | + LoggerVerbosity = loggerVerbosity; |
| 62 | + ProjectLogger = loggerFactory?.CreateLogger<ProjectAnalyzer>(); |
| 63 | + |
| 64 | + if (solutionFilePath != null) |
| 65 | + { |
| 66 | + solutionFilePath = ValidatePath(solutionFilePath, true); |
| 67 | + SolutionDirectory = Path.GetDirectoryName(solutionFilePath); |
| 68 | + GetProjectsInSolution(solutionFilePath); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + public AnalyzerManager(string solutionFilePath, TextWriter logWriter, LoggerVerbosity loggerVerbosity = LoggerVerbosity.Normal) |
| 73 | + { |
| 74 | + LoggerVerbosity = loggerVerbosity; |
| 75 | + if (logWriter != null) |
| 76 | + { |
| 77 | + LoggerFactory loggerFactory = new LoggerFactory(); |
| 78 | + loggerFactory.AddProvider(new TextWriterLoggerProvider(logWriter)); |
| 79 | + ProjectLogger = loggerFactory.CreateLogger<ProjectAnalyzer>(); |
| 80 | + } |
| 81 | + |
| 82 | + if (solutionFilePath != null) |
| 83 | + { |
| 84 | + solutionFilePath = ValidatePath(solutionFilePath, true); |
| 85 | + SolutionDirectory = Path.GetDirectoryName(solutionFilePath); |
| 86 | + GetProjectsInSolution(solutionFilePath); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + private void GetProjectsInSolution(string solutionFilePath) |
| 91 | + { |
| 92 | + var supportedType = new[] |
| 93 | + { |
| 94 | + SolutionProjectType.KnownToBeMSBuildFormat, |
| 95 | + SolutionProjectType.WebProject |
| 96 | + }; |
| 97 | + |
| 98 | + SolutionFile solution = SolutionFile.Parse(solutionFilePath); |
| 99 | + foreach(ProjectInSolution project in solution.ProjectsInOrder) |
| 100 | + { |
| 101 | + if (!supportedType.Contains(project.ProjectType)) |
| 102 | + continue; |
| 103 | + GetProject(project.AbsolutePath); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + public ProjectAnalyzer GetProject(string projectFilePath) |
| 108 | + { |
| 109 | + if (projectFilePath == null) |
| 110 | + { |
| 111 | + throw new ArgumentNullException(nameof(projectFilePath)); |
| 112 | + } |
| 113 | + |
| 114 | + // Normalize as .sln uses backslash regardless of OS the sln is created on |
| 115 | + projectFilePath = projectFilePath.Replace('\\', Path.DirectorySeparatorChar); |
| 116 | + projectFilePath = ValidatePath(projectFilePath, true); |
| 117 | + if (_projects.TryGetValue(projectFilePath, out ProjectAnalyzer project)) |
| 118 | + { |
| 119 | + return project; |
| 120 | + } |
| 121 | + project = new ProjectAnalyzer(this, projectFilePath); |
| 122 | + _projects.Add(projectFilePath, project); |
| 123 | + return project; |
| 124 | + } |
| 125 | + |
| 126 | + public ProjectAnalyzer GetProject(string projectFilePath, XDocument projectDocument) |
| 127 | + { |
| 128 | + if (projectFilePath == null) |
| 129 | + { |
| 130 | + throw new ArgumentNullException(nameof(projectFilePath)); |
| 131 | + } |
| 132 | + if (projectDocument == null) |
| 133 | + { |
| 134 | + throw new ArgumentNullException(nameof(projectDocument)); |
| 135 | + } |
| 136 | + |
| 137 | + // Normalize as .sln uses backslash regardless of OS the sln is created on |
| 138 | + projectFilePath = projectFilePath.Replace('\\', Path.DirectorySeparatorChar); |
| 139 | + projectFilePath = ValidatePath(projectFilePath, false); |
| 140 | + if (_projects.TryGetValue(projectFilePath, out ProjectAnalyzer project)) |
| 141 | + { |
| 142 | + return project; |
| 143 | + } |
| 144 | + project = new ProjectAnalyzer(this, projectFilePath, projectDocument); |
| 145 | + _projects.Add(projectFilePath, project); |
| 146 | + return project; |
| 147 | + } |
| 148 | + |
| 149 | + private static string ValidatePath(string path, bool checkExists) |
| 150 | + { |
| 151 | + if (path == null) |
| 152 | + { |
| 153 | + throw new ArgumentNullException(nameof(path)); |
| 154 | + } |
| 155 | + path = Path.GetFullPath(path); // Normalize the path |
| 156 | + if (checkExists && !File.Exists(path)) |
| 157 | + { |
| 158 | + throw new ArgumentException($"The path {path} could not be found."); |
| 159 | + } |
| 160 | + return path; |
| 161 | + } |
| 162 | + } |
| 163 | +} |
0 commit comments