@@ -14,7 +14,7 @@ namespace TestMyCode.CSharp.Core.Compiler
14
14
public class ProjectCompiler
15
15
{
16
16
private const string BIN_PATH = "bin" ;
17
- private const string OUTPUT_PATH = "output" ;
17
+ private const string OUTPUT_PATH = "TMC- output" ;
18
18
19
19
private readonly ProjectCollection ProjectCollection ;
20
20
@@ -30,19 +30,19 @@ public ICollection<string> CompileTestProjects(string projectPath)
30
30
31
31
foreach ( string projectFile in Directory . EnumerateFiles ( projectPath , "*Tests.csproj" , SearchOption . AllDirectories ) )
32
32
{
33
- Project project = this . ProjectCollection . LoadProject ( projectFile ) ;
34
-
35
33
string projectRoot = Path . GetDirectoryName ( projectFile ) ?? string . Empty ;
36
34
35
+ //Cleanup before loading the project! It may use the files inside obj! That's no good
37
36
this . CleanOutput ( projectRoot ) ;
38
37
39
- if ( ! this . CompileTestProject ( project , out IReadOnlyList < string > ? compilationErrors ) )
38
+ Project project = this . ProjectCollection . LoadProject ( projectFile ) ;
39
+
40
+ if ( ! this . CompileTestProject ( project , out CompilerOutputLogger compilationErrors ) )
40
41
{
41
- throw new CompilationFaultedException ( compilationErrors ?? Array . Empty < string > ( ) ) ;
42
+ throw new CompilationFaultedException ( compilationErrors . CompileErrors ) ;
42
43
}
43
-
44
- string assemblyName = project . GetPropertyValue ( "AssemblyName" ) ;
45
44
45
+ string assemblyName = project . GetPropertyValue ( "AssemblyName" ) ;
46
46
string assemblyPath = Path . Combine ( projectRoot , ProjectCompiler . BIN_PATH , ProjectCompiler . OUTPUT_PATH , $ "{ assemblyName } .dll") ;
47
47
48
48
files . Add ( assemblyPath ) ;
@@ -70,28 +70,41 @@ private void CleanOutput(string projectRoot)
70
70
}
71
71
}
72
72
73
- private bool CompileTestProject ( Project project , out IReadOnlyList < string > ? compilationErrors )
73
+ private bool CompileTestProject ( Project project , out CompilerOutputLogger logger )
74
74
{
75
- CompilerOutputLogger logger = new CompilerOutputLogger ( ) ;
75
+ logger = new CompilerOutputLogger ( ) ;
76
76
77
- bool build = project . Build ( targets : new [ ]
77
+ bool restore = project . Build ( targets : new [ ]
78
78
{
79
- "Clean" ,
80
79
"Restore" ,
81
- "Publish"
82
80
} , loggers : new ILogger [ ]
83
81
{
84
82
logger
85
83
} ) ;
86
84
87
- compilationErrors = logger . CompileErrors ;
85
+ if ( ! restore )
86
+ {
87
+ return false ;
88
+ }
89
+
90
+ //Required for to be able to detect changes made by the Restore target!
91
+ project . MarkDirty ( ) ;
92
+ project . ReevaluateIfNecessary ( ) ;
93
+
94
+ bool build = project . Build ( targets : new [ ]
95
+ {
96
+ "Publish"
97
+ } , loggers : new ILogger [ ]
98
+ {
99
+ logger
100
+ } ) ;
88
101
89
102
return build ;
90
103
}
91
104
92
105
private class CompilerOutputLogger : ILogger
93
106
{
94
- private List < string > ? _CompileErrors ;
107
+ private List < string > _CompileErrors = new List < string > ( ) ;
95
108
96
109
public void Initialize ( IEventSource eventSource )
97
110
{
@@ -103,15 +116,14 @@ public void Initialize(IEventSource eventSource)
103
116
104
117
private void AddCompileError ( BuildErrorEventArgs args )
105
118
{
106
- this . _CompileErrors ??= new List < string > ( ) ;
107
119
this . _CompileErrors . Add ( $ "Error { args . Code } - { args . Message } in file { args . File } on line { args . LineNumber } . { args . GetNoobFriendlyTip ( ) } ") ;
108
120
}
109
121
110
122
public void Shutdown ( )
111
123
{
112
124
}
113
125
114
- internal IReadOnlyList < string > ? CompileErrors => this . _CompileErrors ;
126
+ internal IReadOnlyList < string > CompileErrors => this . _CompileErrors ;
115
127
116
128
string ILogger . Parameters { get ; set ; } = string . Empty ;
117
129
0 commit comments