1
1
// Licensed to the .NET Foundation under one or more agreements.
2
2
// The .NET Foundation licenses this file to you under the MIT license.
3
3
4
+ using System ;
4
5
using System . CommandLine ;
5
6
using Microsoft . Build . Construction ;
6
7
using Microsoft . Build . Exceptions ;
7
8
using Microsoft . Build . Execution ;
9
+ using Microsoft . DotNet . Cli ;
8
10
using Microsoft . DotNet . Cli . Extensions ;
9
11
using Microsoft . DotNet . Cli . Utils ;
12
+ using Microsoft . DotNet . Tools . Common ;
10
13
using Microsoft . VisualStudio . SolutionPersistence ;
11
14
using Microsoft . VisualStudio . SolutionPersistence . Model ;
12
15
using Microsoft . VisualStudio . SolutionPersistence . Serializer . SlnV12 ;
13
16
14
17
namespace Microsoft . DotNet . Cli . Commands . Solution . Add ;
15
-
16
18
internal class AddProjectToSolutionCommand : CommandBase
17
19
{
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" } ;
20
22
private readonly string _fileOrDirectory ;
21
23
private readonly bool _inRoot ;
22
24
private readonly IReadOnlyCollection < string > _projects ;
@@ -91,7 +93,7 @@ private async Task AddProjectsToSolutionAsync(string solutionFileFullPath, IEnum
91
93
}
92
94
}
93
95
94
- SolutionFolderModel ? solutionFolder = ! _inRoot && ! string . IsNullOrEmpty ( _solutionFolderPath )
96
+ SolutionFolderModel ? solutionFolder = ( ! _inRoot && ! string . IsNullOrEmpty ( _solutionFolderPath ) )
95
97
? solution . AddFolder ( GetSolutionFolderPathWithForwardSlashes ( _solutionFolderPath ) )
96
98
: null ;
97
99
@@ -106,7 +108,7 @@ private async Task AddProjectsToSolutionAsync(string solutionFileFullPath, IEnum
106
108
{
107
109
if ( relativeSolutionFolder . Split ( Path . DirectorySeparatorChar ) . LastOrDefault ( ) == Path . GetFileNameWithoutExtension ( relativePath ) )
108
110
{
109
- relativeSolutionFolder = Path . Combine ( [ .. relativeSolutionFolder . Split ( Path . DirectorySeparatorChar ) . SkipLast ( 1 ) ] ) ;
111
+ relativeSolutionFolder = Path . Combine ( relativeSolutionFolder . Split ( Path . DirectorySeparatorChar ) . SkipLast ( 1 ) . ToArray ( ) ) ;
110
112
}
111
113
if ( ! string . IsNullOrEmpty ( relativeSolutionFolder ) )
112
114
{
@@ -130,19 +132,20 @@ private async Task AddProjectsToSolutionAsync(string solutionFileFullPath, IEnum
130
132
await serializer . SaveAsync ( solutionFileFullPath , solution , cancellationToken ) ;
131
133
}
132
134
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 )
134
136
{
135
137
// Open project instance to see if it is a valid project
136
138
ProjectRootElement projectRootElement = ProjectRootElement . Open ( fullPath ) ;
139
+ ProjectInstance projectInstance = new ProjectInstance ( projectRootElement ) ;
137
140
SolutionProjectModel project ;
138
141
try
139
142
{
140
143
project = solution . AddProject ( solutionRelativeProjectPath , null , solutionFolder ) ;
141
144
}
142
- catch ( SolutionArgumentException ex ) when ( ex . ParamName == "projectTypeName" )
145
+ catch ( SolutionArgumentException ex ) when ( ex . Type == SolutionErrorType . InvalidProjectTypeReference )
143
146
{
144
147
// 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 ( ) ;
146
149
if ( string . IsNullOrEmpty ( guid ) )
147
150
{
148
151
Reporter . Error . WriteLine ( CommonLocalizableStrings . UnsupportedProjectType , fullPath ) ;
@@ -151,7 +154,6 @@ private static void AddProject(SolutionModel solution, string solutionRelativePr
151
154
project = solution . AddProject ( solutionRelativeProjectPath , guid , solutionFolder ) ;
152
155
}
153
156
// Add settings based on existing project instance
154
- ProjectInstance projectInstance = new ProjectInstance ( projectRootElement ) ;
155
157
string projectInstanceId = projectInstance . GetProjectId ( ) ;
156
158
if ( ! string . IsNullOrEmpty ( projectInstanceId ) && serializer is ISolutionSerializer < SlnV12SerializerSettings > )
157
159
{
@@ -176,4 +178,4 @@ private static void AddProject(SolutionModel solution, string solutionRelativePr
176
178
}
177
179
Reporter . Output . WriteLine ( CommonLocalizableStrings . ProjectAddedToTheSolution , solutionRelativeProjectPath ) ;
178
180
}
179
- }
181
+ }
0 commit comments