@@ -20,6 +20,7 @@ internal class SolutionAddCommand : CommandBase
2020    private  readonly  IReadOnlyCollection < string >  _projects ; 
2121    private  readonly  string ?  _solutionFolderPath ; 
2222    private  string  _solutionFileFullPath  =  string . Empty ; 
23+     private  bool  _addReferencedProjects ; 
2324
2425    private  static string  GetSolutionFolderPathWithForwardSlashes ( string  path ) 
2526    { 
@@ -41,6 +42,7 @@ public SolutionAddCommand(ParseResult parseResult) : base(parseResult)
4142        _projects  =  ( IReadOnlyCollection < string > ) ( parseResult . GetValue ( SolutionAddCommandParser . ProjectPathArgument )  ??  [ ] ) ; 
4243        _inRoot  =  parseResult . GetValue ( SolutionAddCommandParser . InRootOption ) ; 
4344        _solutionFolderPath  =  parseResult . GetValue ( SolutionAddCommandParser . SolutionFolderOption ) ; 
45+         _addReferencedProjects  =  parseResult . GetValue ( SolutionAddCommandParser . AddReferencedProjectsOption ) ; 
4446        SolutionArgumentValidator . ParseAndValidateArguments ( _fileOrDirectory ,  _projects ,  SolutionArgumentValidator . CommandType . Add ,  _inRoot ,  _solutionFolderPath ) ; 
4547        _solutionFileFullPath  =  SlnFileFactory . GetSolutionFileFullPath ( _fileOrDirectory ) ; 
4648    } 
@@ -195,6 +197,21 @@ private void AddProject(SolutionModel solution, string fullProjectPath, ISolutio
195197            project . AddProjectConfigurationRule ( new  ConfigurationRule ( BuildDimension . BuildType ,  solutionBuildType ,  "*" ,  projectBuildType ) ) ; 
196198        } 
197199
200+         // Get referencedprojects from the project instance 
201+         var  referencedProjectsFullPaths  =  projectInstance . EvaluatedItemElements 
202+             . Where ( item =>  item . ItemType  ==  "ProjectReference" ) 
203+             . Select ( item =>  item . Include ) 
204+             . Select ( item =>  Path . GetFullPath ( item ,  Path . GetDirectoryName ( fullProjectPath ) ) ) 
205+             . ToList ( ) ; 
206+ 
198207        Reporter . Output . WriteLine ( CliStrings . ProjectAddedToTheSolution ,  solutionRelativeProjectPath ) ; 
208+ 
209+         if  ( _addReferencedProjects ) 
210+         { 
211+             foreach  ( var  referencedProjectFullPath  in  referencedProjectsFullPaths ) 
212+             { 
213+                 AddProject ( solution ,  referencedProjectFullPath ,  serializer ) ; 
214+             } 
215+         } 
199216    } 
200217} 
0 commit comments