[bazelisk] Initial commit

Bug: b/336617748
Change-Id: I6f780f59b6fb0abaac386bdd7978986f19cbcec5
Reviewed-on: https://fuchsia-review.googlesource.com/c/infra/3pp/+/1036052
Reviewed-by: Oliver Newman <olivernewman@google.com>
Commit-Queue: Rob Mohr <mohrr@google.com>
diff --git a/bazelisk/3pp.pb b/bazelisk/3pp.pb
new file mode 100644
index 0000000..5e16b5c
--- /dev/null
+++ b/bazelisk/3pp.pb
@@ -0,0 +1,17 @@
+create {
+  platform_re: "(linux|mac)-(amd64|arm64)"
+  source {
+    script { name: "fetch.py" }
+    unpack_archive: false
+  }
+
+  build {
+    install: "install.sh"
+  }
+
+  package {
+    version_file: ".versions/bazelisk.version"
+  }
+}
+
+upload {}
diff --git a/bazelisk/fetch.py b/bazelisk/fetch.py
new file mode 100755
index 0000000..532fbcc
--- /dev/null
+++ b/bazelisk/fetch.py
@@ -0,0 +1,54 @@
+#!/usr/bin/env python
+# Copyright 2024 The Fuchsia Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+import argparse
+import json
+import os
+import sys
+import urllib.request
+
+
+def do_latest():
+    metadata_url = "https://api.github.com/repos/bazelbuild/bazelisk/releases/latest"
+    resp = json.load(urllib.request.urlopen(metadata_url))
+    print(resp["tag_name"].lstrip("v"))
+
+
+def get_download_url(version, platform):
+    os_name, arch = platform.split("-")
+    os_name = os_name.replace("mac", "darwin")
+    ext = ".exe" if platform.startswith("windows") else ""
+
+    name = f"bazelisk-{os_name}-{arch}{ext}"
+    url = f"https://github.com/bazelbuild/bazelisk/releases/download/v{version}/{name}"
+
+    partial_manifest = {
+        "url": [url],
+        "ext": ext,
+    }
+    print(json.dumps(partial_manifest))
+
+
+def main():
+    ap = argparse.ArgumentParser()
+
+    sub = ap.add_subparsers()
+
+    latest = sub.add_parser("latest")
+    latest.set_defaults(func=lambda _opts: do_latest())
+
+    download = sub.add_parser("get_url")
+    download.set_defaults(
+        func=lambda opts: get_download_url(
+            os.environ["_3PP_VERSION"], os.environ["_3PP_PLATFORM"]
+        )
+    )
+
+    opts = ap.parse_args()
+    return opts.func(opts)
+
+
+if __name__ == "__main__":
+    sys.exit(main())
diff --git a/bazelisk/install.sh b/bazelisk/install.sh
new file mode 100755
index 0000000..5c283a9
--- /dev/null
+++ b/bazelisk/install.sh
@@ -0,0 +1,11 @@
+#!/usr/bin/env bash
+# Copyright 2024 The Fuchsia Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+set -eu -o pipefail
+
+PREFIX="$1"
+
+cp raw_source_0 "${PREFIX}/bazelisk"
+chmod a+x "${PREFIX}/bazelisk"