Skip to content

Commit 831ef9a

Browse files
authored
fix: markdown/tools:update_markdown_toc (#162)
- Reworked how `ekalinin/github-markdown-toc.go` is referenced and the dependencies are managed. Let Gazelle do the heavy lifting. - Update `//:update_all` to execute `//:gazelle_update_repos` and `//:gazelle`.
1 parent e7c60cd commit 831ef9a

File tree

19 files changed

+184
-162
lines changed

19 files changed

+184
-162
lines changed

BUILD.bazel

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
load("@bazel_gazelle//:def.bzl", "gazelle")
12
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
23
load(
34
"//bzlformat:defs.bzl",
@@ -7,6 +8,18 @@ load(
78
load("//tests:integration_test_common.bzl", "INTEGRATION_TEST_TAGS")
89
load("//updatesrc:defs.bzl", "updatesrc_update_all")
910

11+
# gazelle:prefix github.com/cgrindel/bazel-starlib
12+
gazelle(name = "gazelle")
13+
14+
gazelle(
15+
name = "gazelle_update_repos",
16+
args = [
17+
"-from_file=go.mod",
18+
"-to_macro=go_deps.bzl%bazel_starlib_go_dependencies",
19+
],
20+
command = "update-repos",
21+
)
22+
1023
# MARK: - Bazel Starlark Lint and Formatting
1124

1225
bzlformat_pkg(name = "bzlformat")
@@ -22,9 +35,16 @@ updatesrc_update_all(
2235
targets_to_run = [
2336
"@contrib_rules_bazel_integration_test//tools:update_deleted_packages",
2437
":bzlformat_missing_pkgs_fix",
38+
":gazelle_update_repos",
39+
":gazelle",
2540
],
2641
)
2742

43+
alias(
44+
name = "tidy",
45+
actual = ":update_all",
46+
)
47+
2848
# MARK: - Markdown Files
2949

3050
package_group(

WORKSPACE

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,20 @@ load("//:deps.bzl", "bazel_starlib_dependencies")
44

55
bazel_starlib_dependencies()
66

7+
# gazelle:repo bazel_gazelle
8+
load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")
9+
load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
10+
load("//:go_deps.bzl", "bazel_starlib_go_dependencies")
11+
12+
# gazelle:repository_macro go_deps.bzl%bazel_starlib_go_dependencies
13+
bazel_starlib_go_dependencies()
14+
15+
go_rules_dependencies()
16+
17+
go_register_toolchains(version = "1.19.1")
18+
19+
gazelle_dependencies()
20+
721
load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")
822

923
bazel_skylib_workspace()
@@ -35,20 +49,6 @@ buildifier_prebuilt_register_toolchains()
3549

3650
# markdown_register_node_deps()
3751

38-
# MARK: - Golang Deps (gh-md-toc)
39-
40-
load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")
41-
load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
42-
load("//markdown:deps.bzl", "bazel_starlib_markdown_dependencies")
43-
44-
bazel_starlib_markdown_dependencies()
45-
46-
go_rules_dependencies()
47-
48-
go_register_toolchains(version = "1.17.6")
49-
50-
gazelle_dependencies()
51-
5252
# MARK: - Integration Testing
5353

5454
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

bazel_versions.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Bazel Version Declarations"""
22

3-
CURRENT_BAZEL_VERSION = "5.1.1"
3+
CURRENT_BAZEL_VERSION = "//:.bazelversion"
44

55
OTHER_BAZEL_VERSIONS = [
66
"4.2.2",

cmd/go_deps/BUILD.bazel

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
load("@cgrindel_bazel_starlib//bzlformat:defs.bzl", "bzlformat_pkg")
2+
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
3+
4+
go_library(
5+
name = "go_deps_lib",
6+
srcs = ["main.go"],
7+
importpath = "github.com/cgrindel/bazel-starlib/cmd/go_deps",
8+
visibility = ["//visibility:private"],
9+
deps = ["@com_github_ekalinin_github_markdown_toc_go//:github-markdown-toc_go"],
10+
)
11+
12+
go_binary(
13+
name = "go_deps",
14+
embed = [":go_deps_lib"],
15+
visibility = ["//visibility:public"],
16+
)
17+
18+
bzlformat_pkg(name = "bzlformat")

cmd/go_deps/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Golang Dependencies
2+
3+
The `bazel-starlib` repository uses Golang modules that contain exeutable packages (e.g., binaries).
4+
To ensure that these binary targets are downloaded and built properly, a simple Golang program
5+
exists to reference the required packages. [Gazelle](https://github.com/bazelbuild/bazel-gazelle) is
6+
then used to identify the transitive dependencies and saves them to `go_deps.bzl`.
7+
8+
## To Add a Golang Dependency
9+
10+
Update the `main.go` in this directory to depend upon the desired Golang package. Be sure to use the
11+
package in some way. Otherwise, `go mod tidy` will remove it.
12+
13+
Execute the following to update the go module files, resolve the Golang dependencies and update the
14+
Bazel build files.
15+
16+
```sh
17+
# bazel run @go_sdk//:bin/go -- mod tidy
18+
$ bazel run //:gazelle_update_repos
19+
$ bazel run //:gazelle
20+
```
21+
22+
Reference the Golang binary target.

cmd/go_deps/main.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
gmt "github.com/ekalinin/github-markdown-toc.go"
8+
)
9+
10+
func main() {
11+
fmt.Println("Hello, world.")
12+
13+
ghtoc := generateToc()
14+
fmt.Printf("TOC:\n%s", ghtoc)
15+
}
16+
17+
// Use the markdown package so that it is a dependency.
18+
func generateToc() string {
19+
doc := gmt.NewGHDoc("", false, 0, 0, true, "", 2, false)
20+
toc := *doc.GrabToc()
21+
return strings.Join(toc, "\n")
22+
}

deps.bzl

Lines changed: 21 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -15,57 +15,6 @@ def _bazeldoc_dependencies():
1515
],
1616
)
1717

18-
def _markdown_dependencies():
19-
# GH140: Temporarily disable markdown while adding support for
20-
# --incompatible_disallow_empty_glob.
21-
22-
# maybe(
23-
# http_archive,
24-
# name = "build_bazel_rules_nodejs",
25-
# sha256 = "e328cb2c9401be495fa7d79c306f5ee3040e8a03b2ebb79b022e15ca03770096",
26-
# urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/5.4.2/rules_nodejs-5.4.2.tar.gz"],
27-
# )
28-
29-
maybe(
30-
http_archive,
31-
name = "io_bazel_rules_go",
32-
sha256 = "099a9fb96a376ccbbb7d291ed4ecbdfd42f6bc822ab77ae6f1b5cb9e914e94fa",
33-
urls = [
34-
"https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.35.0/rules_go-v0.35.0.zip",
35-
"https://github.com/bazelbuild/rules_go/releases/download/v0.35.0/rules_go-v0.35.0.zip",
36-
],
37-
)
38-
39-
maybe(
40-
http_archive,
41-
name = "bazel_gazelle",
42-
sha256 = "de69a09dc70417580aabf20a28619bb3ef60d038470c7cf8442fafcf627c21cb",
43-
urls = [
44-
"https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.24.0/bazel-gazelle-v0.24.0.tar.gz",
45-
"https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.24.0/bazel-gazelle-v0.24.0.tar.gz",
46-
],
47-
)
48-
49-
maybe(
50-
http_archive,
51-
name = "ekalinin_github_markdown_toc",
52-
sha256 = "6bfeab2b28e5c7ad1d5bee9aa6923882a01f56a7f2d0f260f01acde2111f65af",
53-
strip_prefix = "github-markdown-toc.go-1.2.0",
54-
urls = ["https://github.com/ekalinin/github-markdown-toc.go/archive/refs/tags/1.2.0.tar.gz"],
55-
build_file_content = """\
56-
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
57-
58-
go_binary(
59-
name = "gh_md_toc",
60-
srcs = glob(["*.go"], exclude = ["*_test.go"]),
61-
deps = [
62-
"@in_gopkg_alecthomas_kingpin_v2//:go_default_library",
63-
],
64-
visibility = ["//visibility:public"],
65-
)
66-
""",
67-
)
68-
6918
def _prebuilt_buildtools_dependencies():
7019
maybe(
7120
http_archive,
@@ -86,26 +35,6 @@ def _prebuilt_buildtools_dependencies():
8635
)
8736

8837
def _compile_from_source_buildtools_dependencies():
89-
maybe(
90-
http_archive,
91-
name = "io_bazel_rules_go",
92-
sha256 = "099a9fb96a376ccbbb7d291ed4ecbdfd42f6bc822ab77ae6f1b5cb9e914e94fa",
93-
urls = [
94-
"https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.35.0/rules_go-v0.35.0.zip",
95-
"https://github.com/bazelbuild/rules_go/releases/download/v0.35.0/rules_go-v0.35.0.zip",
96-
],
97-
)
98-
99-
maybe(
100-
http_archive,
101-
name = "bazel_gazelle",
102-
sha256 = "de69a09dc70417580aabf20a28619bb3ef60d038470c7cf8442fafcf627c21cb",
103-
urls = [
104-
"https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.24.0/bazel-gazelle-v0.24.0.tar.gz",
105-
"https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.24.0/bazel-gazelle-v0.24.0.tar.gz",
106-
],
107-
)
108-
10938
maybe(
11039
http_archive,
11140
name = "com_google_protobuf",
@@ -136,17 +65,14 @@ def _compile_from_source_buildtools_dependencies():
13665

13766
def bazel_starlib_dependencies(
13867
use_prebuilt_buildtools = True,
139-
use_bazeldoc = True,
140-
use_markdown = True):
68+
use_bazeldoc = True):
14169
"""Declares the dependencies for bazel-starlib.
14270
14371
Args:
14472
use_prebuilt_buildtools: A `bool` specifying whether to use a prebuilt
14573
version of `bazelbuild/buildtools`.
14674
use_bazeldoc: A `bool` specifying whether the `bazeldoc` dependencies
14775
should be loaded.
148-
use_markdown: A `bool` specifying whether the `markdown` depdendencies
149-
should be loaded.
15076
"""
15177
maybe(
15278
http_archive,
@@ -158,12 +84,29 @@ def bazel_starlib_dependencies(
15884
sha256 = "74d544d96f4a5bb630d465ca8bbcfe231e3594e5aae57e1edbf17a6eb3ca2506",
15985
)
16086

87+
maybe(
88+
http_archive,
89+
name = "io_bazel_rules_go",
90+
sha256 = "099a9fb96a376ccbbb7d291ed4ecbdfd42f6bc822ab77ae6f1b5cb9e914e94fa",
91+
urls = [
92+
"https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.35.0/rules_go-v0.35.0.zip",
93+
"https://github.com/bazelbuild/rules_go/releases/download/v0.35.0/rules_go-v0.35.0.zip",
94+
],
95+
)
96+
97+
maybe(
98+
http_archive,
99+
name = "bazel_gazelle",
100+
sha256 = "efbbba6ac1a4fd342d5122cbdfdb82aeb2cf2862e35022c752eaddffada7c3f3",
101+
urls = [
102+
"https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.27.0/bazel-gazelle-v0.27.0.tar.gz",
103+
"https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.27.0/bazel-gazelle-v0.27.0.tar.gz",
104+
],
105+
)
106+
161107
if use_bazeldoc:
162108
_bazeldoc_dependencies()
163109

164-
if use_markdown:
165-
_markdown_dependencies()
166-
167110
if use_prebuilt_buildtools:
168111
_prebuilt_buildtools_dependencies()
169112
else:

examples/bzlformat/build_buildtools/WORKSPACE

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ load("@cgrindel_bazel_starlib//:deps.bzl", "bazel_starlib_dependencies")
1010
# Be sure to specify that you do not want to use the prebuilt tools
1111
bazel_starlib_dependencies(
1212
use_bazeldoc = False,
13-
use_markdown = False,
1413
use_prebuilt_buildtools = False,
1514
)
1615

examples/bzlformat/simple/WORKSPACE

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@ local_repository(
77

88
load("@cgrindel_bazel_starlib//:deps.bzl", "bazel_starlib_dependencies")
99

10-
bazel_starlib_dependencies(
11-
use_bazeldoc = False,
12-
use_markdown = False,
13-
)
10+
bazel_starlib_dependencies(use_bazeldoc = False)
1411

1512
load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")
1613

examples/markdown/simple/WORKSPACE

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@ local_repository(
77

88
load("@cgrindel_bazel_starlib//:deps.bzl", "bazel_starlib_dependencies")
99

10-
bazel_starlib_dependencies(
11-
use_bazeldoc = False,
12-
use_markdown = True,
13-
)
10+
bazel_starlib_dependencies(use_bazeldoc = False)
1411

1512
load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")
1613

0 commit comments

Comments
 (0)