Skip to content

Commit 0a63db0

Browse files
committed
Import from Bazel
1 parent 877456e commit 0a63db0

31 files changed

+951
-1
lines changed

.bazelci/presubmit.yml

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
---
2+
buildifier: latest
3+
4+
matrix:
5+
platform:
6+
- ubuntu2004
7+
- macos
8+
# TODO: Enable Windows once the private API it allowlists rules_shell.
9+
# - windows
10+
11+
tasks:
12+
test_module_bzlmod:
13+
name: "Test module (Bazel 7.3.2, Bzlmod)"
14+
working_directory: "tests/bcr"
15+
bazel: 7.3.2
16+
platform: ${{ platform }}
17+
build_flags:
18+
- "--enable_bzlmod"
19+
- "--noenable_workspace"
20+
build_targets:
21+
- "//..."
22+
test_flags:
23+
- "--enable_bzlmod"
24+
- "--noenable_workspace"
25+
test_targets:
26+
- "//..."
27+
test_module_workspace:
28+
name: "Test module (Bazel 7.3.2, WORKSPACE)"
29+
working_directory: "tests/bcr"
30+
bazel: 7.3.2
31+
platform: ${{ platform }}
32+
build_flags:
33+
- "--noenable_bzlmod"
34+
- "--enable_workspace"
35+
build_targets:
36+
- "//..."
37+
test_flags:
38+
- "--noenable_bzlmod"
39+
- "--enable_workspace"
40+
test_targets:
41+
- "//..."
42+
test_module_head:
43+
name: "Test module (Bazel@HEAD, Bzlmod)"
44+
working_directory: "tests/bcr"
45+
bazel: last_green
46+
platform: ${{ platform }}
47+
build_flags:
48+
- "--enable_bzlmod"
49+
- "--noenable_workspace"
50+
build_targets:
51+
- "//..."
52+
test_flags:
53+
- "--enable_bzlmod"
54+
- "--noenable_workspace"
55+
test_targets:
56+
- "//..."

.bazelignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
tests/bcr

.bcr/metadata.template.json

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"homepage": "https://github.com/bazelbuild/rules_shell",
3+
"maintainers": [
4+
{
5+
"email": "[email protected]",
6+
"github": "meteorcloudy",
7+
"name": "Yun Peng"
8+
},
9+
{
10+
"email": "[email protected]",
11+
"github": "fmeum",
12+
"name": "Fabian Meumertzheim"
13+
}
14+
],
15+
"repository": [
16+
"github:bazelbuild/rules_shell"
17+
],
18+
"versions": [],
19+
"yanked_versions": {}
20+
}

.bcr/presubmit.yml

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
bcr_test_module:
2+
module_path: tests/bcr
3+
matrix:
4+
platform:
5+
- centos7
6+
- debian10
7+
- ubuntu2004
8+
- macos
9+
- windows
10+
bazel: [6.x, 7.x]
11+
tasks:
12+
run_test_module:
13+
name: Run test module
14+
platform: ${{ platform }}
15+
bazel: ${{ bazel }}
16+
test_targets:
17+
- "//..."

.bcr/source.template.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"integrity": "**leave this alone**",
3+
"strip_prefix": "{REPO}-{VERSION}",
4+
"url": "https://github.com/{OWNER}/{REPO}/releases/download/{TAG}/{REPO}-{TAG}.tar.gz"
5+
}

.github/workflows/release.yml

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Cut a release whenever a new tag is pushed to the repo.
2+
# You should use an annotated tag, like `git tag -a v1.2.3`
3+
# and put the release notes into the commit message for the tag.
4+
name: Release
5+
6+
on:
7+
push:
8+
tags:
9+
- "v*.*.*"
10+
11+
permissions:
12+
contents: write
13+
14+
jobs:
15+
release:
16+
uses: bazel-contrib/.github/.github/workflows/release_ruleset.yaml@v6
17+
with:
18+
release_files: rules_shell-*.tar.gz

.github/workflows/release_prep.sh

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env bash
2+
3+
set -o errexit -o nounset -o pipefail
4+
5+
# Set by GH actions, see
6+
# https://docs.github.com/en/actions/learn-github-actions/environment-variables#default-environment-variables
7+
TAG=${GITHUB_REF_NAME}
8+
# The prefix is chosen to match what GitHub generates for source archives
9+
# This guarantees that users can easily switch from a released artifact to a source archive
10+
# with minimal differences in their code (e.g. strip_prefix remains the same)
11+
PREFIX="rules_shell-${TAG:1}"
12+
ARCHIVE="rules_shell-$TAG.tar.gz"
13+
14+
# NB: configuration for 'git archive' is in /.gitattributes
15+
git archive --format=tar --prefix="${PREFIX}/" "${TAG}" | gzip > "$ARCHIVE"
16+
SHA=$(shasum -a 256 "$ARCHIVE" | awk '{print $1}')
17+
18+
cat << EOF
19+
## Using Bzlmod with Bazel 6 or greater
20+
21+
1. (Bazel 6 only) Enable with \`common --enable_bzlmod\` in \`.bazelrc\`.
22+
2. Add to your \`MODULE.bazel\` file:
23+
24+
\`\`\`starlark
25+
bazel_dep(name = "rules_shell", version = "${TAG:1}")
26+
\`\`\`
27+
28+
## Using WORKSPACE
29+
30+
Paste this snippet into your \`WORKSPACE.bazel\` file:
31+
32+
\`\`\`starlark
33+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
34+
http_archive(
35+
name = "rules_shell",
36+
sha256 = "${SHA}",
37+
strip_prefix = "${PREFIX}",
38+
url = "https://github.com/bazelbuild/rules_shell/releases/download/${TAG}/${ARCHIVE}",
39+
)
40+
41+
load("@rules_shell//shell:repositories.bzl", "rules_shell_dependencies", "rules_shell_toolchains")
42+
rules_shell_dependencies()
43+
rules_shell_toolchains()
44+
\`\`\`
45+
EOF

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
bazel-*
2-
2+
MODULE.bazel.lock

MODULE.bazel

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module(
2+
name = "rules_shell",
3+
version = "0.0.0",
4+
)
5+
6+
bazel_dep(name = "platforms", version = "0.0.10")
7+
8+
sh_configure = use_extension("//shell/private/extensions:sh_configure.bzl", "sh_configure")
9+
use_repo(sh_configure, "local_config_shell")
10+
11+
register_toolchains("@local_config_shell//:all")

WORKSPACE

Whitespace-only changes.

shell/BUILD

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# A runtime toolchain for shell scripts.
2+
#
3+
# Use `sh_toolchain` to register a toolchain for this type.
4+
#
5+
# Every toolchain registered for this type has the following attributes:
6+
# - `path`: The path to the shell interpreter for the target platform.
7+
#
8+
# Other attribute may be present but are considered implementation details of
9+
# Bazel's sh_* rules.
10+
#
11+
# Toolchains registered for this type should have target constraints.
12+
toolchain_type(
13+
name = "toolchain_type",
14+
visibility = ["//visibility:public"],
15+
)

shell/private/BUILD

Whitespace-only changes.

shell/private/extensions/BUILD

Whitespace-only changes.
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copyright 2018 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""The sh_configure module extension."""
16+
17+
load("//shell/private/repositories:sh_config.bzl", "sh_config")
18+
19+
def _sh_configure_impl(module_ctx):
20+
sh_config(name = "local_config_shell")
21+
return module_ctx.extension_metadata(reproducible = True)
22+
23+
sh_configure = module_extension(implementation = _sh_configure_impl)

shell/private/repositories/BUILD

Whitespace-only changes.
+140
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
# Copyright 2018 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
"""Configure sh_toolchains based on the local machine."""
15+
16+
visibility(["//shell", "//shell/private/extensions"])
17+
18+
_DEFAULT_SHELL_PATHS = {
19+
"windows": "c:/msys64/usr/bin/bash.exe",
20+
"linux": "/bin/bash",
21+
"osx": "/bin/bash",
22+
"freebsd": "/usr/local/bin/bash",
23+
"openbsd": "/usr/local/bin/bash",
24+
}
25+
26+
_UNIX_SH_TOOLCHAIN_TEMPLATE = """
27+
sh_toolchain(
28+
name = "{os}_sh",
29+
path = {sh_path},
30+
)
31+
"""
32+
33+
_WINDOWS_SH_TOOLCHAIN_TEMPLATE = """
34+
sh_toolchain(
35+
name = "{os}_sh",
36+
path = {sh_path},
37+
launcher = "@bazel_tools//tools/launcher",
38+
launcher_maker = "@bazel_tools//tools/launcher:launcher_maker",
39+
)
40+
"""
41+
42+
_TOOLCHAIN_TEMPLATE = """
43+
toolchain(
44+
name = "{os}_sh_toolchain",
45+
toolchain = ":{os}_sh",
46+
toolchain_type = "@rules_shell//shell:toolchain_type",
47+
target_compatible_with = [
48+
"@platforms//os:{os}",
49+
],
50+
)
51+
"""
52+
53+
def _sh_config_impl(repository_ctx):
54+
"""sh_config rule implementation.
55+
56+
Creates sh_toolchains for commonly supported target platforms.
57+
For the target platform matching the local machine, it detects the path of
58+
the shell interpreter instead of using the default path.
59+
60+
Args:
61+
repository_ctx: the repository rule context object
62+
"""
63+
toolchains = []
64+
for os, default_shell_path in _DEFAULT_SHELL_PATHS.items():
65+
is_host = repository_ctx.os.name.startswith(os)
66+
if is_host:
67+
# This toolchain was first added before optional toolchains were
68+
# available, so instead of not registering a toolchain if we
69+
# couldn't find the shell, we register a toolchain with an empty
70+
# path.
71+
sh_path = _detect_local_shell_path(repository_ctx) or ""
72+
else:
73+
sh_path = default_shell_path
74+
75+
sh_toolchain_template = _WINDOWS_SH_TOOLCHAIN_TEMPLATE if os == "windows" else _UNIX_SH_TOOLCHAIN_TEMPLATE
76+
toolchains.append(sh_toolchain_template.format(
77+
os = os,
78+
sh_path = repr(sh_path),
79+
))
80+
toolchains.append(_TOOLCHAIN_TEMPLATE.format(
81+
os = os,
82+
))
83+
84+
repository_ctx.file("BUILD", """
85+
load("@rules_shell//shell/toolchains:sh_toolchain.bzl", "sh_toolchain")
86+
""" + "\n".join(toolchains))
87+
88+
sh_config = repository_rule(
89+
environ = [
90+
"WINDIR",
91+
"PATH",
92+
],
93+
# TODO: Replace this with configure = True and add BAZEL_SH to the
94+
# environ list above for consistency with CC and other repo rules.
95+
# This would make discovery differ from --shell_executable.
96+
local = True,
97+
implementation = _sh_config_impl,
98+
)
99+
100+
def _detect_local_shell_path(repository_ctx):
101+
if repository_ctx.os.name.startswith("windows"):
102+
return _detect_local_shell_path_windows(repository_ctx)
103+
else:
104+
return _detect_local_shell_path_unix(repository_ctx)
105+
106+
def _detect_local_shell_path_windows(repository_ctx):
107+
sh_path = repository_ctx.os.environ.get("BAZEL_SH")
108+
if sh_path:
109+
return sh_path.replace("\\", "/")
110+
111+
sh_path_obj = repository_ctx.which("bash.exe")
112+
if sh_path_obj:
113+
# repository_ctx.which returns a path object, convert that to
114+
# string so we can call string.startswith on it.
115+
sh_path = str(sh_path_obj)
116+
117+
# When the Windows Subsystem for Linux is installed there's a
118+
# bash.exe under %WINDIR%\system32\bash.exe that launches Ubuntu
119+
# Bash which cannot run native Windows programs so it's not what
120+
# we want.
121+
windir = repository_ctx.os.environ.get("WINDIR")
122+
if not windir or not sh_path.startswith(windir):
123+
return sh_path.replace("\\", "/")
124+
125+
return None
126+
127+
def _detect_local_shell_path_unix(repository_ctx):
128+
sh_path = repository_ctx.os.environ.get("BAZEL_SH")
129+
if sh_path:
130+
return sh_path
131+
132+
sh_path_obj = repository_ctx.which("bash")
133+
if sh_path_obj:
134+
return str(sh_path_obj)
135+
136+
sh_path_obj = repository_ctx.which("sh")
137+
if sh_path_obj:
138+
return str(sh_path_obj)
139+
140+
return None

0 commit comments

Comments
 (0)