Skip to content

Commit 4a5d646

Browse files
committed
Fixed that previosu builds effected the outcome
1 parent 50166f7 commit 4a5d646

File tree

1 file changed

+28
-16
lines changed

1 file changed

+28
-16
lines changed

Core/Compiler/ProjectCompiler.cs

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace TestMyCode.CSharp.Core.Compiler
1414
public class ProjectCompiler
1515
{
1616
private const string BIN_PATH = "bin";
17-
private const string OUTPUT_PATH = "output";
17+
private const string OUTPUT_PATH = "TMC-output";
1818

1919
private readonly ProjectCollection ProjectCollection;
2020

@@ -30,19 +30,19 @@ public ICollection<string> CompileTestProjects(string projectPath)
3030

3131
foreach (string projectFile in Directory.EnumerateFiles(projectPath, "*Tests.csproj", SearchOption.AllDirectories))
3232
{
33-
Project project = this.ProjectCollection.LoadProject(projectFile);
34-
3533
string projectRoot = Path.GetDirectoryName(projectFile) ?? string.Empty;
3634

35+
//Cleanup before loading the project! It may use the files inside obj! That's no good
3736
this.CleanOutput(projectRoot);
3837

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))
4041
{
41-
throw new CompilationFaultedException(compilationErrors ?? Array.Empty<string>());
42+
throw new CompilationFaultedException(compilationErrors.CompileErrors);
4243
}
43-
44-
string assemblyName = project.GetPropertyValue("AssemblyName");
4544

45+
string assemblyName = project.GetPropertyValue("AssemblyName");
4646
string assemblyPath = Path.Combine(projectRoot, ProjectCompiler.BIN_PATH, ProjectCompiler.OUTPUT_PATH, $"{assemblyName}.dll");
4747

4848
files.Add(assemblyPath);
@@ -70,28 +70,41 @@ private void CleanOutput(string projectRoot)
7070
}
7171
}
7272

73-
private bool CompileTestProject(Project project, out IReadOnlyList<string>? compilationErrors)
73+
private bool CompileTestProject(Project project, out CompilerOutputLogger logger)
7474
{
75-
CompilerOutputLogger logger = new CompilerOutputLogger();
75+
logger = new CompilerOutputLogger();
7676

77-
bool build = project.Build(targets: new[]
77+
bool restore = project.Build(targets: new[]
7878
{
79-
"Clean",
8079
"Restore",
81-
"Publish"
8280
}, loggers: new ILogger[]
8381
{
8482
logger
8583
});
8684

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+
});
88101

89102
return build;
90103
}
91104

92105
private class CompilerOutputLogger : ILogger
93106
{
94-
private List<string>? _CompileErrors;
107+
private List<string> _CompileErrors = new List<string>();
95108

96109
public void Initialize(IEventSource eventSource)
97110
{
@@ -103,15 +116,14 @@ public void Initialize(IEventSource eventSource)
103116

104117
private void AddCompileError(BuildErrorEventArgs args)
105118
{
106-
this._CompileErrors ??= new List<string>();
107119
this._CompileErrors.Add($"Error {args.Code} - {args.Message} in file {args.File} on line {args.LineNumber}. {args.GetNoobFriendlyTip()}");
108120
}
109121

110122
public void Shutdown()
111123
{
112124
}
113125

114-
internal IReadOnlyList<string>? CompileErrors => this._CompileErrors;
126+
internal IReadOnlyList<string> CompileErrors => this._CompileErrors;
115127

116128
string ILogger.Parameters { get; set; } = string.Empty;
117129

0 commit comments

Comments
 (0)