Skip to content
This repository has been archived by the owner on Jun 18, 2019. It is now read-only.

Commit

Permalink
Merge pull request #76 from matthewbauer/fix-cabal-paths
Browse files Browse the repository at this point in the history
Fix cabal paths in internal repos
  • Loading branch information
thumphries authored Feb 5, 2019
2 parents 8c35d68 + a213699 commit 0961de3
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 10 deletions.
21 changes: 21 additions & 0 deletions test/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,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"),
],
)
23 changes: 23 additions & 0 deletions test/PathsTest.hs
Original file line number Diff line number Diff line change
@@ -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 ()
32 changes: 22 additions & 10 deletions third_party/cabal2bazel/bzl/cabal_paths.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,31 @@ def _impl_path_module_gen(ctx):
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]))


Expand Down Expand Up @@ -96,7 +108,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,
Expand Down

0 comments on commit 0961de3

Please sign in to comment.