Skip to content

Commit

Permalink
Adding Bazel rules for building .NET targets
Browse files Browse the repository at this point in the history
The rules added handle building the .NET assemblies for .NET
Framework 4.5, 4.6, and 4.7, as well as .NET Standard 2.0.
Additionally, the added rules will also merge the dependency
Json.NET assembly into the base WebDriver.dll assembly, and
will handle the creation of NuGet packages. All of these can
be done for both the normal and the strong-named (the so-called
"signed") case.
  • Loading branch information
jimevans committed May 1, 2019
1 parent baaf56f commit 1bfa64e
Show file tree
Hide file tree
Showing 7 changed files with 725 additions and 0 deletions.
59 changes: 59 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,62 @@ http_archive(
load("@io_bazel_rules_closure//closure:defs.bzl", "closure_repositories")

closure_repositories()

http_archive(
name = "io_bazel_rules_dotnet",
sha256 = "1dad06a55e9543f69f4b4df5711910c9c2c9de554e9d2f1c5eb2ff60a62eb4a9",
strip_prefix = "rules_dotnet-8fadcaaa395bca82cb21aa371f8c30e86df11912",
urls = [
"https://github.com/bazelbuild/rules_dotnet/archive/8fadcaaa395bca82cb21aa371f8c30e86df11912.tar.gz",
]
)

load("@io_bazel_rules_dotnet//dotnet:defs.bzl",
"dotnet_register_toolchains",
"net_register_sdk",
"core_register_sdk",
"mono_register_sdk",
"dotnet_repositories",
"dotnet_nuget_new",
"nuget_package",
"DOTNET_NET_FRAMEWORKS",
"DOTNET_CORE_FRAMEWORKS")

dotnet_register_toolchains()
dotnet_repositories()

mono_register_sdk()

[net_register_sdk(
framework
) for framework in DOTNET_NET_FRAMEWORKS]

[core_register_sdk(
framework
) for framework in DOTNET_CORE_FRAMEWORKS]

# Default core_sdk
core_register_sdk("v2.1.502", name = "core_sdk")

# Default net_sdk
net_register_sdk("net472", name = "net_sdk")

dotnet_nuget_new(
name = "json.net",
package = "newtonsoft.json",
version = "11.0.2",
build_file_content = """
package(default_visibility = [ "//visibility:public" ])
load("@io_bazel_rules_dotnet//dotnet:defs.bzl", "net_import_library", "core_import_library")
net_import_library(
name = "net45",
src = "lib/net45/Newtonsoft.Json.dll"
)
core_import_library(
name = "netcore",
src = "lib/netstandard2.0/Newtonsoft.Json.dll"
)
"""
)
1 change: 1 addition & 0 deletions dotnet/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exports_files(["WebDriver.snk"])
101 changes: 101 additions & 0 deletions dotnet/build-rules.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
load("@io_bazel_rules_dotnet//dotnet/private:context.bzl", "dotnet_context")

def _merged_assembly_impl(ctx):
args = [
"-v4",
"-xmldocs",
"-internalize",
]

if ctx.attr.keyfile != None:
key_path = ctx.expand_location(ctx.attr.keyfile.files.to_list()[0].path)
args.append("-keyfile:{}".format(key_path))

args.append("-out={}".format(ctx.outputs.out.path))
args.append(ctx.attr.src_assembly.files.to_list()[0].path)
for dep in ctx.files.deps:
args.append(ctx.expand_location(dep.path))

ctx.actions.run(
executable = ctx.executable.merge_tool,
arguments = args,
inputs = ctx.attr.src_assembly.files,
outputs = [ctx.outputs.out]
)

merged_assembly = rule(
implementation = _merged_assembly_impl,
attrs = {
"src_assembly": attr.label(),
"deps": attr.label_list(),
"out": attr.output(mandatory = True),
"keyfile": attr.label(allow_single_file = True),
"merge_tool": attr.label(
executable = True,
cfg = "host",
default = Label("//third_party/dotnet/ilmerge:ilmerge.exe"),
allow_single_file = True
),
},
toolchains = ["@io_bazel_rules_dotnet//dotnet:toolchain_net"],
)

def _nuget_package_impl(ctx):
args = [
"pack",
]

package_id = ctx.attr.package_id
package_version = ctx.attr.package_version

package_file = ctx.actions.declare_file("{}.{}.nupkg".format(package_id, package_version))
output_path = ctx.expand_location(package_file.dirname)

# The dependencies are assembly output compiled into directories
# with the appropriate target framework moniker ("<base>/net45",
# "<base>/net46", etc.). The base path for creating the NuGet
# package should be the "<base>" directory, which we need to
# hard-code with the parent operator, because Bazel doesn't
# provide proper path traversal for custom rules.
base_path = ctx.files.deps[0].dirname + "/.."

args.append(ctx.expand_location(ctx.attr.src.files.to_list()[0].path))
args.append("-Properties")
args.append("packageid={}".format(package_id))
args.append("-Version")
args.append(package_version)
args.append("-BasePath")
args.append(base_path)
args.append("-OutputDirectory")
args.append(output_path)

ctx.actions.run(
executable = ctx.executable.nuget_exe,
arguments = args,
inputs = ctx.attr.src.files + ctx.files.deps,
outputs = [
package_file,
]
)

return DefaultInfo(files = depset([
package_file,
]))

nuget_package = rule(
implementation = _nuget_package_impl,
attrs = {
"src": attr.label(
allow_single_file = True
),
"deps": attr.label_list(),
"package_id": attr.string(),
"package_version": attr.string(),
"nuget_exe": attr.label(
executable = True,
cfg = "host",
default = Label("//third_party/dotnet/nuget:nuget.exe"),
allow_single_file = True
),
}
)
1 change: 1 addition & 0 deletions dotnet/selenium-dotnet-version.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@

SE_VERSION = "4.0.0-alpha01"
ASSEMBLY_VERSION = "4.0.0.0"
SUPPORTED_NET_FRAMEWORKS = ["net45", "net46", "net47"]
118 changes: 118 additions & 0 deletions dotnet/src/support/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
load("@io_bazel_rules_dotnet//dotnet:defs.bzl", "net_library", "core_library", "core_resource")
load("//dotnet:build-rules.bzl", "merged_assembly", "nuget_package")
load("//dotnet:selenium-dotnet-version.bzl",
"SE_VERSION",
"ASSEMBLY_VERSION",
"SUPPORTED_NET_FRAMEWORKS")

[net_library(
name = "{}".format(framework),
srcs = glob([
"*.cs",
"Events/*.cs",
"Extensions/*.cs",
"PageObjects/**/*.cs",
"UI/*.cs",
]),
deps = [
"@io_bazel_rules_dotnet//dotnet/stdlib.net:system.dll",
"@io_bazel_rules_dotnet//dotnet/stdlib.net:system.core.dll",
"@io_bazel_rules_dotnet//dotnet/stdlib.net:system.data.dll",
"@io_bazel_rules_dotnet//dotnet/stdlib.net:system.drawing.dll",
"@io_bazel_rules_dotnet//dotnet/stdlib.net:system.xml.dll",
"//dotnet/src/webdriver:{}assembly".format(framework)
],
dotnet_context_data = "@io_bazel_rules_dotnet//:net_context_data_{}".format(framework),
out = "merged/{}/WebDriver.Support.dll".format(framework),
visibility = ["//visibility:public"],
) for framework in SUPPORTED_NET_FRAMEWORKS]

core_library(
name = "netstandard2.0",
srcs = glob([
"*.cs",
"Events/*.cs",
"Extensions/*.cs",
"PageObjects/**/*.cs",
"UI/*.cs",
]),
deps = [
"@io_bazel_rules_dotnet//dotnet/stdlib.core:netstandard.dll",
"//dotnet/src/webdriver:netstandard2.0"
],
defines = [
"NETSTANDARD2_0"
],
out = "merged/netstandard2.0/WebDriver.Support.dll",
visibility = ["//visibility:public"]
)

[net_library(
name = "{}-strongnamed".format(framework),
srcs = glob([
"*.cs",
"Events/*.cs",
"Extensions/*.cs",
"PageObjects/**/*.cs",
"UI/*.cs",
]),
deps = [
"@io_bazel_rules_dotnet//dotnet/stdlib.net:system.dll",
"@io_bazel_rules_dotnet//dotnet/stdlib.net:system.core.dll",
"@io_bazel_rules_dotnet//dotnet/stdlib.net:system.data.dll",
"@io_bazel_rules_dotnet//dotnet/stdlib.net:system.drawing.dll",
"@io_bazel_rules_dotnet//dotnet/stdlib.net:system.xml.dll",
"//dotnet/src/webdriver:{}assembly-strongnamed".format(framework)
],
out = "strongnamed/{}/WebDriver.Support.dll".format(framework),
keyfile = "//dotnet:WebDriver.snk",
visibility = ["//visibility:public"],
dotnet_context_data = "@io_bazel_rules_dotnet//:net_context_data_{}".format(framework)
) for framework in SUPPORTED_NET_FRAMEWORKS]

core_library(
name = "netstandard2.0-strongnamed",
srcs = glob([
"*.cs",
"Events/*.cs",
"Extensions/*.cs",
"PageObjects/**/*.cs",
"UI/*.cs",
]),
deps = [
"@io_bazel_rules_dotnet//dotnet/stdlib.core:netstandard.dll",
"//dotnet/src/webdriver:netstandard2.0-strongnamed"
],
defines = [
"NETSTANDARD2_0"
],
keyfile = "//dotnet:WebDriver.snk",
out = "strongnamed/netstandard2.0/WebDriver.Support.dll",
visibility = ["//visibility:public"]
)

nuget_package(
name = "package",
src = "WebDriver.Support.nuspec",
deps = [
":net45",
":net46",
":net47",
":netstandard2.0",
],
package_id = "Selenium.Support",
package_version = "{}".format(SE_VERSION),
)

nuget_package(
name = "package-strongnamed",
src = "WebDriver.Support.nuspec",
deps = [
":net45-strongnamed",
":net46-strongnamed",
":net47-strongnamed",
":netstandard2.0-strongnamed",
],
package_id = "Selenium.Support.StrongNamed",
package_version = "{}".format(SE_VERSION),
)
Loading

0 comments on commit 1bfa64e

Please sign in to comment.