Skip to content

Commit

Permalink
feat: initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeagle committed Mar 8, 2024
1 parent 7cfa9c2 commit 5647373
Show file tree
Hide file tree
Showing 37 changed files with 537 additions and 520 deletions.
4 changes: 2 additions & 2 deletions .bcr/metadata.template.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"homepage": "https://github.com/myorg/rules_mylang",
"homepage": "https://github.com/alexeagle/rules_protoc",
"maintainers": [],
"repository": ["github:myorg/rules_mylang"],
"repository": ["github:alexeagle/rules_protoc"],
"versions": [],
"yanked_versions": {}
}
13 changes: 0 additions & 13 deletions .gitattributes

This file was deleted.

19 changes: 19 additions & 0 deletions .github/workflows/mirror_protoc_release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Mirror Releases
on:
# Trigger manually in the UI
workflow_dispatch:
# Trigger daily at 06:10 UTC
schedule:
- cron: "10 6 * * *"

jobs:
mirror:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: |
./protoc/private/mirror_protoc_releases.sh
npx @bazel/buildifier protoc/private/versions.bzl
- uses: peter-evans/create-pull-request@v5
with:
commit-message: "chore: mirror protoc release"
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ jobs:
release:
uses: bazel-contrib/.github/.github/workflows/release_ruleset.yaml@v6
with:
release_files: rules_mylang-*.tar.gz
release_files: rules_protoc-*.tar.gz
10 changes: 5 additions & 5 deletions .github/workflows/release_prep.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ TAG=${GITHUB_REF_NAME}
# The prefix is chosen to match what GitHub generates for source archives
# This guarantees that users can easily switch from a released artifact to a source archive
# with minimal differences in their code (e.g. strip_prefix remains the same)
PREFIX="rules_mylang-${TAG:1}"
ARCHIVE="rules_mylang-$TAG.tar.gz"
PREFIX="rules_protoc-${TAG:1}"
ARCHIVE="rules_protoc-$TAG.tar.gz"

# NB: configuration for 'git archive' is in /.gitattributes
git archive --format=tar --prefix=${PREFIX}/ ${TAG} | gzip > $ARCHIVE
Expand All @@ -22,7 +22,7 @@ cat << EOF
2. Add to your \`MODULE.bazel\` file:
\`\`\`starlark
bazel_dep(name = "com_myorg_rules_mylang", version = "${TAG:1}")
bazel_dep(name = "toolchains_protoc", version = "${TAG:1}")
\`\`\`
## Using WORKSPACE
Expand All @@ -32,10 +32,10 @@ Paste this snippet into your \`WORKSPACE.bazel\` file:
\`\`\`starlark
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "com_myorg_rules_mylang",
name = "toolchains_protoc",
sha256 = "${SHA}",
strip_prefix = "${PREFIX}",
url = "https://github.com/myorg/rules_mylang/releases/download/${TAG}/${ARCHIVE}",
url = "https://github.com/alexeagle/rules_protoc/releases/download/${TAG}/${ARCHIVE}",
)
EOF

Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ bazel-*
.bazelrc.user
.idea/
.ijwb/
# Ignore until it is more stable
MODULE.bazel.lock
11 changes: 0 additions & 11 deletions BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,11 +0,0 @@
load("@bazel_gazelle//:def.bzl", "gazelle", "gazelle_binary")

gazelle_binary(
name = "gazelle_bin",
languages = ["@bazel_skylib_gazelle_plugin//bzl"],
)

gazelle(
name = "gazelle",
gazelle = "gazelle_bin",
)
6 changes: 3 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,19 @@ Run `bazel run //:gazelle` to keep them up-to-date.
## Using this as a development dependency of other rules

You'll commonly find that you develop in another WORKSPACE, such as
some other ruleset that depends on rules_mylang, or in a nested
some other ruleset that depends on rules_protoc, or in a nested
WORKSPACE in the integration_tests folder.

To always tell Bazel to use this directory rather than some release
artifact or a version fetched from the internet, run this from this
directory:

```sh
OVERRIDE="--override_repository=rules_mylang=$(pwd)/rules_mylang"
OVERRIDE="--override_repository=rules_protoc=$(pwd)/rules_protoc"
echo "common $OVERRIDE" >> ~/.bazelrc
```

This means that any usage of `@rules_mylang` on your system will point to this folder.
This means that any usage of `@rules_protoc` on your system will point to this folder.

## Releasing

Expand Down
15 changes: 7 additions & 8 deletions MODULE.bazel
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
"Bazel dependencies"

module(
name = "com_myorg_rules_mylang",
name = "toolchains_protoc",
version = "0.0.0",
compatibility_level = 1,
)

bazel_dep(name = "bazel_skylib", version = "1.4.1")
bazel_dep(name = "platforms", version = "0.0.5")
bazel_dep(name = "rules_proto", version = "6.0.0-rc2")
bazel_dep(name = "platforms", version = "0.0.8")

bazel_dep(name = "gazelle", version = "0.35.0", dev_dependency = True, repo_name = "bazel_gazelle")
bazel_dep(name = "bazel_skylib_gazelle_plugin", version = "1.4.1", dev_dependency = True)
bazel_dep(name = "aspect_bazel_lib", version = "1.32.1", dev_dependency = True)
bazel_dep(name = "buildifier_prebuilt", version = "6.1.2", dev_dependency = True)

mylang = use_extension("//mylang:extensions.bzl", "mylang")
mylang.toolchain(mylang_version = "1.14.2")
use_repo(mylang, "mylang_toolchains")
protoc = use_extension("//protoc:extensions.bzl", "protoc")
protoc.toolchain(version = "v25.3")
use_repo(protoc, "toolchains_protoc_hub")

register_toolchains("@mylang_toolchains//:all")
register_toolchains("@toolchains_protoc_hub//:all")
54 changes: 16 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,50 +1,28 @@
# Template for Bazel rules
# Bazel toolchain for pre-built protoc

Copy this template to create a Bazel ruleset.
protoc has always been distributed as pre-built binaries on https://github.com/protocolbuffers/protobuf/releases

Features:
Bazel 7 introduced `--incompatible_enable_proto_toolchain_resolution` to allow us fetch that binary rather than re-build it!

- follows the official style guide at https://docs.bazel.build/versions/main/skylark/deploying.html
- allows for both WORKSPACE.bazel and bzlmod (MODULE.bazel) usage
- includes Bazel formatting as a pre-commit hook (using [buildifier])
- includes stardoc API documentation generator
- includes typical toolchain setup
- CI configured with GitHub Actions
- release using GitHub Actions just by pushing a tag
- the release artifact doesn't need to be built by Bazel, but can still exclude files and stamp the version
See https://github.com/bazelbuild/examples/blob/never_compile_protoc_again/proto/README.md

See https://docs.bazel.build/versions/main/skylark/deploying.html#readme
## Why a separate Bazel module?

[buildifier]: https://github.com/bazelbuild/buildtools/tree/master/buildifier#readme
This belongs in rules_proto, see

Ready to get started? Copy this repo, then
- https://github.com/bazelbuild/rules_proto/pull/205
- https://github.com/bazelbuild/rules_proto/pull/206

1. search for "com_myorg_rules_mylang" and replace with the name you'll use for your workspace
1. search for "myorg" and replace with GitHub org
1. search for "mylang", "Mylang", "MYLANG" and replace with the language/tool your rules are for
1. rename directory "mylang" similarly
1. run `pre-commit install` to get lints (see CONTRIBUTING.md)
1. if you don't need to fetch platform-dependent tools, then remove anything toolchain-related.
1. (optional) install the [Renovate app](https://github.com/apps/renovate) to get auto-PRs to keep the dependencies up-to-date.
1. delete this section of the README (everything up to the SNIP).

---- SNIP ----

# Bazel rules for mylang
Getting reviews from Googlers is hard so let's not wait for them.

## Installation

From the release you wish to use:
<https://github.com/myorg/rules_mylang/releases>
copy the WORKSPACE snippet into your `WORKSPACE` file.

To use a commit rather than a release, you can point at any SHA of the repo.
Make sure your `.bazelrc` contains

For example to use commit `abc123`:
```
# Introduced in Bazel 7.
common --incompatible_enable_proto_toolchain_resolution
```

1. Replace `url = "https://github.com/myorg/rules_mylang/releases/download/v0.1.0/rules_mylang-v0.1.0.tar.gz"` with a GitHub-provided source archive like `url = "https://github.com/myorg/rules_mylang/archive/abc123.tar.gz"`
1. Replace `strip_prefix = "rules_mylang-0.1.0"` with `strip_prefix = "rules_mylang-abc123"`
1. Update the `sha256`. The easiest way to do this is to comment out the line, then Bazel will
print a message with the correct value. Note that GitHub source archives don't have a strong
guarantee on the sha256 stability, see
<https://github.blog/2023-02-21-update-on-the-future-stability-of-source-code-archives-and-hashes/>
Follow instructions from the release you wish to use:
<https://github.com/alexeagle/rules_protoc/releases>
10 changes: 0 additions & 10 deletions docs/BUILD.bazel

This file was deleted.

16 changes: 0 additions & 16 deletions docs/rules.md

This file was deleted.

2 changes: 2 additions & 0 deletions e2e/smoke/.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# The main ingredient: allow us to register toolchains other than com_google_protobuf targets
common --incompatible_enable_proto_toolchain_resolution
19 changes: 4 additions & 15 deletions e2e/smoke/BUILD
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
"""Provides a simple way to test your rules as an external workspace.
Add a basic smoke-test target below.
"""
load("@rules_proto//proto:defs.bzl", "proto_library")

load("@bazel_skylib//rules:build_test.bzl", "build_test")
# load("@com_myorg_rules_mylang//mylang:defs.bzl", "...")

# Replace with a usage of your rule/macro
filegroup(name = "empty")

build_test(
name = "smoke_test",
targets = [
# targets you add above
":empty",
],
proto_library(
name = "foo_proto",
srcs = ["foo.proto"],
)
6 changes: 3 additions & 3 deletions e2e/smoke/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
bazel_dep(name = "com_myorg_rules_mylang", version = "0.0.0", dev_dependency = True)
bazel_dep(name = "bazel_skylib", version = "1.5.0", dev_dependency = True)
bazel_dep(name = "toolchains_protoc", version = "0.0.0")
bazel_dep(name = "rules_proto", version = "6.0.0-rc2")

local_path_override(
module_name = "com_myorg_rules_mylang",
module_name = "toolchains_protoc",
path = "../..",
)
20 changes: 16 additions & 4 deletions e2e/smoke/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# smoke test
# No compiling protoc!

This e2e exercises the repo from an end-users perpective.
It catches mistakes in our install instructions, or usages that fail when called from an "external" repository to rules_mylang.
It is also used by the presubmit check for the Bazel Central Registry.
```
time bazel --output_base=$(mktemp -d) build :all
Starting local Bazel server and connecting to it...
INFO: Analyzed target //:foo_proto (38 packages loaded, 164 targets configured).
INFO: Found 1 target...
Target //:foo_proto up-to-date:
bazel-bin/foo_proto-descriptor-set.proto.bin
INFO: Elapsed time: 1.761s, Critical Path: 0.02s
INFO: 2 processes: 1 internal, 1 linux-sandbox.
INFO: Build completed successfully, 2 total actions
real 0m2.148s
user 0m0.033s
sys 0m0.005s
```
25 changes: 20 additions & 5 deletions e2e/smoke/WORKSPACE.bazel
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
# Override http_archive for local testing
local_repository(
name = "com_myorg_rules_mylang",
name = "toolchains_protoc",
path = "../..",
)

#---SNIP--- Below here is re-used in the workspace snippet published on releases

######################
# rules_mylang setup #
# rules_protoc setup #
######################
# Fetches the rules_mylang dependencies.
# Fetches the rules_protoc dependencies.
# If you want to have a different version of some dependency,
# you should fetch it *before* calling this.
# Alternatively, you can skip calling this function, so long as you've
# already fetched all the dependencies.
load("@com_myorg_rules_mylang//mylang:repositories.bzl", "rules_mylang_dependencies")
load("@toolchains_protoc//protoc:repositories.bzl", "rules_protoc_dependencies")

rules_mylang_dependencies()
rules_protoc_dependencies()

load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies")

rules_proto_dependencies()

load("@bazel_features//:deps.bzl", "bazel_features_deps")

bazel_features_deps()

load("@toolchains_protoc//protoc:toolchain.bzl", "protoc_toolchains")

protoc_toolchains(
name = "protoc_toolchains",
version = "v25.3",
)
5 changes: 5 additions & 0 deletions e2e/smoke/foo.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
syntax = "proto3";

message Foo {
string msg = 1;
}
Loading

0 comments on commit 5647373

Please sign in to comment.