mirror of
https://github.com/github/codeql.git
synced 2025-12-16 16:53:25 +01:00
C#: Re-implement the git version logic using an attribute.
This commit is contained in:
@@ -107,17 +107,14 @@ namespace Semmle.Extraction
|
||||
{
|
||||
get
|
||||
{
|
||||
// the resources for git information are always attached to the entry` assembly by our build system
|
||||
// the attribute for the git information are always attached to the entry assembly by our build system
|
||||
var assembly = Assembly.GetEntryAssembly();
|
||||
var describeAllStream = assembly.GetManifestResourceStream("git-ql-describe-all.log");
|
||||
var headSHAStream = assembly.GetManifestResourceStream("git-ql-rev-parse.log");
|
||||
if (describeAllStream == null || headSHAStream == null)
|
||||
var versionString = assembly!.GetCustomAttribute<AssemblyInformationalVersionAttribute>();
|
||||
if (versionString == null)
|
||||
{
|
||||
return "unknown (not built from internal bazel workspace)";
|
||||
}
|
||||
var describeAll = new StreamReader(describeAllStream).ReadToEnd().Trim('\n');
|
||||
var headSHA = new StreamReader(headSHAStream).ReadToEnd().Trim('\n');
|
||||
return $"{describeAll} ({headSHA})";
|
||||
return versionString.InformationalVersion;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
17
csharp/scripts/BUILD.bazel
Normal file
17
csharp/scripts/BUILD.bazel
Normal file
@@ -0,0 +1,17 @@
|
||||
py_binary(
|
||||
name = "gen-assembly-info",
|
||||
srcs = ["gen-assembly-info.py"],
|
||||
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",
|
||||
srcs = ["@semmle_code//:git_info"],
|
||||
outs = ["AssemblyInfo.cs"],
|
||||
cmd = "$(execpath :gen-assembly-info) $@ $(SRCS)",
|
||||
tools = [":gen-assembly-info"],
|
||||
visibility = ["//csharp:__subpackages__"],
|
||||
)
|
||||
34
csharp/scripts/gen-assembly-info.py
Normal file
34
csharp/scripts/gen-assembly-info.py
Normal file
@@ -0,0 +1,34 @@
|
||||
"""
|
||||
Generates an `AssemblyInfo.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 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)
|
||||
@@ -39,14 +39,15 @@ def codeql_csharp_binary(name, **kwargs):
|
||||
nullable = kwargs.pop("nullable", "enable")
|
||||
visibility = kwargs.pop("visibility", ["//visibility:public"])
|
||||
resources = kwargs.pop("resources", [])
|
||||
srcs = kwargs.pop("srcs", [])
|
||||
|
||||
# the entry assembly always has the git info embedded
|
||||
resources.append("@semmle_code//:git_info")
|
||||
# always add the assembly info file that sets the AssemblyInformationalVersion attribute to the extractor version
|
||||
srcs.append("//csharp/scripts:assembly-info-src")
|
||||
|
||||
target_frameworks = kwargs.pop("target_frameworks", [TARGET_FRAMEWORK])
|
||||
csharp_binary_target = "bin/" + name
|
||||
publish_binary_target = "publish/" + name
|
||||
csharp_binary(name = csharp_binary_target, nullable = nullable, target_frameworks = target_frameworks, resources = resources, visibility = visibility, **kwargs)
|
||||
csharp_binary(name = csharp_binary_target, srcs = srcs, nullable = nullable, target_frameworks = target_frameworks, resources = resources, visibility = visibility, **kwargs)
|
||||
publish_binary(
|
||||
name = publish_binary_target,
|
||||
binary = csharp_binary_target,
|
||||
|
||||
Reference in New Issue
Block a user