Bazelify the PolyGerrit dev server

Formerly, developers were directed to install Go and a specific
library dependency on their workstation. We can pull this all into bazel
using bazelbuild/rules_go and bazelbuild/bazel-gazelle.

Now, instead of running a script from a specific directory, the dev
server can be started with a simple "bazel run" command. Or, continue
using the script, which automatically releases the bazel lock before
starting the server.

Change-Id: I9dbda32a8c8698b3f43d02b2133d3cecbffaf1c3
This commit is contained in:
Logan Hanks 2018-09-12 14:35:10 -07:00 committed by David Ostrovsky
parent 60f59f1cdf
commit 784849c2ca
7 changed files with 126 additions and 52 deletions

View File

@ -1,5 +1,6 @@
workspace(name = "gerrit")
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive", "http_file")
load("//tools/bzl:maven_jar.bzl", "GERRIT", "MAVEN_LOCAL", "maven_jar")
load("//lib/codemirror:cm.bzl", "CM_VERSION", "DIFF_MATCH_PATCH_VERSION")
@ -43,6 +44,42 @@ closure_repositories(
omit_javax_inject = True,
)
# Golang support for PolyGerrit local dev server.
http_archive(
name = "io_bazel_rules_go",
sha256 = "97cf62bdef33519412167fd1e4b0810a318a7c234f5f8dc4f53e2da86241c492",
urls = ["https://github.com/bazelbuild/rules_go/releases/download/0.15.3/rules_go-0.15.3.tar.gz"],
)
load("@io_bazel_rules_go//go:def.bzl", "go_register_toolchains", "go_rules_dependencies")
go_rules_dependencies()
go_register_toolchains()
http_archive(
name = "bazel_gazelle",
sha256 = "c0a5739d12c6d05b6c1ad56f2200cb0b57c5a70e03ebd2f7b87ce88cabf09c7b",
urls = ["https://github.com/bazelbuild/bazel-gazelle/releases/download/0.14.0/bazel-gazelle-0.14.0.tar.gz"],
)
load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies", "go_repository")
gazelle_dependencies()
# Dependencies for PolyGerrit local dev server.
go_repository(
name = "com_github_robfig_soy",
commit = "82face14ebc0883b4ca9c901b5aaf3738b9f6a24",
importpath = "github.com/robfig/soy",
)
go_repository(
name = "com_github_howeyc_fsnotify",
commit = "441bbc86b167f3c1f4786afae9931403b99fdacf",
importpath = "github.com/howeyc/fsnotify",
)
ANTLR_VERS = "3.5.2"
maven_jar(

View File

@ -2,7 +2,10 @@ java_library(
name = "httpd",
srcs = glob(["**/*.java"]),
resource_strip_prefix = "resources",
resources = ["//resources/com/google/gerrit/httpd"],
resources = [
"//resources/com/google/gerrit/httpd",
"//resources/com/google/gerrit/httpd/raw",
],
visibility = ["//visibility:public"],
deps = [
"//java/com/google/gerrit/common:annotations",

View File

@ -2,6 +2,7 @@ package(
default_visibility = ["//visibility:public"],
)
load("@io_bazel_rules_go//go:def.bzl", "go_binary")
load("//tools/bzl:js.bzl", "bower_component_bundle")
load("//tools/bzl:genrule2.bzl", "genrule2")
@ -52,3 +53,19 @@ genrule2(
output_to_bindir = 1,
visibility = ["//visibility:public"],
)
go_binary(
name = "devserver",
srcs = ["server.go"],
data = [
":fonts.zip",
"//polygerrit-ui/app:test_components.zip",
"//resources/com/google/gerrit/httpd/raw",
],
deps = [
"@com_github_robfig_soy//:go_default_library",
"@com_github_robfig_soy//soyhtml:go_default_library",
"@org_golang_x_tools//godoc/vfs/httpfs:go_default_library",
"@org_golang_x_tools//godoc/vfs/zipfs:go_default_library",
],
)

View File

@ -48,32 +48,6 @@ This is a quick and easy way to test your local changes against real data.
Unfortunately, you can't sign in, so testing certain features will require
you to use the "test data" technique described below.
### Installing [go](https://golang.org/)
This is required for running the `run-server.sh` script below.
```sh
# Debian/Ubuntu
sudo apt-get install golang
# OS X with Homebrew
brew install go
```
All other platforms: [download from golang.org](https://golang.org/)
Then add go to your path:
```
PATH=$PATH:/usr/local/go/bin
```
Install the go Soy template library:
```
go get "github.com/robfig/soy"
```
### Running the server
To test the local UI against gerrit-review.googlesource.com:

View File

@ -14,23 +14,7 @@
# limitations under the License.
set -eu
while [[ ! -f WORKSPACE && "$PWD" != / ]]; do
cd ..
done
if [[ ! -f WORKSPACE ]]; then
echo "$(basename "$0"): must be run from a gerrit checkout" 1>&2
exit 1
fi
bazel build \
//polygerrit-ui/app:test_components \
//polygerrit-ui:fonts.zip
cd polygerrit-ui/app
rm -rf bower_components
unzip -q ../../bazel-bin/polygerrit-ui/app/test_components.zip
rm -rf fonts
unzip -q ../../bazel-bin/polygerrit-ui/fonts.zip -d fonts
cd ..
exec go run server.go "$@"
SCRIPTNAME=$(mktemp)
trap "{ rm -f $SCRIPTNAME; }" EXIT
bazel run --script_path="$SCRIPTNAME" //polygerrit-ui:devserver
"$SCRIPTNAME" "$@"

View File

@ -15,6 +15,7 @@
package main
import (
"archive/zip"
"bufio"
"compress/gzip"
"encoding/json"
@ -26,27 +27,50 @@ import (
"net"
"net/http"
"net/url"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/robfig/soy"
"github.com/robfig/soy/soyhtml"
"golang.org/x/tools/godoc/vfs/httpfs"
"golang.org/x/tools/godoc/vfs/zipfs"
)
var (
restHost = flag.String("host", "gerrit-review.googlesource.com", "Host to proxy requests to")
plugins = flag.String("plugins", "", "comma seperated plugin paths to serve")
port = flag.String("port", ":8081", "Port to serve HTTP requests on")
prod = flag.Bool("prod", false, "Serve production assets")
restHost = flag.String("host", "gerrit-review.googlesource.com", "Host to proxy requests to")
scheme = flag.String("scheme", "https", "URL scheme")
plugins = flag.String("plugins", "", "comma seperated plugin paths to serve")
tofu, _ = soy.NewBundle().
AddTemplateFile("../resources/com/google/gerrit/httpd/raw/PolyGerritIndexHtml.soy").
CompileToTofu()
tofu *soyhtml.Tofu
)
func main() {
flag.Parse()
fontsArchive, err := openDataArchive("fonts.zip")
if err != nil {
log.Fatal(err)
}
componentsArchive, err := openDataArchive("app/test_components.zip")
if err != nil {
log.Fatal(err)
}
tofu, err = resolveIndexTemplate()
if err != nil {
log.Fatal(err)
}
workspace := os.Getenv("BUILD_WORKSPACE_DIRECTORY")
if err := os.Chdir(filepath.Join(workspace, "polygerrit-ui")); err != nil {
log.Fatal(err)
}
http.HandleFunc("/index.html", handleIndex)
if *prod {
@ -55,6 +79,11 @@ func main() {
http.Handle("/", http.FileServer(http.Dir("app")))
}
http.Handle("/bower_components/",
http.FileServer(httpfs.New(zipfs.New(componentsArchive, "bower_components"))))
http.Handle("/fonts/",
http.FileServer(httpfs.New(zipfs.New(fontsArchive, "fonts"))))
http.HandleFunc("/changes/", handleRESTProxy)
http.HandleFunc("/accounts/", handleRESTProxy)
http.HandleFunc("/config/", handleRESTProxy)
@ -71,6 +100,28 @@ func main() {
log.Fatal(http.ListenAndServe(*port, &server{}))
}
func resolveIndexTemplate() (*soyhtml.Tofu, error) {
basePath, err := resourceBasePath()
if err != nil {
return nil, err
}
return soy.NewBundle().
AddTemplateFile(basePath + ".runfiles/gerrit/resources/com/google/gerrit/httpd/raw/PolyGerritIndexHtml.soy").
CompileToTofu()
}
func openDataArchive(path string) (*zip.ReadCloser, error) {
absBinPath, err := resourceBasePath()
if err != nil {
return nil, err
}
return zip.OpenReader(absBinPath + ".runfiles/gerrit/polygerrit-ui/" + path)
}
func resourceBasePath() (string, error) {
return filepath.Abs(os.Args[0])
}
func handleIndex(w http.ResponseWriter, r *http.Request) {
var obj = map[string]interface{}{
"canonicalPath": "",

View File

@ -0,0 +1,8 @@
filegroup(
name = "raw",
srcs = glob(
["**/*"],
exclude = ["BUILD"],
),
visibility = ["//visibility:public"],
)