Skip to content

Commit 521edfb

Browse files
committed
Restore sln-add Program.cs
1 parent ead71e5 commit 521edfb

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

src/Cli/dotnet/Commands/Solution/Add/Program.cs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System;
45
using System.CommandLine;
56
using Microsoft.Build.Construction;
67
using Microsoft.Build.Exceptions;
78
using Microsoft.Build.Execution;
9+
using Microsoft.DotNet.Cli;
810
using Microsoft.DotNet.Cli.Extensions;
911
using Microsoft.DotNet.Cli.Utils;
12+
using Microsoft.DotNet.Tools.Common;
1013
using Microsoft.VisualStudio.SolutionPersistence;
1114
using Microsoft.VisualStudio.SolutionPersistence.Model;
1215
using Microsoft.VisualStudio.SolutionPersistence.Serializer.SlnV12;
1316

1417
namespace Microsoft.DotNet.Cli.Commands.Solution.Add;
15-
1618
internal class AddProjectToSolutionCommand : CommandBase
1719
{
18-
private static readonly string[] _defaultPlatforms = ["Any CPU", "x64", "x86"];
19-
private static readonly string[] _defaultBuildTypes = ["Debug", "Release"];
20+
private static string[] _defaultPlatforms = new[] { "Any CPU", "x64", "x86" };
21+
private static string[] _defaultBuildTypes = new[] { "Debug", "Release" };
2022
private readonly string _fileOrDirectory;
2123
private readonly bool _inRoot;
2224
private readonly IReadOnlyCollection<string> _projects;
@@ -91,7 +93,7 @@ private async Task AddProjectsToSolutionAsync(string solutionFileFullPath, IEnum
9193
}
9294
}
9395

94-
SolutionFolderModel? solutionFolder = !_inRoot && !string.IsNullOrEmpty(_solutionFolderPath)
96+
SolutionFolderModel? solutionFolder = (!_inRoot && !string.IsNullOrEmpty(_solutionFolderPath))
9597
? solution.AddFolder(GetSolutionFolderPathWithForwardSlashes(_solutionFolderPath))
9698
: null;
9799

@@ -106,7 +108,7 @@ private async Task AddProjectsToSolutionAsync(string solutionFileFullPath, IEnum
106108
{
107109
if (relativeSolutionFolder.Split(Path.DirectorySeparatorChar).LastOrDefault() == Path.GetFileNameWithoutExtension(relativePath))
108110
{
109-
relativeSolutionFolder = Path.Combine([.. relativeSolutionFolder.Split(Path.DirectorySeparatorChar).SkipLast(1)]);
111+
relativeSolutionFolder = Path.Combine(relativeSolutionFolder.Split(Path.DirectorySeparatorChar).SkipLast(1).ToArray());
110112
}
111113
if (!string.IsNullOrEmpty(relativeSolutionFolder))
112114
{
@@ -130,19 +132,20 @@ private async Task AddProjectsToSolutionAsync(string solutionFileFullPath, IEnum
130132
await serializer.SaveAsync(solutionFileFullPath, solution, cancellationToken);
131133
}
132134

133-
private static void AddProject(SolutionModel solution, string solutionRelativeProjectPath, string fullPath, SolutionFolderModel? solutionFolder, ISolutionSerializer serializer = null)
135+
private void AddProject(SolutionModel solution, string solutionRelativeProjectPath, string fullPath, SolutionFolderModel? solutionFolder, ISolutionSerializer serializer = null)
134136
{
135137
// Open project instance to see if it is a valid project
136138
ProjectRootElement projectRootElement = ProjectRootElement.Open(fullPath);
139+
ProjectInstance projectInstance = new ProjectInstance(projectRootElement);
137140
SolutionProjectModel project;
138141
try
139142
{
140143
project = solution.AddProject(solutionRelativeProjectPath, null, solutionFolder);
141144
}
142-
catch (SolutionArgumentException ex) when (ex.ParamName == "projectTypeName")
145+
catch (SolutionArgumentException ex) when (ex.Type == SolutionErrorType.InvalidProjectTypeReference)
143146
{
144147
// If guid is not identified by vs-solutionpersistence, check in project element itself
145-
var guid = projectRootElement.GetProjectTypeGuid();
148+
var guid = projectRootElement.GetProjectTypeGuid() ?? projectInstance.GetDefaultProjectTypeGuid();
146149
if (string.IsNullOrEmpty(guid))
147150
{
148151
Reporter.Error.WriteLine(CommonLocalizableStrings.UnsupportedProjectType, fullPath);
@@ -151,7 +154,6 @@ private static void AddProject(SolutionModel solution, string solutionRelativePr
151154
project = solution.AddProject(solutionRelativeProjectPath, guid, solutionFolder);
152155
}
153156
// Add settings based on existing project instance
154-
ProjectInstance projectInstance = new ProjectInstance(projectRootElement);
155157
string projectInstanceId = projectInstance.GetProjectId();
156158
if (!string.IsNullOrEmpty(projectInstanceId) && serializer is ISolutionSerializer<SlnV12SerializerSettings>)
157159
{
@@ -176,4 +178,4 @@ private static void AddProject(SolutionModel solution, string solutionRelativePr
176178
}
177179
Reporter.Output.WriteLine(CommonLocalizableStrings.ProjectAddedToTheSolution, solutionRelativeProjectPath);
178180
}
179-
}
181+
}

0 commit comments

Comments
 (0)