@@ -31,6 +31,13 @@ private static string GetSolutionFolderPathWithForwardSlashes(string path)
31
31
return "/" + string . Join ( "/" , PathUtility . GetPathWithDirectorySeparator ( path ) . Split ( Path . DirectorySeparatorChar , StringSplitOptions . RemoveEmptyEntries ) ) + "/" ;
32
32
}
33
33
34
+ private static bool DoesSolutionFolderPathContainInvalidChars ( string pathFragment )
35
+ {
36
+ return pathFragment
37
+ . Split ( Path . DirectorySeparatorChar )
38
+ . Any ( pathFragment => pathFragment == ".." || pathFragment == "." || Path . GetInvalidFileNameChars ( ) . Any ( c => pathFragment . Contains ( c ) ) ) ;
39
+ }
40
+
34
41
public AddProjectToSolutionCommand ( ParseResult parseResult ) : base ( parseResult )
35
42
{
36
43
_fileOrDirectory = parseResult . GetValue ( SlnCommandParser . SlnArgument ) ;
@@ -82,15 +89,10 @@ private async Task AddProjectsToSolutionAsync(string solutionFileFullPath, IEnum
82
89
{
83
90
Encoding = new UTF8Encoding ( encoderShouldEmitUTF8Identifier : true )
84
91
} ) ;
92
+
85
93
// Set default configurations and platforms for sln file
86
- foreach ( var platform in _defaultPlatforms )
87
- {
88
- solution . AddPlatform ( platform ) ;
89
- }
90
- foreach ( var buildType in _defaultBuildTypes )
91
- {
92
- solution . AddBuildType ( buildType ) ;
93
- }
94
+ _defaultPlatforms . ToList ( ) . ForEach ( solution . AddPlatform ) ;
95
+ _defaultBuildTypes . ToList ( ) . ForEach ( solution . AddBuildType ) ;
94
96
}
95
97
96
98
SolutionFolderModel ? solutionFolder = ( ! _inRoot && ! string . IsNullOrEmpty ( _solutionFolderPath ) )
@@ -100,9 +102,9 @@ private async Task AddProjectsToSolutionAsync(string solutionFileFullPath, IEnum
100
102
foreach ( var projectPath in projectPaths )
101
103
{
102
104
string relativePath = Path . GetRelativePath ( Path . GetDirectoryName ( solutionFileFullPath ) , projectPath ) ;
103
- // Add fallback solution folder if relative path does not contain `..`.
104
- string relativeSolutionFolder = relativePath . Split ( Path . DirectorySeparatorChar ) . Any ( p => p == ".." )
105
- ? string . Empty : Path . GetDirectoryName ( relativePath ) ;
105
+
106
+ // Add fallback solution folder if relative path does not contain invalid characters
107
+ string relativeSolutionFolder = DoesSolutionFolderPathContainInvalidChars ( projectPath ) ? string . Empty : Path . GetDirectoryName ( relativePath ) ;
106
108
107
109
if ( ! _inRoot && solutionFolder is null && ! string . IsNullOrEmpty ( relativeSolutionFolder ) )
108
110
{
0 commit comments