Skip to content

Commit

Permalink
Stardoc generates repository.md (bazel-contrib#1123)
Browse files Browse the repository at this point in the history
* Stardoc generates repository.md

This is a mirror of the content in repository.rst, allowing us to manually inspect the delta between them.
As soon as we are satisfied that the generated content is correct, we'll delete repository.rst and update any links to it.

Technique: copied doc strings out of existing rst file into the starlark files. Transcribed rst syntax to markdown.

* Replace repository.rst content with link to markdown version
  • Loading branch information
alexeagle authored Oct 29, 2021
1 parent aa44ca6 commit 425d85d
Show file tree
Hide file tree
Showing 11 changed files with 708 additions and 471 deletions.
3 changes: 3 additions & 0 deletions .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,8 @@ platforms:
# autogazelle is only supported on UNIX-like platforms.
# It requires UNIX domain sockets.
- "-//cmd/autogazelle/..."
# Stardoc produces different line-endings on windows,
# so the documentation it generates doesn't match the checked in files
- "-//docs:all"
# Fails to execute, apparently due to command-line length limit.
- "-//internal:bazel_test"
2 changes: 1 addition & 1 deletion BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ nogo(
visibility = ["//visibility:public"],
)

exports_files(["WORKSPACE"])
exports_files(["WORKSPACE", "repository.md"])

filegroup(
name = "all_files",
Expand Down
12 changes: 12 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,15 @@ gazelle_dependencies()
# gazelle:repository go_repository name=org_golang_x_text importpath=golang.org/x/text
# gazelle:repository go_repository name=org_golang_x_tools importpath=golang.org/x/tools
# gazelle:repository go_repository name=org_golang_x_xerrors importpath=golang.org/x/xerrors

# For API doc generation
# This is a dev dependency, users should not need to install it
# so we declare it in the WORKSPACE
http_archive(
name = "io_bazel_stardoc",
sha256 = "c9794dcc8026a30ff67cf7cf91ebe245ca294b20b071845d12c192afe243ad72",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/stardoc/releases/download/0.5.0/stardoc-0.5.0.tar.gz",
"https://github.com/bazelbuild/stardoc/releases/download/0.5.0/stardoc-0.5.0.tar.gz",
],
)
54 changes: 54 additions & 0 deletions docs/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"""Documentation generation with stardoc
This is in a separate package from both the stardoc source files and the
resulting documentation markdown files, to prevent users trying to load()
the stardoc repository, which is not a dependency users should install.
"""
load("@bazel_skylib//rules:write_file.bzl", "write_file")
load("@bazel_skylib//rules:diff_test.bzl", "diff_test")
load("@io_bazel_stardoc//stardoc:stardoc.bzl", "stardoc")

_DOC_SRCS = {
"//internal:repository_docs": "repository.md",
}

[
stardoc(
name = out.replace(".md", "_docgen"),
out = out,
# Convention: foo.bzl has bzl_library named "foo"
input = input + ".bzl",
deps = [input],
)
for [input, out] in _DOC_SRCS.items()
]

[
diff_test(
name = "check_" + out,
failure_message = "Please run bazel run //docs:update",
# source file (InputArtifact)
file1 = "//:" + out,
# result from stardoc rule above
file2 = out,
)
for out in _DOC_SRCS.values()
]

write_file(
name = "gen_update",
out = "update.sh",
content = [
"#!/usr/bin/env bash",
"cd $BUILD_WORKSPACE_DIRECTORY",
] + [
"cp -fv bazel-bin/docs/{0} {0}".format(v)
for v in _DOC_SRCS.values()
],
)

sh_binary(
name = "update",
srcs = ["update.sh"],
data = _DOC_SRCS.values(),
)
11 changes: 11 additions & 0 deletions internal/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ go_bazel_test(
exports_files(
[
"gazelle.bash.in",
"repository_docs.bzl",
"list_repository_tools_srcs.go",
"repository_rules_test_errors.patch",
],
Expand Down Expand Up @@ -113,3 +114,13 @@ bzl_library(
srcs = ["overlay_repository.bzl"],
visibility = ["//:__subpackages__"],
)

bzl_library(
name = "repository_docs",
srcs = ["repository_docs.bzl"],
deps = [
":go_repository",
":overlay_repository",
],
visibility = ["//:__subpackages__"],
)
228 changes: 207 additions & 21 deletions internal/go_repository.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,58 @@ load("//internal:common.bzl", "env_execute", "executable_extension")
load("@bazel_gazelle//internal:go_repository_cache.bzl", "read_cache_env")
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "read_netrc", "use_netrc")

_DOC = """
`go_repository` downloads a Go project and generates build files with Gazelle
if they are not already present. This is the simplest way to depend on
external Go projects.
When `go_repository` is in module mode, it saves downloaded modules in a shared,
internal cache within Bazel's cache. It may be cleared with `bazel clean --expunge`.
By setting the environment variable `GO_REPOSITORY_USE_HOST_CACHE=1`, you can
force `go_repository` to use the module cache on the host system in the location
returned by `go env GOPATH`.
**Example**
```starlark
load("@bazel_gazelle//:deps.bzl", "go_repository")
# Download using "go mod download"
go_repository(
name = "com_github_pkg_errors",
importpath = "github.com/pkg/errors",
sum = "h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=",
version = "v0.8.1",
)
# Download automatically via git
go_repository(
name = "com_github_pkg_errors",
commit = "816c9085562cd7ee03e7f8188a1cfd942858cded",
importpath = "github.com/pkg/errors",
)
# Download from git fork
go_repository(
name = "com_github_pkg_errors",
commit = "816c9085562cd7ee03e7f8188a1cfd942858cded",
importpath = "github.com/pkg/errors",
remote = "https://example.com/fork/github.com/pkg/errors",
vcs = "git",
)
# Download via HTTP
go_repository(
name = "com_github_pkg_errors",
importpath = "github.com/pkg/errors",
urls = ["https://codeload.github.com/pkg/errors/zip/816c9085562cd7ee03e7f8188a1cfd942858cded"],
strip_prefix = "errors-816c9085562cd7ee03e7f8188a1cfd942858cded",
type = "zip",
)
```
"""

# We can't disable timeouts on Bazel, but we can set them to large values.
_GO_REPOSITORY_TIMEOUT = 86400

Expand Down Expand Up @@ -214,15 +266,39 @@ def _go_repository_impl(ctx):

go_repository = repository_rule(
implementation = _go_repository_impl,
doc = _DOC,
attrs = {
# Fundamental attributes of a go repository
"importpath": attr.string(mandatory = True),
"importpath": attr.string(
doc = """The Go import path that matches the root directory of this repository.
In module mode (when `version` is set), this must be the module path. If
neither `urls` nor `remote` is specified, `go_repository` will
automatically find the true path of the module, applying import path
redirection.
If build files are generated for this repository, libraries will have their
`importpath` attributes prefixed with this `importpath` string. """,
mandatory = True,
),

# Attributes for a repository that should be checked out from VCS
"commit": attr.string(),
"tag": attr.string(),
"commit": attr.string(
doc = """If the repository is downloaded using a version control tool, this is the
commit or revision to check out. With git, this would be a sha1 commit id.
`commit` and `tag` may not both be set.""",
),
"tag": attr.string(
doc = """If the repository is downloaded using a version control tool, this is the
named revision to check out. `commit` and `tag` may not both be set.""",
),
"vcs": attr.string(
default = "",
doc = """One of `"git"`, `"hg"`, `"svn"`, `"bzr"`.
The version control system to use. This is usually determined automatically,
but it may be necessary to set this when `remote` is set and the VCS cannot
be inferred. You must have the corresponding tool installed on your host.""",
values = [
"",
"git",
Expand All @@ -231,31 +307,96 @@ go_repository = repository_rule(
"bzr",
],
),
"remote": attr.string(),
"remote": attr.string(
doc = """The VCS location where the repository should be downloaded from. This is
usually inferred from `importpath`, but you can set `remote` to download
from a private repository or a fork.""",
),

# Attributes for a repository that should be downloaded via HTTP.
"urls": attr.string_list(),
"strip_prefix": attr.string(),
"type": attr.string(),
"sha256": attr.string(),
"canonical_id": attr.string(),
"urls": attr.string_list(
doc = """A list of HTTP(S) URLs where an archive containing the project can be
downloaded. Bazel will attempt to download from the first URL; the others
are mirrors.""",
),
"strip_prefix": attr.string(
doc = """If the repository is downloaded via HTTP (`urls` is set), this is a
directory prefix to strip. See [`http_archive.strip_prefix`].""",
),
"type": attr.string(
doc = """One of `"zip"`, `"tar.gz"`, `"tgz"`, `"tar.bz2"`, `"tar.xz"`.
If the repository is downloaded via HTTP (`urls` is set), this is the
file format of the repository archive. This is normally inferred from the
downloaded file name.""",
),
"sha256": attr.string(
doc = """If the repository is downloaded via HTTP (`urls` is set), this is the
SHA-256 sum of the downloaded archive. When set, Bazel will verify the archive
against this sum before extracting it.
**CAUTION:** Do not use this with services that prepare source archives on
demand, such as codeload.github.com. Any minor change in the server software
can cause differences in file order, alignment, and compression that break
SHA-256 sums.""",
),
"canonical_id": attr.string(
doc = """If the repository is downloaded via HTTP (`urls` is set) and this is set, restrict cache hits to those cases where the
repository was added to the cache with the same canonical id.""",
),

# Attributes for a module that should be downloaded with the Go toolchain.
"version": attr.string(),
"sum": attr.string(),
"replace": attr.string(),
"version": attr.string(
doc = """If specified, `go_repository` will download the module at this version
using `go mod download`. `sum` must also be set. `commit`, `tag`,
and `urls` may not be set. """,
),
"sum": attr.string(
doc = """A hash of the module contents. In module mode, `go_repository` will verify
the downloaded module matches this sum. May only be set when `version`
is also set.
A value for `sum` may be found in the `go.sum` file or by running
`go mod download -json <module>@<version>`.""",
),
"replace": attr.string(
doc = """A replacement for the module named by `importpath`. The module named by
`replace` will be downloaded at `version` and verified with `sum`.
NOTE: There is no `go_repository` equivalent to file path `replace`
directives. Use `local_repository` instead."""
),

# Attributes for a repository that needs automatic build file generation
"build_external": attr.string(
doc = """One of `"external"`, `"vendored"`.
This sets Gazelle's `-external` command line flag.
**NOTE:** This cannot be used to ignore the `vendor` directory in a
repository. The `-external` flag only controls how Gazelle resolves
imports which are not present in the repository. Use
`build_extra_args = ["-exclude=vendor"]` instead.""",
values = [
"",
"external",
"vendored",
],
),
"build_file_name": attr.string(default = "BUILD.bazel,BUILD"),
"build_file_name": attr.string(
default = "BUILD.bazel,BUILD",
doc = """Comma-separated list of names Gazelle will consider to be build files.
If a repository contains files named `build` that aren't related to Bazel,
it may help to set this to `"BUILD.bazel"`, especially on case-insensitive
file systems.""",
),
"build_file_generation": attr.string(
default = "auto",
doc = """One of `"auto"`, `"on"`, `"off"`.
Whether Gazelle should generate build files in the repository. In `"auto"`
mode, Gazelle will run if there is no build file in the repository root
directory.""",
values = [
"on",
"auto",
Expand All @@ -269,9 +410,20 @@ go_repository = repository_rule(
"import_alias",
],
default = "import_alias",
doc = """Sets the library naming convention to use when resolving dependencies against this external
repository. If unset, the convention from the external workspace is used.
Legal values are `go_default_library`, `import`, and `import_alias`.
See the `gazelle:go_naming_convention` directive in [Directives] for more information.""",
),
"build_tags": attr.string_list(
doc = "This sets Gazelle's `-build_tags` command line flag.",
),
"build_tags": attr.string_list(),
"build_file_proto_mode": attr.string(
doc = """One of `"default"`, `"legacy"`, `"disable"`, `"disable_global"` or `"package"`.
This sets Gazelle's `-proto` command line flag. See Directives_ for more
information on each mode.""",
values = [
"",
"default",
Expand All @@ -281,15 +433,49 @@ go_repository = repository_rule(
"legacy",
],
),
"build_extra_args": attr.string_list(),
"build_config": attr.label(default = "@bazel_gazelle_go_repository_config//:WORKSPACE"),
"build_directives": attr.string_list(default = []),
"build_extra_args": attr.string_list(
doc = "A list of additional command line arguments to pass to Gazelle when generating build files.",
),
"build_config": attr.label(
default = "@bazel_gazelle_go_repository_config//:WORKSPACE",
doc = """A file that Gazelle should read to learn about external repositories before
generating build files. This is useful for dependency resolution. For example,
a `go_repository` rule in this file establishes a mapping between a
repository name like `golang.org/x/tools` and a workspace name like
`org_golang_x_tools`. Workspace directives like
`# gazelle:repository_macro` are recognized.
`go_repository` rules will be re-evaluated when parts of WORKSPACE related
to Gazelle's configuration are changed, including Gazelle directives and
`go_repository` `name` and `importpath` attributes.
Their content should still be fetched from a local cache, but build files
will be regenerated. If this is not desirable, `build_config` may be set
to a less frequently updated file or `None` to disable this functionality.""",
),
"build_directives": attr.string_list(
default = [],
doc = """A list of directives to be written to the root level build file before
Calling Gazelle to generate build files. Each string in the list will be
prefixed with `#` automatically. A common use case is to pass a list of
Gazelle directives.""",
),

# Patches to apply after running gazelle.
"patches": attr.label_list(),
"patch_tool": attr.string(default = "patch"),
"patch_args": attr.string_list(default = ["-p0"]),
"patch_cmds": attr.string_list(default = []),
"patches": attr.label_list(
doc = "A list of patches to apply to the repository after gazelle runs.",
),
"patch_tool": attr.string(
default = "patch",
doc = "The patch tool used to apply `patches`.",
),
"patch_args": attr.string_list(
default = ["-p0"],
doc = "Arguments passed to the patch tool when applying patches.",
),
"patch_cmds": attr.string_list(
default = [],
doc = "Commands to run in the repository after patches are applied.",
),
},
)
"""See repository.rst#go-repository for full documentation."""
Expand Down
Loading

0 comments on commit 425d85d

Please sign in to comment.