-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathBuildConstantsGenerator.cs
More file actions
104 lines (94 loc) · 4.34 KB
/
BuildConstantsGenerator.cs
File metadata and controls
104 lines (94 loc) · 4.34 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
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
namespace GitHub.Runner.Sdk.Generator
{
[Generator]
public sealed class BuildConstantsGenerator : IIncrementalGenerator
{
public void Initialize(IncrementalGeneratorInitializationContext context)
{
// CommitHash, PackageRuntime, and RunnerVersion come from MSBuild via <CompilerVisibleProperty>.
IncrementalValueProvider<(string CommitHash, string PackageRuntime, string RunnerVersion)> props =
context.AnalyzerConfigOptionsProvider.Select((options, _) =>
{
string projectDir = ValueOrDefault(options.GlobalOptions, "build_property.MSBuildProjectDirectory", "");
string commitHash = ValueOrDefault(options.GlobalOptions, "build_property.CommitHash", GetCommitHash(projectDir));
string packageRuntime = ValueOrDefault(options.GlobalOptions, "build_property.PackageRuntime", GetRuntimeId());
string runnerVersion = ValueOrDefault(options.GlobalOptions, "build_property.RunnerVersion", GetRunnerVersion(projectDir));
return (commitHash, packageRuntime, runnerVersion);
});
context.RegisterSourceOutput(props, (ctx, values) =>
{
ctx.AddSource("BuildConstants.g.cs", BuildSource(values.CommitHash, values.PackageRuntime, values.RunnerVersion));
});
}
private static string GetCommitHash(string projectDir)
{
using var process = Process.Start(new ProcessStartInfo("git", "rev-parse HEAD")
{
WorkingDirectory = projectDir,
RedirectStandardOutput = true,
UseShellExecute = false,
});
return process.StandardOutput.ReadToEnd().Trim();
}
private static string GetRunnerVersion(string projectDir)
{
return File.ReadAllText(Path.Combine(projectDir, "..", "runnerversion")).Trim();
}
private static string GetRuntimeId()
{
string platform = "unknown";
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
platform = "win";
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
platform = "linux";
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
platform = "osx";
}
return $"{platform}-{RuntimeInformation.OSArchitecture}".ToLowerInvariant();
}
private static string ValueOrDefault(AnalyzerConfigOptions options, string name, string defaultValue)
{
options.TryGetValue(name, out string? value);
if (string.IsNullOrEmpty(value))
{
return defaultValue;
}
return value!;
}
private static string BuildSource(string commitHash, string packageRuntime, string runnerVersion)
{
var sb = new StringBuilder();
sb.AppendLine("namespace GitHub.Runner.Sdk");
sb.AppendLine("{");
sb.AppendLine(" /***");
sb.AppendLine(" * WARNING: This file is automatically regenerated on layout so the runner can provide version/commit info (do not manually edit it).");
sb.AppendLine(" */");
sb.AppendLine(" public static class BuildConstants");
sb.AppendLine(" {");
sb.AppendLine(" public static class Source");
sb.AppendLine(" {");
sb.AppendLine($" public static readonly string CommitHash = \"{commitHash}\";");
sb.AppendLine(" }");
sb.AppendLine();
sb.AppendLine(" public static class RunnerPackage");
sb.AppendLine(" {");
sb.AppendLine($" public static readonly string PackageName = \"{packageRuntime}\";");
sb.AppendLine($" public static readonly string Version = \"{runnerVersion}\";");
sb.AppendLine(" }");
sb.AppendLine(" }");
sb.AppendLine("}");
return sb.ToString();
}
}
}