This repository has been archived by the owner on Jun 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from DACH-NY/case-sensitive
Avoid case-sensitive workspace names
- Loading branch information
Showing
11 changed files
with
128 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ load("@bazel_tools//tools/build_defs/repo:git.bzl", | |
load("@bazel_tools//tools/build_defs/repo:http.bzl", | ||
"http_archive", | ||
) | ||
load("//tools:mangling.bzl", "hazel_binary", "hazel_library", "hazel_workspace") | ||
|
||
def _cabal_haskell_repository_impl(ctx): | ||
pkg = "{}-{}".format(ctx.attr.package_name, ctx.attr.package_version) | ||
|
@@ -65,32 +66,33 @@ _core_library_repository = repository_rule( | |
}) | ||
|
||
def _all_hazel_packages_impl(ctx): | ||
ctx.file("BUILD", """ | ||
all_packages_filegroup = """ | ||
filegroup( | ||
name = "all-package-files", | ||
srcs = [{}], | ||
) | ||
""".format(",".join(["\"{}\"".format(f) for f in ctx.attr.files])), | ||
executable=False) | ||
""".format(",".join(["\"@{}//:bzl\"".format(hazel_workspace(p)) for p in ctx.attr.packages])) | ||
one_package_template = """ | ||
filegroup( | ||
name = "haskell_{package_name}", | ||
srcs = ["@{workspace_name}//:bzl"], | ||
) | ||
""" | ||
package_filegroups = [ | ||
one_package_template.format( | ||
package_name = p, | ||
workspace_name = hazel_workspace(p), | ||
) | ||
for p in ctx.attr.packages | ||
] | ||
ctx.file("BUILD", "\n".join([all_packages_filegroup]+package_filegroups), executable=False) | ||
|
||
_all_hazel_packages = repository_rule( | ||
implementation=_all_hazel_packages_impl, | ||
attrs={ | ||
"files": attr.label_list(mandatory=True), | ||
"packages": attr.string_list(mandatory=True), | ||
}) | ||
|
||
def _fixup_package_name(package_name): | ||
"""Fixup package name by replacing dashes with underscores to get a valid | ||
workspace name from it. | ||
Args: | ||
package_name: string: Package name. | ||
Returns: | ||
string: fixed package name. | ||
""" | ||
return package_name.replace("-", "_") | ||
|
||
def hazel_repositories( | ||
core_packages, | ||
packages, | ||
|
@@ -105,7 +107,7 @@ def hazel_repositories( | |
external dependencies corresponding to the given packages: | ||
- @hazel_base_repository: The compiled "hazel" Haskell binary, along with | ||
support files. | ||
- @haskell_{package}: A build of the given Cabal package, one per entry | ||
- @haskell_{package}_{hash}: A build of the given Cabal package, one per entry | ||
of the "packages" argument. (Note that Bazel only builds these | ||
on-demand when needed by other rules.) This repository automatically | ||
downloads the package's Cabal distribution from Hackage and parses the | ||
|
@@ -155,7 +157,7 @@ def hazel_repositories( | |
flags.update({flag: str(items[flag]) for flag in items}) | ||
|
||
_cabal_haskell_repository( | ||
name = "haskell_" + _fixup_package_name(p), | ||
name = hazel_workspace(p), | ||
package_name = p, | ||
package_version = pkgs[p].version, | ||
package_flags = flags, | ||
|
@@ -165,17 +167,13 @@ def hazel_repositories( | |
|
||
for p in core_packages: | ||
_core_library_repository( | ||
name = "haskell_" + _fixup_package_name(p), | ||
name = hazel_workspace(p), | ||
package = p, | ||
) | ||
|
||
_all_hazel_packages( | ||
name = "all_hazel_packages", | ||
files = ["@haskell_{}//:files".format(_fixup_package_name(p)) for p in pkgs]) | ||
|
||
def hazel_library(name): | ||
"""Returns the label of the haskell_library rule for the given package.""" | ||
return "@haskell_{}//:{}".format(_fixup_package_name(name), name) | ||
packages = [p for p in pkgs]) | ||
|
||
def hazel_custom_package_hackage( | ||
package_name, | ||
|
@@ -193,10 +191,9 @@ def hazel_custom_package_hackage( | |
package_id, | ||
package_id, | ||
) | ||
fixed_package_name = _fixup_package_name(package_name) | ||
http_archive( | ||
name = "haskell_{0}".format(fixed_package_name), | ||
build_file = "//third_party/haskell:BUILD.{0}".format(fixed_package_name), | ||
name = hazel_workspace(package_name), | ||
build_file = "//third_party/haskell:BUILD.{0}".format(package_name), | ||
sha256 = sha256, | ||
strip_prefix = package_id, | ||
urls = [url], | ||
|
@@ -224,13 +221,12 @@ def hazel_custom_package_github( | |
repos). | ||
""" | ||
|
||
fixed_package_name = _fixup_package_name(package_name) | ||
build_file = "//third_party/haskell:BUILD.{0}".format(fixed_package_name) | ||
build_file = "//third_party/haskell:BUILD.{0}".format(package_name) | ||
url = "https://github.com/{0}/{1}".format(github_user, github_repo) | ||
ssh_url = "[email protected]:{0}/{1}".format(github_user, github_repo) | ||
|
||
new_git_repository( | ||
name = "haskell_{0}".format(fixed_package_name), | ||
name = hazel_workspace(package_name), | ||
remote = ssh_url if clone_via_ssh else url, | ||
build_file = build_file, | ||
commit = repo_sha, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
def hazel_library(package_name): | ||
"""Returns the label of the haskell_library rule for the given package.""" | ||
return "@{}//:{}".format(hazel_workspace(package_name), package_name) | ||
|
||
|
||
def hazel_binary(package_name): | ||
"""Returns the label of the haskell_binary rule for the given package.""" | ||
return "@{}//:{}_bin".format(hazel_workspace(package_name), package_name) | ||
|
||
|
||
def hazel_workspace(package_name): | ||
"""Convert a package name to a valid and unambiguous workspace name. | ||
Makes the name unambiguous to case-insensitive file-systems and | ||
converts the package name into a valid Bazel workspace name. | ||
Args: | ||
package_name: string: Package name. | ||
Returns: | ||
string: Workspace name. | ||
""" | ||
return "haskell_{}".format( | ||
fixup_package_name(case_insensitive_name(package_name)) | ||
) | ||
|
||
def fixup_package_name(package_name): | ||
"""Convert a package name to a valid workspace name. | ||
Replaces dashes with underscores. | ||
Args: | ||
package_name: string: Package name. | ||
Returns: | ||
string: A valid workspace name. | ||
""" | ||
return package_name.replace("-", "_") | ||
|
||
|
||
def case_insensitive_name(package_name): | ||
"""Convert a package name to a case-insensitive name. | ||
Appends the hash of the input string, and converts the whole string to | ||
lower case. Note, the appended hash value is represented in decimal, and | ||
may be negative. | ||
Args: | ||
name: string: A potentially case-sensitive package name. | ||
Returns: | ||
string: A case-insensitive package name. | ||
""" | ||
return "{lower}_{hash}".format( | ||
lower = package_name.lower(), | ||
hash = hash(package_name) | ||
) |