extract logic for opening project and preserving directory and path to separate class
This commit is contained in:
parent
1edda43dd1
commit
4e3c73e778
4 changed files with 129 additions and 107 deletions
122
src/dotnet/MsbuildProject.cs
Normal file
122
src/dotnet/MsbuildProject.cs
Normal file
|
@ -0,0 +1,122 @@
|
|||
// Copyright (c) .NET Foundation and contributors. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
|
||||
using Microsoft.Build.Construction;
|
||||
using Microsoft.Build.Evaluation;
|
||||
using Microsoft.DotNet.Cli.Utils;
|
||||
using Microsoft.DotNet.Tools.Common;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
|
||||
namespace Microsoft.DotNet.Tools
|
||||
{
|
||||
internal class MsbuildProject
|
||||
{
|
||||
public ProjectRootElement Project { get; private set; }
|
||||
public string ProjectPath { get; private set; }
|
||||
public string ProjectDirectory { get; private set; }
|
||||
|
||||
private MsbuildProject(ProjectRootElement project, string projectPath, string projectDirectory)
|
||||
{
|
||||
Project = project;
|
||||
ProjectPath = projectPath;
|
||||
ProjectDirectory = PathUtility.EnsureTrailingSlash(projectDirectory);
|
||||
}
|
||||
|
||||
private MsbuildProject(ProjectRootElement project, string projectPath)
|
||||
{
|
||||
Project = project;
|
||||
ProjectPath = projectPath;
|
||||
ProjectDirectory = PathUtility.EnsureTrailingSlash(new FileInfo(projectPath).DirectoryName);
|
||||
}
|
||||
|
||||
public static MsbuildProject FromFileOrDirectory(string fileOrDirectory)
|
||||
{
|
||||
if (File.Exists(fileOrDirectory))
|
||||
{
|
||||
return FromFile(fileOrDirectory);
|
||||
}
|
||||
else
|
||||
{
|
||||
return FromDirectory(fileOrDirectory);
|
||||
}
|
||||
}
|
||||
|
||||
public static MsbuildProject FromFile(string projectPath)
|
||||
{
|
||||
if (!File.Exists(projectPath))
|
||||
{
|
||||
throw new GracefulException(LocalizableStrings.ProjectDoesNotExist, projectPath);
|
||||
}
|
||||
|
||||
var project = TryOpenProject(projectPath);
|
||||
if (project == null)
|
||||
{
|
||||
throw new GracefulException(LocalizableStrings.ProjectIsInvalid, projectPath);
|
||||
}
|
||||
|
||||
return new MsbuildProject(project, Path.GetFullPath(projectPath));
|
||||
}
|
||||
|
||||
public static MsbuildProject FromDirectory(string projectDirectory)
|
||||
{
|
||||
DirectoryInfo dir;
|
||||
try
|
||||
{
|
||||
dir = new DirectoryInfo(projectDirectory);
|
||||
projectDirectory = dir.FullName;
|
||||
}
|
||||
catch (ArgumentException)
|
||||
{
|
||||
throw new GracefulException(LocalizableStrings.CouldNotFindProjectOrDirectory, projectDirectory);
|
||||
}
|
||||
|
||||
if (!dir.Exists)
|
||||
{
|
||||
throw new GracefulException(LocalizableStrings.CouldNotFindProjectOrDirectory, projectDirectory);
|
||||
}
|
||||
|
||||
FileInfo[] files = dir.GetFiles("*proj");
|
||||
if (files.Length == 0)
|
||||
{
|
||||
throw new GracefulException(LocalizableStrings.CouldNotFindAnyProjectInDirectory, projectDirectory);
|
||||
}
|
||||
|
||||
if (files.Length > 1)
|
||||
{
|
||||
throw new GracefulException(LocalizableStrings.MoreThanOneProjectInDirectory, projectDirectory);
|
||||
}
|
||||
|
||||
FileInfo projectFile = files.First();
|
||||
|
||||
if (!projectFile.Exists)
|
||||
{
|
||||
throw new GracefulException(LocalizableStrings.CouldNotFindAnyProjectInDirectory, projectDirectory);
|
||||
}
|
||||
|
||||
var project = TryOpenProject(projectFile.FullName);
|
||||
if (project == null)
|
||||
{
|
||||
throw new GracefulException(LocalizableStrings.FoundInvalidProject, projectFile.FullName);
|
||||
}
|
||||
|
||||
return new MsbuildProject(project, projectFile.FullName, projectDirectory);
|
||||
}
|
||||
|
||||
// There is ProjectRootElement.TryOpen but it does not work as expected
|
||||
// I.e. it returns null for some valid projects
|
||||
private static ProjectRootElement TryOpenProject(string filename)
|
||||
{
|
||||
try
|
||||
{
|
||||
return ProjectRootElement.Open(filename, new ProjectCollection(), preserveFormatting: true);
|
||||
}
|
||||
catch (Microsoft.Build.Exceptions.InvalidProjectFileException)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -40,80 +40,6 @@ namespace Microsoft.DotNet.Tools
|
|||
references = references.Select((r) => PathUtility.GetRelativePath(root, Path.GetFullPath(r))).ToList();
|
||||
}
|
||||
|
||||
// There is ProjectRootElement.TryOpen but it does not work as expected
|
||||
// I.e. it returns null for some valid projects
|
||||
public static ProjectRootElement TryOpenProject(string filename)
|
||||
{
|
||||
try
|
||||
{
|
||||
return ProjectRootElement.Open(filename, new ProjectCollection(), preserveFormatting: true);
|
||||
}
|
||||
catch (Microsoft.Build.Exceptions.InvalidProjectFileException)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static ProjectRootElement GetProjectFromFileOrThrow(string filename)
|
||||
{
|
||||
if (!File.Exists(filename))
|
||||
{
|
||||
throw new GracefulException(LocalizableStrings.ProjectDoesNotExist, filename);
|
||||
}
|
||||
|
||||
var project = TryOpenProject(filename);
|
||||
if (project == null)
|
||||
{
|
||||
throw new GracefulException(LocalizableStrings.ProjectIsInvalid, filename);
|
||||
}
|
||||
|
||||
return project;
|
||||
}
|
||||
|
||||
public static ProjectRootElement GetProjectFromDirectoryOrThrow(string directory)
|
||||
{
|
||||
DirectoryInfo dir;
|
||||
try
|
||||
{
|
||||
dir = new DirectoryInfo(directory);
|
||||
}
|
||||
catch (ArgumentException)
|
||||
{
|
||||
throw new GracefulException(LocalizableStrings.CouldNotFindProjectOrDirectory, directory);
|
||||
}
|
||||
|
||||
if (!dir.Exists)
|
||||
{
|
||||
throw new GracefulException(LocalizableStrings.CouldNotFindProjectOrDirectory, directory);
|
||||
}
|
||||
|
||||
FileInfo[] files = dir.GetFiles("*proj");
|
||||
if (files.Length == 0)
|
||||
{
|
||||
throw new GracefulException(LocalizableStrings.CouldNotFindAnyProjectInDirectory, directory);
|
||||
}
|
||||
|
||||
if (files.Length > 1)
|
||||
{
|
||||
throw new GracefulException(LocalizableStrings.MoreThanOneProjectInDirectory, directory);
|
||||
}
|
||||
|
||||
FileInfo projectFile = files.First();
|
||||
|
||||
if (!projectFile.Exists)
|
||||
{
|
||||
throw new GracefulException(LocalizableStrings.CouldNotFindAnyProjectInDirectory, directory);
|
||||
}
|
||||
|
||||
var ret = TryOpenProject(projectFile.FullName);
|
||||
if (ret == null)
|
||||
{
|
||||
throw new GracefulException(LocalizableStrings.FoundInvalidProject, projectFile.FullName);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
public static string NormalizeSlashesForMsbuild(string path)
|
||||
{
|
||||
return path.Replace('/', '\\');
|
||||
|
|
|
@ -49,20 +49,7 @@ namespace Microsoft.DotNet.Tools.Add.ProjectToProjectReference
|
|||
throw new GracefulException(LocalizableStrings.RequiredArgumentNotPassed, "<Project>");
|
||||
}
|
||||
|
||||
ProjectRootElement project;
|
||||
string projectDir;
|
||||
if (File.Exists(projectArgument.Value))
|
||||
{
|
||||
project = P2PHelpers.GetProjectFromFileOrThrow(projectArgument.Value);
|
||||
projectDir = new FileInfo(projectArgument.Value).DirectoryName;
|
||||
}
|
||||
else
|
||||
{
|
||||
project = P2PHelpers.GetProjectFromDirectoryOrThrow(projectArgument.Value);
|
||||
projectDir = projectArgument.Value;
|
||||
}
|
||||
|
||||
projectDir = PathUtility.EnsureTrailingSlash(projectDir);
|
||||
var msbuildProj = MsbuildProject.FromFileOrDirectory(projectArgument.Value);
|
||||
|
||||
if (app.RemainingArguments.Count == 0)
|
||||
{
|
||||
|
@ -73,17 +60,17 @@ namespace Microsoft.DotNet.Tools.Add.ProjectToProjectReference
|
|||
if (!forceOption.HasValue())
|
||||
{
|
||||
P2PHelpers.EnsureAllReferencesExist(references);
|
||||
P2PHelpers.ConvertPathsToRelative(projectDir, ref references);
|
||||
P2PHelpers.ConvertPathsToRelative(msbuildProj.ProjectDirectory, ref references);
|
||||
}
|
||||
|
||||
int numberOfAddedReferences = P2PHelpers.AddProjectToProjectReference(
|
||||
project,
|
||||
msbuildProj.Project,
|
||||
frameworkOption.Value(),
|
||||
references);
|
||||
|
||||
if (numberOfAddedReferences != 0)
|
||||
{
|
||||
project.Save();
|
||||
msbuildProj.Project.Save();
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
|
|
@ -44,20 +44,7 @@ namespace Microsoft.DotNet.Tools.Remove.ProjectToProjectReference
|
|||
throw new GracefulException(LocalizableStrings.RequiredArgumentNotPassed, "<Project>");
|
||||
}
|
||||
|
||||
ProjectRootElement project;
|
||||
string projectDir;
|
||||
if (File.Exists(projectArgument.Value))
|
||||
{
|
||||
project = P2PHelpers.GetProjectFromFileOrThrow(projectArgument.Value);
|
||||
projectDir = new FileInfo(projectArgument.Value).DirectoryName;
|
||||
}
|
||||
else
|
||||
{
|
||||
project = P2PHelpers.GetProjectFromDirectoryOrThrow(projectArgument.Value);
|
||||
projectDir = projectArgument.Value;
|
||||
}
|
||||
|
||||
projectDir = PathUtility.EnsureTrailingSlash(projectDir);
|
||||
var msbuildProj = MsbuildProject.FromFileOrDirectory(projectArgument.Value);
|
||||
|
||||
if (app.RemainingArguments.Count == 0)
|
||||
{
|
||||
|
@ -67,13 +54,13 @@ namespace Microsoft.DotNet.Tools.Remove.ProjectToProjectReference
|
|||
List<string> references = app.RemainingArguments;
|
||||
|
||||
int numberOfRemovedReferences = P2PHelpers.RemoveProjectToProjectReference(
|
||||
project,
|
||||
msbuildProj.Project,
|
||||
frameworkOption.Value(),
|
||||
references);
|
||||
|
||||
if (numberOfRemovedReferences != 0)
|
||||
{
|
||||
project.Save();
|
||||
msbuildProj.Project.Save();
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
|
Loading…
Add table
Reference in a new issue