dotnet add uses ProjectTypeGuid or DefaultProjectTypeGuid (#5218)

* dotnet add uses ProjectTypeGuid or DefaultProjectTypeGuid

* Create ProjectInstanceExtensions per PR comments
This commit is contained in:
Justin Goshi 2017-01-05 12:04:57 -10:00 committed by GitHub
parent 8effe48566
commit f8de8d5a80
37 changed files with 964 additions and 60 deletions

View file

@ -3,6 +3,7 @@
using Microsoft.Build.Construction;
using Microsoft.Build.Evaluation;
using Microsoft.Build.Execution;
using Microsoft.DotNet.Cli;
using Microsoft.DotNet.Cli.Sln.Internal;
using Microsoft.DotNet.Cli.Utils;
@ -42,15 +43,12 @@ namespace Microsoft.DotNet.Tools.Add.ProjectToSolution
}
PathUtility.EnsureAllPathsExist(RemainingArguments, CommonLocalizableStrings.ProjectDoesNotExist);
var relativeProjectPaths = RemainingArguments.Select((p) =>
PathUtility.GetRelativePath(
PathUtility.EnsureTrailingSlash(slnFile.BaseDirectory),
Path.GetFullPath(p))).ToList();
var fullProjectPaths = RemainingArguments.Select((p) => Path.GetFullPath(p)).ToList();
int preAddProjectCount = slnFile.Projects.Count;
foreach (var project in relativeProjectPaths)
foreach (var fullProjectPath in fullProjectPaths)
{
AddProject(slnFile, project);
AddProject(slnFile, fullProjectPath);
}
if (slnFile.Projects.Count > preAddProjectCount)
@ -61,47 +59,30 @@ namespace Microsoft.DotNet.Tools.Add.ProjectToSolution
return 0;
}
private void AddProject(SlnFile slnFile, string projectPath)
private void AddProject(SlnFile slnFile, string fullProjectPath)
{
var projectPathNormalized = PathUtility.GetPathWithDirectorySeparator(projectPath);
var relativeProjectPath = PathUtility.GetRelativePath(
PathUtility.EnsureTrailingSlash(slnFile.BaseDirectory),
fullProjectPath);
if (slnFile.Projects.Any((p) =>
string.Equals(p.FilePath, projectPathNormalized, StringComparison.OrdinalIgnoreCase)))
string.Equals(p.FilePath, relativeProjectPath, StringComparison.OrdinalIgnoreCase)))
{
Reporter.Output.WriteLine(string.Format(
CommonLocalizableStrings.SolutionAlreadyContainsProject,
slnFile.FullPath,
projectPath));
relativeProjectPath));
}
else
{
string projectGuidString = null;
if (File.Exists(projectPath))
{
var projectElement = ProjectRootElement.Open(
projectPath,
new ProjectCollection(),
preserveFormatting: true);
var projectGuidProperty = projectElement.Properties.Where((p) =>
string.Equals(p.Name, "ProjectGuid", StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
if (projectGuidProperty != null)
{
projectGuidString = projectGuidProperty.Value;
}
}
var projectGuid = (projectGuidString == null)
? Guid.NewGuid()
: new Guid(projectGuidString);
var projectInstance = new ProjectInstance(fullProjectPath);
var slnProject = new SlnProject
{
Id = projectGuid.ToString("B").ToUpper(),
TypeGuid = ProjectTypeGuids.CPSProjectTypeGuid,
Name = Path.GetFileNameWithoutExtension(projectPath),
FilePath = projectPathNormalized
Id = projectInstance.GetProjectId(),
TypeGuid = projectInstance.GetProjectTypeGuid(),
Name = Path.GetFileNameWithoutExtension(relativeProjectPath),
FilePath = relativeProjectPath
};
AddDefaultBuildConfigurations(slnFile, slnProject);
@ -111,7 +92,7 @@ namespace Microsoft.DotNet.Tools.Add.ProjectToSolution
slnFile.Projects.Add(slnProject);
Reporter.Output.WriteLine(
string.Format(CommonLocalizableStrings.ProjectAddedToTheSolution, projectPath));
string.Format(CommonLocalizableStrings.ProjectAddedToTheSolution, relativeProjectPath));
}
}