Move TryGetProject into ProjectReader

This commit is contained in:
David Fowler 2015-10-17 22:42:50 -07:00
parent 8d9e66889e
commit 68d591124e
4 changed files with 56 additions and 59 deletions

View file

@ -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<LibraryRange> 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<DiagnosticMessage> 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)
{

View file

@ -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;
}

View file

@ -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<DiagnosticMessage> 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<DiagnosticMessage>());
@ -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);
}
}
}

View file

@ -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);
}