workspace: Adding bazel build formatter/linter

Adds in buildifier bazel targets that autoformat/lint the entire
WORKSPACE. This can be invoked by running;

'bazel run //:buildifier'

or

'bazel run //:buildifier_test'

Change-Id: Iaaf7f6241e29894eca94aa6b5d0ca3b33255b7f7
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/36160
Reviewed-by: Keir Mierle <keir@google.com>
Commit-Queue: Keir Mierle <keir@google.com>
diff --git a/BUILD b/BUILD
index 31c2571..35c1531 100644
--- a/BUILD
+++ b/BUILD
@@ -12,5 +12,50 @@
 # License for the specific language governing permissions and limitations under
 # the License.
 
+load(
+    "@com_github_bazelbuild_buildtools//buildifier:def.bzl",
+    "buildifier",
+    "buildifier_test",
+)
+
 licenses(["notice"])  # Apache License 2.0
-exports_files(["tsconfig.json"], visibility = ["//:__subpackages__"])
+
+exports_files(
+    ["tsconfig.json"],
+    visibility = ["//:__subpackages__"],
+)
+
+# Fix all Bazel relevant files.
+buildifier(
+    name = "buildifier",
+    # Ignore gn and CIPD outputs in formatting.
+    # NOTE: These globs are not Bazel native and are passed directly
+    # through to the buildifier binary.
+    # TODO: Remove these globs when
+    # https://github.com/bazelbuild/buildtools/issues/623 is addressed.
+    exclude_patterns = [
+        "./.environment/**/*",
+        "./.presubmit/**/*",
+        "./.out/**/*",
+    ],
+)
+
+# Test to ensure all Bazel build files are properly formatted.
+buildifier_test(
+    name = "buildifier_test",
+    srcs = glob(
+        [
+            "**/*.bazel",
+            "**/*.bzl",
+            "**/*.oss",
+            "**/*.sky",
+            "**/BUILD",
+        ],
+        # Node modules do not play nice with buildifier. Exclude these
+        # generated Bazel files from format testing.
+        exclude = ["**/node_modules/**/*"],
+    ) + ["WORKSPACE"],
+    diff_command = "diff -u",
+    mode = "diff",
+    verbose = True,
+)