Skip to content

Commit e1e0bc2

Browse files
committed
Add support for Bazel
We are using Gazelle to auto-generate and update our BUILD files since we're using Go. Also add targets to Makefile to easily update WORKSPACE or BUILD files in response to changes in Go deps or imported repos.
1 parent b606176 commit e1e0bc2

File tree

10 files changed

+245
-1
lines changed

10 files changed

+245
-1
lines changed

.github/workflows/main.yml

+37
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,43 @@ jobs:
4242
- name: Run tests
4343
run: make test VERBOSE=1
4444

45+
server-bazel:
46+
runs-on: ${{ matrix.os }}
47+
strategy:
48+
matrix:
49+
os: [ 'macos-10.15', 'ubuntu-20.04' ]
50+
bazel: [ '4.1.0' ]
51+
go: [ '1.16' ]
52+
name: Bazel ${{ matrix.bazel }} and Go ${{ matrix.go }} on ${{ matrix.os }}
53+
steps:
54+
- name: Checkout repo
55+
uses: actions/checkout@v2
56+
57+
- name: Setup Go
58+
uses: actions/setup-go@v2
59+
with:
60+
go-version: ${{ matrix.go }}
61+
62+
- name: Setup cache
63+
uses: actions/cache@v2
64+
with:
65+
path: "~/.cache/bazel"
66+
key: bazel
67+
68+
- name: Setup Bazelisk
69+
uses: bazelbuild/setup-bazelisk@v1
70+
env:
71+
USE_BAZEL_VERSION: ${{ matrix.bazel }}
72+
73+
- name: Verify Bazel installation
74+
run: bazel version
75+
76+
- name: Build
77+
run: bazel build //...
78+
79+
- name: Run tests
80+
run: bazel test //...
81+
4582
ui:
4683
runs-on: ubuntu-20.04
4784
strategy:

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@ web/server/server
1010
web/ui/build/
1111
web/ui/node_modules/
1212
web/ui/size-plugin.json
13+
14+
# Bazel outputs
15+
bazel-*

.vscode/settings.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
"web/ui/build": true,
1111
"web/ui/node_modules": true,
12-
"web/ui/size-plugin.json": true
12+
"web/ui/size-plugin.json": true,
13+
14+
"bazel-*": true
1315
}
1416
}

BUILD

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Copyright 2021 Google LLC
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+
load("@bazel_gazelle//:def.bzl", "gazelle")
16+
17+
# gazelle:prefix github.com/mbrukman/notebook
18+
gazelle(name = "gazelle")

Makefile

+6
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,10 @@ govet:
3535
$(VERB) echo "Running 'go vet' ..."
3636
$(VERB) ./go_vet_test.sh
3737

38+
go-update-workspace:
39+
$(VERB) bazel run //:gazelle -- update-repos -from_file=go.mod
40+
41+
go-update-build:
42+
$(VERB) bazel run //:gazelle -- -build_file_name BUILD
43+
3844
test: gofmt_test go_mod_tidy_test go-test govet

WORKSPACE

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Copyright 2021 Google LLC
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+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
16+
17+
http_archive(
18+
name = "io_bazel_rules_go",
19+
sha256 = "8e968b5fcea1d2d64071872b12737bbb5514524ee5f0a4f54f5920266c261acb",
20+
urls = [
21+
"https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.28.0/rules_go-v0.28.0.zip",
22+
"https://github.com/bazelbuild/rules_go/releases/download/v0.28.0/rules_go-v0.28.0.zip",
23+
],
24+
)
25+
26+
http_archive(
27+
name = "bazel_gazelle",
28+
sha256 = "62ca106be173579c0a167deb23358fdfe71ffa1e4cfdddf5582af26520f1c66f",
29+
urls = [
30+
"https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.23.0/bazel-gazelle-v0.23.0.tar.gz",
31+
"https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.23.0/bazel-gazelle-v0.23.0.tar.gz",
32+
],
33+
)
34+
35+
load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
36+
load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies", "go_repository")
37+
38+
go_repository(
39+
name = "com_github_ghodss_yaml",
40+
importpath = "github.com/ghodss/yaml",
41+
sum = "h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=",
42+
version = "v1.0.0",
43+
)
44+
45+
go_repository(
46+
name = "in_gopkg_check_v1",
47+
importpath = "gopkg.in/check.v1",
48+
sum = "h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=",
49+
version = "v0.0.0-20161208181325-20d25e280405",
50+
)
51+
52+
go_repository(
53+
name = "in_gopkg_yaml_v2",
54+
importpath = "gopkg.in/yaml.v2",
55+
sum = "h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=",
56+
version = "v2.4.0",
57+
)
58+
59+
go_rules_dependencies()
60+
61+
go_register_toolchains(version = "1.16.5")
62+
63+
gazelle_dependencies()

web/proxy/BUILD

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copyright 2021 Google LLC
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+
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
16+
17+
go_library(
18+
name = "proxy_lib",
19+
srcs = ["proxy.go"],
20+
importpath = "github.com/mbrukman/notebook/web/proxy",
21+
visibility = ["//visibility:private"],
22+
deps = ["@com_github_ghodss_yaml//:go_default_library"],
23+
)
24+
25+
go_binary(
26+
name = "proxy",
27+
embed = [":proxy_lib"],
28+
visibility = ["//visibility:public"],
29+
)

web/server/BUILD

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Copyright 2021 Google LLC
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+
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
16+
17+
go_library(
18+
name = "server_lib",
19+
srcs = ["server.go"],
20+
importpath = "github.com/mbrukman/notebook/web/server",
21+
visibility = ["//visibility:private"],
22+
deps = [
23+
"//web/server/api",
24+
"//web/server/db",
25+
],
26+
)
27+
28+
go_binary(
29+
name = "server",
30+
embed = [":server_lib"],
31+
visibility = ["//visibility:public"],
32+
)

web/server/api/BUILD

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copyright 2021 Google LLC
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+
load("@io_bazel_rules_go//go:def.bzl", "go_library")
16+
17+
go_library(
18+
name = "api",
19+
srcs = ["api.go"],
20+
importpath = "github.com/mbrukman/notebook/web/server/api",
21+
visibility = ["//visibility:public"],
22+
deps = ["//web/server/db"],
23+
)

web/server/db/BUILD

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Copyright 2021 Google LLC
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+
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
16+
17+
go_library(
18+
name = "db",
19+
srcs = [
20+
"db.go",
21+
"inmem.go",
22+
],
23+
importpath = "github.com/mbrukman/notebook/web/server/db",
24+
visibility = ["//visibility:public"],
25+
)
26+
27+
go_test(
28+
name = "db_test",
29+
srcs = ["inmem_test.go"],
30+
embed = [":db"],
31+
)

0 commit comments

Comments
 (0)