-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathGeneratedFileBuildValidator.cs
More file actions
107 lines (91 loc) · 3.7 KB
/
Copy pathGeneratedFileBuildValidator.cs
File metadata and controls
107 lines (91 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
namespace Light.GuardClauses.SourceCodeTransformation;
public static class GeneratedFileBuildValidator
{
private const string SourceValidationProjectFileName = "Light.GuardClauses.SourceValidation.csproj";
public static int Validate(SourceTargetFramework targetFramework, string targetFile)
{
var absoluteTargetPath = Path.GetFullPath(targetFile);
var targetDirectory = Path.GetDirectoryName(absoluteTargetPath);
if (string.IsNullOrWhiteSpace(targetDirectory) || !Directory.Exists(targetDirectory))
{
Console.WriteLine("Generated file build validation skipped because the target directory does not exist.");
return 0;
}
var projectPath = FindRequiredSourceValidationProject();
var targetFrameworkMoniker = MapToTargetFrameworkMoniker(targetFramework);
Console.WriteLine(
$"Building generated project \"{projectPath}\" targeting {targetFrameworkMoniker} with \"{absoluteTargetPath}\"..."
);
var startInfo = new ProcessStartInfo(
"dotnet",
$"build \"{projectPath}\" -f {targetFrameworkMoniker} -p:GeneratedSourceFile=\"{absoluteTargetPath}\""
)
{
RedirectStandardError = true,
RedirectStandardOutput = true,
UseShellExecute = false,
};
using var process = Process.Start(startInfo) ??
throw new InvalidOperationException("Could not start dotnet build process.");
var standardOutputTask = process.StandardOutput.ReadToEndAsync();
var standardErrorTask = process.StandardError.ReadToEndAsync();
process.WaitForExit();
var standardOutput = standardOutputTask.GetAwaiter().GetResult();
var standardError = standardErrorTask.GetAwaiter().GetResult();
if (process.ExitCode == 0)
{
Console.WriteLine("Generated project build validation completed successfully.");
return 0;
}
Console.WriteLine("Generated project build validation failed.");
Console.WriteLine(standardOutput);
if (!string.IsNullOrWhiteSpace(standardError))
{
Console.WriteLine(standardError);
}
return process.ExitCode;
}
private static string MapToTargetFrameworkMoniker(SourceTargetFramework targetFramework) =>
targetFramework switch
{
SourceTargetFramework.Net10_0 => "net10.0",
_ => "netstandard2.0",
};
private static string FindRequiredSourceValidationProject()
{
var searchRoots = new[]
{
AppContext.BaseDirectory,
Directory.GetCurrentDirectory(),
};
var projectPath = TryFindSourceValidationProject(searchRoots);
if (projectPath != null)
{
return projectPath;
}
throw new InvalidOperationException(
$"Could not find \"{SourceValidationProjectFileName}\" by searching from " +
$"\"{AppContext.BaseDirectory}\" and \"{Directory.GetCurrentDirectory()}\"."
);
}
public static string? TryFindSourceValidationProject(IEnumerable<string> searchRoots)
{
var repositoryRoot = RepositoryLayout.TryFindRepositoryRoot(searchRoots);
if (repositoryRoot == null)
{
return null;
}
var projectPath = Path.Combine(
repositoryRoot.FullName,
"tools",
"source-export",
"Light.GuardClauses.SourceValidation",
SourceValidationProjectFileName
);
return File.Exists(projectPath) ? projectPath : null;
}
}