Bazel: Introduce .bazelversion and update to 0.29.1

The minimum required bazel version is defined in the WORKSPACE and is
checked at build time. However, to support the bazelisk wrapper, we
need to define the version in .bazelversion. Bazelisk will automatically
switch to that version (downloading it if necessary).

Add a .bazelversion file, and introduce the mechanism to consume it in
the WORKSPACE rather than defining the version in two separate places.

When bazel is used directly, it still checks that the minimum version
is used. When bazelisk is used, it will switch to the exact required
version.

Bump the bazel version up to 0.29.1 as the currently specified minimum
version (0.19.0) doesn't work with this new setup.

Based on [1] by David Ostrovsky.

[1] https://git.eclipse.org/r/#/c/149966/

Change-Id: I6fe6d2e1f16b15bbad80979fcb8d76f843f69a19
diff --git a/.bazelversion b/.bazelversion
new file mode 100644
index 0000000..25939d3
--- /dev/null
+++ b/.bazelversion
@@ -0,0 +1 @@
+0.29.1
diff --git a/WORKSPACE b/WORKSPACE
index 983074b..11dcf72 100644
--- a/WORKSPACE
+++ b/WORKSPACE
@@ -9,9 +9,14 @@
     urls = ["https://github.com/bazelbuild/bazel-skylib/archive/2169ae1c374aab4a09aa90e65efe1a3aad4e279b.tar.gz"],
 )
 
-load("@bazel_skylib//lib:versions.bzl", "versions")
+# Check Bazel version when invoked by Bazel directly
+load("//tools:bazelisk_version.bzl", "bazelisk_version")
 
-versions.check(minimum_bazel_version = "0.19.0")
+bazelisk_version(name = "bazelisk_version")
+
+load("@bazelisk_version//:check.bzl", "check_bazel_version")
+
+check_bazel_version()
 
 load("//tools:bazlets.bzl", "load_bazlets")
 
diff --git a/tools/bazelisk_version.bzl b/tools/bazelisk_version.bzl
new file mode 100644
index 0000000..d8b3d10
--- /dev/null
+++ b/tools/bazelisk_version.bzl
@@ -0,0 +1,16 @@
+_template = """
+load("@bazel_skylib//lib:versions.bzl", "versions")
+
+def check_bazel_version():
+  versions.check(minimum_bazel_version = "{version}")
+""".strip()
+
+def _impl(repository_ctx):
+    repository_ctx.symlink(Label("@//:.bazelversion"), ".bazelversion")
+    bazelversion = repository_ctx.read(".bazelversion").strip()
+
+    repository_ctx.file("BUILD", executable = False)
+
+    repository_ctx.file("check.bzl", executable = False, content = _template.format(version = bazelversion))
+
+bazelisk_version = repository_rule(implementation = _impl)