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.
This commit is contained in:
Cornelius Riemenschneider
2024-06-07 15:10:59 +02:00
committed by Tamas Vajk
parent 16f8be4ba4
commit 6731bccc92
4 changed files with 73 additions and 24 deletions

View File

@@ -1,17 +1,21 @@
py_binary(
name = "gen-assembly-info",
srcs = ["gen-assembly-info.py"],
name = "gen-git-assembly-info",
srcs = ["gen-git-assembly-info.py"],
deps = ["@rules_python//python/runfiles"],
)
py_binary(
name = "gen-assembly-info",
srcs = ["gen-assembly-info.py"],
visibility = ["//csharp:__subpackages__"],
deps = ["@rules_python//python/runfiles"],
)
# this is an instance of the dbscheme kept in the bazel build tree
# this allows everything that bazel builds to be up-to-date,
# independently from whether //go:gen was already run to update the checked in files
genrule(
name = "assembly-info-src",
name = "git-assembly-info-src",
srcs = ["@semmle_code//:git_info"],
outs = ["AssemblyInfo.cs"],
cmd = "$(execpath :gen-assembly-info) $@ $(SRCS)",
tools = [":gen-assembly-info"],
outs = ["GitAssemblyInfo.cs"],
cmd = "$(execpath :gen-git-assembly-info) $@ $(SRCS)",
tools = [":gen-git-assembly-info"],
visibility = ["//csharp:__subpackages__"],
)

View File

@@ -1,7 +1,6 @@
"""
Generates an `AssemblyInfo.cs` file that specifies the `AssemblyInformationalVersion` attribute.
This attribute is set to the git version string of the repository."""
Generates an `AssemblyInfo.cs` file that specifies a bunch of useful attributes
that we want to set on our assemblies."""
import pathlib
import argparse
@@ -9,26 +8,20 @@ import argparse
def options():
p = argparse.ArgumentParser(
description="Generate the assembly info file that contains the git SHA and branch name"
description="Generate an assembly info file."
)
p.add_argument("output", help="The path to the output file")
p.add_argument("gitinfo_files", nargs="+", help="The path to the gitinfo files")
p.add_argument("name", help="The name of the assembly")
return p.parse_args()
opts = options()
gitfiles = dict()
for file in map(pathlib.Path, opts.gitinfo_files):
gitfiles[file.name] = file
version_string = gitfiles["git-ql-describe-all.log"].read_text().strip()
version_string += f" ({gitfiles['git-ql-rev-parse.log'].read_text().strip()})"
output_file = pathlib.Path(opts.output)
output_file_contents = f"""
using System.Reflection;
[assembly: AssemblyInformationalVersion("{version_string}")]
[assembly: XX("{opts.name}")]
[assembly: YY("ZZ")]
"""
output_file.write_text(output_file_contents)

View File

@@ -0,0 +1,34 @@
"""
Generates an `GitAssemblyInfo.cs` file that specifies the `AssemblyInformationalVersion` attribute.
This attribute is set to the git version string of the repository."""
import pathlib
import argparse
def options():
p = argparse.ArgumentParser(
description="Generate the git assembly info file that contains the git SHA and branch name"
)
p.add_argument("output", help="The path to the output file")
p.add_argument("gitinfo_files", nargs="+", help="The path to the gitinfo files")
return p.parse_args()
opts = options()
gitfiles = dict()
for file in map(pathlib.Path, opts.gitinfo_files):
gitfiles[file.name] = file
version_string = gitfiles["git-ql-describe-all.log"].read_text().strip()
version_string += f" ({gitfiles['git-ql-rev-parse.log'].read_text().strip()})"
output_file = pathlib.Path(opts.output)
output_file_contents = f"""
using System.Reflection;
[assembly: AssemblyInformationalVersion("{version_string}")]
"""
output_file.write_text(output_file_contents)

View File

@@ -4,10 +4,25 @@ load("//misc/bazel:pkg.bzl", "codeql_pkg_files")
TARGET_FRAMEWORK = "net8.0"
def _gen_assembly_info(name):
assembly_info_gen = name + "-assembly-info"
native.genrule(
name = assembly_info_gen,
outs = [name + "AssemblyInfo.cs"],
cmd = "$(execpath //csharp/scripts:gen-assembly-info) $@ " + name,
tools = ["//csharp/scripts:gen-assembly-info"],
)
return ":" + assembly_info_gen
def codeql_csharp_library(name, **kwargs):
assembly_info_gen = _gen_assembly_info(name)
srcs = kwargs.pop("srcs", [])
srcs.append(assembly_info_gen)
kwargs.setdefault("nullable", "enable")
kwargs.setdefault("target_frameworks", [TARGET_FRAMEWORK])
csharp_library(name = name, **kwargs)
csharp_library(name = name, srcs = srcs, **kwargs)
def codeql_xunit_test(name, **kwargs):
kwargs.setdefault("nullable", "enable")
@@ -40,8 +55,11 @@ def codeql_csharp_binary(name, **kwargs):
resources = kwargs.pop("resources", [])
srcs = kwargs.pop("srcs", [])
assembly_info_gen = _gen_assembly_info(name)
srcs.append(assembly_info_gen)
# always add the assembly info file that sets the AssemblyInformationalVersion attribute to the extractor version
srcs.append("//csharp/scripts:assembly-info-src")
srcs.append("//csharp/scripts:git-assembly-info-src")
csharp_binary_target = "bin/" + name
publish_binary_target = "publish/" + name