Skip to content

Commit 6731bcc

Browse files
criementamasvajk
authored andcommitted
C#: Provide skeleton to generate an assemblyInfo file.
Each unit gets a unique assemblyInfo file, on top of the ones for entrypoints that also gets the git info embedded.
1 parent 16f8be4 commit 6731bcc

File tree

4 files changed

+71
-22
lines changed

4 files changed

+71
-22
lines changed

csharp/scripts/BUILD.bazel

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
1+
py_binary(
2+
name = "gen-git-assembly-info",
3+
srcs = ["gen-git-assembly-info.py"],
4+
deps = ["@rules_python//python/runfiles"],
5+
)
6+
17
py_binary(
28
name = "gen-assembly-info",
39
srcs = ["gen-assembly-info.py"],
10+
visibility = ["//csharp:__subpackages__"],
411
deps = ["@rules_python//python/runfiles"],
512
)
613

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
1014
genrule(
11-
name = "assembly-info-src",
15+
name = "git-assembly-info-src",
1216
srcs = ["@semmle_code//:git_info"],
13-
outs = ["AssemblyInfo.cs"],
14-
cmd = "$(execpath :gen-assembly-info) $@ $(SRCS)",
15-
tools = [":gen-assembly-info"],
17+
outs = ["GitAssemblyInfo.cs"],
18+
cmd = "$(execpath :gen-git-assembly-info) $@ $(SRCS)",
19+
tools = [":gen-git-assembly-info"],
1620
visibility = ["//csharp:__subpackages__"],
1721
)

csharp/scripts/gen-assembly-info.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,27 @@
11
"""
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."""
2+
Generates an `AssemblyInfo.cs` file that specifies a bunch of useful attributes
3+
that we want to set on our assemblies."""
54

65
import pathlib
76
import argparse
87

98

109
def options():
1110
p = argparse.ArgumentParser(
12-
description="Generate the assembly info file that contains the git SHA and branch name"
11+
description="Generate an assembly info file."
1312
)
1413
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")
14+
p.add_argument("name", help="The name of the assembly")
1615
return p.parse_args()
1716

1817

1918
opts = options()
2019

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-
2820
output_file = pathlib.Path(opts.output)
2921
output_file_contents = f"""
3022
using System.Reflection;
3123
32-
[assembly: AssemblyInformationalVersion("{version_string}")]
24+
[assembly: XX("{opts.name}")]
25+
[assembly: YY("ZZ")]
3326
"""
3427
output_file.write_text(output_file_contents)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""
2+
Generates an `GitAssemblyInfo.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 git 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: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,25 @@ load("//misc/bazel:pkg.bzl", "codeql_pkg_files")
44

55
TARGET_FRAMEWORK = "net8.0"
66

7+
def _gen_assembly_info(name):
8+
assembly_info_gen = name + "-assembly-info"
9+
10+
native.genrule(
11+
name = assembly_info_gen,
12+
outs = [name + "AssemblyInfo.cs"],
13+
cmd = "$(execpath //csharp/scripts:gen-assembly-info) $@ " + name,
14+
tools = ["//csharp/scripts:gen-assembly-info"],
15+
)
16+
return ":" + assembly_info_gen
17+
718
def codeql_csharp_library(name, **kwargs):
19+
assembly_info_gen = _gen_assembly_info(name)
20+
srcs = kwargs.pop("srcs", [])
21+
srcs.append(assembly_info_gen)
22+
823
kwargs.setdefault("nullable", "enable")
924
kwargs.setdefault("target_frameworks", [TARGET_FRAMEWORK])
10-
csharp_library(name = name, **kwargs)
25+
csharp_library(name = name, srcs = srcs, **kwargs)
1126

1227
def codeql_xunit_test(name, **kwargs):
1328
kwargs.setdefault("nullable", "enable")
@@ -40,8 +55,11 @@ def codeql_csharp_binary(name, **kwargs):
4055
resources = kwargs.pop("resources", [])
4156
srcs = kwargs.pop("srcs", [])
4257

58+
assembly_info_gen = _gen_assembly_info(name)
59+
srcs.append(assembly_info_gen)
60+
4361
# always add the assembly info file that sets the AssemblyInformationalVersion attribute to the extractor version
44-
srcs.append("//csharp/scripts:assembly-info-src")
62+
srcs.append("//csharp/scripts:git-assembly-info-src")
4563

4664
csharp_binary_target = "bin/" + name
4765
publish_binary_target = "publish/" + name

0 commit comments

Comments
 (0)