From 68d591124e839304d335ae1f0b90af0f97f46f3f Mon Sep 17 00:00:00 2001 From: David Fowler Date: Sat, 17 Oct 2015 22:42:50 -0700 Subject: [PATCH] Move TryGetProject into ProjectReader --- .../Project.cs | 55 ------------------ .../ProjectContextBuilder.cs | 2 +- .../ProjectReader.cs | 56 ++++++++++++++++++- .../Resolution/ProjectDependencyProvider.cs | 2 +- 4 files changed, 56 insertions(+), 59 deletions(-) diff --git a/src/Microsoft.Extensions.ProjectModel/Project.cs b/src/Microsoft.Extensions.ProjectModel/Project.cs index 2eb19d68a..82e02e9c3 100644 --- a/src/Microsoft.Extensions.ProjectModel/Project.cs +++ b/src/Microsoft.Extensions.ProjectModel/Project.cs @@ -6,7 +6,6 @@ using System.Collections.Generic; using System.IO; using Microsoft.Extensions.ProjectModel.Files; using Microsoft.Extensions.ProjectModel.Graph; -using Microsoft.Extensions.ProjectModel.Utilities; using NuGet.Frameworks; using NuGet.Versioning; @@ -63,8 +62,6 @@ namespace Microsoft.Extensions.ProjectModel public IList Dependencies { get; set; } - public string WebRoot { get; set; } - public string EntryPoint { get; set; } public string ProjectUrl { get; set; } @@ -93,58 +90,6 @@ namespace Microsoft.Extensions.ProjectModel return _compilerOptionsByConfiguration.Keys; } - public static bool HasProjectFile(string path) - { - string projectPath = Path.Combine(path, FileName); - - return File.Exists(projectPath); - } - - public static bool TryGetProject(string path, out Project project, ICollection diagnostics = null) - { - project = null; - - string projectPath = null; - - if (string.Equals(Path.GetFileName(path), FileName, StringComparison.OrdinalIgnoreCase)) - { - projectPath = path; - path = Path.GetDirectoryName(path); - } - else if (!HasProjectFile(path)) - { - return false; - } - else - { - projectPath = Path.Combine(path, FileName); - } - - // Assume the directory name is the project name if none was specified - var projectName = PathUtility.GetDirectoryName(Path.GetFullPath(path)); - projectPath = Path.GetFullPath(projectPath); - - if (!File.Exists(projectPath)) - { - return false; - } - - try - { - using (var stream = File.OpenRead(projectPath)) - { - var reader = new ProjectReader(); - project = reader.ReadProject(stream, projectName, projectPath, diagnostics); - } - } - catch (Exception ex) - { - throw FileFormatException.Create(ex, projectPath); - } - - return true; - } - public CompilerOptions GetCompilerOptions(NuGetFramework targetFramework, string configurationName) { diff --git a/src/Microsoft.Extensions.ProjectModel/ProjectContextBuilder.cs b/src/Microsoft.Extensions.ProjectModel/ProjectContextBuilder.cs index dd37390bb..8866af9c0 100644 --- a/src/Microsoft.Extensions.ProjectModel/ProjectContextBuilder.cs +++ b/src/Microsoft.Extensions.ProjectModel/ProjectContextBuilder.cs @@ -188,7 +188,7 @@ namespace Microsoft.Extensions.ProjectModel if (Project == null) { Project project; - if (Project.TryGetProject(ProjectDirectory, out project)) + if (ProjectReader.TryGetProject(ProjectDirectory, out project)) { Project = project; } diff --git a/src/Microsoft.Extensions.ProjectModel/ProjectReader.cs b/src/Microsoft.Extensions.ProjectModel/ProjectReader.cs index 97c2affe9..009c4337d 100644 --- a/src/Microsoft.Extensions.ProjectModel/ProjectReader.cs +++ b/src/Microsoft.Extensions.ProjectModel/ProjectReader.cs @@ -8,6 +8,7 @@ using System.Linq; using Microsoft.Extensions.JsonParser.Sources; using Microsoft.Extensions.ProjectModel.Files; using Microsoft.Extensions.ProjectModel.Graph; +using Microsoft.Extensions.ProjectModel.Utilities; using NuGet.Frameworks; using NuGet.Versioning; @@ -15,6 +16,51 @@ namespace Microsoft.Extensions.ProjectModel { public class ProjectReader { + public static bool TryGetProject(string path, out Project project, ICollection diagnostics = null) + { + project = null; + + string projectPath = null; + + if (string.Equals(Path.GetFileName(path), Project.FileName, StringComparison.OrdinalIgnoreCase)) + { + projectPath = path; + path = Path.GetDirectoryName(path); + } + else if (!HasProjectFile(path)) + { + return false; + } + else + { + projectPath = Path.Combine(path, Project.FileName); + } + + // Assume the directory name is the project name if none was specified + var projectName = PathUtility.GetDirectoryName(Path.GetFullPath(path)); + projectPath = Path.GetFullPath(projectPath); + + if (!File.Exists(projectPath)) + { + return false; + } + + try + { + using (var stream = File.OpenRead(projectPath)) + { + var reader = new ProjectReader(); + project = reader.ReadProject(stream, projectName, projectPath, diagnostics); + } + } + catch (Exception ex) + { + throw FileFormatException.Create(ex, projectPath); + } + + return true; + } + public static Project GetProject(string projectFile) { return GetProject(projectFile, new List()); @@ -89,7 +135,6 @@ namespace Microsoft.Extensions.ProjectModel project.Summary = rawProject.ValueAsString("summary"); project.Copyright = rawProject.ValueAsString("copyright"); project.Title = rawProject.ValueAsString("title"); - project.WebRoot = rawProject.ValueAsString("webroot"); project.EntryPoint = rawProject.ValueAsString("entryPoint"); project.ProjectUrl = rawProject.ValueAsString("projectUrl"); project.LicenseUrl = rawProject.ValueAsString("licenseUrl"); @@ -103,7 +148,7 @@ namespace Microsoft.Extensions.ProjectModel project.ReleaseNotes = rawProject.ValueAsString("releaseNotes"); project.RequireLicenseAcceptance = rawProject.ValueAsBoolean("requireLicenseAcceptance", defaultValue: false); - + // REVIEW: Move this to the dependencies node? project.EmbedInteropTypes = rawProject.ValueAsBoolean("embedInteropTypes", defaultValue: false); @@ -506,5 +551,12 @@ namespace Microsoft.Extensions.ProjectModel return null; } + + private static bool HasProjectFile(string path) + { + string projectPath = Path.Combine(path, Project.FileName); + + return File.Exists(projectPath); + } } } diff --git a/src/Microsoft.Extensions.ProjectModel/Resolution/ProjectDependencyProvider.cs b/src/Microsoft.Extensions.ProjectModel/Resolution/ProjectDependencyProvider.cs index 5d285aad4..70d45cd75 100644 --- a/src/Microsoft.Extensions.ProjectModel/Resolution/ProjectDependencyProvider.cs +++ b/src/Microsoft.Extensions.ProjectModel/Resolution/ProjectDependencyProvider.cs @@ -18,7 +18,7 @@ namespace Microsoft.Extensions.ProjectModel.Resolution Project project; // Can't find a project file with the name so bail - if (!Project.TryGetProject(path, out project)) + if (!ProjectReader.TryGetProject(path, out project)) { return new ProjectDescription(name, path); }