From 2686e6d493067c50448374de15e20924da519994 Mon Sep 17 00:00:00 2001 From: Matthew Bauer Date: Mon, 4 Feb 2019 15:33:31 -0500 Subject: [PATCH 1/3] Replace - with _ in package name --- third_party/cabal2bazel/bzl/cabal_paths.bzl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/third_party/cabal2bazel/bzl/cabal_paths.bzl b/third_party/cabal2bazel/bzl/cabal_paths.bzl index d111f63..e41d62a 100644 --- a/third_party/cabal2bazel/bzl/cabal_paths.bzl +++ b/third_party/cabal2bazel/bzl/cabal_paths.bzl @@ -93,7 +93,7 @@ def cabal_paths(name=None, package=None, data_dir='',data=[], version=[], **kwar data: The data files that this package depends on to run. version: The version number of this package (list of ints) """ - module_name = "Paths_" + package + module_name = "Paths_" + package.replace("-", "_") paths_file = module_name + ".hs" _path_module_gen( name = paths_file, From 5f0d7da660e0dfeea8bcca7387d6fa43347099db Mon Sep 17 00:00:00 2001 From: Matthew Bauer Date: Mon, 4 Feb 2019 15:37:11 -0500 Subject: [PATCH 2/3] =?UTF-8?q?Don=E2=80=99t=20use=20relativize=20paths=20?= =?UTF-8?q?in=20internal=20modules?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This makes it possible to use cabal_paths with local projects. Fixes #75. --- third_party/cabal2bazel/bzl/cabal_paths.bzl | 30 ++++++++++++++------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/third_party/cabal2bazel/bzl/cabal_paths.bzl b/third_party/cabal2bazel/bzl/cabal_paths.bzl index e41d62a..f3f5bd4 100644 --- a/third_party/cabal2bazel/bzl/cabal_paths.bzl +++ b/third_party/cabal2bazel/bzl/cabal_paths.bzl @@ -31,19 +31,31 @@ def _impl_path_module_gen(ctx): base_dir = ctx.label.package + ( ("/" + ctx.attr.data_dir) if ctx.attr.data_dir else "") - ctx.template_action( + # If workspace_root is not empty, we have an external repo. + # We need to relativize the paths. + if ctx.label.workspace_root != "": + ctx.template_action( template=ctx.file._template, output=paths_file, substitutions={ - "%{module}": ctx.attr.module, - "%{base_dir}": paths.join( - # TODO: this probably won't work for packages not in external - # repositories. See: - # https://github.com/bazelbuild/bazel/wiki/Updating-the-runfiles-tree-structure - "..", paths.relativize(ctx.label.workspace_root, "external"), base_dir), - "%{version}": str(ctx.attr.version), + "%{module}": ctx.attr.module, + "%{base_dir}": paths.join( + # https://github.com/bazelbuild/bazel/wiki/Updating-the-runfiles-tree-structure + "..", paths.relativize(ctx.label.workspace_root, "external"), base_dir), + "%{version}": str(ctx.attr.version), }, - ) + ) + else: # Otherwise this is a local directory, and we can use relative paths. + ctx.template_action( + template=ctx.file._template, + output=paths_file, + substitutions={ + "%{module}": ctx.attr.module, + "%{base_dir}": base_dir, + "%{version}": str(ctx.attr.version), + }, + ) + return struct(files=depset([paths_file])) From a213699336644722f4d009cc9b34a4a686cb2fdf Mon Sep 17 00:00:00 2001 From: Matthew Bauer Date: Tue, 5 Feb 2019 11:54:48 -0500 Subject: [PATCH 3/3] Add test for cabal_paths These are based off of the tests in google/cabal2bazel here: https://github.com/google/cabal2bazel The test just uses cabal_paths to build PathsTest.hs. --- test/BUILD | 21 +++++++++++++++++++++ test/PathsTest.hs | 23 +++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 test/PathsTest.hs diff --git a/test/BUILD b/test/BUILD index ec3c6dc..1bd5a99 100644 --- a/test/BUILD +++ b/test/BUILD @@ -33,3 +33,24 @@ haskell_doctest( name = "doctest-test", deps = [":DoctestExample"], ) + +load("//:third_party/cabal2bazel/bzl/cabal_paths.bzl", "cabal_paths") + +cabal_paths( + name = "foo-paths", + package = "foo", + version = [ + 1, + 2, + ], +) + +haskell_test( + name = "PathsTest", + srcs = ["PathsTest.hs"], + deps = [ + ":foo-paths", + hazel_library("base"), + hazel_library("filepath"), + ], +) diff --git a/test/PathsTest.hs b/test/PathsTest.hs new file mode 100644 index 0000000..29cc1d4 --- /dev/null +++ b/test/PathsTest.hs @@ -0,0 +1,23 @@ +-- Copyright 2018 Google LLC +-- +-- 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 +-- +-- https://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. + +module Main where + +import qualified Paths_foo + +import Data.Version (versionBranch) +import Control.Exception (assert) + +main = do + assert (versionBranch Paths_foo.version == [1, 2]) $ return ()