1+ using Microsoft . Build . Construction ;
12using Microsoft . Build . Evaluation ;
23
34namespace Nimbleways . Tools . Subset ;
45internal static class RestoreSubset
56{
6- public static void Execute ( string mainProjectPath , string rootFolder , string destinationFolder )
7+ public static void Execute ( string projectOrSolution , string rootFolder , string destinationFolder )
78 {
8- if ( ! IsSameOrUnder ( rootFolder , mainProjectPath ) )
9+ if ( ! IsSameOrUnder ( rootFolder , projectOrSolution ) )
910 {
10- throw new ArgumentException ( $ "Project '${ mainProjectPath } ' must be under the root folder '{ rootFolder } '") ;
11+ throw new ArgumentException ( $ "Project or solution '${ projectOrSolution } ' must be under the root folder '{ rootFolder } '") ;
1112 }
1213 Directory . CreateDirectory ( destinationFolder ) ;
1314
1415 using var projectCollection = new ProjectCollection ( ) ;
1516 var projectsByFullPath = new Dictionary < string , Project > ( ) ;
16- VisitAllProjects ( projectCollection , rootFolder , mainProjectPath , projectsByFullPath ) ;
17+ foreach ( var project in GetRootProjects ( projectOrSolution ) )
18+ {
19+ VisitAllProjects ( projectCollection , rootFolder , project , projectsByFullPath ) ;
20+ }
1721 var projectListAsString = string . Join ( Environment . NewLine + " - " , projectsByFullPath . Keys . OrderBy ( f => f ) ) ;
1822 Console . WriteLine ( $ "Found { projectsByFullPath . Count } project(s) to copy:{ Environment . NewLine + " - " } { projectListAsString } ") ;
1923 var nugetConfigFiles = GetNugetConfigFiles ( rootFolder , projectsByFullPath ) ;
2024 var extraFilesInvolvedInRestore = projectsByFullPath . Values
2125 . SelectMany ( project => GetExtraFilesInvolvedInRestore ( rootFolder , project ) )
2226 . Concat ( nugetConfigFiles )
2327 . Distinct ( )
24- . ToArray ( ) ;
25- if ( extraFilesInvolvedInRestore . Length > 0 )
28+ . ToList ( ) ;
29+ if ( IsSolutionFile ( projectOrSolution ) )
30+ {
31+ extraFilesInvolvedInRestore . Add ( projectOrSolution ) ;
32+ }
33+ if ( extraFilesInvolvedInRestore . Count > 0 )
2634 {
2735 var extraFilesInvolvedInRestoreAsString = string . Join ( Environment . NewLine + " - " , extraFilesInvolvedInRestore . OrderBy ( f => f ) ) ;
28- Console . WriteLine ( $ "Found { extraFilesInvolvedInRestore . Length } extra file(s) to copy:{ Environment . NewLine + " - " } { extraFilesInvolvedInRestoreAsString } ") ;
36+ Console . WriteLine ( $ "Found { extraFilesInvolvedInRestore . Count } extra file(s) to copy:{ Environment . NewLine + " - " } { extraFilesInvolvedInRestoreAsString } ") ;
2937 }
3038 var allFilesToCopy = projectsByFullPath . Keys . Concat ( extraFilesInvolvedInRestore ) . Distinct ( ) ;
3139
@@ -50,6 +58,22 @@ public static void Execute(string mainProjectPath, string rootFolder, string des
5058 }
5159 Console . WriteLine ( $ "Copied { copiedFilesCount } file(s) to '{ destinationFolder } '. { allFilesCount - copiedFilesCount } file(s) already exist in destination.") ;
5260 }
61+ private static IEnumerable < string > GetRootProjects ( string projectOrSolution )
62+ {
63+ if ( IsSolutionFile ( projectOrSolution ) )
64+ {
65+ var solution = SolutionFile . Parse ( projectOrSolution ) ;
66+ return solution . ProjectsInOrder
67+ . Where ( p => p . ProjectType != SolutionProjectType . SolutionFolder )
68+ . Select ( p => p . AbsolutePath ) ;
69+ }
70+ return new [ ] { projectOrSolution } ;
71+ }
72+
73+ private static bool IsSolutionFile ( string projectOrSolution )
74+ {
75+ return ".sln" . Equals ( Path . GetExtension ( projectOrSolution ) , StringComparison . OrdinalIgnoreCase ) ;
76+ }
5377
5478 private static bool IsSameOrUnder ( string rootFolder , string path )
5579 {
0 commit comments