Skip to content

Commit 767d427

Browse files
committed
C#: Re-implement the git version logic using an attribute.
1 parent 731b941 commit 767d427

File tree

4 files changed

+59
-10
lines changed

4 files changed

+59
-10
lines changed

csharp/extractor/Semmle.Extraction/Extractor/Extractor.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,17 +107,14 @@ public static string Version
107107
{
108108
get
109109
{
110-
// the resources for git information are always attached to the entry` assembly by our build system
110+
// the attribute for the git information are always attached to the entry assembly by our build system
111111
var assembly = Assembly.GetEntryAssembly();
112-
var describeAllStream = assembly.GetManifestResourceStream("git-ql-describe-all.log");
113-
var headSHAStream = assembly.GetManifestResourceStream("git-ql-rev-parse.log");
114-
if (describeAllStream == null || headSHAStream == null)
112+
var versionString = assembly!.GetCustomAttribute<AssemblyInformationalVersionAttribute>();
113+
if (versionString == null)
115114
{
116115
return "unknown (not built from internal bazel workspace)";
117116
}
118-
var describeAll = new StreamReader(describeAllStream).ReadToEnd().Trim('\n');
119-
var headSHA = new StreamReader(headSHAStream).ReadToEnd().Trim('\n');
120-
return $"{describeAll} ({headSHA})";
117+
return versionString.InformationalVersion;
121118
}
122119
}
123120

csharp/scripts/BUILD.bazel

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
py_binary(
2+
name = "gen-assembly-info",
3+
srcs = ["gen-assembly-info.py"],
4+
deps = ["@rules_python//python/runfiles"],
5+
)
6+
7+
# this is an instance of the dbscheme kept in the bazel build tree
8+
# this allows everything that bazel builds to be up-to-date,
9+
# independently from whether //go:gen was already run to update the checked in files
10+
genrule(
11+
name = "assembly-info-src",
12+
srcs = ["@semmle_code//:git_info"],
13+
outs = ["AssemblyInfo.cs"],
14+
cmd = "$(execpath :gen-assembly-info) $@ $(SRCS)",
15+
tools = [":gen-assembly-info"],
16+
visibility = ["//csharp:__subpackages__"],
17+
)

csharp/scripts/gen-assembly-info.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""
2+
Generates an `AssemblyInfo.cs` file that specifies the `AssemblyInformationalVersion` attribute.
3+
4+
This attribute is set to the git version string of the repository."""
5+
6+
import pathlib
7+
import argparse
8+
9+
10+
def options():
11+
p = argparse.ArgumentParser(
12+
description="Generate the assembly info file that contains the git SHA and branch name"
13+
)
14+
p.add_argument("output", help="The path to the output file")
15+
p.add_argument("gitinfo_files", nargs="+", help="The path to the gitinfo files")
16+
return p.parse_args()
17+
18+
19+
opts = options()
20+
21+
gitfiles = dict()
22+
for file in map(pathlib.Path, opts.gitinfo_files):
23+
gitfiles[file.name] = file
24+
25+
version_string = gitfiles["git-ql-describe-all.log"].read_text().strip()
26+
version_string += f" ({gitfiles['git-ql-rev-parse.log'].read_text().strip()})"
27+
28+
output_file = pathlib.Path(opts.output)
29+
output_file_contents = f"""
30+
using System.Reflection;
31+
32+
[assembly: AssemblyInformationalVersion("{version_string}")]
33+
"""
34+
output_file.write_text(output_file_contents)

misc/bazel/csharp.bzl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,15 @@ def codeql_csharp_binary(name, **kwargs):
3939
nullable = kwargs.pop("nullable", "enable")
4040
visibility = kwargs.pop("visibility", ["//visibility:public"])
4141
resources = kwargs.pop("resources", [])
42+
srcs = kwargs.pop("srcs", [])
4243

43-
# the entry assembly always has the git info embedded
44-
resources.append("@semmle_code//:git_info")
44+
# always add the assembly info file that sets the AssemblyInformationalVersion attribute to the extractor version
45+
srcs.append("//csharp/scripts:assembly-info-src")
4546

4647
target_frameworks = kwargs.pop("target_frameworks", [TARGET_FRAMEWORK])
4748
csharp_binary_target = "bin/" + name
4849
publish_binary_target = "publish/" + name
49-
csharp_binary(name = csharp_binary_target, nullable = nullable, target_frameworks = target_frameworks, resources = resources, visibility = visibility, **kwargs)
50+
csharp_binary(name = csharp_binary_target, srcs = srcs, nullable = nullable, target_frameworks = target_frameworks, resources = resources, visibility = visibility, **kwargs)
5051
publish_binary(
5152
name = publish_binary_target,
5253
binary = csharp_binary_target,

0 commit comments

Comments
 (0)