1
1
using System ;
2
+ using System . Diagnostics ;
2
3
using System . IO ;
3
4
using System . Linq ;
4
5
using KY . Core ;
@@ -26,19 +27,19 @@ public override IGeneratorCommandResult Run()
26
27
VisualStudioSolutionProject project = parser . ParseProject ( this . Parameters . Project ) ;
27
28
if ( project == null || project . Id == Guid . Empty )
28
29
{
29
- VisualStudioSolution solution = parser . ParseSolution ( this . Parameters . Solution ) ;
30
+ VisualStudioSolution solution = parser . ParseSolution ( this . Parameters . Solution ) ?? this . FindSolution ( parser ) ;
30
31
project = solution ? . Projects . FirstOrDefault ( x => x . Path . EndsWith ( projectFileName ) ) ?? project ;
31
32
}
32
33
if ( project != null && project . Id == Guid . Empty )
33
34
{
34
35
project . Id = Guid . NewGuid ( ) ;
36
+ Logger . Warning ( "Project has no id and solution could not be found. A new id was generated and set to project." ) ;
35
37
parser . SetProjectGuid ( this . Parameters . Project , project . Id ) ;
36
38
}
37
39
if ( project != null && project . Name == null )
38
40
{
39
41
project . Name = projectFileName . Replace ( Path . GetExtension ( projectFileName ) , string . Empty ) ;
40
42
}
41
-
42
43
if ( project == null || project . Id == Guid . Empty )
43
44
{
44
45
Logger . Warning ( $ "Can not read project id. No solution for project '{ this . Parameters . Project } ' found. Automatic file cleanup deactivated!") ;
@@ -53,5 +54,41 @@ public override IGeneratorCommandResult Run()
53
54
54
55
return this . Success ( ) . ForceRerunOnAsync ( ) ;
55
56
}
57
+
58
+ private VisualStudioSolution FindSolution ( VisualStudioParser parser , int levelToGoUp = 3 )
59
+ {
60
+ Stopwatch stopwatch = new ( ) ;
61
+ stopwatch . Start ( ) ;
62
+ try
63
+ {
64
+ string projectFileName = FileSystem . GetFileName ( this . Parameters . Project ) ;
65
+ string solutionDirectory = this . Parameters . Project ;
66
+ while ( levelToGoUp > 0 )
67
+ {
68
+ solutionDirectory = FileSystem . Parent ( solutionDirectory ) ;
69
+ string [ ] solutionFiles = FileSystem . GetFiles ( solutionDirectory , "*.sln" ) ;
70
+ foreach ( string solutionFile in solutionFiles )
71
+ {
72
+ VisualStudioSolution solution = parser . ParseSolution ( solutionFile ) ;
73
+ if ( solution . Projects . Any ( x => x . Path . EndsWith ( projectFileName ) ) )
74
+ {
75
+ return solution ;
76
+ }
77
+ }
78
+ levelToGoUp -- ;
79
+ }
80
+ }
81
+ catch ( Exception exception )
82
+ {
83
+ Logger . Warning ( $ "Can not find solution for project '{ this . Parameters . Project } '. { exception . Message } ") ;
84
+ }
85
+ finally
86
+ {
87
+ stopwatch . Stop ( ) ;
88
+ Logger . Trace ( $ "Searching for solution in { ( stopwatch . ElapsedMilliseconds >= 1 ? stopwatch . ElapsedMilliseconds . ToString ( ) : "<1" ) } ms") ;
89
+ Logger . Trace ( "To skip the previous step, build the solution instead the project or set a <ProjectGuid> in the project file." ) ;
90
+ }
91
+ return null ;
92
+ }
56
93
}
57
94
}
0 commit comments