diff --git a/BUILD.bazel b/BUILD.bazel index abf7653..5b4cf50 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -1,2 +1,3 @@ load("@com_github_bazelbuild_buildtools//buildifier:def.bzl", "buildifier") + buildifier(name = "buildifier") diff --git a/WORKSPACE b/WORKSPACE index 5106d9c..910ac6f 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -4,6 +4,7 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") # rules_jvm_external RULES_JVM_EXTERNAL_TAG = "5.3" + http_archive( name = "rules_jvm_external", sha256 = "6cc8444b20307113a62b676846c29ff018402fd4c7097fcd6d0a0fd5f2e86429", @@ -13,58 +14,74 @@ http_archive( ) load(":workspace.bzl", "twirl_repositories") + twirl_repositories() + load("@twirl//:defs.bzl", twirl_pinned_maven_install = "pinned_maven_install") + twirl_pinned_maven_install() load(":test_workspace.bzl", "twirl_test_repositories") + twirl_test_repositories() + load("@twirl_test//:defs.bzl", twirl_test_pinned_maven_install = "pinned_maven_install") + twirl_test_pinned_maven_install() # Skylib -skylib_version = "1.0.2" # update this as needed +skylib_version = "1.4.2" # update this as needed + http_archive( name = "bazel_skylib", - sha256 = "64ad2728ccdd2044216e4cec7815918b7bb3bb28c95b7e9d951f9d4eccb07625", + sha256 = "0fa67483ecf6a7c62b892b6f0d68aa4135586478077f0f97c4fbf39201648cf4", strip_prefix = "bazel-skylib-{}".format(skylib_version), type = "zip", url = "https://github.com/bazelbuild/bazel-skylib/archive/{}.zip".format(skylib_version), ) + load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace") + bazel_skylib_workspace() # rules_nodejs # To use the JavaScript version of Sass, we need to first install nodejs -rules_nodejs_version = "1.7.0" +rules_nodejs_version = "5.3.0" + http_archive( name = "build_bazel_rules_nodejs", - sha256 = "82030031436c265e7fc427b08d581eeb658d1feca23c73df6e633b0951e9f14f", + sha256 = "322b2ecebd564586f5ccdd645833d7a2ecae2db554886e4435c3d02bdf2ecdf4", strip_prefix = "rules_nodejs-{}".format(rules_nodejs_version), type = "zip", url = "https://github.com/bazelbuild/rules_nodejs/archive/{}.zip".format(rules_nodejs_version), ) +load("@build_bazel_rules_nodejs//:repositories.bzl", "build_bazel_rules_nodejs_dependencies") + +build_bazel_rules_nodejs_dependencies() + load("@build_bazel_rules_nodejs//:index.bzl", "node_repositories") -node_repositories(package_json = []) + +node_repositories() # rules_sass -rules_sass_version = "1.26.2" # update this as needed +rules_sass_version = "1.67.0" # update this as needed + http_archive( name = "io_bazel_rules_sass", - sha256 = "a31026741e4af6f1e5bcc9cce23db0549ecdea6270c8919da09110886102eb8e", + sha256 = "b29eb037568be386f7d4976fd160fefac98e0e26e3056e55f0d3eba2a0afb874", strip_prefix = "rules_sass-{}".format(rules_sass_version), type = "zip", url = "https://github.com/bazelbuild/rules_sass/archive/{}.zip".format(rules_sass_version), ) -load("@io_bazel_rules_sass//:package.bzl", "rules_sass_dependencies") -rules_sass_dependencies() -load("@io_bazel_rules_sass//sass:sass_repositories.bzl", "sass_repositories") +load("@io_bazel_rules_sass//:defs.bzl", "sass_repositories") + sass_repositories() # Skydoc -skydoc_version = "0.3.0" # update this as needed +skydoc_version = "0.3.0" # update this as needed + http_archive( name = "io_bazel_skydoc", sha256 = "8762a212cff5f81505a1632630edcfe9adce381479a50a03c968bd2fc217972d", @@ -72,29 +89,32 @@ http_archive( type = "zip", url = "https://github.com/bazelbuild/skydoc/archive/{}.zip".format(skydoc_version), ) + load("@io_bazel_skydoc//skylark:skylark.bzl", "skydoc_repositories") + skydoc_repositories() # buildifier buildtools_version = "4.2.2" + http_archive( - name = "com_github_bazelbuild_buildtools", - sha256 = "ae34c344514e08c23e90da0e2d6cb700fcd28e80c02e23e4d5715dddcb42f7b3", - strip_prefix = "buildtools-{}".format(buildtools_version), - url = "https://github.com/bazelbuild/buildtools/archive/refs/tags/{}.tar.gz".format(buildtools_version) + name = "com_github_bazelbuild_buildtools", + sha256 = "ae34c344514e08c23e90da0e2d6cb700fcd28e80c02e23e4d5715dddcb42f7b3", + strip_prefix = "buildtools-{}".format(buildtools_version), + url = "https://github.com/bazelbuild/buildtools/archive/refs/tags/{}.tar.gz".format(buildtools_version), ) - # buildifier is written in Go and hence needs rules_go to be built. # See https://github.com/bazelbuild/rules_go for the up to date setup instructions. http_archive( - name = "io_bazel_rules_go", - sha256 = "6dc2da7ab4cf5d7bfc7c949776b1b7c733f05e56edc4bcd9022bb249d2e2a996", - urls = [ - "https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.39.1/rules_go-v0.39.1.zip", - "https://github.com/bazelbuild/rules_go/releases/download/v0.39.1/rules_go-v0.39.1.zip", - ], + name = "io_bazel_rules_go", + sha256 = "6dc2da7ab4cf5d7bfc7c949776b1b7c733f05e56edc4bcd9022bb249d2e2a996", + urls = [ + "https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.39.1/rules_go-v0.39.1.zip", + "https://github.com/bazelbuild/rules_go/releases/download/v0.39.1/rules_go-v0.39.1.zip", + ], ) + load("@io_bazel_rules_go//go:deps.bzl", "go_rules_dependencies") go_rules_dependencies() @@ -103,37 +123,44 @@ load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains") go_register_toolchains(version = "1.20.3") - # Also for buildifier. Comes from # https://github.com/bazelbuild/buildtools/blob/master/buildifier/README.md protobuf_version = "3.19.4" + http_archive( -name = "com_google_protobuf", -sha256 = "3bd7828aa5af4b13b99c191e8b1e884ebfa9ad371b0ce264605d347f135d2568", -strip_prefix = "protobuf-{}".format(protobuf_version), -url = "https://github.com/protocolbuffers/protobuf/archive/v{}.tar.gz".format(protobuf_version), + name = "com_google_protobuf", + sha256 = "3bd7828aa5af4b13b99c191e8b1e884ebfa9ad371b0ce264605d347f135d2568", + strip_prefix = "protobuf-{}".format(protobuf_version), + url = "https://github.com/protocolbuffers/protobuf/archive/v{}.tar.gz".format(protobuf_version), ) + load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps") + protobuf_deps() # higherkindness/rules_scala (used for tests only) # TODO: Move tests into their own worskpace s.t. we don't need their dependenices here rules_scala_annex_version = "938ffdc24fb25a58220aa650948761073f1dd83b" + http_archive( -name = "rules_scala_annex", -sha256 = "ae0cfd560a5238248d5f4c9fcde352e5dfc87cba5efd0b97e4c88ea839fd51b5", -strip_prefix = "rules_scala-{}".format(rules_scala_annex_version), -type = "zip", -url = "https://github.com/lucidsoftware/rules_scala/archive/{}.zip".format(rules_scala_annex_version), + name = "rules_scala_annex", + sha256 = "ae0cfd560a5238248d5f4c9fcde352e5dfc87cba5efd0b97e4c88ea839fd51b5", + strip_prefix = "rules_scala-{}".format(rules_scala_annex_version), + type = "zip", + url = "https://github.com/lucidsoftware/rules_scala/archive/{}.zip".format(rules_scala_annex_version), ) bind( -name = "default_scala", -actual = "@rules_scala_annex//src/main/scala:zinc_2_12_10", + name = "default_scala", + actual = "@rules_scala_annex//src/main/scala:zinc_2_12_10", ) load("@rules_scala_annex//rules/scala:workspace.bzl", "scala_register_toolchains", "scala_repositories") + scala_repositories() + load("@annex//:defs.bzl", annex_pinned_maven_install = "pinned_maven_install") + annex_pinned_maven_install() + scala_register_toolchains() diff --git a/compiler-cli/BUILD.bazel b/compiler-cli/BUILD.bazel index 00f644a..dd04791 100644 --- a/compiler-cli/BUILD.bazel +++ b/compiler-cli/BUILD.bazel @@ -1,6 +1,6 @@ java_binary( - name = "compiler-cli", - runtime_deps = ["@twirl//:com_lucidchart_twirl_compiler_cli"], - main_class = "rulestwirl.twirl.CommandLineTwirlTemplateCompiler", - visibility = ["//visibility:public"] + name = "compiler-cli", + main_class = "rulestwirl.twirl.CommandLineTwirlTemplateCompiler", + visibility = ["//visibility:public"], + runtime_deps = ["@twirl//:com_lucidchart_twirl_compiler_cli"], ) diff --git a/test/BUILD.bazel b/test/BUILD.bazel index c98e6ae..ca2b591 100644 --- a/test/BUILD.bazel +++ b/test/BUILD.bazel @@ -2,54 +2,54 @@ load("@rules_scala_annex//rules:scala.bzl", "scala_test") load("//twirl:twirl.bzl", "twirl_templates") twirl_templates( - name = "twirl-test-templates-basic", - visibility = ["//visibility:public"], - source_directory = "twirl-templates", - srcs = [ - "twirl-templates/twirl/com/foo/views/hello.scala.html", - "twirl-templates/twirl/com/foo/views/hello.scala.txt", - "twirl-templates/twirl/com/foo/views/hello.scala.xml", - "twirl-templates/twirl/com/foo/views/hello.scala.js", - ], + name = "twirl-test-templates-basic", + srcs = [ + "twirl-templates/twirl/com/foo/views/hello.scala.html", + "twirl-templates/twirl/com/foo/views/hello.scala.js", + "twirl-templates/twirl/com/foo/views/hello.scala.txt", + "twirl-templates/twirl/com/foo/views/hello.scala.xml", + ], + source_directory = "twirl-templates", + visibility = ["//visibility:public"], ) twirl_templates( - name = "twirl-test-templates-additional-imports", - visibility = ["//visibility:public"], - source_directory = "twirl-templates", - srcs = [ - "twirl-templates/twirl/com/foo/views/addImports.scala.txt", - ], - additional_imports = ["rulestwirl.test.Person"], + name = "twirl-test-templates-additional-imports", + srcs = [ + "twirl-templates/twirl/com/foo/views/addImports.scala.txt", + ], + additional_imports = ["rulestwirl.test.Person"], + source_directory = "twirl-templates", + visibility = ["//visibility:public"], ) twirl_templates( - name = "twirl-test-templates-custom-formatter", - visibility = ["//visibility:public"], - source_directory = "twirl-templates", - srcs = [ - "twirl-templates/twirl/com/foo/views/customFormatter.scala.txt", - ], - additional_imports = ["rulestwirl.test.Person"], - template_formats = { - "txt": "rulestwirl.test.StrangeTxtFormat" - }, + name = "twirl-test-templates-custom-formatter", + srcs = [ + "twirl-templates/twirl/com/foo/views/customFormatter.scala.txt", + ], + additional_imports = ["rulestwirl.test.Person"], + source_directory = "twirl-templates", + template_formats = { + "txt": "rulestwirl.test.StrangeTxtFormat", + }, + visibility = ["//visibility:public"], ) scala_test( - name = "twirl-compiler-test", - srcs = [ - "TwirlCompilerTest.scala", - "Person.scala", - "StrangeTxtFormatter.scala", - ":twirl-test-templates-basic", - ":twirl-test-templates-additional-imports", - ":twirl-test-templates-custom-formatter", - ], - deps = [ - "@twirl_test//:com_typesafe_play_twirl_api_2_12", - "@twirl_test//:org_specs2_specs2_common_2_12", - "@twirl_test//:org_specs2_specs2_core_2_12", - "@twirl_test//:org_specs2_specs2_matcher_2_12", - ], + name = "twirl-compiler-test", + srcs = [ + "Person.scala", + "StrangeTxtFormatter.scala", + "TwirlCompilerTest.scala", + ":twirl-test-templates-additional-imports", + ":twirl-test-templates-basic", + ":twirl-test-templates-custom-formatter", + ], + deps = [ + "@twirl_test//:com_typesafe_play_twirl_api_2_12", + "@twirl_test//:org_specs2_specs2_common_2_12", + "@twirl_test//:org_specs2_specs2_core_2_12", + "@twirl_test//:org_specs2_specs2_matcher_2_12", + ], ) diff --git a/twirl/twirl.bzl b/twirl/twirl.bzl index 13580bf..78ec158 100644 --- a/twirl/twirl.bzl +++ b/twirl/twirl.bzl @@ -6,84 +6,84 @@ Bazel rules for running the [Twirl Template Compiler](https://github.com/playfra gendir_base_path = "main/twirl" play_imports = [ - "play.twirl.api._", - "play.twirl.api.TwirlFeatureImports._", - "play.twirl.api.TwirlHelperImports._", - "models._", - "controllers._", - "play.api.i18n._", - "views.html._", - "play.api.templates.PlayMagic._", - "play.api.mvc._", - "play.api.data._", + "play.twirl.api._", + "play.twirl.api.TwirlFeatureImports._", + "play.twirl.api.TwirlHelperImports._", + "models._", + "controllers._", + "play.api.i18n._", + "views.html._", + "play.api.templates.PlayMagic._", + "play.api.mvc._", + "play.api.data._", ] def _format_map_arg(format): - return "{}={}".format(*format) + return "{}={}".format(*format) def _format_map_args(formats): - return [_format_map_arg(format) for format in formats] + return [_format_map_arg(format) for format in formats] def _impl(ctx): - imports = play_imports + ctx.attr.additional_imports if ctx.attr.include_play_imports else ctx.attr.additional_imports + imports = play_imports + ctx.attr.additional_imports if ctx.attr.include_play_imports else ctx.attr.additional_imports - outputs = [] - for src in ctx.files.srcs: - parts = src.short_path.split(".") - output = ctx.actions.declare_file("gen/{}/{}".format(ctx.label.name, ".".join(parts[:-2] + [parts[-1], parts[-2]]))) + outputs = [] + for src in ctx.files.srcs: + parts = src.short_path.split(".") + output = ctx.actions.declare_file("gen/{}/{}".format(ctx.label.name, ".".join(parts[:-2] + [parts[-1], parts[-2]]))) - args = ctx.actions.args() - if hasattr(args, "add_all"): # Bazel 0.13.0+ - args.add_all(imports, format_each = "--additionalImport=%s") - args.add_all(ctx.attr.template_formats.items(), format_each = "--templateFormat=%s", map_each = _format_map_arg) - else: - args.add(imports, format = "--additionalImport=%s") - args.add(ctx.attr.template_formats.items(), format = "--templateFormat=%s", map_fn = _format_map_args) - args.add(output) - args.add(ctx.file.source_directory.path) - args.add(src) - args.set_param_file_format("multiline") - args.use_param_file("@%s", use_always = True) + args = ctx.actions.args() + if hasattr(args, "add_all"): # Bazel 0.13.0+ + args.add_all(imports, format_each = "--additionalImport=%s") + args.add_all(ctx.attr.template_formats.items(), format_each = "--templateFormat=%s", map_each = _format_map_arg) + else: + args.add(imports, format = "--additionalImport=%s") + args.add(ctx.attr.template_formats.items(), format = "--templateFormat=%s", map_fn = _format_map_args) + args.add(output) + args.add(ctx.file.source_directory.path) + args.add(src) + args.set_param_file_format("multiline") + args.use_param_file("@%s", use_always = True) - ctx.actions.run( - inputs = [src], - outputs = [output], - arguments = [args], - mnemonic = "TwirlCompile", - execution_requirements = {"supports-workers": "1"}, - progress_message = "Compiling twirl template", - executable = ctx.executable.twirl_compiler, - ) + ctx.actions.run( + inputs = [src], + outputs = [output], + arguments = [args], + mnemonic = "TwirlCompile", + execution_requirements = {"supports-workers": "1"}, + progress_message = "Compiling twirl template", + executable = ctx.executable.twirl_compiler, + ) - outputs.append(output) + outputs.append(output) - return [ - DefaultInfo(files = depset(outputs)) - ] + return [ + DefaultInfo(files = depset(outputs)), + ] twirl_templates = rule( - implementation = _impl, - doc = "Compiles Twirl templates to Scala sources files.", - attrs = { - "source_directory": attr.label( - doc = "Directories where Twirl template files are located.", - allow_single_file = True, - mandatory = True - ), - "srcs": attr.label_list( - doc = "The actual template files contained in the source_directory.", - allow_files = True, - mandatory = True - ), - "additional_imports": attr.string_list( - doc = "Additional imports to import to the Twirl templates.", - ), - "include_play_imports": attr.bool( - doc = "If true, include the imports the Play project includes by default.", - default = False - ), - "template_formats": attr.string_dict( - doc = """ + implementation = _impl, + doc = "Compiles Twirl templates to Scala sources files.", + attrs = { + "source_directory": attr.label( + doc = "Directories where Twirl template files are located.", + allow_single_file = True, + mandatory = True, + ), + "srcs": attr.label_list( + doc = "The actual template files contained in the source_directory.", + allow_files = True, + mandatory = True, + ), + "additional_imports": attr.string_list( + doc = "Additional imports to import to the Twirl templates.", + ), + "include_play_imports": attr.bool( + doc = "If true, include the imports the Play project includes by default.", + default = False, + ), + "template_formats": attr.string_dict( + doc = """ Formatter types for file extensions. The default formats are @@ -93,13 +93,13 @@ The default formats are "xml" -> "play.twirl.api.XmlFormat", "js" -> "play.twirl.api.JavaScriptFormat" ``` -""" - ), - "twirl_compiler": attr.label( - executable = True, - cfg = "host", - allow_files = True, - default = Label("//compiler-cli"), - ) - }, +""", + ), + "twirl_compiler": attr.label( + executable = True, + cfg = "host", + allow_files = True, + default = Label("//compiler-cli"), + ), + }, )