From f1d4cf26b1c6f5c8ddacf45e939060a1e8c1970c Mon Sep 17 00:00:00 2001 From: Marc Fisher Date: Wed, 8 Feb 2017 14:13:33 -0800 Subject: [PATCH] Add go_repository rule for importing go packages. - Remove redundancy from go import paths. --- build_files/com_github_gorilla_mux.BUILD | 40 ------- build_files/com_github_tebeka_selenium.BUILD | 37 ------- go/launcher/proxy/driver_hub.go | 2 +- go/launcher/proxy/driver_session.go | 2 +- go/launcher/proxy/quit_handler_test.go | 2 +- go/launcher/proxy/screenshot_test.go | 2 +- go/screenshotter/screenshotter.go | 2 +- go/screenshotter/screenshotter_test.go | 2 +- go/webtest.go | 2 +- go/webtest_test.go | 2 +- web/internal/go_repository.bzl | 103 +++++++++++++++++++ web/repositories.bzl | 13 ++- 12 files changed, 119 insertions(+), 90 deletions(-) delete mode 100644 build_files/com_github_gorilla_mux.BUILD delete mode 100644 build_files/com_github_tebeka_selenium.BUILD create mode 100644 web/internal/go_repository.bzl diff --git a/build_files/com_github_gorilla_mux.BUILD b/build_files/com_github_gorilla_mux.BUILD deleted file mode 100644 index 9271812a..00000000 --- a/build_files/com_github_gorilla_mux.BUILD +++ /dev/null @@ -1,40 +0,0 @@ -# Copyright 2016 Google Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -################################################################################ -# -licenses(["notice"]) - -exports_files(["LICENSE"]) - -load("@io_bazel_rules_go//go:def.bzl", "go_prefix", "go_library") - -go_prefix("github.com/gorilla/mux") - -go_library( - name = "mux", - srcs = [ - "context_native.go", - "doc.go", - "mux.go", - "regexp.go", - "route.go", - ], -) - -alias( - name = "com_github_gorilla_mux", - actual = ":mux", - visibility = ["//visibility:public"], -) diff --git a/build_files/com_github_tebeka_selenium.BUILD b/build_files/com_github_tebeka_selenium.BUILD deleted file mode 100644 index df7f64fd..00000000 --- a/build_files/com_github_tebeka_selenium.BUILD +++ /dev/null @@ -1,37 +0,0 @@ -# Copyright 2016 Google Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -################################################################################ -# -licenses(["notice"]) # MIT. - -exports_files(["LICENSE"]) - -load("@io_bazel_rules_go//go:def.bzl", "go_prefix", "go_library") - -go_prefix("github.com/tebeka/selenium") - -go_library( - name = "selenium", - srcs = glob( - ["*.go"], - exclude = ["*_test.go"], - ), -) - -alias( - name = "com_github_tebeka_selenium", - actual = ":selenium", - visibility = ["//visibility:public"], -) diff --git a/go/launcher/proxy/driver_hub.go b/go/launcher/proxy/driver_hub.go index 705862d3..41d75e20 100644 --- a/go/launcher/proxy/driver_hub.go +++ b/go/launcher/proxy/driver_hub.go @@ -31,7 +31,7 @@ import ( "github.com/bazelbuild/rules_webtesting/go/launcher/proxy/webdriver" "github.com/bazelbuild/rules_webtesting/go/metadata/metadata" "github.com/bazelbuild/rules_webtesting/go/util/httphelper" - "github.com/gorilla/mux/mux" + "github.com/gorilla/mux" ) const envTimeout = 5 * time.Minute // some environments such as Android take a long time to start up. diff --git a/go/launcher/proxy/driver_session.go b/go/launcher/proxy/driver_session.go index 75c8621a..80bbb6d4 100644 --- a/go/launcher/proxy/driver_session.go +++ b/go/launcher/proxy/driver_session.go @@ -26,7 +26,7 @@ import ( "github.com/bazelbuild/rules_webtesting/go/launcher/errors" "github.com/bazelbuild/rules_webtesting/go/launcher/proxy/webdriver" - "github.com/gorilla/mux/mux" + "github.com/gorilla/mux" ) // WebDriverSession is an http.Handler for forwarding requests to a WebDriver session. diff --git a/go/launcher/proxy/quit_handler_test.go b/go/launcher/proxy/quit_handler_test.go index 09054a0b..86225b0a 100644 --- a/go/launcher/proxy/quit_handler_test.go +++ b/go/launcher/proxy/quit_handler_test.go @@ -20,7 +20,7 @@ import ( "github.com/bazelbuild/rules_webtesting/go/util/bazel" "github.com/bazelbuild/rules_webtesting/go/webtest" - "github.com/tebeka/selenium/selenium" + "github.com/tebeka/selenium" ) func TestCloseOneWindow(t *testing.T) { diff --git a/go/launcher/proxy/screenshot_test.go b/go/launcher/proxy/screenshot_test.go index a6bffc0e..08065704 100644 --- a/go/launcher/proxy/screenshot_test.go +++ b/go/launcher/proxy/screenshot_test.go @@ -23,7 +23,7 @@ import ( "github.com/bazelbuild/rules_webtesting/go/util/bazel" "github.com/bazelbuild/rules_webtesting/go/webtest" - "github.com/tebeka/selenium/selenium" + "github.com/tebeka/selenium" ) func TestScreenshot(t *testing.T) { diff --git a/go/screenshotter/screenshotter.go b/go/screenshotter/screenshotter.go index 3db3a047..f536882e 100644 --- a/go/screenshotter/screenshotter.go +++ b/go/screenshotter/screenshotter.go @@ -29,7 +29,7 @@ import ( "os" "strings" - "github.com/tebeka/selenium/selenium" + "github.com/tebeka/selenium" ) // Screenshotter allows configurable screenshotting. diff --git a/go/screenshotter/screenshotter_test.go b/go/screenshotter/screenshotter_test.go index 3d233632..0056af8b 100644 --- a/go/screenshotter/screenshotter_test.go +++ b/go/screenshotter/screenshotter_test.go @@ -24,7 +24,7 @@ import ( "github.com/bazelbuild/rules_webtesting/go/util/bazel" "github.com/bazelbuild/rules_webtesting/go/webtest" - "github.com/tebeka/selenium/selenium" + "github.com/tebeka/selenium" ) func TestOfElement(t *testing.T) { diff --git a/go/webtest.go b/go/webtest.go index f1f4e057..ce11394a 100644 --- a/go/webtest.go +++ b/go/webtest.go @@ -36,7 +36,7 @@ import ( "github.com/bazelbuild/rules_webtesting/go/metadata/metadata" "github.com/bazelbuild/rules_webtesting/go/util/bazel" - "github.com/tebeka/selenium/selenium" + "github.com/tebeka/selenium" ) var info *BrowserInfo diff --git a/go/webtest_test.go b/go/webtest_test.go index 93e7bef0..4e5cc08b 100644 --- a/go/webtest_test.go +++ b/go/webtest_test.go @@ -18,7 +18,7 @@ import ( "strings" "testing" - "github.com/tebeka/selenium/selenium" + "github.com/tebeka/selenium" ) func TestProvisionBrowser_NoCaps(t *testing.T) { diff --git a/web/internal/go_repository.bzl b/web/internal/go_repository.bzl new file mode 100644 index 00000000..26add336 --- /dev/null +++ b/web/internal/go_repository.bzl @@ -0,0 +1,103 @@ +# Copyright 2016 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +"""Defines a go_repository rule that downloads and sets up a go package.""" + + +def go_repository(name, + urls, + sha256, + license, + import_name, + strip_prefix=None, + excluded_srcs=None, + license_file="LICENSE", + deps=None, + testonly=None): + """go_repository downloads a go package and makes it available for import. + + The bazel dep for the go package is "@name". + The go import for the go package is "import_name". + + Args: + name: Name of the repository rule. BUILD files can depend on the package + as "@name" + urls: A list of URLs where the package can be downloaded from. + sha256: SHA256 for downloaded file. + license: A string indicating the licenses for the package. + e.g. "licenses([\"notice\"]) # MIT.". + import_name: The value for go_prefix. Go files can import this package + as import "import_name". + strip_prefix: Any directories to strip of the prefix when extracting the + downloaded file. + excluded_srcs: Any .go file in the package that should be excluded. + **/*test.go is always excluded. + license_file: The name of the license file. By default "LICENSE". + deps: Any dependencies for the go package. + testonly: Whether the rules should be marked testonly. + """ + if deps: + deps_str = '"' + '",\n "'.join(deps) + '"' + else: + deps_str = '' + + if excluded_srcs: + excludes_str = '"' + '",\n "'.join(excluded_srcs) + '",' + else: + excludes_str = '' + + build_file = """ +load("@io_bazel_rules_go//go:def.bzl", "go_prefix", "go_library") + +{license} + +exports_files(["{license_file}"]) + +go_prefix("{import_name}") + +go_library( + name = "go_default_library", + srcs = glob( + ["*.go"], + exclude = [ + "*test.go", + {excludes} + ], + ), + testonly = {testonly}, + deps = [ + {deps} + ], +) + +alias( + name = "{name}", + actual = ":go_default_library", + testonly = {testonly}, + visibility = ["//visibility:public"], +) +""".format( + name=name, + license=license, + license_file=license_file, + import_name=import_name, + testonly=testonly, + deps=deps_str, + excludes=excludes_str) + + native.new_http_archive( + name=name, + build_file_content=build_file, + sha256=sha256, + urls=urls, + strip_prefix=strip_prefix) diff --git a/web/repositories.bzl b/web/repositories.bzl index a5d160a8..a3ef08d9 100644 --- a/web/repositories.bzl +++ b/web/repositories.bzl @@ -15,7 +15,7 @@ load("//web/internal:java_import_external.bzl", "java_import_external") load("//web/internal:platform_http_file.bzl", "platform_http_file") - +load("//web/internal:go_repository.bzl", "go_repository") def web_test_repositories( omit_cglib_nodep=False, @@ -137,11 +137,13 @@ def cglib_nodep(): def com_github_gorilla_mux(): - native.new_http_archive( + go_repository( name="com_github_gorilla_mux", - build_file=str(Label("//build_files:com_github_gorilla_mux.BUILD")), + import_name="github.com/gorilla/mux", sha256="a32c13a36c58cb321136231ae8b67b0c6ad3c5f462e65eb6771f59c44b44ccba", strip_prefix="mux-757bef944d0f21880861c2dd9c871ca543023cba", + excluded_srcs = ["context_gorilla.go"], + license = "licenses([\"notice\"])", urls=[ "http://bazel-mirror.storage.googleapis.com/github.com/gorilla/mux/archive/757bef944d0f21880861c2dd9c871ca543023cba.tar.gz", "https://github.com/gorilla/mux/archive/757bef944d0f21880861c2dd9c871ca543023cba.tar.gz", @@ -149,11 +151,12 @@ def com_github_gorilla_mux(): def com_github_tebeka_selenium(): - native.new_http_archive( + go_repository( name="com_github_tebeka_selenium", - build_file=str(Label("//build_files:com_github_tebeka_selenium.BUILD")), + import_name="github.com/tebeka/selenium", sha256="c33decb47a9b81d5221cda29c8f040ca5cf874956bbb002ef82b06e07ed78c3d", strip_prefix="selenium-f6f9a3638fa049f85b0aaf42e693e1c4ab257d4f", + license="licenses([\"notice\"]) # MIT.", urls=[ "http://bazel-mirror.storage.googleapis.com/github.com/tebeka/selenium/archive/f6f9a3638fa049f85b0aaf42e693e1c4ab257d4f.tar.gz", "https://github.com/tebeka/selenium/archive/f6f9a3638fa049f85b0aaf42e693e1c4ab257d4f.tar.gz",